mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Refactor flutter create logic in prep for add2app (#17992)
This commit is contained in:
parent
055ad0ce4c
commit
0d1574c313
@ -158,7 +158,6 @@ class CreateCommand extends FlutterCommand {
|
|||||||
organization: organization,
|
organization: organization,
|
||||||
projectName: projectName,
|
projectName: projectName,
|
||||||
projectDescription: argResults['description'],
|
projectDescription: argResults['description'],
|
||||||
dirPath: dirPath,
|
|
||||||
flutterRoot: flutterRoot,
|
flutterRoot: flutterRoot,
|
||||||
renderDriverTest: argResults['with-driver-test'],
|
renderDriverTest: argResults['with-driver-test'],
|
||||||
withPluginHook: generatePlugin,
|
withPluginHook: generatePlugin,
|
||||||
@ -167,77 +166,26 @@ class CreateCommand extends FlutterCommand {
|
|||||||
);
|
);
|
||||||
|
|
||||||
printStatus('Creating project ${fs.path.relative(dirPath)}...');
|
printStatus('Creating project ${fs.path.relative(dirPath)}...');
|
||||||
int generatedCount = 0;
|
int generatedFileCount = 0;
|
||||||
if (generatePackage) {
|
|
||||||
final String description = argResults.wasParsed('description')
|
|
||||||
? argResults['description']
|
|
||||||
: 'A new flutter package project.';
|
|
||||||
templateContext['description'] = description;
|
|
||||||
generatedCount += _renderTemplate('package', dirPath, templateContext);
|
|
||||||
|
|
||||||
if (argResults['pub'])
|
|
||||||
await pubGet(
|
|
||||||
context: PubContext.createPackage,
|
|
||||||
directory: dirPath,
|
|
||||||
offline: argResults['offline'],
|
|
||||||
);
|
|
||||||
|
|
||||||
final String relativePath = fs.path.relative(dirPath);
|
|
||||||
printStatus('Wrote $generatedCount files.');
|
|
||||||
printStatus('');
|
|
||||||
printStatus('Your package code is in lib/$projectName.dart in the $relativePath directory.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String appPath = dirPath;
|
String appPath = dirPath;
|
||||||
if (generatePlugin) {
|
switch (template) {
|
||||||
final String description = argResults.wasParsed('description')
|
case 'app':
|
||||||
? argResults['description']
|
generatedFileCount += await _generateApp(dirPath, templateContext);
|
||||||
: 'A new flutter plugin project.';
|
break;
|
||||||
templateContext['description'] = description;
|
case 'package':
|
||||||
generatedCount += _renderTemplate('plugin', dirPath, templateContext);
|
generatedFileCount += await _generatePackage(dirPath, templateContext);
|
||||||
|
break;
|
||||||
if (argResults['pub'])
|
case 'plugin':
|
||||||
await pubGet(
|
|
||||||
context: PubContext.createPlugin,
|
|
||||||
directory: dirPath,
|
|
||||||
offline: argResults['offline'],
|
|
||||||
);
|
|
||||||
|
|
||||||
if (android_sdk.androidSdk != null)
|
|
||||||
gradle.updateLocalProperties(projectPath: dirPath);
|
|
||||||
|
|
||||||
appPath = fs.path.join(dirPath, 'example');
|
appPath = fs.path.join(dirPath, 'example');
|
||||||
final String androidPluginIdentifier = templateContext['androidIdentifier'];
|
generatedFileCount += await _generatePlugin(dirPath, appPath, templateContext);
|
||||||
final String exampleProjectName = projectName + '_example';
|
break;
|
||||||
templateContext['projectName'] = exampleProjectName;
|
|
||||||
templateContext['androidIdentifier'] = _createAndroidIdentifier(organization, exampleProjectName);
|
|
||||||
templateContext['iosIdentifier'] = _createUTIIdentifier(organization, exampleProjectName);
|
|
||||||
templateContext['description'] = 'Demonstrates how to use the $projectName plugin.';
|
|
||||||
templateContext['pluginProjectName'] = projectName;
|
|
||||||
templateContext['androidPluginIdentifier'] = androidPluginIdentifier;
|
|
||||||
}
|
}
|
||||||
|
printStatus('Wrote $generatedFileCount files.');
|
||||||
generatedCount += _renderTemplate('create', appPath, templateContext);
|
|
||||||
generatedCount += _injectGradleWrapper(appPath);
|
|
||||||
if (argResults['with-driver-test']) {
|
|
||||||
final String testPath = fs.path.join(appPath, 'test_driver');
|
|
||||||
generatedCount += _renderTemplate('driver', testPath, templateContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
printStatus('Wrote $generatedCount files.');
|
|
||||||
printStatus('');
|
printStatus('');
|
||||||
|
if (generatePackage) {
|
||||||
if (argResults['pub']) {
|
final String relativePath = fs.path.relative(dirPath);
|
||||||
await pubGet(context: PubContext.create, directory: appPath, offline: argResults['offline']);
|
printStatus('Your package code is in lib/${templateContext['projectName']}.dart in the $relativePath directory.');
|
||||||
new FlutterProject(fs.directory(appPath)).ensureReadyForPlatformSpecificTooling();
|
} else {
|
||||||
}
|
|
||||||
|
|
||||||
if (android_sdk.androidSdk != null)
|
|
||||||
gradle.updateLocalProperties(projectPath: appPath);
|
|
||||||
|
|
||||||
printStatus('');
|
|
||||||
|
|
||||||
// Run doctor; tell the user the next steps.
|
// Run doctor; tell the user the next steps.
|
||||||
final String relativeAppPath = fs.path.relative(appPath);
|
final String relativeAppPath = fs.path.relative(appPath);
|
||||||
final String relativePluginPath = fs.path.relative(dirPath);
|
final String relativePluginPath = fs.path.relative(dirPath);
|
||||||
@ -276,6 +224,79 @@ To edit platform code in an IDE see https://flutter.io/platform-plugins/#edit-co
|
|||||||
printStatus('Your main program file is: $relativeAppPath/lib/main.dart');
|
printStatus('Your main program file is: $relativeAppPath/lib/main.dart');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<int> _generatePackage(String dirPath, Map<String, dynamic> templateContext) async {
|
||||||
|
int generatedCount = 0;
|
||||||
|
final String description = argResults.wasParsed('description')
|
||||||
|
? argResults['description']
|
||||||
|
: 'A new flutter package project.';
|
||||||
|
templateContext['description'] = description;
|
||||||
|
generatedCount += _renderTemplate('package', dirPath, templateContext);
|
||||||
|
|
||||||
|
if (argResults['pub']) {
|
||||||
|
await pubGet(
|
||||||
|
context: PubContext.createPackage,
|
||||||
|
directory: dirPath,
|
||||||
|
offline: argResults['offline'],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return generatedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<int> _generatePlugin(String dirPath, String appPath, Map<String, dynamic> templateContext) async {
|
||||||
|
int generatedCount = 0;
|
||||||
|
final String description = argResults.wasParsed('description')
|
||||||
|
? argResults['description']
|
||||||
|
: 'A new flutter plugin project.';
|
||||||
|
templateContext['description'] = description;
|
||||||
|
generatedCount += _renderTemplate('plugin', dirPath, templateContext);
|
||||||
|
|
||||||
|
if (argResults['pub']) {
|
||||||
|
await pubGet(
|
||||||
|
context: PubContext.createPlugin,
|
||||||
|
directory: dirPath,
|
||||||
|
offline: argResults['offline'],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (android_sdk.androidSdk != null)
|
||||||
|
gradle.updateLocalProperties(projectPath: dirPath);
|
||||||
|
|
||||||
|
final String projectName = templateContext['projectName'];
|
||||||
|
final String organization = templateContext['organization'];
|
||||||
|
final String androidPluginIdentifier = templateContext['androidIdentifier'];
|
||||||
|
final String exampleProjectName = projectName + '_example';
|
||||||
|
templateContext['projectName'] = exampleProjectName;
|
||||||
|
templateContext['androidIdentifier'] = _createAndroidIdentifier(organization, exampleProjectName);
|
||||||
|
templateContext['iosIdentifier'] = _createUTIIdentifier(organization, exampleProjectName);
|
||||||
|
templateContext['description'] = 'Demonstrates how to use the $projectName plugin.';
|
||||||
|
templateContext['pluginProjectName'] = projectName;
|
||||||
|
templateContext['androidPluginIdentifier'] = androidPluginIdentifier;
|
||||||
|
|
||||||
|
generatedCount += await _generateApp(appPath, templateContext);
|
||||||
|
return generatedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<int> _generateApp(String appPath, Map<String, dynamic> templateContext) async {
|
||||||
|
int generatedCount = 0;
|
||||||
|
generatedCount += _renderTemplate('create', appPath, templateContext);
|
||||||
|
generatedCount += _injectGradleWrapper(appPath);
|
||||||
|
|
||||||
|
if (argResults['with-driver-test']) {
|
||||||
|
final String testPath = fs.path.join(appPath, 'test_driver');
|
||||||
|
generatedCount += _renderTemplate('driver', testPath, templateContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argResults['pub']) {
|
||||||
|
await pubGet(context: PubContext.create, directory: appPath, offline: argResults['offline']);
|
||||||
|
new FlutterProject(fs.directory(appPath)).ensureReadyForPlatformSpecificTooling();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (android_sdk.androidSdk != null)
|
||||||
|
gradle.updateLocalProperties(projectPath: appPath);
|
||||||
|
|
||||||
|
return generatedCount;
|
||||||
|
}
|
||||||
|
|
||||||
Map<String, dynamic> _templateContext({
|
Map<String, dynamic> _templateContext({
|
||||||
String organization,
|
String organization,
|
||||||
@ -283,7 +304,6 @@ To edit platform code in an IDE see https://flutter.io/platform-plugins/#edit-co
|
|||||||
String projectDescription,
|
String projectDescription,
|
||||||
String androidLanguage,
|
String androidLanguage,
|
||||||
String iosLanguage,
|
String iosLanguage,
|
||||||
String dirPath,
|
|
||||||
String flutterRoot,
|
String flutterRoot,
|
||||||
bool renderDriverTest: false,
|
bool renderDriverTest: false,
|
||||||
bool withPluginHook: false,
|
bool withPluginHook: false,
|
||||||
|
Loading…
Reference in New Issue
Block a user