packages.dart 12.7 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'package:args/args.dart';

7
import '../base/common.dart';
8
import '../base/os.dart';
9 10 11 12
import '../build_info.dart';
import '../build_system/build_system.dart';
import '../cache.dart';
import '../dart/generate_synthetic_packages.dart';
13
import '../dart/pub.dart';
14
import '../flutter_plugins.dart';
15
import '../globals.dart' as globals;
16
import '../plugins.dart';
17
import '../project.dart';
18
import '../reporting/reporting.dart';
19 20 21 22
import '../runner/flutter_command.dart';

class PackagesCommand extends FlutterCommand {
  PackagesCommand() {
23
    addSubcommand(PackagesGetCommand('get', false));
24
    addSubcommand(PackagesInteractiveGetCommand('upgrade', "Upgrade the current package's dependencies to latest versions."));
25 26
    addSubcommand(PackagesInteractiveGetCommand('add', 'Add a dependency to pubspec.yaml.'));
    addSubcommand(PackagesInteractiveGetCommand('remove', 'Removes a dependency from the current package.'));
27
    addSubcommand(PackagesTestCommand());
28 29 30 31 32 33 34
    addSubcommand(PackagesForwardCommand('publish', 'Publish the current package to pub.dartlang.org.', requiresPubspec: true));
    addSubcommand(PackagesForwardCommand('downgrade', 'Downgrade packages in a Flutter project.', requiresPubspec: true));
    addSubcommand(PackagesForwardCommand('deps', 'Print package dependencies.')); // path to package can be specified with --directory argument
    addSubcommand(PackagesForwardCommand('run', 'Run an executable from a package.', requiresPubspec: true));
    addSubcommand(PackagesForwardCommand('cache', 'Work with the Pub system cache.'));
    addSubcommand(PackagesForwardCommand('version', 'Print Pub version.'));
    addSubcommand(PackagesForwardCommand('uploader', 'Manage uploaders for a package on pub.dev.'));
35 36
    addSubcommand(PackagesForwardCommand('login', 'Log into pub.dev.'));
    addSubcommand(PackagesForwardCommand('logout', 'Log out of pub.dev.'));
37 38
    addSubcommand(PackagesForwardCommand('global', 'Work with Pub global packages.'));
    addSubcommand(PackagesForwardCommand('outdated', 'Analyze dependencies to find which ones can be upgraded.', requiresPubspec: true));
39
    addSubcommand(PackagesForwardCommand('token', 'Manage authentication tokens for hosted pub repositories.'));
40
    addSubcommand(PackagesPassthroughCommand());
41 42 43
  }

  @override
44
  final String name = 'pub';
45 46

  @override
47
  List<String> get aliases => const <String>['packages'];
48 49 50 51

  @override
  final String description = 'Commands for managing Flutter packages.';

52 53 54
  @override
  String get category => FlutterCommandCategory.project;

55
  @override
56
  Future<FlutterCommandResult> runCommand() async => FlutterCommandResult.fail();
57 58 59
}

class PackagesGetCommand extends FlutterCommand {
60 61 62
  PackagesGetCommand(this.name, this.upgrade) {
    argParser.addFlag('offline',
      negatable: false,
63
      help: 'Use cached packages instead of accessing the network.',
64 65
    );
  }
66

67 68 69 70 71
  @override
  final String name;

  final bool upgrade;

72
  @override
73 74 75
  String get description {
    return '${ upgrade ? "Upgrade" : "Get" } packages in a Flutter project.';
  }
76 77

