generate method

Future<void> generate({
  1. String package = 'my_api',
  2. required String destination,
  3. bool formatOutput = true,
  4. bool quiet = false,
  5. bool replace = false,
  6. SchemaGeneratorOptions schemaOptions = const SchemaGeneratorOptions(),
  7. ClientGeneratorOptions clientOptions = const ClientGeneratorOptions(),
  8. ServerGeneratorOptions serverOptions = const ServerGeneratorOptions(),
})

Generate code from the OpenApi object

The schemas will be generated by default, users can optionally toggle the generation of the client and/or server code

Implementation

Future<void> generate({
  String package = 'my_api',
  required String destination,
  bool formatOutput = true,
  bool quiet = false,
  bool replace = false,
  SchemaGeneratorOptions schemaOptions = const SchemaGeneratorOptions(),
  ClientGeneratorOptions clientOptions = const ClientGeneratorOptions(),
  ServerGeneratorOptions serverOptions = const ServerGeneratorOptions(),
}) async {
  // Ensure that the folder exists
  final dir = Directory(destination);
  final dirPath = p.normalize(dir.absolute.path);

  if (dir.existsSync()) {
    if (replace) {
      await dir.delete(recursive: true);
    } else {
      throw Exception(
        'Destination directory already exists: $dirPath\n\nEither remove it or set the "replace" option to true to delete the existing destination\n',
      );
    }
  }

  if (!dir.existsSync()) {
    dir.createSync(recursive: true);
  }

  // Generate the schemas
  SchemaGenerator? schemaGenerator;
  if (components?.schemas?.isNotEmpty ?? false) {
    schemaGenerator = SchemaGenerator(
      spec: this,
      package: package.snakeCase,
      destination: destination,
      quiet: quiet,
      options: schemaOptions,
    );
    await schemaGenerator.generate();
  } else {
    // ignore: avoid_print
    print(
      'No schemas found in OpenAPI spec - Not generating schema library.',
    );
  }

  if (clientOptions.enabled) {
    if (paths == null || (paths?.isEmpty ?? true)) {
      // ignore: avoid_print
      print(
        'No client paths/operations found in OpenAPI spec - Not generating client library.',
      );
    } else {
      final clientGenerator = ClientGenerator(
        spec: this,
        package: package.snakeCase,
        destination: destination,
        quiet: quiet,
        options: clientOptions,
        schemaGenerator: schemaGenerator,
      );
      await clientGenerator.generate();
    }
  }

  if (serverOptions.enabled) {
    if (paths == null || (paths?.isEmpty ?? true)) {
      // ignore: avoid_print
      print(
        'No client paths/operations found in OpenAPI spec - Not generating server library.',
      );
    } else {
      final serverGenerator = ServerGenerator(
        spec: this,
        package: package.snakeCase,
        destination: destination,
        quiet: quiet,
        options: serverOptions,
      );
      await serverGenerator.generate();
    }
  }

  // Apply the Dart formatting and fix logic
  if (formatOutput) {
    final resultFix =
        await Process.run('dart', ['fix', '--apply', dir.absolute.path]);
    final resultFormat =
        await Process.run('dart', ['format', dir.absolute.path]);
    if (resultFix.exitCode != 0) {
      throw ('\n\nError running dart fix:\n${resultFix.stderr}\n');
    }
    if (resultFormat.exitCode != 0) {
      throw ('\n\nError running dart fix:\n${resultFormat.stderr}\n');
    }
  }
}