Write snippets index file when generating docs (#25515)

This commit is contained in:
Danny Tuppeny
2019-01-08 10:38:59 +00:00
committed by GitHub
parent 891036c9b9
commit b5c5bae42b
4 changed files with 91 additions and 9 deletions

View File

@@ -52,6 +52,7 @@ void main(List<String> argList) {
defaultsTo: null,
help: 'The output path for the generated snippet application. Overrides '
'the naming generated by the --package/--library/--element arguments. '
'Metadata will be written alongside in a .json file. '
'The basename of this argument is used as the ID',
);
parser.addOption(
@@ -113,20 +114,21 @@ void main(List<String> argList) {
template = args[_kTemplateOption].toString().replaceAll(RegExp(r'.tmpl$'), '');
}
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;
final List<String> id = <String>[];
if (args[_kOutputOption] != null) {
id.add(path.basename(path.basenameWithoutExtension(args[_kOutputOption])));
} else {
if (args[_kPackageOption] != null &&
args[_kPackageOption].isNotEmpty &&
args[_kPackageOption] != 'flutter') {
id.add(args[_kPackageOption]);
if (packageName != null && packageName != 'flutter') {
id.add(packageName);
}
if (args[_kLibraryOption] != null && args[_kLibraryOption].isNotEmpty) {
id.add(args[_kLibraryOption]);
if (libraryName != null) {
id.add(libraryName);
}
if (args[_kElementOption] != null && args[_kElementOption].isNotEmpty) {
id.add(args[_kElementOption]);
if (elementName != null) {
id.add(elementName);
}
if (id.isEmpty) {
errorExit('Unable to determine ID. At least one of --$_kPackageOption, '
@@ -142,6 +144,13 @@ void main(List<String> argList) {
template: template,
id: id.join('.'),
output: args[_kOutputOption] != null ? File(args[_kOutputOption]) : null,
metadata: <String, Object>{
'sourcePath': environment['SOURCE_PATH'],
'package': packageName,
'library': libraryName,
'element': elementName,
},
));
exit(0);
}

View File

@@ -39,6 +39,8 @@ class SnippetGenerator {
/// snippet.
final Configuration configuration;
static const JsonEncoder jsonEncoder = JsonEncoder.withIndent(' ');
/// A Dart formatted used to format the snippet code and finished application
/// code.
static DartFormatter formatter = DartFormatter(pageWidth: 80, fixes: StyleFix.all);
@@ -193,7 +195,7 @@ class SnippetGenerator {
/// The [id] is a string ID to use for the output file, and to tell the user
/// about in the `flutter create` hint. It must not be null if the [type] is
/// [SnippetType.application].
String generate(File input, SnippetType type, {String template, String id, File output}) {
String generate(File input, SnippetType type, {String template, String id, File output, Map<String, Object> metadata}) {
assert(template != null || type != SnippetType.application);
assert(id != null || type != SnippetType.application);
assert(input != null);
@@ -226,6 +228,23 @@ class SnippetGenerator {
final File outputFile = output ?? getOutputFile(id);
stderr.writeln('Writing to ${outputFile.absolute.path}');
outputFile.writeAsStringSync(app);
final File metadataFile = File(path.join(path.dirname(outputFile.path),
'${path.basenameWithoutExtension(outputFile.path)}.json'));
stderr.writeln('Writing metadata to ${metadataFile.absolute.path}');
final _ComponentTuple description = snippetData.firstWhere(
(_ComponentTuple data) => data.name == 'description',
orElse: () => null,
);
metadata ??= <String, Object>{};
metadata.addAll(<String, Object>{
'id': id,
'file': path.basename(outputFile.path),
'description': description != null
? description.mergedContent
: null,
});
metadataFile.writeAsStringSync(jsonEncoder.convert(metadata));
break;
case SnippetType.sample:
break;