run method

  1. @override
FutureOr<bool> run()
override

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 directory = argDirectory;
  var output = argOutput;
  var ignore = argIgnore;
  var regexp = parseRegExp();

  if (directory == null) {
    showErrorOptionNotProvided('directory');
  }

  if (output == null) {
    showErrorOptionNotProvided('output');
  }

  File outputFile = File(output!).absolute;

  if (outputFile.existsSync()) {
    showError("Output File already exists: $outputFile");
  }

  if (hasError) {
    return false;
  }

  printToConsole(cliTitle);

  printToConsole('\nTEMPLATE:\n  $directory');

  var template = await loadTemplate(directory!,
      ignorePath: ignore, ignoreRegexp: regexp);

  printToConsole('\nENTRIES(${template.length}):');
  for (var e in template.entriesPaths) {
    printToConsole('  > $e');
  }

  showTemplateInfos(template);

  var ext = TemplateEntry.parseNameExtension(output).toLowerCase();

  dynamic encoded;
  String format;
  if (ext == 'yml' || ext == 'yaml') {
    encoded = template.toYAMLEncoded();
    format = 'YAML';
  } else if (ext == 'zip') {
    var storage = StorageZip();
    await template.saveTo(storage);
    encoded = await storage.compress();
    format = 'Zip';
  } else if (ext == 'tar') {
    var storage = StorageTarGzip();
    await template.saveTo(storage);
    encoded = await storage.compress(compressionLevel: 0);
    format = 'tar';
  } else if (ext == 'gz') {
    var storage = StorageTarGzip();
    await template.saveTo(storage);
    encoded = await storage.compress();

    var pathParts = pack_path.split(outputFile.path);
    if (!pathParts.last.toLowerCase().endsWith('tar.gz')) {
      log('WARNING', 'Output file without `tar.gz` extension!');
    }

    format = 'tar+Gzip';
  } else {
    encoded = template.toJsonEncoded(pretty: true);
    format = 'JSON';
  }

  printToConsole('\n-- Format: $format');

  if (encoded is String) {
    outputFile.writeAsStringSync(encoded);
  } else {
    outputFile.writeAsBytesSync(encoded);
  }

  printToConsole(
      '\n-- Saved Template> size: ${outputFile.lengthSync()} ; path: ${outputFile.path}\n');

  return true;
}