createSlate static method
Creates a new Slate using templates
.
Implementation
static createSlate(List<NyTemplate> templates, {bool? hasForceFlag}) async {
String pubspecYaml = await MetroService.loadAsset('pubspec.yaml');
for (var template in templates) {
for (var pluginRequired in template.pluginsRequired) {
if ((!pubspecYaml.contains(pluginRequired))) {
MetroConsole.writeInRed(
"Your project is missing the $pluginRequired package in your pubspec.yaml file.");
MetroConsole.writeInGreen("Run 'flutter pub add $pluginRequired'");
exit(1);
}
}
}
for (var template in templates) {
switch (template.saveTo) {
case controllersFolder:
{
await makeController(template.name, template.stub,
forceCreate: (hasForceFlag ?? false));
break;
}
case widgetsFolder:
{
await makeStatelessWidget(template.name, template.stub,
forceCreate: (hasForceFlag ?? false));
break;
}
case pagesFolder:
{
await makePage(template.name, template.stub,
forceCreate: (hasForceFlag ?? false));
String className = template.name;
String? creationPath;
if (className.contains("/")) {
List<String> pathSegments =
Uri.parse(className).pathSegments.toList();
className = pathSegments.removeLast();
String folder = pagesFolder;
for (var segment in pathSegments) {
await MetroService.makeDirectory("$folder/$segment");
folder += '/$segment';
}
creationPath = pathSegments.join("/");
}
String classImport =
"import '/resources/pages/${creationPath != null ? '$creationPath/' : ''}${className.toLowerCase()}_page.dart';";
ReCase classReCase = ReCase(className);
await addToRouter(
classImport: classImport,
createTemplate: (file) {
String routeName =
'router.route("/${classReCase.pathCase}", (context) => ${classReCase.pascalCase}Page());';
if (file.contains(routeName)) {
return "";
}
RegExp reg = RegExp(
r'appRouter\(\) => nyRoutes\(\(router\) {([^}]*)}\);');
if (reg
.allMatches(file)
.map((e) => e.group(1))
.toList()
.isEmpty) {
return "";
}
String temp = """
appRouter() => nyRoutes((router) {${reg.allMatches(file).map((e) => e.group(1)).toList()[0]}
router.route("/${classReCase.paramCase}", (context) => ${classReCase.pascalCase}Page());
});
""";
return file.replaceFirst(
RegExp(
r'appRouter\(\) => nyRoutes\(\(router\) {([^}]*)\n}\);'),
temp,
);
});
break;
}
case modelsFolder:
{
await makeModel(template.name, template.stub,
forceCreate: (hasForceFlag ?? false));
ReCase classReCase = ReCase(template.name);
String classImport = makeImportPathModel(classReCase.snakeCase);
await MetroService.addToConfig(
configName: "decoders",
classImport: classImport,
createTemplate: (file) {
String modelName = classReCase.pascalCase;
if (file.contains(modelName)) {
return "";
}
RegExp reg = RegExp(
r'final Map<Type, dynamic> modelDecoders = {([^};]*)};');
String template = """
final Map<Type, dynamic> modelDecoders = {${reg.allMatches(file).map((e) => e.group(1)).toList()[0]}
List<$modelName>: (data) => List.from(data).map((json) => $modelName.fromJson(json)).toList(),
$modelName: (data) => $modelName.fromJson(data),
};""";
return file.replaceFirst(
RegExp(
r'final Map<Type, dynamic> modelDecoders = {([^};]*)};'),
template);
});
break;
}
case themesFolder:
{
await makeTheme(template.name, template.stub,
forceCreate: (hasForceFlag ?? false));
break;
}
case providerFolder:
{
await makeProvider(template.name, template.stub,
forceCreate: (hasForceFlag ?? false));
break;
}
case eventsFolder:
{
await makeEvent(template.name, template.stub,
forceCreate: (hasForceFlag ?? false));
break;
}
case networkingFolder:
{
await makeApiService(template.name, template.stub,
forceCreate: (hasForceFlag ?? false));
break;
}
case themeColorsFolder:
{
await makeThemeColors(template.name, template.stub,
forceCreate: (hasForceFlag ?? false));
break;
}
default:
{
continue;
}
}
MetroConsole.writeInGreen('${template.name} created 🎉');
}
}