createLaunchJson method
Generates VS Code launch configurations based on environment settings.
Implementation
Future<void> createLaunchJson() async {
// Load the pubspec.yaml file from the Flutter project
final pubspecFile = File('pubspec.yaml');
if (!pubspecFile.existsSync()) {
logger.e('pubspec.yaml not found in the current directory.');
return;
}
final pubspecContent = pubspecFile.readAsStringSync();
final pubspecYaml = loadYaml(pubspecContent);
final appName = pubspecYaml['name'];
if (appName == null) {
logger.e('Error: Unable to find the app name in pubspec.yaml.');
return;
}
final vscodeDir = Directory('.vscode');
if (!vscodeDir.existsSync()) {
vscodeDir.createSync();
}
// Define the launch configurations
final launchConfigurations = [
{
"name": "$appName dev",
"request": "launch",
"type": "dart",
"args": ["--flavor", "dev", "--dart-define-from-file", "env_dev.json"]
},
{
"name": "$appName (profile mode) dev",
"request": "launch",
"type": "dart",
"flutterMode": "profile",
"args": ["--flavor", "dev", "--dart-define-from-file", "env_dev.json"]
},
{
"name": "$appName (release mode) dev",
"request": "launch",
"type": "dart",
"flutterMode": "release",
"args": ["--flavor", "dev", "--dart-define-from-file", "env_dev.json"]
},
{
"name": "$appName staging",
"request": "launch",
"type": "dart",
"args": [
"--flavor",
"staging",
"--dart-define-from-file",
"env_staging.json"
]
},
{
"name": "$appName (profile mode) staging",
"request": "launch",
"type": "dart",
"flutterMode": "profile",
"args": [
"--flavor",
"staging",
"--dart-define-from-file",
"env_staging.json"
]
},
{
"name": "$appName (release mode) staging",
"request": "launch",
"type": "dart",
"flutterMode": "release",
"args": [
"--flavor",
"staging",
"--dart-define-from-file",
"env_staging.json"
]
},
{
"name": "$appName prod",
"request": "launch",
"type": "dart",
"args": ["--flavor", "prod", "--dart-define-from-file", "env_prod.json"]
},
{
"name": "$appName (profile mode) prod",
"request": "launch",
"type": "dart",
"flutterMode": "profile",
"args": ["--flavor", "prod", "--dart-define-from-file", "env_prod.json"]
},
{
"name": "$appName (release mode) prod",
"request": "launch",
"type": "dart",
"flutterMode": "release",
"args": ["--flavor", "prod", "--dart-define-from-file", "env_prod.json"]
}
];
// Write the launch configuration JSON to a file
final launchConfigFile = File('.vscode/launch.json');
final launchConfigJson = {
"version": "0.2.0",
"configurations": launchConfigurations
};
await launchConfigFile.writeAsString(
const JsonEncoder.withIndent(' ').convert(launchConfigJson),
flush: true,
);
}