From ad154d1d861b15f96004ca3f41a4af23139676bb Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Fri, 13 Sep 2024 11:46:31 +0200 Subject: [PATCH] feat: Expose 'pub get --offline' flag on bootstrap command --- docs/commands/bootstrap.mdx | 2 + .../lib/src/command_runner/bootstrap.dart | 7 ++++ .../melos/lib/src/commands/bootstrap.dart | 4 +- .../melos/test/commands/bootstrap_test.dart | 42 ++++++++++++++++++- 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/docs/commands/bootstrap.mdx b/docs/commands/bootstrap.mdx index 1d12e5ae..518aecb2 100644 --- a/docs/commands/bootstrap.mdx +++ b/docs/commands/bootstrap.mdx @@ -91,6 +91,8 @@ melos bootstrap --diff="main" - The `--enforce-lockfile` flag is used to enforce versions from `.lock` files. - Ensure .lock files exist, as failure may occur if they're not checked in. - The `--skip-linking` flag is used to skip the local linking of workspace packages. +- The `--offline` flag is used to only resolve dependencies from the local cache by running + `pub get` with the `--offline` flag. In addition to the above flags, the `melos bootstrap` command supports a few different flags that can be defined in diff --git a/packages/melos/lib/src/command_runner/bootstrap.dart b/packages/melos/lib/src/command_runner/bootstrap.dart index 11119155..d2c58de7 100644 --- a/packages/melos/lib/src/command_runner/bootstrap.dart +++ b/packages/melos/lib/src/command_runner/bootstrap.dart @@ -22,6 +22,12 @@ class BootstrapCommand extends MelosCommand { negatable: false, help: 'Skips locally linking workspace packages.', ); + argParser.addFlag( + 'offline', + negatable: false, + help: 'Run pub get with --offline to resolve dependencies from local ' + 'cache.', + ); } @override @@ -44,6 +50,7 @@ class BootstrapCommand extends MelosCommand { enforceLockfile: argResults?['enforce-lockfile'] as bool? ?? false, noExample: argResults?['no-example'] as bool, skipLinking: argResults?['skip-linking'] as bool, + offline: argResults?['offline'] as bool, ); } } diff --git a/packages/melos/lib/src/commands/bootstrap.dart b/packages/melos/lib/src/commands/bootstrap.dart index 62d20499..83069717 100644 --- a/packages/melos/lib/src/commands/bootstrap.dart +++ b/packages/melos/lib/src/commands/bootstrap.dart @@ -7,6 +7,7 @@ mixin _BootstrapMixin on _CleanMixin { bool noExample = false, bool enforceLockfile = false, bool skipLinking = false, + bool offline = false, }) async { final workspace = await createWorkspace(global: global, packageFilters: packageFilters); @@ -16,6 +17,7 @@ mixin _BootstrapMixin on _CleanMixin { CommandWithLifecycle.bootstrap, () async { final bootstrapCommandConfig = workspace.config.commands.bootstrap; + final runOffline = bootstrapCommandConfig.runPubGetOffline || offline; late final hasLockFile = File(p.join(workspace.path, 'pubspec.lock')).existsSync(); final enforceLockfileConfigValue = @@ -30,7 +32,7 @@ mixin _BootstrapMixin on _CleanMixin { ), 'get', if (noExample) '--no-example', - if (bootstrapCommandConfig.runPubGetOffline) '--offline', + if (runOffline) '--offline', if (shouldEnforceLockfile) '--enforce-lockfile', ].join(' '); diff --git a/packages/melos/test/commands/bootstrap_test.dart b/packages/melos/test/commands/bootstrap_test.dart index 68b5cae6..f56d3b81 100644 --- a/packages/melos/test/commands/bootstrap_test.dart +++ b/packages/melos/test/commands/bootstrap_test.dart @@ -928,6 +928,42 @@ Updating common dependencies in workspace packages... └> Updated environment > SUCCESS +Generating IntelliJ IDE files... + > SUCCESS + + -> 1 packages bootstrapped +''', + ), + ); + }); + }); + + group('melos bs --offline', () { + test('should run pub get with --offline', () async { + final workspaceDir = await createTemporaryWorkspace(); + await createProject( + workspaceDir, + const PubSpec(name: 'a'), + ); + + final logger = TestLogger(); + final config = await MelosWorkspaceConfig.fromWorkspaceRoot(workspaceDir); + final melos = Melos(logger: logger, config: config); + + await runMelosBootstrap(melos, logger, offline: true); + + expect( + logger.output, + ignoringAnsii( + ''' +melos bootstrap + └> ${workspaceDir.path} + +Running "dart pub get --offline" in workspace packages... + ✓ a + └> packages/a + > SUCCESS + Generating IntelliJ IDE files... > SUCCESS @@ -943,9 +979,13 @@ Future runMelosBootstrap( Melos melos, TestLogger logger, { bool skipLinking = false, + bool offline = false, }) async { try { - await melos.bootstrap(skipLinking: skipLinking); + await melos.bootstrap( + skipLinking: skipLinking, + offline: offline, + ); } on BootstrapException { // ignore: avoid_print print(logger.output);