run method
Runs this command.
The return value is wrapped in a Future
if necessary and returned by
CommandRunner.runCommand
.
Implementation
@override
FutureOr<bool> run() async {
var templatePath = argTemplate;
var output = argOutput;
var ignore = argIgnore;
var regexp = parseRegExp();
var properties = parseProperties();
if (templatePath == null || templatePath.isEmpty) {
showErrorOptionNotProvided('template');
}
late File outputFile;
if (output == null || output.isEmpty) {
showErrorOptionNotProvided('output');
} else {
outputFile = File(output).absolute;
if (outputFile.existsSync()) {
showError('Output already exists: $output');
}
}
if (properties.isEmpty) {
log('WARNING',
'Empty template properties. Use `-p varName=X` to define properties/variables.');
}
if (hasError) {
return false;
}
printToConsole(cliTitle);
printToConsole('\nTEMPLATE:\n $templatePath');
var template = await loadTemplate(templatePath!,
ignorePath: ignore, ignoreRegexp: regexp);
showTemplateInfos(template);
printToConsole('\nDEFINED PROPERTIES:');
for (var e in properties.entries) {
printToConsole(' - ${e.key}: ${e.value}');
}
var notPresentVars = template.getNotDefinedVariables(properties);
if (notPresentVars.isNotEmpty) {
printToConsole('');
log('ERROR', 'Missing properties: ${notPresentVars.join(', ')}');
return false;
}
printToConsole('\nOUTPUT:\n ${outputFile.path}');
printToConsole('\n-- Resolving template...');
var resolvedTemplate = template.resolve(properties);
var outputLC = output!.toLowerCase();
List<String> savedFiles;
String savedAt;
if (outputLC.endsWith('.zip')) {
var storage = StorageZip();
savedFiles = await resolvedTemplate.saveTo(storage);
outputFile.writeAsBytesSync(await storage.compress());
savedAt = outputFile.path;
} else if (outputLC.endsWith('.tar') || outputLC.endsWith('.tar.gz')) {
var storage = StorageTarGzip();
savedFiles = await resolvedTemplate.saveTo(storage);
outputFile.writeAsBytesSync(await storage.compress());
savedAt = outputFile.path;
} else {
var outputDir = Directory(outputFile.path);
outputDir.createSync(recursive: true);
var storage = StorageIO(outputDir);
savedFiles = await resolvedTemplate.saveTo(storage);
savedAt = outputDir.path;
}
printToConsole('\nSAVED FILES(${savedFiles.length}):');
for (var f in savedFiles) {
printToConsole(' > $f');
}
var mainEntryPath = resolvedTemplate.mainEntryPath;
printToConsole('\n-- mainEntryPath: $mainEntryPath');
printToConsole('\n-- Template generated at: $savedAt');
printToConsole('');
return true;
}