<commit_before>// Copyright (c) 2015, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library source_gen.generate;

import 'dart:async';
import 'dart:io';

import 'package:analyzer/src/generated/element.dart';
import 'package:analyzer/src/generated/source_io.dart';
import 'package:dart_style/src/dart_formatter.dart';
import 'package:path/path.dart' as p;

import 'generated_output.dart';
import 'generator.dart';
import 'io.dart';
import 'results.dart';
import 'utils.dart';

/// Updates generated code for [projectPath] with the provided [generators].
///
/// [changeFilePaths] and [librarySearchPaths] must be relative to
/// [projectPath].
///
/// If [librarySearchPaths] is not provided, `['lib']` is used.
///
/// if [omitGenerateTimestamp] is `true`, no timestamp will be added to the
/// output. The default value is `false`.
Future<GenerationResult> generate(
    String projectPath, List<Generator> generators,
    {Iterable<String> changeFilePaths,
    List<String> librarySearchPaths,
    bool omitGenerateTimestamp}) async {
  if (omitGenerateTimestamp == null) {
    omitGenerateTimestamp = false;
  }

  if (changeFilePaths == null || changeFilePaths.isEmpty) {
    if (librarySearchPaths != null && librarySearchPaths.isEmpty) {
      return const GenerationResult.noChangesOrLibraries();
    }
  }

  if (librarySearchPaths == null) {
    librarySearchPaths = const ['lib'];
  }

  var foundFiles =
      await getDartFiles(projectPath, searchList: librarySearchPaths);

  if (changeFilePaths == null || changeFilePaths.isEmpty) {
    changeFilePaths =
        foundFiles.map((path) => p.relative(path, from: projectPath)).toList();
  }

  // If any generator specifies that it tracks a non-Dart source file, include
  // all files next to any changed file.
  // TODO: test this feature set
  var includeDir = generators
      .any((g) => g.associatedFileSet == AssociatedFileSet.sameDirectory);
  if (includeDir) {
    changeFilePaths = expandFileListToIncludePeers(changeFilePaths);
  }

  var fullPaths = changeFilePaths
      .where(pathToDartFile)
      .where((path) => !isGeneratedFile(path))
      .map((path) => p.join(projectPath, path))
      .where((path) => FileSystemEntity.isFileSync(path));

  var context = await getAnalysisContextForProjectPath(projectPath, foundFiles);

  var libs = getLibraries(context, fullPaths);

  if (libs.isEmpty) {
    return new GenerationResult.noLibrariesFound(changeFilePaths);
  }

  var results = <LibraryGenerationResult>[];

  await Future.forEach(libs, (elementLibrary) async {
    var msg = await _generateForLibrary(
        elementLibrary, projectPath, generators, !omitGenerateTimestamp);
    results.add(msg);
  });

  return new GenerationResult.okay(results);
}

Future<LibraryGenerationResult> _generateForLibrary(
    LibraryElement library,
    String projectPath,
    List<Generator> generators,
    bool includeTimestamp) async {
  var generatedOutputs = await _generate(library, generators).toList();

  var genFileName = _getGeneratedFilePath(library, projectPath);
<commit_msg>simplify the generate code a bit<commit_after>// Copyright (c) 2015, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library source_gen.generate;

import 'dart:async';
import 'dart:io';

import 'package:analyzer/src/generated/element.dart';
import 'package:analyzer/src/generated/source_io.dart';
import 'package:dart_style/src/dart_formatter.dart';
import 'package:path/path.dart' as p;

import 'generated_output.dart';
import 'generator.dart';
import 'io.dart';
import 'results.dart';
import 'utils.dart';

/// Updates generated code for [projectPath] with the provided [generators].
///
/// [changeFilePaths] and [librarySearchPaths] must be relative to
/// [projectPath].
///
/// If [librarySearchPaths] is not provided, `['lib']` is used.
///
/// if [omitGenerateTimestamp] is `true`, no timestamp will be added to the
/// output. The default value is `false`.
Future<GenerationResult> generate(
    String projectPath, List<Generator> generators,
    {Iterable<String> changeFilePaths,
    List<String> librarySearchPaths,
    bool omitGenerateTimestamp}) async {
  if (omitGenerateTimestamp == null) {
    omitGenerateTimestamp = false;
  }

  if (changeFilePaths == null || changeFilePaths.isEmpty) {
    if (librarySearchPaths != null && librarySearchPaths.isEmpty) {
      return const GenerationResult.noChangesOrLibraries();
    }
  }

  if (librarySearchPaths == null) {
    librarySearchPaths = const ['lib'];
  }

  var foundFiles =
      await getDartFiles(projectPath, searchList: librarySearchPaths);

  if (changeFilePaths == null || changeFilePaths.isEmpty) {
    changeFilePaths =
        foundFiles.map((path) => p.relative(path, from: projectPath)).toList();
  }

  // If any generator specifies that it tracks a non-Dart source file, include
  // all files next to any changed file.
  // TODO: test this feature set
  var includeDir = generators
      .any((g) => g.associatedFileSet == AssociatedFileSet.sameDirectory);
  if (includeDir) {
    changeFilePaths = expandFileListToIncludePeers(changeFilePaths);
  }

  var fullPaths = changeFilePaths
      .where(pathToDartFile)
      .where((path) => !isGeneratedFile(path))
      .map((path) => p.join(projectPath, path))
      .where((path) => FileSystemEntity.isFileSync(path));

  var context = await getAnalysisContextForProjectPath(projectPath, foundFiles);

  var libs = getLibraries(context, fullPaths);

  if (libs.isEmpty) {
    return new GenerationResult.noLibrariesFound(changeFilePaths);
  }

  var results = <LibraryGenerationResult>[];

  for (var elementLibrary in libs) {
    var msg = await _generateForLibrary(
        elementLibrary, projectPath, generators, !omitGenerateTimestamp);
    results.add(msg);
  }

  return new GenerationResult.okay(results);
}

Future<LibraryGenerationResult> _generateForLibrary(
    LibraryElement library,
    String projectPath,
    List<Generator> generators,
    bool includeTimestamp) async {
  var generatedOutputs = await _generate(library, generators).toList();

  var genFileName = _getGeneratedFilePath(library, projectPath);

{'fraction_non_alphanumeric': 0.11323987538940809, 'fraction_numerical': 0.0012461059190031153, 'mean_word_length': 4.297854785478548, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5774082', 'n_tokens_mistral': 1880, 'n_tokens_neox': 1814, 'n_words': 512}