analyze function analyzer

AnalysisResult analyze({
  1. String? pathToFile,
  2. String? fileContent,
  3. String? pathToOutputFolder,
  4. bool overwrite = true,
})

The analyzer can read files and return metadata about (dart) classes.

Metadata can be read and written as JSON files. The analyzer supports:

  1. Any .dart file containing valid dart classes.
  2. Metadata file in AST JSON format.

Implementation

AnalysisResult analyze({
  /// File to be analyzed.
  ///
  /// Must be a valid .dart file containing 1 or more classes
  /// or a metadata JSON file.
  String? pathToFile,

  /// Content of file to be analysed.
  String? fileContent,

  /// Folder to store analysis result as JSON.
  ///
  /// For each data class a new file is created in this folder.
  String? pathToOutputFolder,

  /// Allow overwriting existing output files or not.
  bool overwrite = true,
}) {
  File? file;

  if (pathToFile != null) {
    file = File(pathToFile);
  }

  if (fileContent != null) {
    file = Directory.systemTemp.resolve("squinttempfile.dart")
      ..createSync()
      ..writeAsStringSync(fileContent);
  }

  if (!(file?.existsSync() ?? false)) {
    throw SquintException("File does not exist: ${file?.path ?? ''}");
  }

  final result = file!.path.contains(metadataMarkerPrefix)
      ? file.parseMetadata
      : file.parseDataClass;

  if (pathToOutputFolder != null) {
    if (!Directory(pathToOutputFolder).existsSync()) {
      throw SquintException("Folder does not exist: $pathToOutputFolder");
    }

    result.saveAsJson(pathToOutputFolder, overwrite: overwrite);
  }

  return result;
}