generate method

FeatureGenerationResult generate({
  1. required Directory projectRoot,
  2. required ArchSherpaConfig config,
  3. required String featureName,
  4. bool dryRun = false,
})

Generates a feature scaffold under the configured features base path.

Returned paths are relative to projectRoot.

Implementation

FeatureGenerationResult generate({
  required Directory projectRoot,
  required ArchSherpaConfig config,
  required String featureName,
  bool dryRun = false,
}) {
  final created = <String>[];
  final skipped = <String>[];
  final rootPath = projectRoot.path;

  final featureRoot = p.join(config.features.basePath, featureName);
  final featureRootAbsolute = _fileUtils.resolveSafePath(
    projectRootPath: rootPath,
    relativePath: featureRoot,
  );
  final featureRootExists = Directory(featureRootAbsolute).existsSync();
  final featureRootCreated = dryRun
      ? !featureRootExists
      : _fileUtils.createDirectoryIfMissing(featureRootAbsolute);
  (featureRootCreated ? created : skipped).add(featureRoot);

  for (final entry in config.features.structure.entries) {
    final section = entry.key;
    final folders = entry.value;

    final sectionPath = p.join(featureRoot, section);
    final sectionAbsolute = _fileUtils.resolveSafePath(
      projectRootPath: rootPath,
      relativePath: sectionPath,
    );
    final sectionExists = Directory(sectionAbsolute).existsSync();
    final sectionCreated = dryRun
        ? !sectionExists
        : _fileUtils.createDirectoryIfMissing(sectionAbsolute);
    (sectionCreated ? created : skipped).add(sectionPath);

    for (final folder in folders) {
      final folderPath = p.join(sectionPath, folder);
      final folderAbsolute = _fileUtils.resolveSafePath(
        projectRootPath: rootPath,
        relativePath: folderPath,
      );
      final folderExists = Directory(folderAbsolute).existsSync();
      final folderCreated = dryRun
          ? !folderExists
          : _fileUtils.createDirectoryIfMissing(folderAbsolute);
      (folderCreated ? created : skipped).add(folderPath);
    }
  }

  final templates = _featureTemplates(
    featureName: featureName,
    stateType: config.stateManagement.type,
    testsEnabled: config.tests.enabled,
    structure: config.features.structure,
  );
  for (final entry in templates.entries) {
    final relative = entry.key;
    final absolute = _fileUtils.resolveSafePath(
      projectRootPath: rootPath,
      relativePath: relative,
    );
    final exists = File(absolute).existsSync();
    final wrote = dryRun
        ? !exists
        : _fileUtils.writeFileIfMissing(
            filePath: absolute, contents: entry.value);
    (wrote ? created : skipped).add(relative);
  }

  return FeatureGenerationResult(
    createdPaths: created,
    skippedPaths: skipped,
  );
}