wyatt-bricks/tools/brick_generator/bin/brick_generator.dart

94 lines
2.5 KiB
Dart

import 'dart:io';
import 'package:brick_generator/file_system_utils.dart';
import 'package:brick_generator/logger.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';
const _cfgFileName = 'brick.yaml';
Future<void> main(List<String> arguments) async {
try {
if (arguments.length != 1) {
Logger.error('Please provide project path.');
exit(1);
}
final projectPath = arguments[0];
// Store options from yaml file
final configs =
YamlReader.readYamlFile(path.join(projectPath, _yamlFileName));
final brickConfig = BrickConfig.from(configs);
Logger.info('Config retrieved.');
brickConfig?.checkFormat();
// Define paths
final sourcePath = path.join(
projectPath,
brickConfig!.pathToBrickify,
);
final cfgTarget = path.join(
_bricks,
brickConfig.name,
_cfgFileName,
);
final targetPath = path.join(
_bricks,
brickConfig.name,
_brickFolderLabel,
);
Logger.info('Path defined.');
// Remove previously generated files
final targetDir = Directory(targetPath);
if (targetDir.existsSync()) {
await targetDir.delete(recursive: true);
}
final cfgTargetFile = File(cfgTarget);
if (cfgTargetFile.existsSync()) {
await cfgTargetFile.delete();
}
// create target folder
await Shell.mkdir(targetPath);
// Copy project files
await Shell.cp(sourcePath, targetPath);
Logger.info('Files copied.');
// Remove ignored folders
final brickPath = path.join(targetPath, brickConfig.pathToBrickify);
await FileSystemUtils.deleteIgnoredFolders(brickConfig, brickPath);
// Convert values to variables
await FileSystemUtils.convertValuesToVariablesInFolder(
brickConfig,
targetPath,
);
Logger.info('Values converted into variables.');
// Rename files and folders
await FileSystemUtils.renamePathsInFolder(brickConfig, targetPath);
Logger.info('Folders and files renamed.');
// Create config file
cfgTargetFile.writeAsStringSync(brickConfig.toBrickYaml());
Logger.info('brick.yml added.');
await FileSystemUtils.deleteEmptyFolders(targetPath);
Logger.info('Empty folders removed');
Logger.success('Brick template available at $targetPath');
} catch (e) {
Logger.error(e);
}
}