Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/add-platform-and-org-name-on-create-app #18

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions lib/src/commands/create/create_app_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import 'package:args/command_runner.dart';
import 'package:stacked_cli/src/constants/command_constants.dart';
import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/mixins/args_validator_mixin.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/file_service.dart';
import 'package:stacked_cli/src/services/process_service.dart';
import 'package:stacked_cli/src/services/template_service.dart';

class CreateAppCommand extends Command {
class CreateAppCommand extends Command with ArgsValidator {
final _cLog = locator<ColorizedLogService>();
final _configService = locator<ConfigService>();
final _fileService = locator<FileService>();
Expand Down Expand Up @@ -50,6 +52,18 @@ class CreateAppCommand extends Command {
defaultsTo: 'mobile',
help: kCommandHelpCreateAppTemplate,
);

argParser.addOption(
ksOrganization,
abbr: "o",
help: kCommandOrganizationHelp,
);

argParser.addOption(
ksPlatform,
abbr: "p",
help: kCommandPlatformHelp,
);
}

@override
Expand All @@ -58,10 +72,19 @@ class CreateAppCommand extends Command {
final appName = argResults!.rest.first;
final appNameWithoutPath = appName.split('/').last;
final templateType = argResults![ksTemplateType];
final organization = argResults![ksOrganization];
final platforms = argResults![ksPlatform];

validateOrganization(organization: organization);
validateAppName(appName: appName);

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

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

Expand Down
2 changes: 2 additions & 0 deletions lib/src/constants/command_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const String ksActivate = 'activate';
const String ksStackedCli = 'stacked_cli';
const String ksAnalyze = 'analyze';
const String ksModel = 'model';
const String ksOrganization = "org";
const String ksPlatform = "platform";

/// A list of strings that are used to run the pub run build runner build --delete-conflicting-outputs command.
const List<String> buildRunnerArguments = [
Expand Down
14 changes: 14 additions & 0 deletions lib/src/constants/message_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ We have plans to support this in the future. If you want to help build it, check
for the stacked-cli roadmap.
''';

/// The message shown when the given organization name does not comply with the expected standard.
const String kInvalidOrganization =
'Organization must be a valid bundle name. (com.example)';

/// The message shown when the given app name does not comply with the expected standard.
const String kInvalidAppName =
'''App name must contain lower letters and is not allowed to start
with numbers. Also it can't contain special characters, spaces and
should be different from dart reserved words.''';

const String kCommandHelpVersion = 'Print the current version.';

const String kCommandHelpEnableAnalytics = 'Enable Analytics.';
Expand Down Expand Up @@ -56,6 +66,10 @@ const String kCommandHelpCreateBottomSheetTemplate =
const String kCommandHelpExcludeDependency =
'When a service is excluded it will not be added to your app.dart dependencies collection.';

const String kCommandOrganizationHelp = "Specifiy the organization bundle-name";

const String kCommandPlatformHelp = "Specifiy the platform to generate for";

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

Expand Down
9 changes: 9 additions & 0 deletions lib/src/exceptions/invalid_arg_exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class InvalidArgException implements Exception {
final String message;
InvalidArgException(this.message);

@override
String toString() {
return message;
}
}
73 changes: 73 additions & 0 deletions lib/src/mixins/args_validator_mixin.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/exceptions/invalid_arg_exception.dart';

mixin ArgsValidator {
final organizationPattern = RegExp(r'^[a-z0-9]+(\.[a-z0-9]+)+$');

bool _isOrganizationValid(String orgName) {
return organizationPattern.hasMatch(orgName);
}

/// Logs an error if the the organization name is invalid
void validateOrganization({String? organization}) {
if (organization == null) {
return;
}
if (!_isOrganizationValid(organization)) {
throw InvalidArgException(kInvalidOrganization);
}
}

final appNamePattern = RegExp(r'^[a-z][a-z0-9_]*(?:_[a-z0-9_]+)*$');

static const _reservedWords = [
'assert',
'break',
'case',
'catch',
'class',
'const',
'continue',
'default',
'do',
'else',
'enum',
'extends',
'false',
'final',
'finally',
'for',
'if',
'in',
'is',
'new',
'null',
'rethrow',
'return',
'super',
'switch',
'this',
'throw',
'true',
'try',
'var',
'void',
'while',
'with'
];

bool _isValidAppName(String appName) {
return appNamePattern.hasMatch(appName) &&
!_reservedWords.contains(appName);
}

/// Logs an error if the the app name is invalid
void validateAppName({String? appName}) {
if (appName == null) {
return;
}
if (!_isValidAppName(appName)) {
throw InvalidArgException(kInvalidAppName);
}
}
}
2 changes: 2 additions & 0 deletions lib/src/mixins/project_structure_validator_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import 'package:path/path.dart' as path;
import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/exceptions/invalid_stacked_structure_exception.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/services/colorized_log_service.dart';
import 'package:stacked_cli/src/services/config_service.dart';

mixin ProjectStructureValidator {
final _configService = locator<ConfigService>();
final _cLog = locator<ColorizedLogService>();

/// Returns true if the cli is running from the root of a flutter
/// or dart project
Expand Down
12 changes: 10 additions & 2 deletions lib/src/services/process_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ class ProcessService {
///
/// Args:
/// appName (String): The name of the app that's going to be create.
Future<void> runCreateApp({required String appName}) async {
Future<void> runCreateApp(
{required String appName,
String? organization,
String? platforms}) async {
await _runProcess(
programName: ksFlutter,
arguments: [ksCreate, appName],
arguments: [
ksFlutter,
appName,
if (organization != null) organization,
if (platforms != null) platforms,
],
);
}

Expand Down
14 changes: 11 additions & 3 deletions test/helpers/test_helpers.mocks.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Mocks generated by Mockito 5.3.2 from annotations
// Mocks generated by Mockito 5.4.0 from annotations
// in stacked_cli/test/helpers/test_helpers.dart.
// Do not manually edit this file.

Expand Down Expand Up @@ -1070,12 +1070,20 @@ class MockProcessService extends _i1.Mock implements _i16.ProcessService {
returnValueForMissingStub: null,
);
@override
_i6.Future<void> runCreateApp({required String? appName}) =>
_i6.Future<void> runCreateApp({
required String? appName,
String? organization,
String? platforms,
}) =>
(super.noSuchMethod(
Invocation.method(
#runCreateApp,
[],
{#appName: appName},
{
#appName: appName,
#organization: organization,
#platforms: platforms,
},
),
returnValue: _i6.Future<void>.value(),
returnValueForMissingStub: _i6.Future<void>.value(),
Expand Down
Loading