wyatt-bricks/tools/brick_generator/bin/brick_generator.dart
2022-08-10 15:48:45 +01:00

72 lines
2.0 KiB
Dart

import 'dart:io';
import 'package:brick_generator/file_system_utils.dart';
import 'package:brick_generator/models/brick_config.dart';
import 'package:brick_generator/shell.dart';
import 'package:brick_generator/yaml_reader.dart';
import 'package:path/path.dart' as path;
// Constants
const _bricks = 'bricks';
const _brickFolderLabel = '__brick__';
const _yamlFileName = 'brick_config.yaml';
Future<void> main(List<String> arguments) async {
try {
if (arguments.length != 1) {
throw ArgumentError('Please entry exemple project path');
}
final projectPath = arguments[0];
// Store options from yaml file
final configs =
YamlReader.readYamlFile(path.join(projectPath, _yamlFileName));
final brickConfig = BrickConfig.from(configs);
stdout.writeln('🍺 config retrieved');
brickConfig?.checkFormat();
// Define paths
final sourcePath = path.join(
projectPath,
brickConfig!.pathToBrickify,
);
final targetPath = path.join(
_bricks,
brickConfig.brickName,
_brickFolderLabel,
);
stdout.writeln('🍺 path defined');
// Remove previously generated files
final targetDir = Directory(targetPath);
if (targetDir.existsSync()) {
await targetDir.delete(recursive: true);
}
// create target folder
await Shell.mkdir(targetPath);
// Copy project files
await Shell.cp(sourcePath, targetPath);
stdout.writeln('🍺 files copied');
// Convert values to variables
await FileSystemUtils.convertValuesToVariablesInFolder(
brickConfig, targetPath);
stdout.writeln('🍺 values converted into variables');
// Rename files and folders
await FileSystemUtils.renamePathsInFolder(brickConfig, targetPath);
stdout.writeln('🍺 folders and files renamed');
await FileSystemUtils.deleteEmptyFolders(targetPath);
stdout
..writeln('🍺 empty folders removed')
..writeln('✅ brick template available at $targetPath');
} catch (_) {
stdout.writeln('${_.toString()}');
}
}