generateEndpoints function

Future<void> generateEndpoints()

Implementation

Future<void> generateEndpoints() async {
  //file loading
  //get currectdirectory
  final pathToFolder = Directory.current.path.replaceAll('\\', '/');
  //read the yamlfile
  final yamlFile = File('$pathToFolder/endpoints.yaml');
  //if file is exidentally deleted recreate it
  if (!await yamlFile.exists()) {
    await yamlFile.create();
    return;
  }
  //load the yaml from the file
  final YamlMap yaml = loadYaml(await yamlFile.readAsString());
  //stop if the file is empty
  if (yaml.isEmpty) {
    print('endpoints.yaml is empty');
    return;
  }
  //get the generated file location
  final generatedFile = File('$pathToFolder/lib/endpoints.dart');

  //"refresh" file (delete and recreate it)
  if (await generatedFile.exists()) {
    await generatedFile.delete();
  }
  await generatedFile.create();

  //file writing
  //create the endpoints tree structure
  GeneratedEndpoint endpoint = GeneratedEndpoint(yaml);
  //create the buffer with the required imports
  StringBuffer fileContents = StringBuffer(
      '// ignore_for_file: library_private_types_in_public_api \n import "dart:convert";\n\nimport "package:http/http.dart" as http; \n\nimport "./model.dart"; \n\n\n');
  //generate the tree structure
  endpoint.generate(fileContents);
  //write the generated string to file
  generatedFile.writeAsString(fileContents.toString());
}