Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ jobs:
uses: Workiva/gha-dart-oss/.github/workflows/checks.yaml@v0.1.12
with:
sdk: stable
format-check: false

format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dart-lang/setup-dart@v1
with:
sdk: stable
- run: |
dart pub get
dart --version
dart format .
- run: |
git diff --quiet -- . ':(exclude)**pubspec.lock' || {
echo "::error::The format job failed with source changes"
git --no-pager diff -- . ':(exclude)**pubspec.lock'
exit 1
}

unit-tests:
uses: Workiva/gha-dart-oss/.github/workflows/test-unit.yaml@v0.1.12
Expand Down
27 changes: 13 additions & 14 deletions bin/dependency_validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,19 @@ example:
usage:''';

/// Parses the command-line arguments
final ArgParser argParser =
ArgParser()
..addFlag(helpArg, abbr: 'h', help: 'Displays this info.')
..addFlag(
verboseArg,
defaultsTo: false,
help: 'Display extra information for debugging.',
)
..addOption(
rootDirArg,
abbr: "C",
help: 'Validate dependencies in a subdirectory',
defaultsTo: '.',
);
final ArgParser argParser = ArgParser()
..addFlag(helpArg, abbr: 'h', help: 'Displays this info.')
..addFlag(
verboseArg,
defaultsTo: false,
help: 'Display extra information for debugging.',
)
..addOption(
rootDirArg,
abbr: "C",
help: 'Validate dependencies in a subdirectory',
defaultsTo: '.',
);

void showHelpAndExit({ExitCode exitCode = ExitCode.success}) {
Logger.root.shout(helpMessage);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class DependencyPinEvaluation {
/// possible prerelease.
static const DependencyPinEvaluation buildOrPrerelease =
DependencyPinEvaluation._(
'Builds or preleases as max bounds block minor bumps and patches.',
);
'Builds or preleases as max bounds block minor bumps and patches.',
);

/// 1.2.3
static const DependencyPinEvaluation directPin = DependencyPinEvaluation._(
Expand Down
54 changes: 28 additions & 26 deletions lib/src/dependency_validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,17 @@ Future<bool> checkPackage({required String root}) async {
config = pubspecConfig.dependencyValidator;
}

final excludes =
config.exclude
.map((s) {
try {
return makeGlob("$root/$s");
} catch (_, __) {
logger.shout(yellow.wrap('invalid glob syntax: "$s"'));
return null;
}
})
.nonNulls
.toList();
final excludes = config.exclude
.map((s) {
try {
return makeGlob("$root/$s");
} catch (_, __) {
logger.shout(yellow.wrap('invalid glob syntax: "$s"'));
return null;
}
})
.nonNulls
.toList();
logger.fine('excludes:\n${bulletItems(excludes.map((g) => g.pattern))}\n');
final ignoredPackages = config.ignore;
logger.fine('ignored packages:\n${bulletItems(ignoredPackages)}\n');
Expand Down Expand Up @@ -268,13 +267,13 @@ Future<bool> checkPackage({required String root}) async {
// Packages that are not used in lib/, but are used elsewhere, that are
// dependencies when they should be dev_dependencies.
final overPromotedDependencies =
// Start with dependencies that are not used in lib/
(deps
.difference(packagesUsedInPublicFiles)
// Intersect with deps that are used outside lib/ (excludes unused deps)
.intersection(packagesUsedOutsidePublicDirs))
// Ignore known over-promoted packages.
..removeAll(ignoredPackages);
// Start with dependencies that are not used in lib/
(deps
.difference(packagesUsedInPublicFiles)
// Intersect with deps that are used outside lib/ (excludes unused deps)
.intersection(packagesUsedOutsidePublicDirs))
// Ignore known over-promoted packages.
..removeAll(ignoredPackages);

if (overPromotedDependencies.isNotEmpty) {
log(
Expand All @@ -287,10 +286,10 @@ Future<bool> checkPackage({required String root}) async {

// Packages that are used in lib/, but are dev_dependencies.
final underPromotedDependencies =
// Start with dev_dependencies that are used in lib/
devDeps.intersection(packagesUsedInPublicFiles)
// Ignore known under-promoted packages
..removeAll(ignoredPackages);
// Start with dev_dependencies that are used in lib/
devDeps.intersection(packagesUsedInPublicFiles)
// Ignore known under-promoted packages
..removeAll(ignoredPackages);

if (underPromotedDependencies.isNotEmpty) {
log(
Expand Down Expand Up @@ -334,8 +333,8 @@ Future<bool> checkPackage({required String root}) async {
for (final target in rootBuildConfig.buildTargets.values)
...target.builders.keys,
]
.map((key) => normalizeBuilderKeyUsage(key, pubspec.name))
.any((key) => key.startsWith('$dependencyName:'));
.map((key) => normalizeBuilderKeyUsage(key, pubspec.name))
.any((key) => key.startsWith('$dependencyName:'));

final packagesWithConsumedBuilders = Set<String>();
for (final name in unusedDependencies) {
Expand Down Expand Up @@ -420,7 +419,10 @@ Future<bool> checkPackage({required String root}) async {
Future<bool> dependencyDefinesAutoAppliedBuilder(String path) async =>
(await BuildConfig.fromPackageDir(
path,
)).builderDefinitions.values.any((def) => def.autoApply != AutoApply.none);
))
.builderDefinitions
.values
.any((def) => def.autoApply != AutoApply.none);

/// Checks for dependency pins.
///
Expand Down
2 changes: 1 addition & 1 deletion lib/src/pubspec_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class PubspecDepValidatorConfig {
dependencyValidator.ignore.isNotEmpty;

PubspecDepValidatorConfig({DepValidatorConfig? dependencyValidator})
: dependencyValidator = dependencyValidator ?? DepValidatorConfig();
: dependencyValidator = dependencyValidator ?? DepValidatorConfig();

factory PubspecDepValidatorConfig.fromJson(Map json) =>
_$PubspecDepValidatorConfigFromJson(json);
Expand Down
67 changes: 34 additions & 33 deletions test/pubspec_to_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,45 @@ typedef Json = Map<String, dynamic>;

extension on Dependency {
Json toJson() => switch (this) {
SdkDependency(:final sdk, :final version) => {
"sdk": sdk,
"version": version.toString(),
},
HostedDependency(:final hosted, :final version) => {
if (hosted != null) "hosted": hosted.url.toString(),
"version": version.toString(),
},
GitDependency(:final url, :final ref, :final path) => {
"git": {
"url": url.toString(),
if (path != null) "ref": ref,
if (path != null) "path": path,
},
},
PathDependency(:final path) => {"path": path.replaceAll(r'\', '/')},
};
SdkDependency(:final sdk, :final version) => {
"sdk": sdk,
"version": version.toString(),
},
HostedDependency(:final hosted, :final version) => {
if (hosted != null) "hosted": hosted.url.toString(),
"version": version.toString(),
},
GitDependency(:final url, :final ref, :final path) => {
"git": {
"url": url.toString(),
if (path != null) "ref": ref,
if (path != null) "path": path,
},
},
PathDependency(:final path) => {"path": path.replaceAll(r'\', '/')},
};
}

/// An as-needed implementation of `Pubspec.toJson` for testing.
///
/// See: https://github.com/dart-lang/tools/issues/1801
extension PubspecToJson on Pubspec {
Json toJson() => {
"name": name,
"environment": {
for (final (sdk, version) in environment.records) sdk: version.toString(),
},
if (resolution != null) "resolution": resolution,
if (workspace != null) "workspace": workspace,
"dependencies": {
for (final (name, dependency) in dependencies.records)
name: dependency.toJson(),
},
"dev_dependencies": {
for (final (name, dependency) in devDependencies.records)
name: dependency.toJson(),
},
// ...
};
"name": name,
"environment": {
for (final (sdk, version) in environment.records)
sdk: version.toString(),
},
if (resolution != null) "resolution": resolution,
if (workspace != null) "workspace": workspace,
"dependencies": {
for (final (name, dependency) in dependencies.records)
name: dependency.toJson(),
},
"dev_dependencies": {
for (final (name, dependency) in devDependencies.records)
name: dependency.toJson(),
},
// ...
};
}
4 changes: 2 additions & 2 deletions test/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ Future<ProcessResult> checkProject({
}

Dependency hostedCompatibleWith(String version) => HostedDependency(
version: VersionConstraint.compatibleWith(Version.parse(version)),
);
version: VersionConstraint.compatibleWith(Version.parse(version)),
);

Dependency hostedPinned(String version) =>
HostedDependency(version: Version.parse(version));
Expand Down
Loading
Loading