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

fix: Simple hosted dependencies should be inlined #795

Merged
merged 1 commit into from
Nov 21, 2024
Merged
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
32 changes: 23 additions & 9 deletions packages/melos/lib/src/common/extensions/dependency.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ extension DependencyExtension on Dependency {
return null;
}

/// Whether the json can be inlined with its parent.
///
/// For example for [HostedDependency] the version shouldn't be on a separate
/// line when only the version is defined.
bool get inlineVersion {
if (this is HostedDependency) {
return (this as HostedDependency).hosted == null &&
versionConstraint != null;
}
return false;
}

Object toJson() {
final self = this;
if (self is PathDependency) {
Expand All @@ -34,15 +46,17 @@ extension PathDependencyExtension on PathDependency {
}

extension HostedDependencyExtension on HostedDependency {
Map<String, dynamic> toJson() {
return {
'version': version.toString(),
if (hosted != null)
'hosted': {
'name': hosted!.declaredName,
'url': hosted!.url?.toString(),
},
};
Object toJson() {
return inlineVersion
? version.toString()
: {
'version': version.toString(),
if (hosted != null)
'hosted': {
'name': hosted!.declaredName,
'url': hosted!.url?.toString(),
},
};
}
}

Expand Down
54 changes: 53 additions & 1 deletion packages/melos/test/commands/bootstrap_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ 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';
import 'package:path/path.dart' as p;
import 'package:pub_semver/pub_semver.dart';
import 'package:pubspec_parse/pubspec_parse.dart';
import 'package:test/test.dart';
import 'package:yaml/yaml.dart';

import '../matchers.dart';
import '../utils.dart';
Expand Down Expand Up @@ -745,7 +747,7 @@ Generating IntelliJ IDE files...
});

test(
'applies dependencies from melos config',
'applies shared dependencies from melos config',
() async {
final workspaceDir = await createTemporaryWorkspace(
configBuilder: (path) => MelosWorkspaceConfig(
Expand Down Expand Up @@ -910,6 +912,51 @@ Generating IntelliJ IDE files...
},
timeout: const Timeout(Duration(days: 2)),
);

test('correctly inlines shared dependencies', () async {
final workspaceDir = await createTemporaryWorkspace(
configBuilder: (path) => MelosWorkspaceConfig.fromYaml(
createYamlMap(
{
'command': {
'bootstrap': {
'dependencies': {
'flame': '^1.21.0',
},
},
},
},
defaults: configMapDefaults,
),
path: path,
),
);

final pkgA = await createProject(
workspaceDir,
Pubspec(
'a',
dependencies: {
'flame': HostedDependency(version: VersionConstraint.any),
},
),
);

final logger = TestLogger();
final config = await MelosWorkspaceConfig.fromWorkspaceRoot(workspaceDir);
final melos = Melos(
logger: logger,
config: config,
);

await runMelosBootstrap(melos, logger);

final pubspecContent = _pubspecContent(pkgA);
expect(
(pubspecContent['dependencies']! as YamlMap)['flame'],
'^1.21.0',
);
});
});

group('melos bs --skip-linking', () {
Expand Down Expand Up @@ -1142,3 +1189,8 @@ Future<void> dependencyResolutionTest(

await Future.wait<void>(packages.keys.map(validatePackage));
}

YamlMap _pubspecContent(io.Directory directory) {
final source = readTextFile(pubspecPath(directory.path));
return loadYaml(source) as YamlMap;
}
4 changes: 4 additions & 0 deletions packages/melos/test/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ String packageConfigPath(String packageRoot) {
);
}

String pubspecPath(String directory) {
return p.join(directory, 'pubspec.yaml');
}

PackageConfig packageConfigForPackageAt(Directory dir) {
final source = readTextFile(packageConfigPath(dir.path));
return PackageConfig.fromJson(json.decode(source) as Map<String, Object?>);
Expand Down
Loading