  @override
78
  String get invocation {
79
    return '${runner!.executableName} pub $name [<target directory>]';
80
  }
81

82 83
  /// The pub packages usage values are incorrect since these are calculated/sent
  /// before pub get completes. This needs to be performed after dependency resolution.
84
  @override
85
  Future<CustomDimensions> get usageValues async {
86 87 88
    final ArgResults argumentResults = argResults!;
    final String? workingDirectory = argumentResults.rest.length == 1 ? argumentResults.rest[0] : null;
    final String? target = findProjectRoot(globals.fs, workingDirectory);
89
    if (target == null) {
90
      return const CustomDimensions();
91
    }
92 93 94

    int numberPlugins;

95
    final FlutterProject rootProject = FlutterProject.fromDirectory(globals.fs.directory(target));
96 97 98
    // Do not send plugin analytics if pub has not run before.
    final bool hasPlugins = rootProject.flutterPluginsDependenciesFile.existsSync()
      && rootProject.packageConfigFile.existsSync();
99
    if (hasPlugins) {
100 101 102
      // Do not fail pub get if package config files are invalid before pub has
      // had a chance to run.
      final List<Plugin> plugins = await findPlugins(rootProject, throwOnError: false);
103
      numberPlugins = plugins.length;
104
    } else {
105
      numberPlugins = 0;
106
    }
107 108 109 110 111 112

    return CustomDimensions(
      commandPackagesNumberPlugins: numberPlugins,
      commandPackagesProjectModule: rootProject.isModule,
      commandPackagesAndroidEmbeddingVersion: rootProject.android.getEmbeddingVersion().toString().split('.').last,
    );
113 114
  }

115
  Future<void> _runPubGet(String directory, FlutterProject flutterProject) async {
116 117
    if (flutterProject.manifest.generateSyntheticPackage) {
      final Environment environment = Environment(
118
        artifacts: globals.artifacts!,
119 120 121 122 123 124 125
        logger: globals.logger,
        cacheDir: globals.cache.getRoot(),
        engineVersion: globals.flutterVersion.engineRevision,
        fileSystem: globals.fs,
        flutterRootDir: globals.fs.directory(Cache.flutterRoot),
        outputDir: globals.fs.directory(getBuildDirectory()),
        processManager: globals.processManager,
126
        platform: globals.platform,
127
        usage: globals.flutterUsage,
128
        projectDir: flutterProject.directory,
129
        generateDartPluginRegistry: true,
130 131 132 133 134 135 136 137
      );

      await generateLocalizationsSyntheticPackage(
        environment: environment,
        buildSystem: globals.buildSystem,
      );
    }

138 139
    final Stopwatch pubGetTimer = Stopwatch()..start();
    try {
140 141
      await pub.get(
        context: PubContext.pubGet,
142
        directory: directory,
143
        upgrade: upgrade,
144
        shouldSkipThirdPartyGenerator: false,
145
        offline: boolArgDeprecated('offline'),
146
        generateSyntheticPackage: flutterProject.manifest.generateSyntheticPackage,
147 148
      );
      pubGetTimer.stop();
149
      globals.flutterUsage.sendTiming('pub', 'get', pubGetTimer.elapsed, label: 'success');
150 151
    // Not limiting to catching Exception because the exception is rethrown.
    } catch (_) { // ignore: avoid_catches_without_on_clauses
152
      pubGetTimer.stop();
153
      globals.flutterUsage.sendTiming('pub', 'get', pubGetTimer.elapsed, label: 'failure');
154 155
      rethrow;
    }
156 157
  }

158
  @override
159
  Future<FlutterCommandResult> runCommand() async {
160 161
    final ArgResults argumentResults = argResults!;
    if (argumentResults.rest.length > 1) {
162
      throwToolExit('Too many arguments.\n$usage');
163
    }
164

165 166
    final String? workingDirectory = argumentResults.rest.length == 1 ? argumentResults.rest[0] : null;
    final String? target = findProjectRoot(globals.fs, workingDirectory);
167
    if (target == null) {
168
      throwToolExit(
169 170
        'Expected to find project root in '
        '${ workingDirectory ?? "current working directory" }.'
171 172
      );
    }
173
    final FlutterProject rootProject = FlutterProject.fromDirectory(globals.fs.directory(target));
174 175

    await _runPubGet(target, rootProject);
176
    await rootProject.regeneratePlatformSpecificTooling();
177 178

    // Get/upgrade packages in example app as well
179
    if (rootProject.hasExampleApp && rootProject.example.pubspecFile.existsSync()) {
180
      final FlutterProject exampleProject = rootProject.example;
181
      await _runPubGet(exampleProject.directory.path, exampleProject);
182
      await exampleProject.regeneratePlatformSpecificTooling();
183
    }
184

185
    return FlutterCommandResult.success();
186 187
  }
}
188 189

class PackagesTestCommand extends FlutterCommand {
190 191 192 193
  PackagesTestCommand() {
    requiresPubspecYaml();
  }

194 195 196 197 198 199
  @override
  String get name => 'test';

  @override
  String get description {
    return 'Run the "test" package.\n'
200 201 202 203
           'This is similar to "flutter test", but instead of hosting the tests in the '
           'flutter environment it hosts the tests in a pure Dart environment. The main '
           'differences are that the "dart:ui" library is not available and that tests '
           'run faster. This is helpful for testing libraries that do not depend on any '
204 205 206 207 208
           'packages from the Flutter SDK. It is equivalent to "pub run test".';
  }

  @override
  String get invocation {
209
    return '${runner!.executableName} pub test [<tests...>]';
210 211 212
  }

  @override
213
  Future<FlutterCommandResult> runCommand() async {
214
    await pub.batch(<String>['run', 'test', ...argResults!.rest], context: PubContext.runTest, retry: false);
215
    return FlutterCommandResult.success();
216
  }
217 218
}

