main.dart 2.67 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:io';

import 'package:args/args.dart';
import 'package:vitool/vitool.dart';

const String kCodegenComment =
  '// AUTOGENERATED FILE DO NOT EDIT!\n'
  '// This file was generated by vitool.\n';

void main(List<String> args) {
15
  final ArgParser parser = ArgParser();
16 17 18 19 20

  parser.addFlag(
      'help',
      abbr: 'h',
      negatable: false,
21
      help: "Display the tool's usage instructions and quit.",
22 23 24 25 26
  );

  parser.addOption(
      'output',
      abbr: 'o',
27
      help: 'Target path to write the generated Dart file to.',
28 29 30 31 32
  );

  parser.addOption(
      'asset-name',
      abbr: 'n',
33
      help: 'Name to be used for the generated constant.',
34 35 36 37 38
  );

  parser.addOption(
      'part-of',
      abbr: 'p',
39
      help: "Library name to add a dart 'part of' clause for.",
40 41 42 43 44 45
  );

  parser.addOption(
      'header',
      abbr: 'd',
      help: 'File whose contents are to be prepended to the beginning of '
46
            'the generated Dart file; this can be used for a license comment.',
47 48 49 50 51 52 53
  );

  parser.addFlag(
      'codegen_comment',
      abbr: 'c',
      defaultsTo: true,
      help: 'Whether to include the following comment after the header:\n'
54
            '$kCodegenComment',
55 56 57 58
  );

  final ArgResults argResults = parser.parse(args);

59
  if (argResults['help'] as bool ||
60 61 62 63 64 65 66
    !argResults.wasParsed('output') ||
    !argResults.wasParsed('asset-name') ||
    argResults.rest.isEmpty) {
    printUsage(parser);
    return;
  }

67
  final List<FrameData> frames = <FrameData>[
68
    for (final String filePath in argResults.rest) interpretSvg(filePath),
69
  ];
70

71
  final StringBuffer generatedSb = StringBuffer();
72 73

  if (argResults.wasParsed('header')) {
74
    generatedSb.write(File(argResults['header'] as String).readAsStringSync());
75 76 77
    generatedSb.write('\n');
  }

78
  if (argResults['codegen_comment'] as bool) {
79
    generatedSb.write(kCodegenComment);
80
  }
81

82
  if (argResults.wasParsed('part-of')) {
83
    generatedSb.write('part of ${argResults['part-of']}; // ignore: use_string_in_part_of_directives\n');
84
  }
85

86
  final Animation animation = Animation.fromFrameData(frames);
87
  generatedSb.write(animation.toDart('_AnimatedIconData', argResults['asset-name'] as String));
88

89
  final File outFile = File(argResults['output'] as String);
90 91 92 93 94 95 96 97
  outFile.writeAsStringSync(generatedSb.toString());
}

void printUsage(ArgParser parser) {
  print('Usage: vitool --asset-name=<asset_name> --output=<output_path> <frames_list>');
  print('\nExample: vitool --asset-name=_\$menu_arrow --output=lib/data/menu_arrow.g.dart assets/svg/menu_arrow/*.svg\n');
  print(parser.usage);
}