generateComponent method

Future<RenderResult> generateComponent({
  1. required String rawFeatureName,
  2. required String componentType,
  3. required String rawComponentName,
  4. required String projectPath,
})

Generates a single component (screen, model, repository) into projectPath.

Implementation

Future<RenderResult> generateComponent({
  required String rawFeatureName,
  required String componentType, // 'screen', 'model', 'repository'
  required String rawComponentName,
  required String projectPath,
}) async {
  final projectDir = Directory(projectPath);
  Validators.validateExistingFlutterProject(projectDir);

  final variables = VariableResolver.resolveComponent(
    rawFeatureName: rawFeatureName,
    componentType: componentType,
    rawComponentName: rawComponentName,
  );

  final featureSnakeCase = variables['feature_name_snake_case']!;
  final targetFeatureDir =
      Directory(p.join(projectPath, 'lib', 'features', featureSnakeCase));

  if (!targetFeatureDir.existsSync()) {
    throw ValidationException(
      'Feature "$rawFeatureName" does not exist at "${targetFeatureDir.path}". Create the feature first using "flutter_setup feature $rawFeatureName".',
    );
  }

  final fragmentsBasePath = await _getFragmentsBasePath();
  final fragmentDir =
      Directory(p.join(fragmentsBasePath, componentType.toLowerCase()));
  if (!fragmentDir.existsSync()) {
    throw CliException(
        'Fragment template for "$componentType" not found at "${fragmentDir.path}".');
  }

  final fragmentTemplate = Template(
    name: '${componentType}_fragment',
    description: '$componentType fragment',
    path: fragmentDir.path,
    config: const TemplateConfig(
      name: 'component_fragment',
      description: '',
      dependencies: [],
      folders: [],
    ),
  );

  return _renderWithScopedRollback(
    template: fragmentTemplate,
    variables: variables,
    projectPath: projectPath,
  );
}