run method
Runs this command.
The return value is wrapped in a Future
if necessary and returned by
CommandRunner.runCommand.
Implementation
@override
Future<void> run() async {
final String? packageName = argResults?['package'] as String?;
final bool verify = argResults?['verify'] as bool? ?? false;
final root = SidekickContext.projectRoot;
final List<DartPackage> allPackages = findAllPackages(root);
final globExcludes =
excludeGlob.map<Glob>((rule) => Glob("${root.path}/$rule"));
if (packageName != null) {
final package =
allPackages.where((it) => it.name == packageName).firstOrNull;
if (package == null) {
throw "Package with name $packageName not found in repository "
"${SidekickContext.repository?.path}";
}
final int lineLength = getLineLength(package) ?? defaultLineLength;
final allDartFiles = package.root.findFilesToFormat(globExcludes);
_format(
name: "package:${package.name}",
lineLength: lineLength,
files: allDartFiles,
verify: verify,
);
_verifyThrow();
return;
}
final allFiles =
SidekickContext.projectRoot.findFilesToFormat(globExcludes).toList();
final sortedPackages =
allPackages.sortedByDescending((element) => element.root.path.length);
for (final package in sortedPackages) {
final resolvedLineLength = getLineLength(package) ?? defaultLineLength;
final filesInPackage = allFiles
.where((file) => file.path.contains(package.root.path))
.toList();
for (final file in filesInPackage) {
allFiles.remove(file);
}
_format(
name: "package:${package.name}",
lineLength: resolvedLineLength,
files: filesInPackage,
verify: verify,
);
}
if (allFiles.isNotEmpty) {
_format(
name: "Other",
lineLength: defaultLineLength,
files: allFiles,
verify: verify,
);
}
_verifyThrow();
}