create method
Method called on app creation
Implementation
void create(List<String> args) async {
final ArgParser parser = ArgParser()
..addOption(
"name",
abbr: "n",
defaultsTo: null,
)
..addOption(
"template",
abbr: "t",
defaultsTo: null,
)
..addOption(
"org",
abbr: "o",
defaultsTo: null,
)
..addFlag(
"config",
abbr: "c",
negatable: false,
defaultsTo: false,
)
..addFlag(
"save",
abbr: "s",
negatable: false,
defaultsTo: false,
)
..addFlag(
"help",
abbr: "h",
negatable: false,
defaultsTo: false,
);
final results = parser.parse(args);
final bool save = results["save"];
final bool showConfig = results["config"];
final bool showHelp = results["help"];
if (showHelp) {
_showHelp();
return;
}
final AppModel appModelFomConfig = AppModel.fromConfigFile();
if (showConfig) {
Logger.logConfigKeyValue("name", appModelFomConfig.name);
Logger.logConfigKeyValue("organization", appModelFomConfig.organization);
Logger.logConfigKeyValue(
"template", appModelFomConfig.templateRepository);
return;
}
final AppModel appModel = AppModel(
name: results["name"] ?? appModelFomConfig.name,
organization: results["org"] ?? appModelFomConfig.organization,
templateRepository:
results["template"] ?? appModelFomConfig.templateRepository,
);
bool hasOneFiledNull = false;
if (appModel.name == null) {
Logger.logError(
"Package identifier argument not found, neither in config. --name or -n to add one.");
hasOneFiledNull = true;
}
if (appModel.organization == null) {
Logger.logError(
"Organization identifier not found, neither in config. --org or -o to add one.");
hasOneFiledNull = true;
}
if (appModel.templateRepository == null) {
Logger.logError(
"Template url not found, neither in config. --template or -t to use one.");
hasOneFiledNull = true;
}
if (!appModel.hasValidPackageName()) {
Logger.logError("${appModel.name} is not a dart valid package name");
hasOneFiledNull = true;
}
if (hasOneFiledNull) return;
if (save) {
appModel.writeInConfigFile();
}
Logger.logInfo("Let's create ${appModel.name} application !");
final Directory current = Directory.current;
final String workingDirectoryPath = current.path;
try {
Logger.logInfo(
"Creating flutter project using your current flutter version...");
Process.runSync(
"flutter",
[
"create",
"--org",
appModel.organization!,
appModel.name!,
],
workingDirectory: workingDirectoryPath,
runInShell: true,
);
Logger.logInfo(
"Retrieving your template from ${appModel.templateRepository}...");
Process.runSync(
"git",
[
"clone",
appModel.templateRepository!,
"temp",
],
workingDirectory: "$workingDirectoryPath",
runInShell: true,
);
final String content =
await File("$workingDirectoryPath/temp/pubspec.yaml").readAsString();
final mapData = loadYaml(content);
final String templatePackageName = mapData["name"];
_copyPasteDirectory(
"$workingDirectoryPath/temp/lib",
"$workingDirectoryPath/${appModel.name}/lib",
);
_copyPasteDirectory(
"$workingDirectoryPath/temp/test",
"$workingDirectoryPath/${appModel.name}/test",
);
await _copyPasteFileContent(
"$workingDirectoryPath/temp/pubspec.yaml",
"$workingDirectoryPath/${appModel.name}/pubspec.yaml",
);
await _changeAllInFile(
"$workingDirectoryPath/${appModel.name}/pubspec.yaml",
templatePackageName,
appModel.name!,
);
await _changeAllInDirectory(
"$workingDirectoryPath/${appModel.name}/lib",
templatePackageName,
appModel.name!,
);
await _changeAllInDirectory(
"$workingDirectoryPath/${appModel.name}/test",
templatePackageName,
appModel.name!,
);
Process.runSync(
"flutter",
[
"pub",
"get",
],
workingDirectory: "$workingDirectoryPath/${appModel.name}",
);
Logger.logInfo("Deleting temp files used for generation...");
Process.runSync(
"rm",
[
"-rf",
"$workingDirectoryPath/temp",
],
);
Logger.logInfo("You are good to go ! :)", lineBreak: true);
} catch (error) {
Logger.logError("Error creating project : $error");
Process.runSync(
"rm",
[
"-rf",
"$workingDirectoryPath/${appModel.name}",
],
);
Process.runSync(
"rm",
[
"-rf",
"$workingDirectoryPath/temp",
],
);
}
}