generate method

Library generate()

Implementation

Library generate() {
  // if true use an awaited initializer
  final hasPreResolvedDeps = hasPreResolvedDependencies(_dependencies);

  // all environment keys used
  final environments = <String>{};
  // all register modules
  final modules = <ModuleConfig>{};
  _dependencies.forEach((dep) {
    environments.addAll(dep.environments);
    if (dep.moduleConfig != null) {
      modules.add(dep.moduleConfig!);
    }
  });

  final ignoreForFileComments = [
    '// ignore_for_file: unnecessary_lambdas',
    '// ignore_for_file: lines_longer_than_80_chars'
  ];
  final getInstanceRefer = refer(_asExtension ? 'this' : 'get');
  final intiMethod = Method(
    (b) => b
      ..docs.addAll([
        if (!_asExtension) ...ignoreForFileComments,
        '/// initializes the registration of provided dependencies inside of [GetIt]'
      ])
      ..returns = hasPreResolvedDeps
          ? TypeReference((b) => b
            ..symbol = 'Future'
            ..types.add(_getItRefer))
          : _getItRefer
      ..name = _initializerName
      ..modifier = hasPreResolvedDeps ? MethodModifier.async : null
      ..requiredParameters.addAll([
        if (!_asExtension)
          Parameter(
            (b) => b
              ..name = 'get'
              ..type = _getItRefer,
          )
      ])
      ..optionalParameters.addAll([
        Parameter((b) => b
          ..named = true
          ..name = 'environment'
          ..type = nullableRefer(
            'String',
            nullable: true,
          )),
        Parameter((b) => b
          ..named = true
          ..name = 'environmentFilter'
          ..type = nullableRefer(
            'EnvironmentFilter',
            url: _injectableImport,
            nullable: true,
          ))
      ])
      ..body = Block(
        (b) => b.statements.addAll([
          refer('GetItHelper', _injectableImport)
              .newInstance(
                [
                  getInstanceRefer,
                  refer('environment'),
                  refer('environmentFilter'),
                ],
              )
              .assignFinal('gh')
              .statement,
          ...modules.map((module) => refer('_\$${module.type.name}')
              .call([
                if (moduleHasOverrides(
                  _dependencies.where((e) => e.moduleConfig == module),
                ))
                  getInstanceRefer
              ])
              .assignFinal(toCamelCase(module.type.name))
              .statement),
          ..._dependencies.map((dep) {
            if (dep.injectableType == InjectableType.singleton) {
              return buildSingletonRegisterFun(dep);
            } else {
              return buildLazyRegisterFun(dep);
            }
          }),
          getInstanceRefer.returned.statement,
        ]),
      ),
  );

  return Library(
    (b) => b
      ..body.addAll(
        [
          ...environments.map((env) => Field(
                (b) => b
                  ..name = '_$env'
                  ..type = refer('String')
                  ..assignment = literalString(env).code
                  ..modifier = FieldModifier.constant,
              )),

          if (_asExtension)
            Extension(
              (b) => b
                ..docs.addAll([
                  ...ignoreForFileComments,
                  '/// an extension to register the provided dependencies inside of [GetIt]',
                ])
                ..name = 'GetItInjectableX'
                ..on = _getItRefer
                ..methods.add(intiMethod),
            ),
          if (!_asExtension) intiMethod,
          // build modules
          ...modules.map(
            (module) => _buildModule(
              module,
              _dependencies.where((e) => e.moduleConfig == module),
            ),
          )
        ],
      ),
  );
}