create.dart 8.44 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'dart:io';

8
import 'package:path/path.dart' as path;
9

10
import '../android/android.dart' as android;
11
import '../base/utils.dart';
12
import '../cache.dart';
13
import '../dart/pub.dart';
14
import '../globals.dart';
15
import '../runner/flutter_command.dart';
16
import '../template.dart';
Hixie's avatar
Hixie committed
17

18
class CreateCommand extends FlutterCommand {
19
  CreateCommand() {
20
    argParser.addFlag('pub',
21
      defaultsTo: true,
22
      help: 'Whether to run "flutter packages get" after the project has been created.'
23
    );
24 25 26 27
    argParser.addFlag(
      'with-driver-test',
      negatable: true,
      defaultsTo: false,
28
      help: 'Also add a flutter_driver dependency and generate a sample \'flutter drive\' test.'
29
    );
30 31 32 33 34
    argParser.addOption(
      'description',
      defaultsTo: 'A new flutter project.',
      help: 'The description to use for your new flutter project. This string ends up in the pubspec.yaml file.'
    );
35 36
  }

37 38 39 40 41 42 43
  @override
  final String name = 'create';

  @override
  final String description = 'Create a new Flutter project.\n\n'
    'If run on a project that already exists, this will repair the project, recreating any files that are missing.';

44
  @override
45 46
  String get invocation => "${runner.executableName} $name <output directory>";

47
  @override
48
  Future<int> runCommand() async {
49
    if (argResults.rest.isEmpty) {
50 51
      printError('No option specified for the output directory.');
      printError(usage);
52
      return 2;
53 54
    }

55
    if (argResults.rest.length > 1) {
56 57 58 59 60 61 62
      printError('Multiple output directories specified.');
      for (String arg in argResults.rest) {
        if (arg.startsWith('-')) {
          printError('Try moving $arg to be immediately following $name');
          break;
        }
      }
63 64 65
      return 2;
    }

66
    if (Cache.flutterRoot == null) {
67 68
      printError('Neither the --flutter-root command line flag nor the FLUTTER_ROOT environment\n'
        'variable was specified. Unable to find package:flutter.');
69 70
      return 2;
    }
71

72 73
    await Cache.instance.updateAll();

74
    String flutterRoot = path.absolute(Cache.flutterRoot);
75

76 77
    String flutterPackagesDirectory = path.join(flutterRoot, 'packages');
    String flutterPackagePath = path.join(flutterPackagesDirectory, 'flutter');
78
    if (!FileSystemEntity.isFileSync(path.join(flutterPackagePath, 'pubspec.yaml'))) {
79
      printError('Unable to find package:flutter in $flutterPackagePath');
80 81 82
      return 2;
    }

83 84 85 86 87 88
    String flutterDriverPackagePath = path.join(flutterRoot, 'packages', 'flutter_driver');
    if (!FileSystemEntity.isFileSync(path.join(flutterDriverPackagePath, 'pubspec.yaml'))) {
      printError('Unable to find package:flutter_driver in $flutterDriverPackagePath');
      return 2;
    }

89
    Directory projectDir = new Directory(argResults.rest.first);
90
    String dirPath = path.normalize(projectDir.absolute.path);
Devon Carew's avatar
Devon Carew committed
91
    String relativePath = path.relative(dirPath);
92 93
    String projectName = _normalizeProjectName(path.basename(dirPath));

94 95 96
    String error =_validateProjectDir(dirPath, flutterRoot: flutterRoot);
    if (error != null) {
      printError(error);
97 98
      return 1;
    }
Devon Carew's avatar
Devon Carew committed
99

100 101 102
    error = _validateProjectName(projectName);
    if (error != null) {
      printError(error);
103 104 105
      return 1;
    }

Devon Carew's avatar
Devon Carew committed
106
    int generatedCount = _renderTemplates(
107 108 109 110 111 112
      projectName,
      argResults['description'],
      dirPath,
      flutterPackagesDirectory,
      renderDriverTest: argResults['with-driver-test']
    );
Devon Carew's avatar
Devon Carew committed
113
    printStatus('Wrote $generatedCount files.');
114

115 116
    printStatus('');

117
    if (argResults['pub']) {
118
      int code = await pubGet(directory: dirPath);
Hixie's avatar
Hixie committed
119 120 121 122
      if (code != 0)
        return code;
    }

123
    printStatus('');
124 125 126 127

    // Run doctor; tell the user the next steps.
    if (doctor.canLaunchAnything) {
      // Let them know a summary of the state of their tooling.
128
      await doctor.summary();
129 130 131 132

      printStatus('''
All done! In order to run your application, type:

Devon Carew's avatar
Devon Carew committed
133
  \$ cd $relativePath
134 135 136
  \$ flutter run

Your main program file is lib/main.dart in the $relativePath directory.
137 138 139 140 141 142 143
''');
    } else {
      printStatus("You'll need to install additional components before you can run "
        "your Flutter app:");
      printStatus('');

      // Give the user more detailed analysis.
144
      await doctor.diagnose();
145 146 147
      printStatus('');
      printStatus("After installing components, run 'flutter doctor' in order to "
        "re-validate your setup.");
148
      printStatus("When complete, type 'flutter run' from the '$relativePath' "
149
        "directory in order to launch your app.");
150
      printStatus("Your main program file is: $relativePath/lib/main.dart");
151 152
    }

Hixie's avatar
Hixie committed
153 154
    return 0;
  }
155

Devon Carew's avatar
Devon Carew committed
156
  int _renderTemplates(String projectName, String projectDescription, String dirPath,
157
      String flutterPackagesDirectory, { bool renderDriverTest: false }) {
Devon Carew's avatar
Devon Carew committed
158
    new Directory(dirPath).createSync(recursive: true);
159

Devon Carew's avatar
Devon Carew committed
160 161
    flutterPackagesDirectory = path.normalize(flutterPackagesDirectory);
    flutterPackagesDirectory = _relativePath(from: dirPath, to: flutterPackagesDirectory);
162

Devon Carew's avatar
Devon Carew committed
163
    printStatus('Creating project ${path.relative(dirPath)}...');
164

Ian Hickson's avatar
Ian Hickson committed
165
    Map<String, dynamic> templateContext = <String, dynamic>{
166
      'projectName': projectName,
167 168
      'androidIdentifier': _createAndroidIdentifier(projectName),
      'iosIdentifier': _createUTIIdentifier(projectName),
169
      'description': projectDescription,
Devon Carew's avatar
Devon Carew committed
170
      'flutterPackagesDirectory': flutterPackagesDirectory,
171
      'androidMinApiLevel': android.minApiLevel
172
    };
173

Devon Carew's avatar
Devon Carew committed
174 175
    int fileCount = 0;

176
    templateContext['withDriverTest'] = renderDriverTest;
177

178
    Template createTemplate = new Template.fromName('create');
179 180 181 182 183
    fileCount += createTemplate.render(
      new Directory(dirPath),
      templateContext, overwriteExisting: false,
      projectName: projectName
    );
184

185 186
    if (renderDriverTest) {
      Template driverTemplate = new Template.fromName('driver');
Devon Carew's avatar
Devon Carew committed
187
      fileCount += driverTemplate.render(new Directory(path.join(dirPath, 'test_driver')),
188 189
          templateContext, overwriteExisting: false);
    }
Devon Carew's avatar
Devon Carew committed
190 191

    return fileCount;
192
  }
193 194 195 196 197
}

