Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: downgrade, upgrade #872

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/melos/lib/src/command_runner/bootstrap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ import 'base.dart';
class BootstrapCommand extends MelosCommand {
BootstrapCommand(super.config) {
setupPackageFilterParser();
argParser.addOption(
'mode',
allowed: const ['get', 'upgrade', 'downgrade'],
defaultsTo: 'get',
help: 'Run pub get, pub upgrade, or pub downgrade',
);
argParser.addFlag(
'no-example',
negatable: false,
help: 'Run pub get with/without example pub get',
help: 'Run with/without fetching example dependencies.',
);
argParser.addFlag(
'enforce-lockfile',
Expand All @@ -21,8 +27,7 @@ class BootstrapCommand extends MelosCommand {
argParser.addFlag(
'offline',
negatable: false,
help: 'Run pub get with --offline to resolve dependencies from local '
'cache.',
help: 'Run with --offline to resolve dependencies from local cache.',
);
}

Expand All @@ -41,6 +46,7 @@ class BootstrapCommand extends MelosCommand {
FutureOr<void>? run() {
final melos = Melos(logger: logger, config: config);
return melos.bootstrap(
mode: BootstrapMode.fromString(argResults?['mode'] as String? ?? 'get'),
global: global,
packageFilters: parsePackageFilters(config.path),
enforceLockfile: argResults?['enforce-lockfile'] as bool?,
Expand Down
36 changes: 30 additions & 6 deletions packages/melos/lib/src/commands/bootstrap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mixin _BootstrapMixin on _CleanMixin {
bool noExample = false,
bool? enforceLockfile,
bool offline = false,
BootstrapMode mode = BootstrapMode.get,
}) async {
final workspace =
await createWorkspace(global: global, packageFilters: packageFilters);
Expand All @@ -21,10 +22,12 @@ mixin _BootstrapMixin on _CleanMixin {
File(p.join(workspace.path, 'pubspec.lock')).existsSync();
final enforceLockfileConfigValue =
workspace.config.commands.bootstrap.enforceLockfile;
final shouldEnforceLockfile =
(enforceLockfile ?? enforceLockfileConfigValue) && hasLockFile;
final shouldEnforceLockfile = (mode == BootstrapMode.get) &&
(enforceLockfile ?? enforceLockfileConfigValue) &&
hasLockFile;

final pubCommandForLogging = _buildPubGetCommand(
final pubCommandForLogging = _buildPubCommand(
mode: mode,
workspace: workspace,
noExample: noExample,
runOffline: runOffline,
Expand Down Expand Up @@ -67,6 +70,7 @@ mixin _BootstrapMixin on _CleanMixin {
);

await _runPubGetForWorkspace(
mode,
workspace,
noExample: noExample,
runOffline: runOffline,
Expand Down Expand Up @@ -110,12 +114,14 @@ mixin _BootstrapMixin on _CleanMixin {
}

Future<void> _runPubGetForWorkspace(
BootstrapMode mode,
MelosWorkspace workspace, {
required bool noExample,
required bool runOffline,
required bool enforceLockfile,
}) async {
await runPubGetForPackage(
mode,
workspace,
workspace.rootPackage,
noExample: noExample,
Expand All @@ -126,13 +132,15 @@ mixin _BootstrapMixin on _CleanMixin {

@visibleForTesting
Future<void> runPubGetForPackage(
BootstrapMode mode,
MelosWorkspace workspace,
Package package, {
required bool noExample,
required bool runOffline,
required bool enforceLockfile,
}) async {
final command = _buildPubGetCommand(
final command = _buildPubCommand(
mode: mode,
workspace: workspace,
noExample: noExample,
runOffline: runOffline,
Expand Down Expand Up @@ -171,7 +179,8 @@ mixin _BootstrapMixin on _CleanMixin {
}
}

List<String> _buildPubGetCommand({
List<String> _buildPubCommand({
required BootstrapMode mode,
required MelosWorkspace workspace,
required bool noExample,
required bool runOffline,
Expand All @@ -182,7 +191,7 @@ mixin _BootstrapMixin on _CleanMixin {
useFlutter: workspace.isFlutterWorkspace,
workspace: workspace,
),
'get',
mode.name,
if (noExample) '--no-example',
if (runOffline) '--offline',
if (enforceLockfile) '--enforce-lockfile',
Expand Down Expand Up @@ -411,3 +420,18 @@ class BootstrapException implements MelosException {
'${package.path}.';
}
}

enum BootstrapMode {
get,
upgrade,
downgrade;

factory BootstrapMode.fromString(String value) {
return switch (value) {
'get' => BootstrapMode.get,
'upgrade' => BootstrapMode.upgrade,
'downgrade' => BootstrapMode.downgrade,
_ => throw ArgumentError('Invalid mode specified'),
};
}
}
2 changes: 2 additions & 0 deletions packages/melos/test/commands/bootstrap_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';

import 'package:melos/melos.dart';
import 'package:melos/src/command_configs/command_configs.dart';
import 'package:melos/src/commands/runner.dart';
import 'package:melos/src/common/glob.dart';
import 'package:melos/src/common/io.dart';
import 'package:melos/src/common/utils.dart';
Expand Down Expand Up @@ -589,6 +590,7 @@ Generating IntelliJ IDE files...
);

await melos.runPubGetForPackage(
BootstrapMode.get,
workspace,
workspace.rootPackage,
noExample: true,
Expand Down