load static method

Future<GeneratorConfig> load([
  1. String dir = ''
])

Create a new GeneratorConfig by loading the configuration in the dir.

Implementation

static Future<GeneratorConfig> load([String dir = '']) async {
  var serverPackageDirectoryPathParts = p.split(dir);

  Pubspec? pubspec;
  try {
    pubspec = parsePubspec(File(p.join(dir, 'pubspec.yaml')));
  } catch (_) {}

  if (pubspec == null) {
    log.error(
      'Failed to load pubspec.yaml. Are you running serverpod from your '
      'projects root directory?',
    );

    throw const ServerpodProjectNotFoundException(
      'Failed to load valid pubspec.yaml',
    );
  }

  if (!isServerDirectory(Directory(dir))) {
    log.error(
      'Could not find the Serverpod dependency. Are you running serverpod from your '
      'projects root directory?',
    );

    throw const ServerpodProjectNotFoundException(
      'Serverpod dependency not found.',
    );
  }

  var serverPackage = pubspec.name;
  var name = _stripPackage(serverPackage);

  Map generatorConfig = {};
  try {
    var file = File(p.join(dir, 'config', 'generator.yaml'));
    var yamlStr = file.readAsStringSync();
    generatorConfig = loadYaml(yamlStr);
  } catch (_) {}

  PackageType type = getPackageType(generatorConfig);

  var relativeDartClientPackagePathParts = ['..', '${name}_client'];

  if (generatorConfig['client_package_path'] != null) {
    relativeDartClientPackagePathParts =
        p.split(generatorConfig['client_package_path']);
  }

  late String dartClientPackage;
  late bool dartClientDependsOnServiceClient;

  try {
    var file = File(p.joinAll([
      ...serverPackageDirectoryPathParts,
      ...relativeDartClientPackagePathParts,
      'pubspec.yaml'
    ]));
    var yamlStr = file.readAsStringSync();
    var yaml = loadYaml(yamlStr);
    dartClientPackage = yaml['name'];
    dartClientDependsOnServiceClient =
        yaml['dependencies'].containsKey('serverpod_service_client');
  } catch (_) {
    log.error(
      'Failed to load client pubspec.yaml. If you are using a none default '
      'path it has to be specified in the config/generator.yaml file!',
    );
    throw const ServerpodProjectNotFoundException(
      'Failed to load client pubspec.yaml',
    );
  }

  var manualModules = <String, String?>{};
  if (generatorConfig['modules'] != null) {
    Map modulesData = generatorConfig['modules'];
    for (var package in modulesData.keys) {
      var nickname = modulesData[package]?['nickname'];
      manualModules[package] = nickname is String ? nickname : null;
    }
  }

  var modules = await locateModules(
    directory: Directory(dir),
    manualModules: manualModules,
  );

  if (modules == null) {
    throw const ServerpodModulesNotFoundException(
      'Failed to locate modules',
    );
  }

  // Load extraClasses
  var extraClasses = <TypeDefinition>[];
  if (generatorConfig['extraClasses'] != null) {
    try {
      for (var extraClassConfig in generatorConfig['extraClasses']) {
        extraClasses.add(
          parseType(
            extraClassConfig,
            extraClasses: null,
          ),
        );
      }
    } on SourceSpanException catch (_) {
      rethrow;
    } catch (e) {
      throw SourceSpanFormatException(
          'Failed to load \'extraClasses\' config',
          generatorConfig['extraClasses'].span);
    }
  }

  return GeneratorConfig(
    name: name,
    type: type,
    serverPackage: serverPackage,
    dartClientPackage: dartClientPackage,
    dartClientDependsOnServiceClient: dartClientDependsOnServiceClient,
    serverPackageDirectoryPathParts: serverPackageDirectoryPathParts,
    relativeDartClientPackagePathParts: relativeDartClientPackagePathParts,
    modules: modules,
    extraClasses: extraClasses,
  );
}