execute method
Implementation
Future<void> execute() async {
Logger.header('Initialize Maloc Project');
// Determine target directory
final target = targetPath ?? Directory.current.path;
final targetDir = Directory(target);
// Get project name from directory name
final projectName = targetDir.absolute.path
.split(Platform.pathSeparator)
.last;
// Check if directory exists, create if not
if (!targetDir.existsSync()) {
Logger.step('Creating directory: $target');
targetDir.createSync(recursive: true);
}
// Check if directory is empty
final contents = targetDir.listSync();
if (contents.isNotEmpty) {
Logger.warning('Directory is not empty!');
stdout.write('Continue anyway? (y/n): ');
final response = stdin.readLineSync()?.toLowerCase().trim();
if (response != 'y' && response != 'yes') {
Logger.info('Operation cancelled.');
exit(0);
}
}
// Get additional info
final packageName = _promptForInput(
'Package name (e.g., com.company.appname)',
defaultValue: 'com.example.$projectName',
);
final description = _promptForInput(
'Project description',
defaultValue: 'A new Flutter project with Clean Architecture',
);
Logger.header('Initializing Project: $projectName');
print('');
try {
// Step 1: Download template from GitHub
Logger.step('Downloading template from GitHub...');
final tempDir = Directory('${targetDir.path}/.maloc_temp');
final cloneResult = await Process.run('git', [
'clone',
'--depth=1',
'https://github.com/Farhan-S/flutter_monorepo_clean_architecture.git',
tempDir.path,
]);
if (cloneResult.exitCode != 0) {
Logger.error('Failed to download template repository');
print(cloneResult.stderr);
exit(1);
}
Logger.success('Template downloaded successfully');
// Step 2: Copy template files to target directory
Logger.step('Extracting template files...');
await _copyDirectory(tempDir, targetDir, exclude: ['.git', 'cli']);
Logger.success('Template files extracted');
// Step 3: Clean up temporary directory
Logger.step('Cleaning up...');
tempDir.deleteSync(recursive: true);
Logger.success('Cleanup complete');
// Step 4: Initialize git repository if not exists
final gitDir = Directory('${targetDir.path}/.git');
if (!gitDir.existsSync()) {
Logger.step('Initializing git repository...');
await Process.run('git', ['init'], workingDirectory: targetDir.path);
Logger.success('Git repository initialized');
}
// Step 5: Update pubspec.yaml files
Logger.step('Updating project configuration...');
await _updatePubspecFiles(
targetDir.path,
projectName,
packageName,
description,
);
Logger.success('Configuration updated');
// Step 6: Run melos bootstrap
Logger.step('Installing dependencies (this may take a while)...');
final bootstrapFile = File('${targetDir.path}/bootstrap.dart');
if (bootstrapFile.existsSync()) {
final bootstrapResult = await Process.run('dart', [
'bootstrap.dart',
], workingDirectory: targetDir.path);
if (bootstrapResult.exitCode == 0) {
Logger.success('Dependencies installed');
} else {
Logger.warning(
'Some dependencies failed to install. You can run "dart bootstrap.dart" later.',
);
}
} else {
Logger.warning(
'bootstrap.dart not found. Skipping dependency installation.',
);
}
// Step 7: Initial git commit (if new repo)
if (!gitDir.existsSync() || (await _isGitRepoEmpty(targetDir.path))) {
Logger.step('Creating initial commit...');
await Process.run('git', [
'add',
'.',
], workingDirectory: targetDir.path);
await Process.run('git', [
'commit',
'-m',
'Initial commit',
], workingDirectory: targetDir.path);
Logger.success('Initial commit created');
}
Logger.header('Project Initialized Successfully! 🎉');
print('''
${Logger.green}✨ Next steps:${Logger.reset}
1. Navigate to your project (if not already there):
${Logger.cyan}cd $target${Logger.reset}
2. Open in your IDE:
${Logger.cyan}code .${Logger.reset} or ${Logger.cyan}open -a "Android Studio" .${Logger.reset}
3. Run the app:
${Logger.cyan}flutter run${Logger.reset}
4. Create a new feature:
${Logger.cyan}maloc feature feature_name${Logger.reset}
${Logger.yellow}📚 Documentation:${Logger.reset}
• README.md - Project overview and setup
• Check packages/ folder for modular structure
${Logger.green}Happy coding! 🚀${Logger.reset}
''');
} catch (e) {
Logger.error('Failed to initialize project: $e');
// Cleanup temp directory on failure
final tempDir = Directory('${targetDir.path}/.maloc_temp');
if (tempDir.existsSync()) {
Logger.step('Cleaning up...');
tempDir.deleteSync(recursive: true);
}
exit(1);
}
}