Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Pub Workspaces #138

Merged
merged 21 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions lib/src/dependency_validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,28 @@ import 'pubspec_config.dart';
import 'utils.dart';

/// Check for missing, under-promoted, over-promoted, and unused dependencies.
Future<void> run() async {
if (!File('pubspec.yaml').existsSync()) {
Future<void> run({String root = "."}) async {
if (!File('$root/pubspec.yaml').existsSync()) {
logger.shout(red.wrap('pubspec.yaml not found'));
exit(1);
}
if (!File('.dart_tool/package_config.json').existsSync()) {
logger.shout(red.wrap(
'No .dart_tool/package_config.json file found, please run "pub get" first.'));
exit(1);
}

DepValidatorConfig config;
final configFile = File('dart_dependency_validator.yaml');
final configFile = File('$root/dart_dependency_validator.yaml');
if (configFile.existsSync()) {
config = DepValidatorConfig.fromYaml(configFile.readAsStringSync());
} else {
final pubspecConfig = PubspecDepValidatorConfig.fromYaml(
File('pubspec.yaml').readAsStringSync());
File('$root/pubspec.yaml').readAsStringSync(),
);
if (pubspecConfig.isNotEmpty) {
logger.warning(yellow.wrap(
'Configuring dependency_validator in pubspec.yaml is deprecated.\n'
'Use dart_dependency_validator.yaml instead.'));
}
config = pubspecConfig.dependencyValidator;
}

final excludes = config.exclude
matthewnitschke-wk marked this conversation as resolved.
Show resolved Hide resolved
.map((s) {
try {
Expand All @@ -70,13 +67,22 @@ Future<void> run() async {
logger.fine('ignored packages:\n${bulletItems(ignoredPackages)}\n');

// Read and parse the analysis_options.yaml in the current working directory.
final optionsIncludePackage = getAnalysisOptionsIncludePackage();
final optionsIncludePackage = getAnalysisOptionsIncludePackage(path: root);

// Read and parse the pubspec.yaml in the current working directory.
final pubspecFile = File('pubspec.yaml');
final pubspecFile = File('$root/pubspec.yaml');
final pubspec =
Pubspec.parse(pubspecFile.readAsStringSync(), sourceUrl: pubspecFile.uri);

if (pubspec.isWorkspaceRoot) {
logger.fine('In a workspace. Recursing through sub-packages...');
for (final package in pubspec.workspace ?? []) {
await run(root: package);
logger.info('');
}
return;
}

logger.info('Validating dependencies for ${pubspec.name}...');

if (!config.allowPins) {
Expand All @@ -92,7 +98,7 @@ Future<void> run() async {
logger.fine('dev_dependencies:\n'
'${bulletItems(devDeps)}\n');

final publicDirs = ['bin/', 'lib/'];
final publicDirs = ['$root/bin/', '$root/lib/'];
final publicDartFiles = [
for (final dir in publicDirs) ...listDartFilesIn(dir, excludes),
];
Expand Down Expand Up @@ -135,11 +141,11 @@ Future<void> run() async {
final publicDirGlobs = [for (final dir in publicDirs) Glob('$dir**')];

final nonPublicDartFiles =
listDartFilesIn('./', [...excludes, ...publicDirGlobs]);
listDartFilesIn('$root/', [...excludes, ...publicDirGlobs]);
final nonPublicScssFiles =
listScssFilesIn('./', [...excludes, ...publicDirGlobs]);
listScssFilesIn('$root/', [...excludes, ...publicDirGlobs]);
final nonPublicLessFiles =
listLessFilesIn('./', [...excludes, ...publicDirGlobs]);
listLessFilesIn('$root/', [...excludes, ...publicDirGlobs]);

logger
..fine('non-public dart files:\n'
Expand Down
9 changes: 9 additions & 0 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,12 @@ DependencyPinEvaluation inspectVersionForPins(VersionConstraint constraint) {

return DependencyPinEvaluation.emptyPin;
}

/// Utilities for Pubspec objects.
extension PubspecUtils on Pubspec {
/// Whether this package is the root of a Pub Workspace.
bool get isWorkspaceRoot => workspace != null;

/// Whether this package is a sub-package in a Pub Workspace.
bool get isInWorkspace => resolution == 'workspace';
}
7 changes: 7 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,12 @@ dev_dependencies:
test_descriptor: ^2.0.0
workiva_analysis_options: ^1.2.2

dependency_overrides:
pubspec_parse:
git:
url: https://github.com/Levi-Lesches/dart-lang-tools
ref: pubspec-workspaces-1814
path: pkgs/pubspec_parse

executables:
dependency_validator: