Skip to content
Merged
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
14 changes: 12 additions & 2 deletions lib/commands/create_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ Future<void> createCommand(ArgResults command) async {
'Choose a name for your project: ',
desc: 'Note: this must be a valid dart identifier (no dashes). '
'For example: my_game',
validate: (it) => !it.contains('-') && it != 'test',
validate: (it) => switch (it) {
_ when it.isEmpty => 'Name cannot be empty',
_ when it.contains('-') => 'Name cannot contain dashes',
_ when it == 'test' => 'Name cannot be "test", '
'as it conflicts with the Dart package',
_ => null,
},
);

final org = getString(
Expand All @@ -35,7 +41,11 @@ Future<void> createCommand(ArgResults command) async {
desc: 'Note: this is a dot separated list of "packages", '
'normally in reverse domain notation. '
'For example: org.flame_engine.games',
validate: (it) => !it.contains('-'),
validate: (it) => switch (it) {
_ when it.isEmpty => 'Org cannot be empty',
_ when it.contains('-') => 'Org cannot contain dashes',
_ => null,
},
);

final versions = FlameVersionManager.singleton.versions;
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/version_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Future<void> versionCommand() async {
await runExecutableArguments('dart', ['--version'], verbose: true);
print('');
await runExecutableArguments('flutter', ['--version'], verbose: true);
}
}
33 changes: 25 additions & 8 deletions lib/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,39 @@ String getString(
String message, {
required bool isInteractive,
String? desc,
bool Function(String)? validate,
String? Function(String)? validate,
}) {
var value = results[name] as String?;
if (!isInteractive) {
if (value == null || value.isEmpty) {
print('Missing parameter $name is required.');
exit(1);
}
final error = validate?.call(value);
if (error != null) {
print('Invalid value $value provided: $error');
exit(1);
}
}
while (value == null || value.isEmpty) {
if (desc != null) {
stdout.write(ansi.darkGray.wrap('\n$desc\u{1B}[1A\r'));
}
value = prompts.get(message, validate: validate);
value = prompts.get(
message,
validate: (e) {
final error = validate?.call(e);
if (error != null) {
// clear the line
stdout.write('\n\r\u{1B}[K');
stdout.write(ansi.red.wrap('$error\u{1B}[1A\r'));
return false;
} else {
stdout.write('\n\r\u{1B}[K');
return true;
}
},
);
if (desc != null) {
stdout.write('\r\u{1B}[K');
}
Expand Down Expand Up @@ -77,14 +96,14 @@ String getOption(
}

List<String> _unwrap(dynamic value) {
return switch(value) {
return switch (value) {
null => [],
String _ => [value],
List<String> _ => value,
List _ => value.map((e) => e.toString()).toList(),
_ => throw ArgumentError(
'Invalid type for value (${value.runtimeType}): $value',
),
'Invalid type for value (${value.runtimeType}): $value',
),
};
}

Expand Down Expand Up @@ -118,9 +137,7 @@ List<String> getMultiOption(
if (desc != null) {
stdout.write(ansi.darkGray.wrap('\n$desc\u{1B}[1A\r'));
}
final selectedOptions = value.isEmpty
? startingOptions
: value;
final selectedOptions = value.isEmpty ? startingOptions : value;
value = cbx(message, options, selectedOptions);
if (desc != null) {
stdout.write('\r\u{1B}[K');
Expand Down