run method
Runs this command.
The return value is wrapped in a Future if necessary and returned by
CommandRunner.runCommand.
Implementation
@override
Future<int> run() async {
final rest = argResults?.rest ?? [];
if (rest.isEmpty) {
printUsage();
return 64;
}
final templateId = rest.first;
final targetVersion = argResults?['version'] as String? ?? '1.0.0';
final profile = argResults?['profile'] as String? ?? 'standard';
final artifactPath = argResults?['artifact'] as String?;
final outputDir = argResults?['output'] as String? ?? 'doc/release';
final writeDisk = argResults?['write'] as bool? ?? false;
final jsonOutput = argResults?['json'] as bool? ?? false;
final discoveryService = _buildDiscoveryService();
final entry = discoveryService.get(templateId);
if (entry == null) {
if (jsonOutput) {
print(jsonEncode(
{'error': 'Template "$templateId" not found.', 'success': false}));
} else {
print('Error: Template "$templateId" not found in catalog.');
}
return 1;
}
final tmpl = entry.template;
final validator = PubDevPackageValidator();
final options = PubDevValidationOptions(
packageName: tmpl.manifest.name.toLowerCase().replaceAll(' ', '_'),
version: targetVersion,
profile: profile,
artifactPath: artifactPath,
);
final plan = validator.planValidation(options);
final result = validator.validatePackage(plan);
if (writeDisk) {
final baseDir = Directory(outputDir);
await baseDir.create(recursive: true);
final file = File(
'${baseDir.path}/pubdev_validation_report.${jsonOutput ? 'json' : 'md'}');
await file.writeAsString(
jsonOutput ? jsonEncode(result.toJson()) : result.toMarkdown());
if (!jsonOutput) {
print(
'Successfully wrote pub.dev validation report to "${file.path}".');
}
}
if (jsonOutput) {
print(jsonEncode(result.toJson()));
} else if (!writeDisk) {
print(
'Generated Pub.dev Package Validation Plan Preview for "${result.packageName}":');
print('══════════════════════════════════════════════════════════════');
print('Package Version : ${plan.version}');
print('Profile : ${plan.profile}');
print('Total Checks : ${result.checks.length}');
print(
'Status : ${result.isPublishable ? "PUBLISHABLE TO PUB.DEV ✓" : "NOT PUBLISHABLE ✗"}');
print('══════════════════════════════════════════════════════════════');
}
return result.isPublishable ? 0 : 1;
}