Skip to content

Commit

Permalink
fix: stacked config not used on commands (#23)
Browse files Browse the repository at this point in the history
* Improve and refactor ConfigService

* Fix config loading call on commands

* Verify file exists before deletion
  • Loading branch information
ferrarafer authored May 25, 2023
1 parent c9b1766 commit 083aa0b
Show file tree
Hide file tree
Showing 12 changed files with 496 additions and 172 deletions.
26 changes: 16 additions & 10 deletions lib/src/commands/create/create_app_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'package:stacked_cli/src/services/template_service.dart';
import 'package:stacked_cli/src/templates/template_constants.dart';

class CreateAppCommand extends Command {
final _cLog = locator<ColorizedLogService>();
final _log = locator<ColorizedLogService>();
final _configService = locator<ConfigService>();
final _fileService = locator<FileService>();
final _processService = locator<ProcessService>();
Expand Down Expand Up @@ -57,14 +57,16 @@ class CreateAppCommand extends Command {
argParser.addOption(
ksConfigPath,
abbr: 'c',
help: kCommandHelpCreateAppConfigFile,
help: kCommandHelpConfigFilePath,
);
}

@override
Future<void> run() async {
try {
await _configService.loadConfig(path: argResults![ksConfigPath]);
await _configService.findAndLoadConfigFile(
configFilePath: argResults![ksConfigPath],
);

final appName = argResults!.rest.first;
final appNameWithoutPath = appName.split('/').last;
Expand All @@ -74,7 +76,7 @@ class CreateAppCommand extends Command {
_processService.formattingLineLength = argResults![ksLineLength];
await _processService.runCreateApp(appName: appName);

_cLog.stackedOutput(message: 'Add Stacked Magic ... ', isBold: true);
_log.stackedOutput(message: 'Add Stacked Magic ... ', isBold: true);

await _templateService.renderTemplate(
templateName: name,
Expand All @@ -91,7 +93,7 @@ class CreateAppCommand extends Command {
await _processService.runFormat(appName: appName);
await _clean(appName: appName);
} catch (e) {
_cLog.warn(message: e.toString());
_log.warn(message: e.toString());
}
}

Expand All @@ -100,13 +102,17 @@ class CreateAppCommand extends Command {
/// - Deletes widget_test.dart file
/// - Removes unused imports
Future<void> _clean({required String appName}) async {
_cLog.stackedOutput(message: 'Cleaning project...');
_log.stackedOutput(message: 'Cleaning project...');

// Removes `widget_test` file to avoid failing unit tests on created app
await _fileService.deleteFile(
if (await _fileService.fileExists(
filePath: '$appName/test/widget_test.dart',
verbose: false,
);
)) {
await _fileService.deleteFile(
filePath: '$appName/test/widget_test.dart',
verbose: false,
);
}

// Analyze the project and return output lines
final issues = await _processService.runAnalyze(appName: appName);
Expand All @@ -122,7 +128,7 @@ class CreateAppCommand extends Command {
);
}

_cLog.stackedOutput(message: 'Project cleaned.');
_log.stackedOutput(message: 'Project cleaned.');
}

/// Replaces configuration file in the project created.
Expand Down
53 changes: 35 additions & 18 deletions lib/src/commands/create/create_bottom_sheet_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/mixins/project_structure_validator_mixin.dart';
import 'package:stacked_cli/src/services/analytics_service.dart';
import 'package:stacked_cli/src/services/colorized_log_service.dart';
import 'package:stacked_cli/src/services/config_service.dart';
import 'package:stacked_cli/src/services/process_service.dart';
import 'package:stacked_cli/src/services/pubspec_service.dart';
import 'package:stacked_cli/src/services/template_service.dart';
import 'package:stacked_cli/src/templates/template_constants.dart';

class CreateBottomSheetCommand extends Command with ProjectStructureValidator {
final _log = locator<ColorizedLogService>();
final _configService = locator<ConfigService>();
final _processService = locator<ProcessService>();
final _pubspecService = locator<PubspecService>();
Expand Down Expand Up @@ -53,29 +55,44 @@ class CreateBottomSheetCommand extends Command with ProjectStructureValidator {
defaultsTo: 'empty',
help: kCommandHelpCreateBottomSheetTemplate,
);

argParser.addOption(
ksConfigPath,
abbr: 'c',
help: kCommandHelpConfigFilePath,
);
}

@override
Future<void> run() async {
final bottomSheetName = argResults!.rest.first;
final templateType = argResults![ksTemplateType];
unawaited(_analyticsService.createBottomSheetEvent(name: bottomSheetName));
final outputPath = argResults!.rest.length > 1 ? argResults!.rest[1] : null;
await _configService.loadConfig(path: outputPath);
_processService.formattingLineLength = argResults![ksLineLength];
await _pubspecService.initialise(workingDirectory: outputPath);
await validateStructure(outputPath: outputPath);
try {
final bottomSheetName = argResults!.rest.first;
final templateType = argResults![ksTemplateType];
unawaited(
_analyticsService.createBottomSheetEvent(name: bottomSheetName));
final outputPath =
argResults!.rest.length > 1 ? argResults!.rest[1] : null;
await _configService.composeAndLoadConfigFile(
configFilePath: argResults![ksConfigPath],
projectPath: outputPath,
);
_processService.formattingLineLength = argResults![ksLineLength];
await _pubspecService.initialise(workingDirectory: outputPath);
await validateStructure(outputPath: outputPath);

await _templateService.renderTemplate(
templateName: name,
name: bottomSheetName,
outputPath: outputPath,
verbose: true,
excludeRoute: argResults![ksExcludeRoute],
hasModel: argResults![ksModel],
templateType: templateType,
);
await _templateService.renderTemplate(
templateName: name,
name: bottomSheetName,
outputPath: outputPath,
verbose: true,
excludeRoute: argResults![ksExcludeRoute],
hasModel: argResults![ksModel],
templateType: templateType,
);

await _processService.runBuildRunner(appName: outputPath);
await _processService.runBuildRunner(appName: outputPath);
} catch (e) {
_log.warn(message: e.toString());
}
}
}
52 changes: 34 additions & 18 deletions lib/src/commands/create/create_dialog_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/mixins/project_structure_validator_mixin.dart';
import 'package:stacked_cli/src/services/analytics_service.dart';
import 'package:stacked_cli/src/services/colorized_log_service.dart';
import 'package:stacked_cli/src/services/config_service.dart';
import 'package:stacked_cli/src/services/process_service.dart';
import 'package:stacked_cli/src/services/pubspec_service.dart';
import 'package:stacked_cli/src/services/template_service.dart';
import 'package:stacked_cli/src/templates/template_constants.dart';

class CreateDialogCommand extends Command with ProjectStructureValidator {
final _log = locator<ColorizedLogService>();
final _configService = locator<ConfigService>();
final _processService = locator<ProcessService>();
final _pubspecService = locator<PubspecService>();
Expand Down Expand Up @@ -53,29 +55,43 @@ class CreateDialogCommand extends Command with ProjectStructureValidator {
defaultsTo: 'empty',
help: kCommandHelpCreateDialogTemplate,
);

argParser.addOption(
ksConfigPath,
abbr: 'c',
help: kCommandHelpConfigFilePath,
);
}

@override
Future<void> run() async {
final dialogName = argResults!.rest.first;
final templateType = argResults![ksTemplateType];
unawaited(_analyticsService.createDialogEvent(name: dialogName));
final outputPath = argResults!.rest.length > 1 ? argResults!.rest[1] : null;
await _configService.loadConfig(path: outputPath);
_processService.formattingLineLength = argResults![ksLineLength];
await _pubspecService.initialise(workingDirectory: outputPath);
await validateStructure(outputPath: outputPath);
try {
final dialogName = argResults!.rest.first;
final templateType = argResults![ksTemplateType];
unawaited(_analyticsService.createDialogEvent(name: dialogName));
final outputPath =
argResults!.rest.length > 1 ? argResults!.rest[1] : null;
await _configService.composeAndLoadConfigFile(
configFilePath: argResults![ksConfigPath],
projectPath: outputPath,
);
_processService.formattingLineLength = argResults![ksLineLength];
await _pubspecService.initialise(workingDirectory: outputPath);
await validateStructure(outputPath: outputPath);

await _templateService.renderTemplate(
templateName: name,
name: dialogName,
outputPath: outputPath,
verbose: true,
excludeRoute: argResults![ksExcludeRoute],
hasModel: argResults![ksModel],
templateType: templateType,
);
await _templateService.renderTemplate(
templateName: name,
name: dialogName,
outputPath: outputPath,
verbose: true,
excludeRoute: argResults![ksExcludeRoute],
hasModel: argResults![ksModel],
templateType: templateType,
);

await _processService.runBuildRunner(appName: outputPath);
await _processService.runBuildRunner(appName: outputPath);
} catch (e) {
_log.warn(message: e.toString());
}
}
}
50 changes: 33 additions & 17 deletions lib/src/commands/create/create_service_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/mixins/project_structure_validator_mixin.dart';
import 'package:stacked_cli/src/services/analytics_service.dart';
import 'package:stacked_cli/src/services/colorized_log_service.dart';
import 'package:stacked_cli/src/services/config_service.dart';
import 'package:stacked_cli/src/services/process_service.dart';
import 'package:stacked_cli/src/services/pubspec_service.dart';
import 'package:stacked_cli/src/services/template_service.dart';
import 'package:stacked_cli/src/templates/template_constants.dart';

class CreateServiceCommand extends Command with ProjectStructureValidator {
final _log = locator<ColorizedLogService>();
final _configService = locator<ConfigService>();
final _processService = locator<ProcessService>();
final _pubspecService = locator<PubspecService>();
Expand Down Expand Up @@ -48,27 +50,41 @@ class CreateServiceCommand extends Command with ProjectStructureValidator {
defaultsTo: 'empty',
help: kCommandHelpCreateAppTemplate,
);

argParser.addOption(
ksConfigPath,
abbr: 'c',
help: kCommandHelpConfigFilePath,
);
}

@override
Future<void> run() async {
final serviceName = argResults!.rest.first;
final templateType = argResults![ksTemplateType];
unawaited(_analyticsService.createServiceEvent(name: serviceName));
final outputPath = argResults!.rest.length > 1 ? argResults!.rest[1] : null;
await _configService.loadConfig(path: outputPath);
_processService.formattingLineLength = argResults?[ksLineLength];
await _pubspecService.initialise(workingDirectory: outputPath);
await validateStructure(outputPath: outputPath);
try {
final serviceName = argResults!.rest.first;
final templateType = argResults![ksTemplateType];
unawaited(_analyticsService.createServiceEvent(name: serviceName));
final outputPath =
argResults!.rest.length > 1 ? argResults!.rest[1] : null;
await _configService.composeAndLoadConfigFile(
configFilePath: argResults![ksConfigPath],
projectPath: outputPath,
);
_processService.formattingLineLength = argResults?[ksLineLength];
await _pubspecService.initialise(workingDirectory: outputPath);
await validateStructure(outputPath: outputPath);

await _templateService.renderTemplate(
templateName: name,
name: serviceName,
outputPath: outputPath,
verbose: true,
excludeRoute: argResults![ksExcludeDependency],
templateType: templateType,
);
await _processService.runBuildRunner(appName: outputPath);
await _templateService.renderTemplate(
templateName: name,
name: serviceName,
outputPath: outputPath,
verbose: true,
excludeRoute: argResults![ksExcludeDependency],
templateType: templateType,
);
await _processService.runBuildRunner(appName: outputPath);
} catch (e) {
_log.warn(message: e.toString());
}
}
}
62 changes: 39 additions & 23 deletions lib/src/commands/create/create_view_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/mixins/project_structure_validator_mixin.dart';
import 'package:stacked_cli/src/services/analytics_service.dart';
import 'package:stacked_cli/src/services/colorized_log_service.dart';
import 'package:stacked_cli/src/services/config_service.dart';
import 'package:stacked_cli/src/services/process_service.dart';
import 'package:stacked_cli/src/services/pubspec_service.dart';
import 'package:stacked_cli/src/services/template_service.dart';
import 'package:stacked_cli/src/templates/template_constants.dart';

class CreateViewCommand extends Command with ProjectStructureValidator {
final _log = locator<ColorizedLogService>();
final _configService = locator<ConfigService>();
final _processService = locator<ProcessService>();
final _pubspecService = locator<PubspecService>();
Expand Down Expand Up @@ -53,34 +55,48 @@ class CreateViewCommand extends Command with ProjectStructureValidator {
allowed: ['empty', 'web'],
help: kCommandHelpCreateViewTemplate,
);

argParser.addOption(
ksConfigPath,
abbr: 'c',
help: kCommandHelpConfigFilePath,
);
}

@override
Future<void> run() async {
final viewName = argResults!.rest.first;
var templateType = argResults![ksTemplateType] as String?;
unawaited(_analyticsService.createViewEvent(name: viewName));
final outputPath = argResults!.rest.length > 1 ? argResults!.rest[1] : null;
await _configService.loadConfig(path: outputPath);
_processService.formattingLineLength = argResults![ksLineLength];
await _pubspecService.initialise(workingDirectory: outputPath);
await validateStructure(outputPath: outputPath);
try {
final viewName = argResults!.rest.first;
var templateType = argResults![ksTemplateType] as String?;
unawaited(_analyticsService.createViewEvent(name: viewName));
final outputPath =
argResults!.rest.length > 1 ? argResults!.rest[1] : null;
await _configService.composeAndLoadConfigFile(
configFilePath: argResults![ksConfigPath],
projectPath: outputPath,
);
_processService.formattingLineLength = argResults![ksLineLength];
await _pubspecService.initialise(workingDirectory: outputPath);
await validateStructure(outputPath: outputPath);

// Determine which template to use with the following rules:
// 1. If the template is supplied we use that template
// 2. If the template is null use config web to decide
print('templateType:$templateType preferWeb:${_configService.preferWeb}');
templateType ??= _configService.preferWeb ? 'web' : 'empty';
// Determine which template to use with the following rules:
// 1. If the template is supplied we use that template
// 2. If the template is null use config web to decide
print('templateType:$templateType preferWeb:${_configService.preferWeb}');
templateType ??= _configService.preferWeb ? 'web' : 'empty';

await _templateService.renderTemplate(
templateName: name,
name: viewName,
outputPath: outputPath,
verbose: true,
excludeRoute: argResults![ksExcludeRoute],
useBuilder: argResults![ksV1] ?? _configService.v1,
templateType: templateType,
);
await _processService.runBuildRunner(appName: outputPath);
await _templateService.renderTemplate(
templateName: name,
name: viewName,
outputPath: outputPath,
verbose: true,
excludeRoute: argResults![ksExcludeRoute],
useBuilder: argResults![ksV1] ?? _configService.v1,
templateType: templateType,
);
await _processService.runBuildRunner(appName: outputPath);
} catch (e) {
_log.warn(message: e.toString());
}
}
}
Loading

0 comments on commit 083aa0b

Please sign in to comment.