void _extractSnippet()

in tool/extract.dart [119:151]


void _extractSnippet(
    File sourceFile, int snippet, int startLine, Iterable<String> lines,
    {String includeSource}) {
  if (lines.isEmpty || lines.every((String line) => line.isEmpty)) {
    throw StateError('Passed empty lines to extractSnippet');
  }

  final int importCount =
      lines.where((String line) => line.trim().startsWith('import ')).length;
  if (importCount == lines.length) {
    return;
  }

  bool hasImport = importCount > 0;

  String path = _createDirAndFileName(sourceFile, snippet);

  StringBuffer source =
      StringBuffer('// Extracted from ${sourceFile.path}, line $startLine\n');

  if (!hasImport) {
    source.writeln(defaultImports);
  }

  if (includeSource != null) {
    source.writeln('$includeSource');
  }

  lines.map(_removeMarkup).forEach(source.writeln);

  File(path).writeAsStringSync(source.toString());
  print('  ${lines.length} line snippet ==> $path');
}