String _normalizeProjectName(String name) {
  name = name.replaceAll('-', '_').replaceAll(' ', '_');
  // Strip any extension (like .dart).
198
  if (name.contains('.'))
199 200 201 202
    name = name.substring(0, name.indexOf('.'));
  return name;
}

203 204 205 206 207
String _createAndroidIdentifier(String name) {
  return 'com.yourcompany.${camelCase(name)}';
}

String _createUTIIdentifier(String name) {
208
  // Create a UTI (https://en.wikipedia.org/wiki/Uniform_Type_Identifier) from a base name
209
  RegExp disallowed = new RegExp(r"[^a-zA-Z0-9\-\.\u0080-\uffff]+");
210 211
  name = camelCase(name).replaceAll(disallowed, '');
  name = name.isEmpty ? 'untitled' : name;
Chinmay Garde's avatar
Chinmay Garde committed
212
  return 'com.yourcompany.$name';
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

final Set<String> _packageDependencies = new Set<String>.from(<String>[
  'args',
  'async',
  'collection',
  'convert',
  'flutter',
  'html',
  'intl',
  'logging',
  'matcher',
  'mime',
  'path',
  'plugin',
  'pool',
  'test',
  'utf',
  'watcher',
  'yaml'
]);

/// Return `null` if the project name is legal. Return a validation message if
/// we should disallow the project name.
String _validateProjectName(String projectName) {
  if (_packageDependencies.contains(projectName)) {
    return "Invalid project name: '$projectName' - this will conflict with Flutter "
240
      "package dependencies.";
241
  }
242 243
  return null;
}
244

245 246
/// Return `null` if the project directory is legal. Return a validation message
/// if we should disallow the directory name.
247 248 249 250 251 252 253
String _validateProjectDir(String dirPath, { String flutterRoot }) {
  if (path.isWithin(flutterRoot, dirPath)) {
    return "Cannot create a project within the Flutter SDK.\n"
      "Target directory '$dirPath' is within the Flutter SDK at '$flutterRoot'.";
  }

  FileSystemEntityType type = FileSystemEntity.typeSync(dirPath);
254

255 256 257 258
  if (type != FileSystemEntityType.NOT_FOUND) {
    switch(type) {
      case FileSystemEntityType.FILE:
        // Do not overwrite files.
259
        return "Invalid project name: '$dirPath' - file exists.";
260 261
      case FileSystemEntityType.LINK:
        // Do not overwrite links.
262
        return "Invalid project name: '$dirPath' - refers to a link.";
263 264
    }
  }
265

266 267
  return null;
}
Devon Carew's avatar
Devon Carew committed
268 269 270 271 272 273 274 275

String _relativePath({ String from, String to }) {
  String result = path.relative(to, from: from);
  // `path.relative()` doesn't always return a correct result: dart-lang/path#12.
  if (FileSystemEntity.isDirectorySync(path.join(from, result)))
    return result;
  return to;
}