Ian Hickson | 449f4a6 | 2019-11-27 15:04:02 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
Devon Carew | 2efd131 | 2015-08-07 14:20:14 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Devon Carew | 2efd131 | 2015-08-07 14:20:14 -0700 | [diff] [blame] | 5 | import 'dart:async'; |
Devon Carew | 2efd131 | 2015-08-07 14:20:14 -0700 | [diff] [blame] | 6 | |
Jonah Williams | 0a2d8e0 | 2019-12-16 18:37:20 -0800 | [diff] [blame] | 7 | import 'package:meta/meta.dart'; |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 8 | import 'package:yaml/yaml.dart' as yaml; |
Michael Goderbauer | ca4d721 | 2017-05-08 14:08:59 -0700 | [diff] [blame] | 9 | |
Devon Carew | 5ee1cba | 2016-02-24 23:40:00 -0800 | [diff] [blame] | 10 | import '../android/android.dart' as android; |
Brian Slesinsky | 5cb9097 | 2017-04-14 10:12:48 -0700 | [diff] [blame] | 11 | import '../android/android_sdk.dart' as android_sdk; |
Emmanuel Garcia | 175b372 | 2019-10-31 13:19:15 -0700 | [diff] [blame] | 12 | import '../android/gradle_utils.dart' as gradle; |
Dan Rubel | cccd917 | 2016-11-11 10:42:09 -0500 | [diff] [blame] | 13 | import '../base/common.dart'; |
Todd Volkert | 8bb2703 | 2017-01-06 16:51:44 -0800 | [diff] [blame] | 14 | import '../base/file_system.dart'; |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 15 | import '../base/net.dart'; |
Mikkel Nygaard Ravn | c5999c7 | 2017-06-26 12:47:43 +0200 | [diff] [blame] | 16 | import '../base/os.dart'; |
Devon Carew | 4c56919 | 2016-03-02 11:22:19 -0800 | [diff] [blame] | 17 | import '../base/utils.dart'; |
Adam Barth | 412ce9d | 2016-04-08 08:51:44 -0700 | [diff] [blame] | 18 | import '../cache.dart'; |
Jonah Williams | 91fd89e | 2019-01-25 16:16:26 -0800 | [diff] [blame] | 19 | import '../convert.dart'; |
Adam Barth | cf81163 | 2016-02-16 11:02:41 -0800 | [diff] [blame] | 20 | import '../dart/pub.dart'; |
John McCutchan | 0b737ac | 2016-11-29 07:54:20 -0800 | [diff] [blame] | 21 | import '../doctor.dart'; |
Jonah Williams | 3fedb8c | 2019-07-22 15:34:03 -0700 | [diff] [blame] | 22 | import '../features.dart'; |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 23 | import '../globals.dart' as globals; |
Mikkel Nygaard Ravn | 4353297 | 2018-01-18 09:21:24 +0100 | [diff] [blame] | 24 | import '../project.dart'; |
Zachary Anderson | ef146f6 | 2019-07-29 07:24:02 -0700 | [diff] [blame] | 25 | import '../reporting/reporting.dart'; |
Devon Carew | adac927 | 2016-04-26 16:25:11 -0700 | [diff] [blame] | 26 | import '../runner/flutter_command.dart'; |
Chinmay Garde | 038367a | 2016-02-23 15:40:51 -0800 | [diff] [blame] | 27 | import '../template.dart'; |
xster | c17099f | 2017-11-03 10:07:57 -0700 | [diff] [blame] | 28 | import '../version.dart'; |
Hixie | a0227ca | 2015-11-11 10:29:05 -0800 | [diff] [blame] | 29 | |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 30 | enum _ProjectType { |
Greg Spencer | 0ff9e8a | 2018-10-10 11:01:40 -0700 | [diff] [blame] | 31 | /// This is the default project with the user-managed host code. |
| 32 | /// It is different than the "module" template in that it exposes and doesn't |
| 33 | /// manage the platform code. |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 34 | app, |
Greg Spencer | 0ff9e8a | 2018-10-10 11:01:40 -0700 | [diff] [blame] | 35 | /// The is a project that has managed platform host code. It is an application with |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 36 | /// ephemeral .ios and .android directories that can be updated automatically. |
Greg Spencer | 0ff9e8a | 2018-10-10 11:01:40 -0700 | [diff] [blame] | 37 | module, |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 38 | /// This is a Flutter Dart package project. It doesn't have any native |
| 39 | /// components, only Dart. |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 40 | package, |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 41 | /// This is a native plugin project. |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 42 | plugin, |
| 43 | } |
| 44 | |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 45 | _ProjectType _stringToProjectType(String value) { |
| 46 | _ProjectType result; |
Alexandre Ardhuin | 4f9b6cf | 2020-01-07 16:32:04 +0100 | [diff] [blame] | 47 | for (final _ProjectType type in _ProjectType.values) { |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 48 | if (value == getEnumName(type)) { |
| 49 | result = type; |
| 50 | break; |
| 51 | } |
| 52 | } |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 53 | return result; |
| 54 | } |
| 55 | |
Devon Carew | adac927 | 2016-04-26 16:25:11 -0700 | [diff] [blame] | 56 | class CreateCommand extends FlutterCommand { |
Greg Spencer | 0ff9e8a | 2018-10-10 11:01:40 -0700 | [diff] [blame] | 57 | CreateCommand() { |
Kevin Moore | 64feb95 | 2017-12-07 14:29:12 -0800 | [diff] [blame] | 58 | argParser.addFlag('pub', |
| 59 | defaultsTo: true, |
Michael Thomsen | 7ae3caf | 2019-05-21 16:38:58 +0200 | [diff] [blame] | 60 | help: 'Whether to run "flutter pub get" after the project has been created.', |
Kevin Moore | 64feb95 | 2017-12-07 14:29:12 -0800 | [diff] [blame] | 61 | ); |
Greg Spencer | 6b8ceb9 | 2017-12-11 21:21:41 -0800 | [diff] [blame] | 62 | argParser.addFlag('offline', |
| 63 | defaultsTo: false, |
Michael Thomsen | 7ae3caf | 2019-05-21 16:38:58 +0200 | [diff] [blame] | 64 | help: 'When "flutter pub get" is run by the create command, this indicates ' |
Greg Spencer | 6b8ceb9 | 2017-12-11 21:21:41 -0800 | [diff] [blame] | 65 | 'whether to run it in offline mode or not. In offline mode, it will need to ' |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 66 | 'have all dependencies already available in the pub cache to succeed.', |
Greg Spencer | 6b8ceb9 | 2017-12-11 21:21:41 -0800 | [diff] [blame] | 67 | ); |
yjbanov | 278630e | 2016-02-22 14:55:17 -0800 | [diff] [blame] | 68 | argParser.addFlag( |
| 69 | 'with-driver-test', |
| 70 | negatable: true, |
| 71 | defaultsTo: false, |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 72 | help: "Also add a flutter_driver dependency and generate a sample 'flutter drive' test.", |
yjbanov | 278630e | 2016-02-22 14:55:17 -0800 | [diff] [blame] | 73 | ); |
Jakob Andersen | 5d0d612 | 2017-08-23 13:29:31 +0200 | [diff] [blame] | 74 | argParser.addOption( |
| 75 | 'template', |
| 76 | abbr: 't', |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 77 | allowed: _ProjectType.values.map<String>((_ProjectType type) => getEnumName(type)), |
Jakob Andersen | 5d0d612 | 2017-08-23 13:29:31 +0200 | [diff] [blame] | 78 | help: 'Specify the type of project to create.', |
| 79 | valueHelp: 'type', |
| 80 | allowedHelp: <String, String>{ |
Greg Spencer | 0ff9e8a | 2018-10-10 11:01:40 -0700 | [diff] [blame] | 81 | getEnumName(_ProjectType.app): '(default) Generate a Flutter application.', |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 82 | getEnumName(_ProjectType.package): 'Generate a shareable Flutter project containing modular ' |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 83 | 'Dart code.', |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 84 | getEnumName(_ProjectType.plugin): 'Generate a shareable Flutter project containing an API ' |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 85 | 'in Dart code with a platform-specific implementation for Android, for iOS code, or ' |
| 86 | 'for both.', |
Jenn Magder | e57ab1f | 2019-11-13 18:28:45 -0800 | [diff] [blame] | 87 | getEnumName(_ProjectType.module): 'Generate a project to add a Flutter module to an ' |
| 88 | 'existing Android or iOS application.', |
Greg Spencer | 0ff9e8a | 2018-10-10 11:01:40 -0700 | [diff] [blame] | 89 | }, |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 90 | defaultsTo: null, |
Jakob Andersen | 5d0d612 | 2017-08-23 13:29:31 +0200 | [diff] [blame] | 91 | ); |
Ian Hickson | a701606 | 2016-03-19 18:37:32 -0700 | [diff] [blame] | 92 | argParser.addOption( |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 93 | 'sample', |
| 94 | abbr: 's', |
| 95 | help: 'Specifies the Flutter code sample to use as the main.dart for an application. Implies ' |
Greg Spencer | b316940 | 2019-02-21 15:56:59 -0800 | [diff] [blame] | 96 | '--template=app. The value should be the sample ID of the desired sample from the API ' |
Jonah Williams | be73969 | 2019-04-22 22:22:46 -0700 | [diff] [blame] | 97 | 'documentation website (http://docs.flutter.dev). An example can be found at ' |
| 98 | 'https://master-api.flutter.dev/flutter/widgets/SingleChildScrollView-class.html', |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 99 | defaultsTo: null, |
Greg Spencer | b316940 | 2019-02-21 15:56:59 -0800 | [diff] [blame] | 100 | valueHelp: 'id', |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 101 | ); |
Danny Tuppeny | 126c58e | 2019-03-12 11:47:17 +0000 | [diff] [blame] | 102 | argParser.addOption( |
| 103 | 'list-samples', |
| 104 | help: 'Specifies a JSON output file for a listing of Flutter code samples ' |
| 105 | 'that can created with --sample.', |
| 106 | valueHelp: 'path', |
| 107 | ); |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 108 | argParser.addFlag( |
| 109 | 'overwrite', |
| 110 | negatable: true, |
| 111 | defaultsTo: false, |
| 112 | help: 'When performing operations, overwrite existing files.', |
| 113 | ); |
| 114 | argParser.addOption( |
Ian Hickson | a701606 | 2016-03-19 18:37:32 -0700 | [diff] [blame] | 115 | 'description', |
Mikkel Nygaard Ravn | 251d83a | 2017-05-24 16:19:16 +0200 | [diff] [blame] | 116 | defaultsTo: 'A new Flutter project.', |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 117 | help: 'The description to use for your new Flutter project. This string ends up in the pubspec.yaml file.', |
Mikkel Nygaard Ravn | 251d83a | 2017-05-24 16:19:16 +0200 | [diff] [blame] | 118 | ); |
| 119 | argParser.addOption( |
| 120 | 'org', |
Mikkel Nygaard Ravn | 4353297 | 2018-01-18 09:21:24 +0100 | [diff] [blame] | 121 | defaultsTo: 'com.example', |
Greg Spencer | 081d2a7 | 2018-10-10 18:17:56 -0700 | [diff] [blame] | 122 | help: 'The organization responsible for your new Flutter project, in reverse domain name notation. ' |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 123 | 'This string is used in Java package names and as prefix in the iOS bundle identifier.', |
Ian Hickson | a701606 | 2016-03-19 18:37:32 -0700 | [diff] [blame] | 124 | ); |
Mikkel Nygaard Ravn | 10f6483 | 2017-05-24 08:22:50 +0200 | [diff] [blame] | 125 | argParser.addOption( |
Sebastian Roth | b95b67a | 2018-10-17 23:25:46 +0800 | [diff] [blame] | 126 | 'project-name', |
| 127 | defaultsTo: null, |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 128 | help: 'The project name for this new Flutter project. This must be a valid dart package name.', |
Sebastian Roth | b95b67a | 2018-10-17 23:25:46 +0800 | [diff] [blame] | 129 | ); |
| 130 | argParser.addOption( |
Mikkel Nygaard Ravn | 10f6483 | 2017-05-24 08:22:50 +0200 | [diff] [blame] | 131 | 'ios-language', |
| 132 | abbr: 'i', |
Zachary Anderson | e24a27d | 2019-08-15 12:13:28 -0700 | [diff] [blame] | 133 | defaultsTo: 'swift', |
Mikkel Nygaard Ravn | 10f6483 | 2017-05-24 08:22:50 +0200 | [diff] [blame] | 134 | allowed: <String>['objc', 'swift'], |
| 135 | ); |
| 136 | argParser.addOption( |
| 137 | 'android-language', |
| 138 | abbr: 'a', |
Zachary Anderson | e24a27d | 2019-08-15 12:13:28 -0700 | [diff] [blame] | 139 | defaultsTo: 'kotlin', |
Mikkel Nygaard Ravn | 10f6483 | 2017-05-24 08:22:50 +0200 | [diff] [blame] | 140 | allowed: <String>['java', 'kotlin'], |
| 141 | ); |
Josh Burton | d0e45a2 | 2019-06-01 13:33:02 +1200 | [diff] [blame] | 142 | argParser.addFlag( |
| 143 | 'androidx', |
| 144 | negatable: true, |
Emmanuel Garcia | 48ce608 | 2019-09-24 16:55:09 -0700 | [diff] [blame] | 145 | defaultsTo: true, |
Josh Burton | d0e45a2 | 2019-06-01 13:33:02 +1200 | [diff] [blame] | 146 | help: 'Generate a project using the AndroidX support libraries', |
| 147 | ); |
Devon Carew | 2efd131 | 2015-08-07 14:20:14 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Hixie | 797e27e | 2016-03-14 13:31:43 -0700 | [diff] [blame] | 150 | @override |
Devon Carew | adac927 | 2016-04-26 16:25:11 -0700 | [diff] [blame] | 151 | final String name = 'create'; |
| 152 | |
| 153 | @override |
| 154 | final String description = 'Create a new Flutter project.\n\n' |
| 155 | 'If run on a project that already exists, this will repair the project, recreating any files that are missing.'; |
| 156 | |
| 157 | @override |
Alexandre Ardhuin | 1fce14a | 2017-10-22 18:11:36 +0200 | [diff] [blame] | 158 | String get invocation => '${runner.executableName} $name <output directory>'; |
Devon Carew | 0da7463 | 2016-02-18 14:33:59 -0800 | [diff] [blame] | 159 | |
Emmanuel Garcia | 3bbdf01 | 2019-05-29 20:56:28 -0700 | [diff] [blame] | 160 | @override |
Zachary Anderson | ef146f6 | 2019-07-29 07:24:02 -0700 | [diff] [blame] | 161 | Future<Map<CustomDimensions, String>> get usageValues async { |
| 162 | return <CustomDimensions, String>{ |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 163 | CustomDimensions.commandCreateProjectType: stringArg('template'), |
| 164 | CustomDimensions.commandCreateAndroidLanguage: stringArg('android-language'), |
| 165 | CustomDimensions.commandCreateIosLanguage: stringArg('ios-language'), |
Emmanuel Garcia | 3bbdf01 | 2019-05-29 20:56:28 -0700 | [diff] [blame] | 166 | }; |
| 167 | } |
| 168 | |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 169 | // If it has a .metadata file with the project_type in it, use that. |
| 170 | // If it has an android dir and an android/app dir, it's a legacy app |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 171 | // If it has an ios dir and an ios/Flutter dir, it's a legacy app |
| 172 | // Otherwise, we don't presume to know what type of project it could be, since |
| 173 | // many of the files could be missing, and we can't really tell definitively. |
| 174 | _ProjectType _determineTemplateType(Directory projectDir) { |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 175 | yaml.YamlMap loadMetadata(Directory projectDir) { |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 176 | if (!projectDir.existsSync()) { |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 177 | return null; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 178 | } |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 179 | final File metadataFile = globals.fs.file(globals.fs.path.join(projectDir.absolute.path, '.metadata')); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 180 | if (!metadataFile.existsSync()) { |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 181 | return null; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 182 | } |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 183 | final dynamic metadataYaml = yaml.loadYaml(metadataFile.readAsStringSync()); |
| 184 | if (metadataYaml is yaml.YamlMap) { |
| 185 | return metadataYaml; |
| 186 | } else { |
| 187 | throwToolExit('pubspec.yaml is malformed.'); |
| 188 | return null; |
| 189 | } |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 192 | bool exists(List<String> path) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 193 | return globals.fs.directory(globals.fs.path.joinAll(<String>[projectDir.absolute.path, ...path])).existsSync(); |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 196 | // If it exists, the project type in the metadata is definitive. |
| 197 | final yaml.YamlMap metadata = loadMetadata(projectDir); |
| 198 | if (metadata != null && metadata['project_type'] != null) { |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 199 | final dynamic projectType = metadata['project_type']; |
| 200 | if (projectType is String) { |
| 201 | return _stringToProjectType(projectType); |
| 202 | } else { |
| 203 | throwToolExit('.metadata is malformed.'); |
| 204 | return null; |
| 205 | } |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // There either wasn't any metadata, or it didn't contain the project type, |
| 209 | // so try and figure out what type of project it is from the existing |
| 210 | // directory structure. |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 211 | if (exists(<String>['android', 'app']) |
| 212 | || exists(<String>['ios', 'Runner']) |
| 213 | || exists(<String>['ios', 'Flutter'])) { |
| 214 | return _ProjectType.app; |
| 215 | } |
| 216 | // Since we can't really be definitive on nearly-empty directories, err on |
| 217 | // the side of prudence and just say we don't know. |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 218 | return null; |
| 219 | } |
| 220 | |
Danny Tuppeny | 126c58e | 2019-03-12 11:47:17 +0000 | [diff] [blame] | 221 | /// The hostname for the Flutter docs for the current channel. |
| 222 | String get _snippetsHost => FlutterVersion.instance.channel == 'stable' |
| 223 | ? 'docs.flutter.io' |
| 224 | : 'master-docs.flutter.io'; |
| 225 | |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 226 | Future<String> _fetchSampleFromServer(String sampleId) async { |
| 227 | // Sanity check the sampleId |
| 228 | if (sampleId.contains(RegExp(r'[^-\w\.]'))) { |
| 229 | throwToolExit('Sample ID "$sampleId" contains invalid characters. Check the ID in the ' |
| 230 | 'documentation and try again.'); |
| 231 | } |
| 232 | |
Danny Tuppeny | 126c58e | 2019-03-12 11:47:17 +0000 | [diff] [blame] | 233 | return utf8.decode(await fetchUrl(Uri.https(_snippetsHost, 'snippets/$sampleId.dart'))); |
| 234 | } |
| 235 | |
| 236 | /// Fetches the samples index file from the Flutter docs website. |
| 237 | Future<String> _fetchSamplesIndexFromServer() async { |
Devon Carew | 50a9c31 | 2019-05-16 18:20:30 -0700 | [diff] [blame] | 238 | return utf8.decode( |
| 239 | await fetchUrl(Uri.https(_snippetsHost, 'snippets/index.json'), maxAttempts: 2)); |
Danny Tuppeny | 126c58e | 2019-03-12 11:47:17 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /// Fetches the samples index file from the server and writes it to |
| 243 | /// [outputFilePath]. |
| 244 | Future<void> _writeSamplesJson(String outputFilePath) async { |
| 245 | try { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 246 | final File outputFile = globals.fs.file(outputFilePath); |
Danny Tuppeny | 126c58e | 2019-03-12 11:47:17 +0000 | [diff] [blame] | 247 | if (outputFile.existsSync()) { |
| 248 | throwToolExit('File "$outputFilePath" already exists', exitCode: 1); |
| 249 | } |
Devon Carew | 50a9c31 | 2019-05-16 18:20:30 -0700 | [diff] [blame] | 250 | final String samplesJson = await _fetchSamplesIndexFromServer(); |
| 251 | if (samplesJson == null) { |
| 252 | throwToolExit('Unable to download samples', exitCode: 2); |
| 253 | } |
| 254 | else { |
| 255 | outputFile.writeAsStringSync(samplesJson); |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 256 | globals.printStatus('Wrote samples JSON to "$outputFilePath"'); |
Devon Carew | 50a9c31 | 2019-05-16 18:20:30 -0700 | [diff] [blame] | 257 | } |
Danny Tuppeny | 126c58e | 2019-03-12 11:47:17 +0000 | [diff] [blame] | 258 | } catch (e) { |
| 259 | throwToolExit('Failed to write samples JSON to "$outputFilePath": $e', exitCode: 2); |
| 260 | } |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Emmanuel Garcia | 3bbdf01 | 2019-05-29 20:56:28 -0700 | [diff] [blame] | 263 | _ProjectType _getProjectType(Directory projectDir) { |
| 264 | _ProjectType template; |
| 265 | _ProjectType detectedProjectType; |
| 266 | final bool metadataExists = projectDir.absolute.childFile('.metadata').existsSync(); |
| 267 | if (argResults['template'] != null) { |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 268 | template = _stringToProjectType(stringArg('template')); |
Emmanuel Garcia | 3bbdf01 | 2019-05-29 20:56:28 -0700 | [diff] [blame] | 269 | } else { |
| 270 | // If the project directory exists and isn't empty, then try to determine the template |
| 271 | // type from the project directory. |
| 272 | if (projectDir.existsSync() && projectDir.listSync().isNotEmpty) { |
| 273 | detectedProjectType = _determineTemplateType(projectDir); |
| 274 | if (detectedProjectType == null && metadataExists) { |
| 275 | // We can only be definitive that this is the wrong type if the .metadata file |
| 276 | // exists and contains a type that we don't understand, or doesn't contain a type. |
| 277 | throwToolExit('Sorry, unable to detect the type of project to recreate. ' |
| 278 | 'Try creating a fresh project and migrating your existing code to ' |
| 279 | 'the new project manually.'); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | template ??= detectedProjectType ?? _ProjectType.app; |
| 284 | if (detectedProjectType != null && template != detectedProjectType && metadataExists) { |
| 285 | // We can only be definitive that this is the wrong type if the .metadata file |
| 286 | // exists and contains a type that doesn't match. |
| 287 | throwToolExit("The requested template type '${getEnumName(template)}' doesn't match the " |
| 288 | "existing template type of '${getEnumName(detectedProjectType)}'."); |
| 289 | } |
| 290 | return template; |
| 291 | } |
| 292 | |
Ian Fischer | 384ded5 | 2015-09-10 14:40:25 -0700 | [diff] [blame] | 293 | @override |
Alexandre Ardhuin | 2d3ff10 | 2018-10-05 07:54:56 +0200 | [diff] [blame] | 294 | Future<FlutterCommandResult> runCommand() async { |
Danny Tuppeny | 126c58e | 2019-03-12 11:47:17 +0000 | [diff] [blame] | 295 | if (argResults['list-samples'] != null) { |
Devon Carew | 50a9c31 | 2019-05-16 18:20:30 -0700 | [diff] [blame] | 296 | // _writeSamplesJson can potentially be long-lived. |
| 297 | Cache.releaseLockEarly(); |
| 298 | |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 299 | await _writeSamplesJson(stringArg('list-samples')); |
Danny Tuppeny | 126c58e | 2019-03-12 11:47:17 +0000 | [diff] [blame] | 300 | return null; |
| 301 | } |
| 302 | |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 303 | if (argResults.rest.isEmpty) { |
Dan Rubel | cccd917 | 2016-11-11 10:42:09 -0500 | [diff] [blame] | 304 | throwToolExit('No option specified for the output directory.\n$usage', exitCode: 2); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 305 | } |
Devon Carew | 2efd131 | 2015-08-07 14:20:14 -0700 | [diff] [blame] | 306 | |
Ian Hickson | a701606 | 2016-03-19 18:37:32 -0700 | [diff] [blame] | 307 | if (argResults.rest.length > 1) { |
Dan Rubel | cccd917 | 2016-11-11 10:42:09 -0500 | [diff] [blame] | 308 | String message = 'Multiple output directories specified.'; |
Alexandre Ardhuin | 4f9b6cf | 2020-01-07 16:32:04 +0100 | [diff] [blame] | 309 | for (final String arg in argResults.rest) { |
Dan Rubel | 949e27a | 2016-10-26 19:11:55 +0100 | [diff] [blame] | 310 | if (arg.startsWith('-')) { |
Dan Rubel | cccd917 | 2016-11-11 10:42:09 -0500 | [diff] [blame] | 311 | message += '\nTry moving $arg to be immediately following $name'; |
Dan Rubel | 949e27a | 2016-10-26 19:11:55 +0100 | [diff] [blame] | 312 | break; |
| 313 | } |
| 314 | } |
Dan Rubel | cccd917 | 2016-11-11 10:42:09 -0500 | [diff] [blame] | 315 | throwToolExit(message, exitCode: 2); |
Ian Hickson | a701606 | 2016-03-19 18:37:32 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 318 | if (Cache.flutterRoot == null) { |
Greg Spencer | 081d2a7 | 2018-10-10 18:17:56 -0700 | [diff] [blame] | 319 | throwToolExit('Neither the --flutter-root command line flag nor the FLUTTER_ROOT environment ' |
Dan Rubel | cccd917 | 2016-11-11 10:42:09 -0500 | [diff] [blame] | 320 | 'variable was specified. Unable to find package:flutter.', exitCode: 2); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 321 | } |
Devon Carew | 0da7463 | 2016-02-18 14:33:59 -0800 | [diff] [blame] | 322 | |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 323 | final String flutterRoot = globals.fs.path.absolute(Cache.flutterRoot); |
Adam Barth | 2710e0f | 2015-11-07 15:02:27 -0800 | [diff] [blame] | 324 | |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 325 | final String flutterPackagesDirectory = globals.fs.path.join(flutterRoot, 'packages'); |
| 326 | final String flutterPackagePath = globals.fs.path.join(flutterPackagesDirectory, 'flutter'); |
| 327 | if (!globals.fs.isFileSync(globals.fs.path.join(flutterPackagePath, 'pubspec.yaml'))) { |
Dan Rubel | cccd917 | 2016-11-11 10:42:09 -0500 | [diff] [blame] | 328 | throwToolExit('Unable to find package:flutter in $flutterPackagePath', exitCode: 2); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 329 | } |
Adam Barth | 2710e0f | 2015-11-07 15:02:27 -0800 | [diff] [blame] | 330 | |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 331 | final String flutterDriverPackagePath = globals.fs.path.join(flutterRoot, 'packages', 'flutter_driver'); |
| 332 | if (!globals.fs.isFileSync(globals.fs.path.join(flutterDriverPackagePath, 'pubspec.yaml'))) { |
Dan Rubel | cccd917 | 2016-11-11 10:42:09 -0500 | [diff] [blame] | 333 | throwToolExit('Unable to find package:flutter_driver in $flutterDriverPackagePath', exitCode: 2); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 334 | } |
yjbanov | 278630e | 2016-02-22 14:55:17 -0800 | [diff] [blame] | 335 | |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 336 | final Directory projectDir = globals.fs.directory(argResults.rest.first); |
| 337 | final String projectDirPath = globals.fs.path.normalize(projectDir.absolute.path); |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 338 | |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 339 | String sampleCode; |
| 340 | if (argResults['sample'] != null) { |
| 341 | if (argResults['template'] != null && |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 342 | _stringToProjectType(stringArg('template') ?? 'app') != _ProjectType.app) { |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 343 | throwToolExit('Cannot specify --sample with a project type other than ' |
| 344 | '"${getEnumName(_ProjectType.app)}"'); |
| 345 | } |
| 346 | // Fetch the sample from the server. |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 347 | sampleCode = await _fetchSampleFromServer(stringArg('sample')); |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Emmanuel Garcia | 3bbdf01 | 2019-05-29 20:56:28 -0700 | [diff] [blame] | 350 | final _ProjectType template = _getProjectType(projectDir); |
Greg Spencer | 0ff9e8a | 2018-10-10 11:01:40 -0700 | [diff] [blame] | 351 | final bool generateModule = template == _ProjectType.module; |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 352 | final bool generatePlugin = template == _ProjectType.plugin; |
| 353 | final bool generatePackage = template == _ProjectType.package; |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 354 | |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 355 | String organization = stringArg('org'); |
Mikkel Nygaard Ravn | 4353297 | 2018-01-18 09:21:24 +0100 | [diff] [blame] | 356 | if (!argResults.wasParsed('org')) { |
Jonah Williams | 4ff4671 | 2019-04-29 08:21:32 -0700 | [diff] [blame] | 357 | final FlutterProject project = FlutterProject.fromDirectory(projectDir); |
Zachary Anderson | 8a33d24 | 2019-09-16 07:51:50 -0700 | [diff] [blame] | 358 | final Set<String> existingOrganizations = await project.organizationNames; |
Mikkel Nygaard Ravn | 4353297 | 2018-01-18 09:21:24 +0100 | [diff] [blame] | 359 | if (existingOrganizations.length == 1) { |
| 360 | organization = existingOrganizations.first; |
Afzaal Ahmad Zeeshan | 9492dcc | 2019-11-07 23:17:03 +0500 | [diff] [blame] | 361 | } else if (existingOrganizations.length > 1) { |
Mikkel Nygaard Ravn | 4353297 | 2018-01-18 09:21:24 +0100 | [diff] [blame] | 362 | throwToolExit( |
Greg Spencer | 081d2a7 | 2018-10-10 18:17:56 -0700 | [diff] [blame] | 363 | 'Ambiguous organization in existing files: $existingOrganizations. ' |
Mikkel Nygaard Ravn | 4353297 | 2018-01-18 09:21:24 +0100 | [diff] [blame] | 364 | 'The --org command line argument must be specified to recreate project.' |
| 365 | ); |
| 366 | } |
| 367 | } |
Devon Carew | ebf1ecc | 2016-03-01 10:01:37 -0800 | [diff] [blame] | 368 | |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 369 | final bool overwrite = boolArg('overwrite'); |
| 370 | String error = _validateProjectDir(projectDirPath, flutterRoot: flutterRoot, overwrite: overwrite); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 371 | if (error != null) { |
Dan Rubel | cccd917 | 2016-11-11 10:42:09 -0500 | [diff] [blame] | 372 | throwToolExit(error); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 373 | } |
Devon Carew | 95fa9e3 | 2016-10-05 08:26:17 -0700 | [diff] [blame] | 374 | |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 375 | final String projectName = stringArg('project-name') ?? globals.fs.path.basename(projectDirPath); |
Adam Barth | 030f623 | 2016-10-28 20:29:19 -0700 | [diff] [blame] | 376 | error = _validateProjectName(projectName); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 377 | if (error != null) { |
Dan Rubel | cccd917 | 2016-11-11 10:42:09 -0500 | [diff] [blame] | 378 | throwToolExit(error); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 379 | } |
Devon Carew | ebf1ecc | 2016-03-01 10:01:37 -0800 | [diff] [blame] | 380 | |
Jakob Andersen | f34f8a3 | 2017-03-30 12:39:21 +0200 | [diff] [blame] | 381 | final Map<String, dynamic> templateContext = _templateContext( |
Mikkel Nygaard Ravn | 251d83a | 2017-05-24 16:19:16 +0200 | [diff] [blame] | 382 | organization: organization, |
Mikkel Nygaard Ravn | 10f6483 | 2017-05-24 08:22:50 +0200 | [diff] [blame] | 383 | projectName: projectName, |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 384 | projectDescription: stringArg('description'), |
Mikkel Nygaard Ravn | 10f6483 | 2017-05-24 08:22:50 +0200 | [diff] [blame] | 385 | flutterRoot: flutterRoot, |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 386 | renderDriverTest: boolArg('with-driver-test'), |
Mikkel Nygaard Ravn | 10f6483 | 2017-05-24 08:22:50 +0200 | [diff] [blame] | 387 | withPluginHook: generatePlugin, |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 388 | androidX: boolArg('androidx'), |
| 389 | androidLanguage: stringArg('android-language'), |
| 390 | iosLanguage: stringArg('ios-language'), |
Jonah Williams | 76ebcc8 | 2019-09-03 11:53:27 -0700 | [diff] [blame] | 391 | web: featureFlags.isWebEnabled, |
Jonah Williams | 0b2bf99 | 2019-12-03 08:13:08 -0800 | [diff] [blame] | 392 | macos: featureFlags.isMacOSEnabled, |
Ian Hickson | a701606 | 2016-03-19 18:37:32 -0700 | [diff] [blame] | 393 | ); |
Jakob Andersen | f34f8a3 | 2017-03-30 12:39:21 +0200 | [diff] [blame] | 394 | |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 395 | final String relativeDirPath = globals.fs.path.relative(projectDirPath); |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 396 | if (!projectDir.existsSync() || projectDir.listSync().isEmpty) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 397 | globals.printStatus('Creating project $relativeDirPath... androidx: ${boolArg('androidx')}'); |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 398 | } else { |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 399 | if (sampleCode != null && !overwrite) { |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 400 | throwToolExit('Will not overwrite existing project in $relativeDirPath: ' |
| 401 | 'must specify --overwrite for samples to overwrite.'); |
| 402 | } |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 403 | globals.printStatus('Recreating project $relativeDirPath...'); |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 404 | } |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 405 | |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 406 | final Directory relativeDir = globals.fs.directory(projectDirPath); |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 407 | int generatedFileCount = 0; |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 408 | switch (template) { |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 409 | case _ProjectType.app: |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 410 | generatedFileCount += await _generateApp(relativeDir, templateContext, overwrite: overwrite); |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 411 | break; |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 412 | case _ProjectType.module: |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 413 | generatedFileCount += await _generateModule(relativeDir, templateContext, overwrite: overwrite); |
Mikkel Nygaard Ravn | d89a6b5 | 2018-06-22 18:19:37 +0200 | [diff] [blame] | 414 | break; |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 415 | case _ProjectType.package: |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 416 | generatedFileCount += await _generatePackage(relativeDir, templateContext, overwrite: overwrite); |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 417 | break; |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 418 | case _ProjectType.plugin: |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 419 | generatedFileCount += await _generatePlugin(relativeDir, templateContext, overwrite: overwrite); |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 420 | break; |
Jakob Andersen | f34f8a3 | 2017-03-30 12:39:21 +0200 | [diff] [blame] | 421 | } |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 422 | if (sampleCode != null) { |
Zachary Anderson | 398ac1f | 2019-08-20 13:15:08 -0700 | [diff] [blame] | 423 | generatedFileCount += _applySample(relativeDir, sampleCode); |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 424 | } |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 425 | globals.printStatus('Wrote $generatedFileCount files.'); |
| 426 | globals.printStatus('\nAll done!'); |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 427 | final String application = sampleCode != null ? 'sample application' : 'application'; |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 428 | if (generatePackage) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 429 | final String relativeMainPath = globals.fs.path.normalize(globals.fs.path.join( |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 430 | relativeDirPath, |
| 431 | 'lib', |
| 432 | '${templateContext['projectName']}.dart', |
| 433 | )); |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 434 | globals.printStatus('Your package code is in $relativeMainPath'); |
Greg Spencer | 0ff9e8a | 2018-10-10 11:01:40 -0700 | [diff] [blame] | 435 | } else if (generateModule) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 436 | final String relativeMainPath = globals.fs.path.normalize(globals.fs.path.join( |
Greg Spencer | 21a32fd | 2018-10-05 15:49:53 -0700 | [diff] [blame] | 437 | relativeDirPath, |
| 438 | 'lib', |
| 439 | 'main.dart', |
| 440 | )); |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 441 | globals.printStatus('Your module code is in $relativeMainPath.'); |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 442 | } else { |
| 443 | // Run doctor; tell the user the next steps. |
Jonah Williams | 4ff4671 | 2019-04-29 08:21:32 -0700 | [diff] [blame] | 444 | final FlutterProject project = FlutterProject.fromPath(projectDirPath); |
Sigurd Meldgaard | 2d3a5c7 | 2018-07-20 08:00:30 +0200 | [diff] [blame] | 445 | final FlutterProject app = project.hasExampleApp ? project.example : project; |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 446 | final String relativeAppPath = globals.fs.path.normalize(globals.fs.path.relative(app.directory.path)); |
| 447 | final String relativeAppMain = globals.fs.path.join(relativeAppPath, 'lib', 'main.dart'); |
| 448 | final String relativePluginPath = globals.fs.path.normalize(globals.fs.path.relative(projectDirPath)); |
| 449 | final String relativePluginMain = globals.fs.path.join(relativePluginPath, 'lib', '$projectName.dart'); |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 450 | if (doctor.canLaunchAnything) { |
| 451 | // Let them know a summary of the state of their tooling. |
| 452 | await doctor.summary(); |
Jakob Andersen | f34f8a3 | 2017-03-30 12:39:21 +0200 | [diff] [blame] | 453 | |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 454 | globals.printStatus(''' |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 455 | In order to run your $application, type: |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 456 | |
| 457 | \$ cd $relativeAppPath |
| 458 | \$ flutter run |
| 459 | |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 460 | Your $application code is in $relativeAppMain. |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 461 | '''); |
| 462 | if (generatePlugin) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 463 | globals.printStatus(''' |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 464 | Your plugin code is in $relativePluginMain. |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 465 | |
Greg Spencer | 9f23866 | 2018-10-04 13:03:20 -0700 | [diff] [blame] | 466 | Host platform code is in the "android" and "ios" directories under $relativePluginPath. |
Tim Sneath | 5291897 | 2019-04-05 11:39:30 -0700 | [diff] [blame] | 467 | To edit platform code in an IDE see https://flutter.dev/developing-packages/#edit-plugin-package. |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 468 | '''); |
| 469 | } |
| 470 | } else { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 471 | globals.printStatus("You'll need to install additional components before you can run " |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 472 | 'your Flutter app:'); |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 473 | globals.printStatus(''); |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 474 | |
| 475 | // Give the user more detailed analysis. |
| 476 | await doctor.diagnose(); |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 477 | globals.printStatus(''); |
| 478 | globals.printStatus("After installing components, run 'flutter doctor' in order to " |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 479 | 're-validate your setup.'); |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 480 | globals.printStatus("When complete, type 'flutter run' from the '$relativeAppPath' " |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 481 | 'directory in order to launch your app.'); |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 482 | globals.printStatus('Your $application code is in $relativeAppMain'); |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 483 | } |
| 484 | } |
Alexandre Ardhuin | 2d3ff10 | 2018-10-05 07:54:56 +0200 | [diff] [blame] | 485 | |
| 486 | return null; |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 487 | } |
| 488 | |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 489 | Future<int> _generateModule(Directory directory, Map<String, dynamic> templateContext, { bool overwrite = false }) async { |
Mikkel Nygaard Ravn | d89a6b5 | 2018-06-22 18:19:37 +0200 | [diff] [blame] | 490 | int generatedCount = 0; |
| 491 | final String description = argResults.wasParsed('description') |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 492 | ? stringArg('description') |
Greg Spencer | 0ff9e8a | 2018-10-10 11:01:40 -0700 | [diff] [blame] | 493 | : 'A new flutter module project.'; |
Mikkel Nygaard Ravn | d89a6b5 | 2018-06-22 18:19:37 +0200 | [diff] [blame] | 494 | templateContext['description'] = description; |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 495 | generatedCount += _renderTemplate(globals.fs.path.join('module', 'common'), directory, templateContext, overwrite: overwrite); |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 496 | if (boolArg('pub')) { |
Jonah Williams | fde2675 | 2019-10-08 14:53:28 -0700 | [diff] [blame] | 497 | await pub.get( |
Mikkel Nygaard Ravn | d89a6b5 | 2018-06-22 18:19:37 +0200 | [diff] [blame] | 498 | context: PubContext.create, |
Mikkel Nygaard Ravn | b280074 | 2018-08-02 14:12:25 +0200 | [diff] [blame] | 499 | directory: directory.path, |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 500 | offline: boolArg('offline'), |
Mikkel Nygaard Ravn | d89a6b5 | 2018-06-22 18:19:37 +0200 | [diff] [blame] | 501 | ); |
Jonah Williams | 4ff4671 | 2019-04-29 08:21:32 -0700 | [diff] [blame] | 502 | final FlutterProject project = FlutterProject.fromDirectory(directory); |
Jonah Williams | a476a08 | 2019-04-22 15:18:15 -0700 | [diff] [blame] | 503 | await project.ensureReadyForPlatformSpecificTooling(checkProjects: false); |
Mikkel Nygaard Ravn | d89a6b5 | 2018-06-22 18:19:37 +0200 | [diff] [blame] | 504 | } |
| 505 | return generatedCount; |
| 506 | } |
| 507 | |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 508 | Future<int> _generatePackage(Directory directory, Map<String, dynamic> templateContext, { bool overwrite = false }) async { |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 509 | int generatedCount = 0; |
| 510 | final String description = argResults.wasParsed('description') |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 511 | ? stringArg('description') |
Greg Spencer | 0ff9e8a | 2018-10-10 11:01:40 -0700 | [diff] [blame] | 512 | : 'A new Flutter package project.'; |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 513 | templateContext['description'] = description; |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 514 | generatedCount += _renderTemplate('package', directory, templateContext, overwrite: overwrite); |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 515 | if (boolArg('pub')) { |
Jonah Williams | fde2675 | 2019-10-08 14:53:28 -0700 | [diff] [blame] | 516 | await pub.get( |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 517 | context: PubContext.createPackage, |
Mikkel Nygaard Ravn | b280074 | 2018-08-02 14:12:25 +0200 | [diff] [blame] | 518 | directory: directory.path, |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 519 | offline: boolArg('offline'), |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 520 | ); |
| 521 | } |
| 522 | return generatedCount; |
| 523 | } |
| 524 | |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 525 | Future<int> _generatePlugin(Directory directory, Map<String, dynamic> templateContext, { bool overwrite = false }) async { |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 526 | int generatedCount = 0; |
| 527 | final String description = argResults.wasParsed('description') |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 528 | ? stringArg('description') |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 529 | : 'A new flutter plugin project.'; |
| 530 | templateContext['description'] = description; |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 531 | generatedCount += _renderTemplate('plugin', directory, templateContext, overwrite: overwrite); |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 532 | if (boolArg('pub')) { |
Jonah Williams | fde2675 | 2019-10-08 14:53:28 -0700 | [diff] [blame] | 533 | await pub.get( |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 534 | context: PubContext.createPlugin, |
Mikkel Nygaard Ravn | b280074 | 2018-08-02 14:12:25 +0200 | [diff] [blame] | 535 | directory: directory.path, |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 536 | offline: boolArg('offline'), |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 537 | ); |
| 538 | } |
Jonah Williams | 4ff4671 | 2019-04-29 08:21:32 -0700 | [diff] [blame] | 539 | final FlutterProject project = FlutterProject.fromDirectory(directory); |
Mikkel Nygaard Ravn | d4e5e1e | 2018-08-16 13:21:55 +0200 | [diff] [blame] | 540 | gradle.updateLocalProperties(project: project, requireAndroidSdk: false); |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 541 | |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 542 | final String projectName = templateContext['projectName'] as String; |
| 543 | final String organization = templateContext['organization'] as String; |
| 544 | final String androidPluginIdentifier = templateContext['androidIdentifier'] as String; |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 545 | final String exampleProjectName = projectName + '_example'; |
| 546 | templateContext['projectName'] = exampleProjectName; |
| 547 | templateContext['androidIdentifier'] = _createAndroidIdentifier(organization, exampleProjectName); |
| 548 | templateContext['iosIdentifier'] = _createUTIIdentifier(organization, exampleProjectName); |
| 549 | templateContext['description'] = 'Demonstrates how to use the $projectName plugin.'; |
| 550 | templateContext['pluginProjectName'] = projectName; |
| 551 | templateContext['androidPluginIdentifier'] = androidPluginIdentifier; |
| 552 | |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 553 | generatedCount += await _generateApp(project.example.directory, templateContext, overwrite: overwrite); |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 554 | return generatedCount; |
| 555 | } |
| 556 | |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 557 | Future<int> _generateApp(Directory directory, Map<String, dynamic> templateContext, { bool overwrite = false }) async { |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 558 | int generatedCount = 0; |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 559 | generatedCount += _renderTemplate('app', directory, templateContext, overwrite: overwrite); |
Jonah Williams | 4ff4671 | 2019-04-29 08:21:32 -0700 | [diff] [blame] | 560 | final FlutterProject project = FlutterProject.fromDirectory(directory); |
Sigurd Meldgaard | 2d3a5c7 | 2018-07-20 08:00:30 +0200 | [diff] [blame] | 561 | generatedCount += _injectGradleWrapper(project); |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 562 | |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 563 | if (boolArg('with-driver-test')) { |
Mikkel Nygaard Ravn | b280074 | 2018-08-02 14:12:25 +0200 | [diff] [blame] | 564 | final Directory testDirectory = directory.childDirectory('test_driver'); |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 565 | generatedCount += _renderTemplate('driver', testDirectory, templateContext, overwrite: overwrite); |
Jakob Andersen | f34f8a3 | 2017-03-30 12:39:21 +0200 | [diff] [blame] | 566 | } |
| 567 | |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 568 | if (boolArg('pub')) { |
| 569 | await pub.get(context: PubContext.create, directory: directory.path, offline: boolArg('offline')); |
Jonah Williams | a476a08 | 2019-04-22 15:18:15 -0700 | [diff] [blame] | 570 | await project.ensureReadyForPlatformSpecificTooling(checkProjects: false); |
Jakob Andersen | 7ffa82a | 2017-04-10 15:44:19 +0200 | [diff] [blame] | 571 | } |
Devon Carew | e6b45c5 | 2015-09-11 16:12:27 -0700 | [diff] [blame] | 572 | |
Mikkel Nygaard Ravn | d4e5e1e | 2018-08-16 13:21:55 +0200 | [diff] [blame] | 573 | gradle.updateLocalProperties(project: project, requireAndroidSdk: false); |
Jakob Andersen | 6b54137 | 2017-05-05 14:53:51 +0200 | [diff] [blame] | 574 | |
Mikkel Nygaard Ravn | 0d1574c | 2018-05-30 11:25:21 +0200 | [diff] [blame] | 575 | return generatedCount; |
Devon Carew | 2efd131 | 2015-08-07 14:20:14 -0700 | [diff] [blame] | 576 | } |
Devon Carew | 2efd131 | 2015-08-07 14:20:14 -0700 | [diff] [blame] | 577 | |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 578 | // Takes an application template and replaces the main.dart with one from the |
| 579 | // documentation website in sampleCode. Returns the difference in the number |
| 580 | // of files after applying the sample, since it also deletes the application's |
| 581 | // test directory (since the template's test doesn't apply to the sample). |
Zachary Anderson | 398ac1f | 2019-08-20 13:15:08 -0700 | [diff] [blame] | 582 | int _applySample(Directory directory, String sampleCode) { |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 583 | final File mainDartFile = directory.childDirectory('lib').childFile('main.dart'); |
Zachary Anderson | 398ac1f | 2019-08-20 13:15:08 -0700 | [diff] [blame] | 584 | mainDartFile.createSync(recursive: true); |
| 585 | mainDartFile.writeAsStringSync(sampleCode); |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 586 | final Directory testDir = directory.childDirectory('test'); |
| 587 | final List<FileSystemEntity> files = testDir.listSync(recursive: true); |
Zachary Anderson | 398ac1f | 2019-08-20 13:15:08 -0700 | [diff] [blame] | 588 | testDir.deleteSync(recursive: true); |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 589 | return -files.length; |
| 590 | } |
| 591 | |
Mikkel Nygaard Ravn | 10f6483 | 2017-05-24 08:22:50 +0200 | [diff] [blame] | 592 | Map<String, dynamic> _templateContext({ |
Mikkel Nygaard Ravn | 251d83a | 2017-05-24 16:19:16 +0200 | [diff] [blame] | 593 | String organization, |
Mikkel Nygaard Ravn | 10f6483 | 2017-05-24 08:22:50 +0200 | [diff] [blame] | 594 | String projectName, |
| 595 | String projectDescription, |
| 596 | String androidLanguage, |
Josh Burton | d0e45a2 | 2019-06-01 13:33:02 +1200 | [diff] [blame] | 597 | bool androidX, |
Mikkel Nygaard Ravn | 10f6483 | 2017-05-24 08:22:50 +0200 | [diff] [blame] | 598 | String iosLanguage, |
Mikkel Nygaard Ravn | 10f6483 | 2017-05-24 08:22:50 +0200 | [diff] [blame] | 599 | String flutterRoot, |
Alexandre Ardhuin | 09276be | 2018-06-05 08:50:40 +0200 | [diff] [blame] | 600 | bool renderDriverTest = false, |
| 601 | bool withPluginHook = false, |
Jonah Williams | bd413bf | 2019-06-07 13:15:38 -0700 | [diff] [blame] | 602 | bool web = false, |
stuartmorgan | 0412977 | 2019-09-19 17:06:18 -0700 | [diff] [blame] | 603 | bool macos = false, |
Mikkel Nygaard Ravn | 10f6483 | 2017-05-24 08:22:50 +0200 | [diff] [blame] | 604 | }) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 605 | flutterRoot = globals.fs.path.normalize(flutterRoot); |
Chinmay Garde | 038367a | 2016-02-23 15:40:51 -0800 | [diff] [blame] | 606 | |
Jakob Andersen | f34f8a3 | 2017-03-30 12:39:21 +0200 | [diff] [blame] | 607 | final String pluginDartClass = _createPluginClassName(projectName); |
| 608 | final String pluginClass = pluginDartClass.endsWith('Plugin') |
| 609 | ? pluginDartClass |
| 610 | : pluginDartClass + 'Plugin'; |
stuartmorgan | 0412977 | 2019-09-19 17:06:18 -0700 | [diff] [blame] | 611 | final String appleIdentifier = _createUTIIdentifier(organization, projectName); |
Devon Carew | 2efd131 | 2015-08-07 14:20:14 -0700 | [diff] [blame] | 612 | |
Jakob Andersen | f34f8a3 | 2017-03-30 12:39:21 +0200 | [diff] [blame] | 613 | return <String, dynamic>{ |
Mikkel Nygaard Ravn | 251d83a | 2017-05-24 16:19:16 +0200 | [diff] [blame] | 614 | 'organization': organization, |
Chinmay Garde | 038367a | 2016-02-23 15:40:51 -0800 | [diff] [blame] | 615 | 'projectName': projectName, |
Mikkel Nygaard Ravn | 251d83a | 2017-05-24 16:19:16 +0200 | [diff] [blame] | 616 | 'androidIdentifier': _createAndroidIdentifier(organization, projectName), |
stuartmorgan | 0412977 | 2019-09-19 17:06:18 -0700 | [diff] [blame] | 617 | 'iosIdentifier': appleIdentifier, |
| 618 | 'macosIdentifier': appleIdentifier, |
Ian Hickson | a701606 | 2016-03-19 18:37:32 -0700 | [diff] [blame] | 619 | 'description': projectDescription, |
Brian Slesinsky | 5cb9097 | 2017-04-14 10:12:48 -0700 | [diff] [blame] | 620 | 'dartSdk': '$flutterRoot/bin/cache/dart-sdk', |
Josh Burton | d0e45a2 | 2019-06-01 13:33:02 +1200 | [diff] [blame] | 621 | 'androidX': androidX, |
Emmanuel Garcia | 08c645b | 2019-10-16 21:26:10 -0700 | [diff] [blame] | 622 | 'useAndroidEmbeddingV2': featureFlags.isAndroidEmbeddingV2Enabled, |
Jakob Andersen | f34f8a3 | 2017-03-30 12:39:21 +0200 | [diff] [blame] | 623 | 'androidMinApiLevel': android.minApiLevel, |
Brian Slesinsky | 5cb9097 | 2017-04-14 10:12:48 -0700 | [diff] [blame] | 624 | 'androidSdkVersion': android_sdk.minimumAndroidSdkVersion, |
Alexandre Ardhuin | 1fce14a | 2017-10-22 18:11:36 +0200 | [diff] [blame] | 625 | 'androidFlutterJar': '$flutterRoot/bin/cache/artifacts/engine/android-arm/flutter.jar', |
Jakob Andersen | f34f8a3 | 2017-03-30 12:39:21 +0200 | [diff] [blame] | 626 | 'withDriverTest': renderDriverTest, |
| 627 | 'pluginClass': pluginClass, |
| 628 | 'pluginDartClass': pluginDartClass, |
| 629 | 'withPluginHook': withPluginHook, |
Mikkel Nygaard Ravn | 10f6483 | 2017-05-24 08:22:50 +0200 | [diff] [blame] | 630 | 'androidLanguage': androidLanguage, |
| 631 | 'iosLanguage': iosLanguage, |
xster | c17099f | 2017-11-03 10:07:57 -0700 | [diff] [blame] | 632 | 'flutterRevision': FlutterVersion.instance.frameworkRevision, |
| 633 | 'flutterChannel': FlutterVersion.instance.channel, |
Jonah Williams | 76ebcc8 | 2019-09-03 11:53:27 -0700 | [diff] [blame] | 634 | 'web': web, |
stuartmorgan | 0412977 | 2019-09-19 17:06:18 -0700 | [diff] [blame] | 635 | 'macos': macos, |
| 636 | 'year': DateTime.now().year, |
| 637 | // For now, the new plugin schema is only used when a desktop plugin is |
| 638 | // enabled. Once the new schema is supported on stable, this should be |
| 639 | // removed, and the new schema should always be used. |
| 640 | 'useNewPluginSchema': macos, |
| 641 | // If a desktop platform is included, add a workaround for #31366. |
Greg Spencer | 245d1b5 | 2019-11-26 18:32:34 -0800 | [diff] [blame] | 642 | // When Linux and Windows are added, we will need this workaround again. |
| 643 | 'includeTargetPlatformWorkaround': false, |
Chinmay Garde | 038367a | 2016-02-23 15:40:51 -0800 | [diff] [blame] | 644 | }; |
Jakob Andersen | f34f8a3 | 2017-03-30 12:39:21 +0200 | [diff] [blame] | 645 | } |
Chinmay Garde | 038367a | 2016-02-23 15:40:51 -0800 | [diff] [blame] | 646 | |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 647 | int _renderTemplate(String templateName, Directory directory, Map<String, dynamic> context, { bool overwrite = false }) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 648 | final Template template = Template.fromName(templateName); |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 649 | return template.render(directory, context, overwriteExisting: overwrite); |
Devon Carew | 2efd131 | 2015-08-07 14:20:14 -0700 | [diff] [blame] | 650 | } |
Mikkel Nygaard Ravn | c5999c7 | 2017-06-26 12:47:43 +0200 | [diff] [blame] | 651 | |
Sigurd Meldgaard | 2d3a5c7 | 2018-07-20 08:00:30 +0200 | [diff] [blame] | 652 | int _injectGradleWrapper(FlutterProject project) { |
Mikkel Nygaard Ravn | c5999c7 | 2017-06-26 12:47:43 +0200 | [diff] [blame] | 653 | int filesCreated = 0; |
| 654 | copyDirectorySync( |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 655 | globals.cache.getArtifactDirectory('gradle_wrapper'), |
Mikkel Nygaard Ravn | d4e5e1e | 2018-08-16 13:21:55 +0200 | [diff] [blame] | 656 | project.android.hostAppGradleRoot, |
Emmanuel Garcia | 4a1c62c | 2019-08-28 14:52:08 -0700 | [diff] [blame] | 657 | onFileCopied: (File sourceFile, File destinationFile) { |
Mikkel Nygaard Ravn | c5999c7 | 2017-06-26 12:47:43 +0200 | [diff] [blame] | 658 | filesCreated++; |
| 659 | final String modes = sourceFile.statSync().modeString(); |
| 660 | if (modes != null && modes.contains('x')) { |
| 661 | os.makeExecutable(destinationFile); |
| 662 | } |
| 663 | }, |
| 664 | ); |
| 665 | return filesCreated; |
| 666 | } |
Devon Carew | 2efd131 | 2015-08-07 14:20:14 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Mikkel Nygaard Ravn | 251d83a | 2017-05-24 16:19:16 +0200 | [diff] [blame] | 669 | String _createAndroidIdentifier(String organization, String name) { |
KyleWong | 199ebaa | 2019-02-13 11:40:53 +0800 | [diff] [blame] | 670 | // Android application ID is specified in: https://developer.android.com/studio/build/application-id |
| 671 | // All characters must be alphanumeric or an underscore [a-zA-Z0-9_]. |
| 672 | String tmpIdentifier = '$organization.$name'; |
| 673 | final RegExp disallowed = RegExp(r'[^\w\.]'); |
| 674 | tmpIdentifier = tmpIdentifier.replaceAll(disallowed, ''); |
| 675 | |
| 676 | // It must have at least two segments (one or more dots). |
| 677 | final List<String> segments = tmpIdentifier |
| 678 | .split('.') |
| 679 | .where((String segment) => segment.isNotEmpty) |
| 680 | .toList(); |
| 681 | while (segments.length < 2) { |
| 682 | segments.add('untitled'); |
| 683 | } |
| 684 | |
| 685 | // Each segment must start with a letter. |
| 686 | final RegExp segmentPatternRegex = RegExp(r'^[a-zA-Z][\w]*$'); |
| 687 | final List<String> prefixedSegments = segments |
| 688 | .map((String segment) { |
| 689 | if (!segmentPatternRegex.hasMatch(segment)) { |
| 690 | return 'u'+segment; |
| 691 | } |
| 692 | return segment; |
| 693 | }) |
| 694 | .toList(); |
| 695 | return prefixedSegments.join('.'); |
Jakob Andersen | f34f8a3 | 2017-03-30 12:39:21 +0200 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | String _createPluginClassName(String name) { |
| 699 | final String camelizedName = camelCase(name); |
| 700 | return camelizedName[0].toUpperCase() + camelizedName.substring(1); |
Devon Carew | 4c56919 | 2016-03-02 11:22:19 -0800 | [diff] [blame] | 701 | } |
| 702 | |
Mikkel Nygaard Ravn | 251d83a | 2017-05-24 16:19:16 +0200 | [diff] [blame] | 703 | String _createUTIIdentifier(String organization, String name) { |
Chinmay Garde | f6a2477 | 2016-02-19 12:30:18 -0800 | [diff] [blame] | 704 | // Create a UTI (https://en.wikipedia.org/wiki/Uniform_Type_Identifier) from a base name |
KyleWong | 199ebaa | 2019-02-13 11:40:53 +0800 | [diff] [blame] | 705 | name = camelCase(name); |
| 706 | String tmpIdentifier = '$organization.$name'; |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 707 | final RegExp disallowed = RegExp(r'[^a-zA-Z0-9\-\.\u0080-\uffff]+'); |
KyleWong | 199ebaa | 2019-02-13 11:40:53 +0800 | [diff] [blame] | 708 | tmpIdentifier = tmpIdentifier.replaceAll(disallowed, ''); |
| 709 | |
| 710 | // It must have at least two segments (one or more dots). |
| 711 | final List<String> segments = tmpIdentifier |
| 712 | .split('.') |
| 713 | .where((String segment) => segment.isNotEmpty) |
| 714 | .toList(); |
| 715 | while (segments.length < 2) { |
| 716 | segments.add('untitled'); |
| 717 | } |
| 718 | |
| 719 | return segments.join('.'); |
Chinmay Garde | f6a2477 | 2016-02-19 12:30:18 -0800 | [diff] [blame] | 720 | } |
Devon Carew | ebf1ecc | 2016-03-01 10:01:37 -0800 | [diff] [blame] | 721 | |
Phil Quitslund | 802eca2 | 2019-03-06 11:05:16 -0800 | [diff] [blame] | 722 | const Set<String> _packageDependencies = <String>{ |
Jason Simmons | 96ce9d6 | 2018-03-09 13:11:33 -0800 | [diff] [blame] | 723 | 'analyzer', |
Devon Carew | ebf1ecc | 2016-03-01 10:01:37 -0800 | [diff] [blame] | 724 | 'args', |
| 725 | 'async', |
| 726 | 'collection', |
| 727 | 'convert', |
Jason Simmons | 96ce9d6 | 2018-03-09 13:11:33 -0800 | [diff] [blame] | 728 | 'crypto', |
Devon Carew | ebf1ecc | 2016-03-01 10:01:37 -0800 | [diff] [blame] | 729 | 'flutter', |
Jason Simmons | 96ce9d6 | 2018-03-09 13:11:33 -0800 | [diff] [blame] | 730 | 'flutter_test', |
| 731 | 'front_end', |
Devon Carew | ebf1ecc | 2016-03-01 10:01:37 -0800 | [diff] [blame] | 732 | 'html', |
Jason Simmons | 96ce9d6 | 2018-03-09 13:11:33 -0800 | [diff] [blame] | 733 | 'http', |
Devon Carew | ebf1ecc | 2016-03-01 10:01:37 -0800 | [diff] [blame] | 734 | 'intl', |
Jason Simmons | 96ce9d6 | 2018-03-09 13:11:33 -0800 | [diff] [blame] | 735 | 'io', |
| 736 | 'isolate', |
| 737 | 'kernel', |
Devon Carew | ebf1ecc | 2016-03-01 10:01:37 -0800 | [diff] [blame] | 738 | 'logging', |
| 739 | 'matcher', |
Jason Simmons | 96ce9d6 | 2018-03-09 13:11:33 -0800 | [diff] [blame] | 740 | 'meta', |
Devon Carew | ebf1ecc | 2016-03-01 10:01:37 -0800 | [diff] [blame] | 741 | 'mime', |
| 742 | 'path', |
| 743 | 'plugin', |
| 744 | 'pool', |
| 745 | 'test', |
| 746 | 'utf', |
| 747 | 'watcher', |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 748 | 'yaml', |
Phil Quitslund | 802eca2 | 2019-03-06 11:05:16 -0800 | [diff] [blame] | 749 | }; |
Devon Carew | ebf1ecc | 2016-03-01 10:01:37 -0800 | [diff] [blame] | 750 | |
Jonah Williams | 0a2d8e0 | 2019-12-16 18:37:20 -0800 | [diff] [blame] | 751 | // A valid Dart identifier. |
| 752 | // https://dart.dev/guides/language/language-tour#important-concepts |
| 753 | final RegExp _identifierRegExp = RegExp('[a-zA-Z_][a-zA-Z0-9_]*'); |
| 754 | |
| 755 | // non-contextual dart keywords. |
| 756 | //' https://dart.dev/guides/language/language-tour#keywords |
| 757 | const Set<String> _keywords = <String>{ |
| 758 | 'abstract', |
| 759 | 'as', |
| 760 | 'assert', |
| 761 | 'async', |
| 762 | 'await', |
| 763 | 'break', |
| 764 | 'case', |
| 765 | 'catch', |
| 766 | 'class', |
| 767 | 'const', |
| 768 | 'continue', |
| 769 | 'covariant', |
| 770 | 'default', |
| 771 | 'deferred', |
| 772 | 'do', |
| 773 | 'dynamic', |
| 774 | 'else', |
| 775 | 'enum', |
| 776 | 'export', |
| 777 | 'extends', |
| 778 | 'extension', |
| 779 | 'external', |
| 780 | 'factory', |
| 781 | 'false', |
| 782 | 'final', |
| 783 | 'finally', |
| 784 | 'for', |
| 785 | 'function', |
| 786 | 'get', |
| 787 | 'hide', |
| 788 | 'if', |
| 789 | 'implements', |
| 790 | 'import', |
| 791 | 'in', |
| 792 | 'inout', |
| 793 | 'interface', |
| 794 | 'is', |
| 795 | 'late', |
| 796 | 'library', |
| 797 | 'mixin', |
| 798 | 'native', |
| 799 | 'new', |
| 800 | 'null', |
| 801 | 'of', |
| 802 | 'on', |
| 803 | 'operator', |
| 804 | 'out', |
| 805 | 'part', |
| 806 | 'patch', |
| 807 | 'required', |
| 808 | 'rethrow', |
| 809 | 'return', |
| 810 | 'set', |
| 811 | 'show', |
| 812 | 'source', |
| 813 | 'static', |
| 814 | 'super', |
| 815 | 'switch', |
| 816 | 'sync', |
| 817 | 'this', |
| 818 | 'throw', |
| 819 | 'true', |
| 820 | 'try', |
| 821 | 'typedef', |
| 822 | 'var', |
| 823 | 'void', |
| 824 | 'while', |
| 825 | 'with', |
| 826 | 'yield', |
| 827 | }; |
| 828 | |
| 829 | /// Whether [name] is a valid Pub package. |
| 830 | @visibleForTesting |
| 831 | bool isValidPackageName(String name) { |
| 832 | final Match match = _identifierRegExp.matchAsPrefix(name); |
| 833 | return match != null && match.end == name.length && !_keywords.contains(name); |
| 834 | } |
| 835 | |
Ian Hickson | 0f1a703 | 2017-06-08 17:13:03 -0700 | [diff] [blame] | 836 | /// Return null if the project name is legal. Return a validation message if |
Devon Carew | ebf1ecc | 2016-03-01 10:01:37 -0800 | [diff] [blame] | 837 | /// we should disallow the project name. |
| 838 | String _validateProjectName(String projectName) { |
Jonah Williams | 0a2d8e0 | 2019-12-16 18:37:20 -0800 | [diff] [blame] | 839 | if (!isValidPackageName(projectName)) { |
| 840 | return '"$projectName" is not a valid Dart package name.\n\n' |
| 841 | 'See https://dart.dev/tools/pub/pubspec#name for more information.'; |
Phil Quitslund | a4bc470 | 2017-12-01 10:54:24 -0800 | [diff] [blame] | 842 | } |
Devon Carew | ebf1ecc | 2016-03-01 10:01:37 -0800 | [diff] [blame] | 843 | if (_packageDependencies.contains(projectName)) { |
| 844 | return "Invalid project name: '$projectName' - this will conflict with Flutter " |
Alexandre Ardhuin | 1fce14a | 2017-10-22 18:11:36 +0200 | [diff] [blame] | 845 | 'package dependencies.'; |
Devon Carew | ebf1ecc | 2016-03-01 10:01:37 -0800 | [diff] [blame] | 846 | } |
stevemessick | 8847b86 | 2016-04-08 20:37:50 -0700 | [diff] [blame] | 847 | return null; |
| 848 | } |
Devon Carew | ebf1ecc | 2016-03-01 10:01:37 -0800 | [diff] [blame] | 849 | |
Ian Hickson | 0f1a703 | 2017-06-08 17:13:03 -0700 | [diff] [blame] | 850 | /// Return null if the project directory is legal. Return a validation message |
stevemessick | 8847b86 | 2016-04-08 20:37:50 -0700 | [diff] [blame] | 851 | /// if we should disallow the directory name. |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 852 | String _validateProjectDir(String dirPath, { String flutterRoot, bool overwrite = false }) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 853 | if (globals.fs.path.isWithin(flutterRoot, dirPath)) { |
Greg Spencer | 081d2a7 | 2018-10-10 18:17:56 -0700 | [diff] [blame] | 854 | return 'Cannot create a project within the Flutter SDK. ' |
Adam Barth | 030f623 | 2016-10-28 20:29:19 -0700 | [diff] [blame] | 855 | "Target directory '$dirPath' is within the Flutter SDK at '$flutterRoot'."; |
| 856 | } |
| 857 | |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 858 | // If the destination directory is actually a file, then we refuse to |
| 859 | // overwrite, on the theory that the user probably didn't expect it to exist. |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 860 | if (globals.fs.isFileSync(dirPath)) { |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 861 | return "Invalid project name: '$dirPath' - refers to an existing file." |
| 862 | '${overwrite ? ' Refusing to overwrite a file with a directory.' : ''}'; |
| 863 | } |
| 864 | |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 865 | if (overwrite) { |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 866 | return null; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 867 | } |
Greg Spencer | f9c6f30 | 2018-10-30 16:01:14 -0700 | [diff] [blame] | 868 | |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 869 | final FileSystemEntityType type = globals.fs.typeSync(dirPath); |
Devon Carew | 80fabfd | 2016-04-19 19:30:49 -0700 | [diff] [blame] | 870 | |
Leaf Petersen | 32f9444 | 2018-07-20 15:07:24 -0700 | [diff] [blame] | 871 | if (type != FileSystemEntityType.notFound) { |
Ian Hickson | efb45ea | 2017-09-27 16:13:48 -0700 | [diff] [blame] | 872 | switch (type) { |
Leaf Petersen | 32f9444 | 2018-07-20 15:07:24 -0700 | [diff] [blame] | 873 | case FileSystemEntityType.file: |
stevemessick | 8847b86 | 2016-04-08 20:37:50 -0700 | [diff] [blame] | 874 | // Do not overwrite files. |
Adam Barth | 030f623 | 2016-10-28 20:29:19 -0700 | [diff] [blame] | 875 | return "Invalid project name: '$dirPath' - file exists."; |
Leaf Petersen | 32f9444 | 2018-07-20 15:07:24 -0700 | [diff] [blame] | 876 | case FileSystemEntityType.link: |
stevemessick | 8847b86 | 2016-04-08 20:37:50 -0700 | [diff] [blame] | 877 | // Do not overwrite links. |
Adam Barth | 030f623 | 2016-10-28 20:29:19 -0700 | [diff] [blame] | 878 | return "Invalid project name: '$dirPath' - refers to a link."; |
stevemessick | 8847b86 | 2016-04-08 20:37:50 -0700 | [diff] [blame] | 879 | } |
| 880 | } |
Devon Carew | 80fabfd | 2016-04-19 19:30:49 -0700 | [diff] [blame] | 881 | |
Devon Carew | ebf1ecc | 2016-03-01 10:01:37 -0800 | [diff] [blame] | 882 | return null; |
| 883 | } |