219 220 221 222 223 224
class PackagesForwardCommand extends FlutterCommand {
  PackagesForwardCommand(this._commandName, this._description, {bool requiresPubspec = false}) {
    if (requiresPubspec) {
      requiresPubspecYaml();
    }
  }
225 226 227 228

  @override
  ArgParser argParser = ArgParser.allowAnything();

229 230 231 232 233 234 235 236
  final String _commandName;
  final String _description;

  @override
  String get name => _commandName;

  @override
  String get description {
237
    return '$_description\n'
238 239 240 241 242
           'This runs the "pub" tool in a Flutter context.';
  }

  @override
  String get invocation {
243
    return '${runner!.executableName} pub $_commandName [<arguments...>]';
244 245 246 247
  }

  @override
  Future<FlutterCommandResult> runCommand() async {
248
    final List<String> subArgs = argResults!.rest.toList()
249 250
      ..removeWhere((String arg) => arg == '--');
    await pub.interactively(<String>[_commandName, ...subArgs], stdio: globals.stdio);
251
    return FlutterCommandResult.success();
252 253 254
  }
}

255
class PackagesPassthroughCommand extends FlutterCommand {
256 257
  @override
  ArgParser argParser = ArgParser.allowAnything();
258 259 260 261 262 263 264 265 266 267 268 269

  @override
  String get name => 'pub';

  @override
  String get description {
    return 'Pass the remaining arguments to Dart\'s "pub" tool.\n'
           'This runs the "pub" tool in a Flutter context.';
  }

  @override
  String get invocation {
270
    return '${runner!.executableName} packages pub [<arguments...>]';
271 272 273
  }

  @override
274
  Future<FlutterCommandResult> runCommand() async {
275
    await pub.interactively(argResults!.rest, stdio: globals.stdio);
276
    return FlutterCommandResult.success();
277
  }
278
}
279 280

class PackagesInteractiveGetCommand extends FlutterCommand {
281
  PackagesInteractiveGetCommand(this._commandName, this._description);
282 283 284 285 286 287 288 289 290 291 292 293

  @override
  ArgParser argParser = ArgParser.allowAnything();

  final String _commandName;
  final String _description;

  @override
  String get name => _commandName;

  @override
  String get description {
294
    return '$_description\n'
295 296 297 298 299
           'This runs the "pub" tool in a Flutter context.';
  }

  @override
  String get invocation {
300
    return '${runner!.executableName} pub $_commandName [<arguments...>]';
301 302 303 304
  }

  @override
  Future<FlutterCommandResult> runCommand() async {
305
    List<String> rest = argResults!.rest;
306
    final bool isHelp = rest.contains('-h') || rest.contains('--help');
307
    String? target;
308 309 310 311 312
    if (rest.length == 1 && (rest.single.contains('/') || rest.single.contains(r'\'))) {
      // For historical reasons, if there is one argument to the command and it contains
      // a multiple-component path (i.e. contains a slash) then we use that to determine
      // to which project we're applying the command.
      target = findProjectRoot(globals.fs, rest.single);
313 314
      rest = <String>[];
    } else {
315
      target = findProjectRoot(globals.fs);
316 317
    }

318
    FlutterProject? flutterProject;
319 320 321 322 323 324 325 326
    if (!isHelp) {
      if (target == null) {
        throwToolExit('Expected to find project root in current working directory.');
      }
      flutterProject = FlutterProject.fromDirectory(globals.fs.directory(target));

      if (flutterProject.manifest.generateSyntheticPackage) {
        final Environment environment = Environment(
327
          artifacts: globals.artifacts!,
328 329 330 331 332 333 334 335
          logger: globals.logger,
          cacheDir: globals.cache.getRoot(),
          engineVersion: globals.flutterVersion.engineRevision,
          fileSystem: globals.fs,
          flutterRootDir: globals.fs.directory(Cache.flutterRoot),
          outputDir: globals.fs.directory(getBuildDirectory()),
          processManager: globals.processManager,
          platform: globals.platform,
336
          usage: globals.flutterUsage,
337
          projectDir: flutterProject.directory,
338
          generateDartPluginRegistry: true,
339 340 341 342 343 344 345
        );

        await generateLocalizationsSyntheticPackage(
          environment: environment,
          buildSystem: globals.buildSystem,
        );
      }
346 347
    }

348
    final List<String> subArgs = rest.toList()..removeWhere((String arg) => arg == '--');
349 350 351 352
    await pub.interactively(
      <String>[name, ...subArgs],
      directory: target,
      stdio: globals.stdio,
353
      touchesPackageConfig: !isHelp,
354
      generateSyntheticPackage: flutterProject?.manifest.generateSyntheticPackage ?? false,
355 356
    );

357
    await flutterProject?.regeneratePlatformSpecificTooling();
358 359 360
    return FlutterCommandResult.success();
  }
}