Skip to content

Commit

Permalink
fix: custom config not copied to target created project (#21)
Browse files Browse the repository at this point in the history
* When stacked_cli is not installed, install it

An improvement for development more than production because on
production should not happen.

* Modify resolveConfigFile to use custom filepath

Improved resolveConfigFile to accept custom config filepath which
overrides default custom config filepath in XDG_CONFIG_HOME.

* Add config-path option to create app command

- Added config-path option to create app command
- Modify create app command to replace custom config on app created

---------

Co-authored-by: Dane Mackier <[email protected]>
  • Loading branch information
ferrarafer and FilledStacks authored May 23, 2023
1 parent ace129a commit 71a40a6
Show file tree
Hide file tree
Showing 14 changed files with 316 additions and 425 deletions.
74 changes: 50 additions & 24 deletions lib/src/commands/create/create_app_command.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'dart:async';
import 'dart:io';

import 'package:args/command_runner.dart';
import 'package:stacked_cli/src/constants/command_constants.dart';
import 'package:stacked_cli/src/constants/config_constants.dart';
import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/services/analytics_service.dart';
Expand All @@ -10,6 +12,7 @@ import 'package:stacked_cli/src/services/config_service.dart';
import 'package:stacked_cli/src/services/file_service.dart';
import 'package:stacked_cli/src/services/process_service.dart';
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>();
Expand All @@ -24,7 +27,7 @@ class CreateAppCommand extends Command {
'Creates a stacked application with all the basics setup';

@override
String get name => 'app';
String get name => kTemplateNameApp;

CreateAppCommand() {
argParser.addFlag(
Expand All @@ -50,34 +53,46 @@ class CreateAppCommand extends Command {
defaultsTo: 'mobile',
help: kCommandHelpCreateAppTemplate,
);

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

@override
Future<void> run() async {
await _configService.loadConfig();
final appName = argResults!.rest.first;
final appNameWithoutPath = appName.split('/').last;
final templateType = argResults![ksTemplateType];

unawaited(_analyticsService.createAppEvent(name: appNameWithoutPath));
_processService.formattingLineLength = argResults![ksLineLength];
await _processService.runCreateApp(appName: appName);

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

await _templateService.renderTemplate(
templateName: name,
name: appNameWithoutPath,
verbose: true,
outputPath: appName,
useBuilder: argResults![ksV1] ?? _configService.v1,
templateType: templateType,
);
try {
await _configService.loadConfig(path: argResults![ksConfigPath]);

final appName = argResults!.rest.first;
final appNameWithoutPath = appName.split('/').last;
final templateType = argResults![ksTemplateType];

unawaited(_analyticsService.createAppEvent(name: appNameWithoutPath));
_processService.formattingLineLength = argResults![ksLineLength];
await _processService.runCreateApp(appName: appName);

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

await _templateService.renderTemplate(
templateName: name,
name: appNameWithoutPath,
verbose: true,
outputPath: appName,
useBuilder: argResults![ksV1] ?? _configService.v1,
templateType: templateType,
);

await _processService.runPubGet(appName: appName);
await _processService.runBuildRunner(appName: appName);
await _processService.runFormat(appName: appName);
await _clean(appName: appName);
_replaceConfigFile(appName: appName);
await _processService.runPubGet(appName: appName);
await _processService.runBuildRunner(appName: appName);
await _processService.runFormat(appName: appName);
await _clean(appName: appName);
} catch (e) {
_cLog.warn(message: e.toString());
}
}

/// Cleans the project.
Expand Down Expand Up @@ -109,4 +124,15 @@ class CreateAppCommand extends Command {

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

/// Replaces configuration file in the project created.
///
/// If has NO custom config, does nothing.
void _replaceConfigFile({required String appName}) {
if (!_configService.hasCustomConfig) return;

File('$appName/$kConfigFileName').writeAsStringSync(
_configService.exportConfig(),
);
}
}
1 change: 1 addition & 0 deletions lib/src/constants/command_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const String ksActivate = 'activate';
const String ksStackedCli = 'stacked_cli';
const String ksAnalyze = 'analyze';
const String ksModel = 'model';
const String ksConfigPath = 'config-path';
const String ksWatch = 'watch';

/// A list of strings that are used to run the run build_runner
Expand Down
5 changes: 4 additions & 1 deletion lib/src/constants/config_constants.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/// Defines the filename of the stacked configuration file
/// Defines default filename of the stacked configuration file
const String kConfigFileName = 'stacked.json';

/// Defines default message for current version
const String currentVersionNotAvailable = 'Current Version Not Available';
10 changes: 8 additions & 2 deletions lib/src/constants/message_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ const String kCommandHelpLineLength =
const String kCommandHelpCreateAppTemplate =
'Selects the type of starter template to use when creating a new app. One oriented for mobile first or web first';

const String kCommandHelpCreateAppConfigFile =
'Sets the file path for the custom config';

const String kCommandHelpCreateViewTemplate =
'Selects the type of view to create instead of the default empty view';

Expand All @@ -63,10 +66,13 @@ const String kCommandHelpDeleteConflictingOutputs =
'Assume conflicting outputs in the users package are from previous builds, and skip the user prompt that would usually be provided.';

const String kConfigFileNotFound =
'No stacked.json file found. Default stacked values will be used.';
'No configuration file found. Default Stacked values will be used.';

const String kConfigFileNotFoundRetry =
'No configuration file found. Please, correct the config path passed as argument.';

const String kConfigFileMalformed =
'Your stacked.json is malformed. Double check to make sure you have properly formatted json.';
'Your configuration file is malformed. Double check to make sure you have properly formatted json.';

const String kDeprecatedConfigFileName = '''
Stacked config file should be renamed from "stacked.config.json" to "stacked.json". Stacked cli will not read "stacked.config.json" files after the next minor release.
Expand Down
3 changes: 2 additions & 1 deletion lib/src/exceptions/config_file_not_found_exception.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class ConfigFileNotFoundException implements Exception {
final String message;
ConfigFileNotFoundException(this.message);
final bool shouldHaltCommand;
ConfigFileNotFoundException(this.message, {this.shouldHaltCommand = false});

@override
String toString() {
Expand Down
20 changes: 10 additions & 10 deletions lib/src/models/config_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,51 @@ class Config with _$Config {
/// Path where bottom sheets will be genereated.
@JsonKey(name: 'bottom_sheets_path')
@Default('ui/bottom_sheets')
String bottomSheetsPath,
String bottomSheetsPath,

/// File path where BottomSheetType enum values are located.
@JsonKey(name: 'bottom_sheet_type_file_path')
@Default('enums/bottom_sheet_type.dart')
String bottomSheetTypeFilePath,
String bottomSheetTypeFilePath,

/// File path where BottomSheet builders are located.
@JsonKey(name: 'bottom_sheet_builder_file_path')
@Default('ui/setup/setup_bottom_sheet_ui.dart')
String bottomSheetBuilderFilePath,
String bottomSheetBuilderFilePath,

/// Path where dialogs will be genereated.
@JsonKey(name: 'dialogs_path') @Default('ui/dialogs') String dialogsPath,

/// File path where DialogType enum values are located.
@JsonKey(name: 'dialog_type_file_path')
@Default('enums/dialog_type.dart')
String dialogTypeFilePath,
String dialogTypeFilePath,

/// File path where Dialog builders are located.
@JsonKey(name: 'dialog_builder_file_path')
@Default('ui/setup/setup_dialog_ui.dart')
String dialogBuilderFilePath,
String dialogBuilderFilePath,

/// File path where StackedApp is setup.
@JsonKey(name: 'stacked_app_file_path')
@Default('app/app.dart')
String stackedAppFilePath,
String stackedAppFilePath,

/// File path where register functions for unit test setup and mock
/// declarations are located.
@JsonKey(name: 'test_helpers_file_path')
@Default('helpers/test_helpers.dart')
String testHelpersFilePath,
String testHelpersFilePath,

/// Paths where services unit tests will be genereated.
@JsonKey(name: 'test_services_path')
@Default('services')
String testServicesPath,
String testServicesPath,

/// Path where viewmodels unit tests will be genereated.
@JsonKey(name: 'test_views_path')
@Default('viewmodels')
String testViewsPath,
String testViewsPath,

/// The name of the locator to use when registering test mocks
@JsonKey(name: 'locator_name') @Default('locator') String locatorName,
Expand All @@ -71,7 +71,7 @@ class Config with _$Config {
/// This is used when creating a test file during the `create service` command
@JsonKey(name: 'register_mocks_function')
@Default('registerServices')
String registerMocksFunction,
String registerMocksFunction,

/// Boolean value to determine view builder style
///
Expand Down
Loading

0 comments on commit 71a40a6

Please sign in to comment.