-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcreate_command.dart
More file actions
278 lines (245 loc) · 8.07 KB
/
create_command.dart
File metadata and controls
278 lines (245 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import 'dart:io';
import 'package:args/args.dart';
import 'package:dartlin/dartlin.dart';
import 'package:ignite_cli/commands/ignite_command.dart';
import 'package:ignite_cli/flame_version_manager.dart';
import 'package:ignite_cli/ignite_context.dart';
import 'package:ignite_cli/templates/template.dart';
import 'package:ignite_cli/utils.dart';
import 'package:mason/mason.dart';
class CreateCommand extends IgniteCommand {
CreateCommand(super.context) {
final packages = context.flameVersionManager.versions;
final flameVersions = packages[Package.flame]!;
argParser.addFlag(
'interactive',
abbr: 'i',
help: 'Whether to run in interactive mode or not.',
defaultsTo: true,
);
argParser.addOption(
'name',
help: 'The name of your game (valid dart identifier).',
);
argParser.addOption(
'org',
help: 'The org name, in reverse domain notation '
'(package name/bundle identifier).',
);
argParser.addFlag(
'create-folder',
abbr: 'f',
help: 'If you want to create a new folder on the current location with '
"the project name or if you are already on the new project's folder.",
);
argParser.addOption(
'template',
help: 'What Flame template you would like to use for your new project',
allowed: ['simple', 'example'],
);
argParser.addOption(
'flame-version',
help: 'What Flame version you would like to use.',
allowed: [
...flameVersions.versions.take(5),
'...',
flameVersions.versions.last,
],
);
argParser.addMultiOption(
'extra-packages',
help: 'Which packages to use',
allowed: packages.keys.map((e) => e.name).toList(),
);
}
@override
String get description => 'Create a new Flame project';
@override
String get name => 'create';
@override
Future<int> run() async {
final argResults = this.argResults;
if (argResults == null) {
return ExitCode.usage.code;
}
final code = await createCommand(context, argResults);
return code;
}
}
Future<int> createCommand(
IgniteContext context,
ArgResults command,
) async {
final interactive = command['interactive'] != 'false';
if (interactive) {
context.logger
..info('\nWelcome to ${red.wrap('Ignite CLI')}! 🔥')
..info("Let's create a new project!\n");
}
final name = getString(
isInteractive: interactive,
logger: context.logger,
command,
'name',
'Choose a name for your project',
desc: 'Note: this must be a valid dart identifier (no dashes). '
'For example: my_game',
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(
logger: context.logger,
isInteractive: interactive,
command,
'org',
'Choose an org for your project:',
desc: 'Note: this is a dot separated list of "packages", '
'normally in reverse domain notation. '
'For example: org.flame_engine.games',
validate: (it) => switch (it) {
_ when it.isEmpty => 'Org cannot be empty',
_ when it.contains('-') => 'Org cannot contain dashes',
_ => null,
},
);
final versions = context.flameVersionManager.versions;
final flameVersions = versions[Package.flame]!;
final flameVersion = getOption(
logger: context.logger,
isInteractive: interactive,
command,
'flame-version',
'Which Flame version do you wish to use?',
flameVersions.visible.associateWith((e) => e),
defaultsTo: flameVersions.versions.first,
fullOptions: flameVersions.versions.associateWith((e) => e),
);
final extraPackageOptions = context.flameVersionManager.versions.keys
.where((key) => !Package.includedByDefault.contains(key))
.map((key) => key.name)
.toList();
final extraPackages = getMultiOption(
logger: context.logger,
isInteractive: interactive,
isRequired: false,
command,
'extra-packages',
'Which extra packages do you wish to include?',
extraPackageOptions,
startingOptions: Package.preSelectedByDefault.map((e) => e.name).toList(),
);
final packages = extraPackages
.map(Package.valueOf)
.toSet()
.union(Package.includedByDefault);
// TODO(luan): use partition function
final dependencies = packages.where((e) => !e.isDevDependency);
final devDependencies = packages.where((e) => e.isDevDependency);
final currentDir = Directory.current.path;
context.logger.info('Your current directory is: $currentDir');
bool createFolder;
if (!interactive) {
createFolder = command['create-folder'] == true;
} else {
createFolder = context.logger.confirm(
'Create project a folder called $name?',
defaultValue: command['create-folder'] == true,
);
}
final template = getOption(
logger: context.logger,
isInteractive: interactive,
command,
'template',
'What template would you like to use for your new project?',
Template.templates
.associate((e) => Pair('${e.name}: ${e.description}', e.key)),
);
final actualDir = '$currentDir${createFolder ? '/$name' : ''}';
final progress = context.logger.progress('Generating project');
ProcessResult? processResult;
var code = ExitCode.success.code;
try {
if (createFolder) {
progress.update('Running [mkdir] on $actualDir');
processResult = await context.run('mkdir', [actualDir]);
if (processResult.exitCode > ExitCode.success.code) {
return code = processResult.exitCode;
}
}
progress.update('Running [flutter create] on $actualDir');
processResult = await context.run(
'flutter',
'create --org $org --project-name $name .'.split(' '),
workingDirectory: actualDir,
);
if (processResult.exitCode > ExitCode.success.code) {
return code = processResult.exitCode;
}
progress.update('Running [rm -rf lib test] on $actualDir');
processResult = await context.run(
'rm',
'-rf lib test'.split(' '),
workingDirectory: actualDir,
);
if (processResult.exitCode > ExitCode.success.code) {
return code = processResult.exitCode;
}
progress.update('Bundling game template');
final bundle = Template.byKey(template).bundle;
final generator = await context.generatorFromBundle(bundle);
final target = context.createTarget(Directory(actualDir));
final variables = <String, dynamic>{
'name': name,
'description': 'A simple Flame game.',
'version': '0.1.0',
'extra-dependencies': dependencies
.sortedBy((e) => e.name)
.map((package) => package.toMustache(versions, flameVersion))
.toList(),
'extra-dev-dependencies': devDependencies
.sortedBy((e) => e.name)
.map((package) => package.toMustache(versions, flameVersion))
.toList(),
};
final files = await generator.generate(target, vars: variables);
final canHaveTests = devDependencies.contains(Package.flameTest);
if (!canHaveTests) {
progress.update('Removing tests');
processResult = await context.run(
'rm',
'-rf test'.split(' '),
workingDirectory: actualDir,
);
if (processResult.exitCode > ExitCode.success.code) {
return code = processResult.exitCode;
}
}
progress.update('Removing tests');
processResult = await context.run(
'flutter',
'pub get'.split(' '),
workingDirectory: actualDir,
);
if (processResult.exitCode > ExitCode.success.code) {
return code = processResult.exitCode;
}
progress
..complete('Updated ${files.length} files on top of flutter create.')
..complete('Your new Flame project was successfully created!');
return code;
} catch (_) {
if (processResult != null) {
progress.fail(processResult.stderr.toString());
code = processResult.exitCode;
} else {
progress.fail();
}
rethrow;
}
}