main.dart 6.49 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:io' hide Platform;

import 'package:args/args.dart';
8
import 'package:path/path.dart' as path;
9 10 11 12 13
import 'package:platform/platform.dart';

import 'configuration.dart';
import 'snippets.dart';

14
const String _kSerialOption = 'serial';
15
const String _kElementOption = 'element';
16
const String _kHelpOption = 'help';
17 18
const String _kInputOption = 'input';
const String _kLibraryOption = 'library';
19
const String _kOutputOption = 'output';
20 21 22
const String _kPackageOption = 'package';
const String _kTemplateOption = 'template';
const String _kTypeOption = 'type';
23
const String _kShowDartPad = 'dartpad';
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

/// Generates snippet dartdoc output for a given input, and creates any sample
/// applications needed by the snippet.
void main(List<String> argList) {
  const Platform platform = LocalPlatform();
  final Map<String, String> environment = platform.environment;
  final ArgParser parser = ArgParser();
  final List<String> snippetTypes =
      SnippetType.values.map<String>((SnippetType type) => getEnumName(type)).toList();
  parser.addOption(
    _kTypeOption,
    defaultsTo: getEnumName(SnippetType.application),
    allowed: snippetTypes,
    allowedHelp: <String, String>{
      getEnumName(SnippetType.application):
          'Produce a code snippet complete with embedding the sample in an '
          'application template.',
      getEnumName(SnippetType.sample):
          'Produce a nicely formatted piece of sample code. Does not embed the '
43
          'sample into an application template.',
44 45 46 47 48 49 50 51
    },
    help: 'The type of snippet to produce.',
  );
  parser.addOption(
    _kTemplateOption,
    defaultsTo: null,
    help: 'The name of the template to inject the code into.',
  );
52 53 54 55 56
  parser.addOption(
    _kOutputOption,
    defaultsTo: null,
    help: 'The output path for the generated snippet application. Overrides '
        'the naming generated by the --package/--library/--element arguments. '
57
        'Metadata will be written alongside in a .json file. '
58 59
        'The basename of this argument is used as the ID',
  );
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  parser.addOption(
    _kInputOption,
    defaultsTo: environment['INPUT'],
    help: 'The input file containing the snippet code to inject.',
  );
  parser.addOption(
    _kPackageOption,
    defaultsTo: environment['PACKAGE_NAME'],
    help: 'The name of the package that this snippet belongs to.',
  );
  parser.addOption(
    _kLibraryOption,
    defaultsTo: environment['LIBRARY_NAME'],
    help: 'The name of the library that this snippet belongs to.',
  );
  parser.addOption(
    _kElementOption,
    defaultsTo: environment['ELEMENT_NAME'],
    help: 'The name of the element that this snippet belongs to.',
  );
80 81 82 83 84
  parser.addOption(
    _kSerialOption,
    defaultsTo: environment['INVOCATION_INDEX'],
    help: 'A unique serial number for this snippet tool invocation.',
  );
85 86 87 88 89 90
  parser.addFlag(
    _kHelpOption,
    defaultsTo: false,
    negatable: false,
    help: 'Prints help documentation for this command',
  );
91 92 93 94 95 96 97 98
  parser.addFlag(
    _kShowDartPad,
    defaultsTo: false,
    negatable: false,
    help: 'Indicates whether DartPad should be included in the snippet\'s '
        'final HTML output. This flag only applies when the type parameter is '
        '"application".',
  );
99 100 101

  final ArgResults args = parser.parse(argList);

102 103 104 105 106
  if (args[_kHelpOption]) {
    stderr.writeln(parser.usage);
    exit(0);
  }

107 108 109 110
  final SnippetType snippetType = SnippetType.values
      .firstWhere((SnippetType type) => getEnumName(type) == args[_kTypeOption], orElse: () => null);
  assert(snippetType != null, "Unable to find '${args[_kTypeOption]}' in SnippetType enum.");

111 112 113 114 115
  if (args[_kShowDartPad] == true && snippetType != SnippetType.application) {
    errorExit('${args[_kTypeOption]} was selected, but the --dartpad flag is only valid '
      'for application snippets.');
  }

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  if (args[_kInputOption] == null) {
    stderr.writeln(parser.usage);
    errorExit('The --$_kInputOption option must be specified, either on the command '
        'line, or in the INPUT environment variable.');
  }

  final File input = File(args['input']);
  if (!input.existsSync()) {
    errorExit('The input file ${input.path} does not exist.');
  }

  String template;
  if (snippetType == SnippetType.application) {
    if (args[_kTemplateOption] == null || args[_kTemplateOption].isEmpty) {
      stderr.writeln(parser.usage);
      errorExit('The --$_kTemplateOption option must be specified on the command '
          'line for application snippets.');
    }
    template = args[_kTemplateOption].toString().replaceAll(RegExp(r'.tmpl$'), '');
  }

137 138 139
  final String packageName = args[_kPackageOption] != null && args[_kPackageOption].isNotEmpty ? args[_kPackageOption] : null;
  final String libraryName = args[_kLibraryOption] != null && args[_kLibraryOption].isNotEmpty ? args[_kLibraryOption] : null;
  final String elementName = args[_kElementOption] != null && args[_kElementOption].isNotEmpty ? args[_kElementOption] : null;
140
  final String serial = args[_kSerialOption] != null && args[_kSerialOption].isNotEmpty ? args[_kSerialOption] : null;
141
  final List<String> id = <String>[];
142 143 144
  if (args[_kOutputOption] != null) {
    id.add(path.basename(path.basenameWithoutExtension(args[_kOutputOption])));
  } else {
145 146
    if (packageName != null && packageName != 'flutter') {
      id.add(packageName);
147
    }
148 149
    if (libraryName != null) {
      id.add(libraryName);
150
    }
151 152
    if (elementName != null) {
      id.add(elementName);
153
    }
154 155 156
    if (serial != null) {
      id.add(serial);
    }
157 158
    if (id.isEmpty) {
      errorExit('Unable to determine ID. At least one of --$_kPackageOption, '
159 160
          '--$_kLibraryOption, --$_kElementOption, -$_kSerialOption, or the environment variables '
          'PACKAGE_NAME, LIBRARY_NAME, ELEMENT_NAME, or INVOCATION_INDEX must be non-empty.');
161
    }
162 163 164 165 166 167
  }

  final SnippetGenerator generator = SnippetGenerator();
  stdout.write(generator.generate(
    input,
    snippetType,
168
    showDartPad: args[_kShowDartPad],
169
    template: template,
170
    output: args[_kOutputOption] != null ? File(args[_kOutputOption]) : null,
171 172
    metadata: <String, Object>{
      'sourcePath': environment['SOURCE_PATH'],
173 174 175
      'sourceLine': environment['SOURCE_LINE'] != null
          ? int.tryParse(environment['SOURCE_LINE'])
          : null,
176
      'id': id.join('.'),
177
      'serial': serial,
178 179 180 181
      'package': packageName,
      'library': libraryName,
      'element': elementName,
    },
182
  ));
183

184 185
  exit(0);
}