Skip to content

Commit 776f154

Browse files
committed
refactor: improve pubspec_overrides handling
1 parent 64f1c28 commit 776f154

File tree

10 files changed

+77
-48
lines changed

10 files changed

+77
-48
lines changed

bricks/dart_frog_prod_server/hooks/lib/src/create_external_packages_folder.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Future<List<String>> createExternalPackagesFolder({
6767
pathResolver.join(buildDirectory.path, 'pubspec_overrides.yaml'),
6868
).writeAsStringSync(
6969
'''
70-
70+
resolution: null
7171
dependency_overrides:
7272
${copiedExternalPathDependencies.map(
7373
(dependency) {
@@ -78,7 +78,6 @@ ${copiedExternalPathDependencies.map(
7878
},
7979
).join('\n')}
8080
''',
81-
mode: FileMode.append,
8281
);
8382

8483
return copiedExternalPathDependencies
Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,46 @@
11
import 'dart:io';
22
import 'package:mason/mason.dart';
33
import 'package:path/path.dart' as path;
4+
import 'package:yaml/yaml.dart';
5+
6+
/// A void callback function (e.g. `void Function()`).
7+
typedef VoidCallback = void Function();
48

59
/// Opts out of dart workspaces until we can generate per package lockfiles.
610
/// https://github.com/dart-lang/pub/issues/4594
7-
void disableWorkspaceResolution(
11+
VoidCallback disableWorkspaceResolution(
812
HookContext context, {
913
required String projectDirectory,
1014
required void Function(int exitCode) exit,
1115
}) {
1216
try {
13-
overrideResolutionInPubspecOverrides(projectDirectory);
17+
return overrideResolutionInPubspecOverrides(projectDirectory);
1418
} on Exception catch (e) {
1519
context.logger.err('$e');
1620
exit(1);
21+
return () {};
1722
}
1823
}
1924

20-
void overrideResolutionInPubspecOverrides(String projectDirectory) {
21-
final pubspecOverrides = File(
25+
void Function() overrideResolutionInPubspecOverrides(String projectDirectory) {
26+
final pubspecOverridesFile = File(
2227
path.join(projectDirectory, 'pubspec_overrides.yaml'),
2328
);
2429

25-
if (pubspecOverrides.existsSync()) {
26-
return pubspecOverrides.writeAsStringSync(
27-
'\nresolution: null\n',
28-
mode: FileMode.append,
29-
);
30+
if (!pubspecOverridesFile.existsSync()) {
31+
pubspecOverridesFile.writeAsStringSync('resolution: null');
32+
return pubspecOverridesFile.deleteSync;
3033
}
3134

32-
pubspecOverrides
33-
..createSync(recursive: true)
34-
..writeAsStringSync('resolution: null');
35+
final contents = pubspecOverridesFile.readAsStringSync();
36+
final pubspecOverrides = loadYaml(contents) as YamlMap?;
37+
38+
if (pubspecOverrides?['resolution'] == 'null') return () {};
39+
pubspecOverridesFile.writeAsStringSync(
40+
'''
41+
resolution: null
42+
$contents''',
43+
);
44+
45+
return () => pubspecOverridesFile.writeAsStringSync(contents);
3546
}
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'dart:io';
22
import 'package:mason/mason.dart';
33
import 'package:path/path.dart' as path;
4-
import 'package:pubspec_parse/pubspec_parse.dart';
4+
import 'package:yaml/yaml.dart';
55

66
/// Determines whether the project in the provided [workingDirectory]
77
/// is configured to use `resolution: workspace`.
@@ -13,14 +13,18 @@ bool usesWorkspaceResolution(
1313
final pubspecFile = File(path.join(workingDirectory, 'pubspec.yaml'));
1414
if (!pubspecFile.existsSync()) return false;
1515

16-
final Pubspec pubspec;
16+
final YamlMap pubspec;
1717
try {
18-
pubspec = Pubspec.parse(pubspecFile.readAsStringSync());
18+
final yaml = loadYaml(pubspecFile.readAsStringSync());
19+
if (yaml is! YamlMap) {
20+
throw Exception('Unable to parse ${pubspecFile.path}');
21+
}
22+
pubspec = yaml;
1923
} on Exception catch (e) {
2024
context.logger.err('$e');
2125
exit(1);
2226
return false;
2327
}
2428

25-
return pubspec.resolution == 'workspace';
29+
return pubspec['resolution'] == 'workspace';
2630
}

bricks/dart_frog_prod_server/hooks/pre_gen.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ Future<void> preGen(
2929
exit: exit,
3030
);
3131

32+
VoidCallback? restoreWorkspaceResolution;
33+
3234
if (usesWorkspaces) {
3335
// Disable workspace resolution until we can generate per-package lockfiles.
3436
// https://github.com/dart-lang/pub/issues/4594
35-
disableWorkspaceResolution(
37+
restoreWorkspaceResolution = disableWorkspaceResolution(
3638
context,
3739
projectDirectory: projectDirectory.path,
3840
exit: exit,
@@ -58,6 +60,8 @@ Future<void> preGen(
5860
exit: exit,
5961
);
6062

63+
restoreWorkspaceResolution?.call();
64+
6165
final RouteConfiguration configuration;
6266
try {
6367
configuration = buildConfiguration(projectDirectory);

bricks/dart_frog_prod_server/hooks/pubspec.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ dependencies:
1010
io: ^1.0.3
1111
mason: ^0.1.0
1212
path: ^1.8.1
13-
pubspec_parse: ^1.5.0
1413
yaml: ^3.1.2
1514

1615
dev_dependencies:

bricks/dart_frog_prod_server/hooks/test/pre_gen_test.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,11 @@ packages:
230230
exit: exitCalls.add,
231231
directory: directory,
232232
runProcess: successRunProcess,
233-
copyPath: (_, __) async {},
233+
copyPath: (from, to) async {
234+
File(
235+
path.join(to, 'pubspec_overrides.yaml'),
236+
).createSync(recursive: true);
237+
},
234238
);
235239

236240
expect(exitCalls, isEmpty);

bricks/dart_frog_prod_server/hooks/test/src/create_external_packages_folder_test.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ void main() {
1717
await createExternalPackagesFolder(
1818
projectDirectory: projectDirectory,
1919
buildDirectory: Directory(path.join(projectDirectory.path, 'build')),
20-
copyPath: (from, to) {
20+
copyPath: (from, to) async {
2121
copyCalls.add('$from -> $to');
22-
return Future.value();
22+
File(
23+
path.join(to, 'pubspec_overrides.yaml'),
24+
).createSync(recursive: true);
2325
},
2426
);
2527

@@ -56,9 +58,11 @@ void main() {
5658
await createExternalPackagesFolder(
5759
projectDirectory: projectDirectory,
5860
buildDirectory: Directory(path.join(projectDirectory.path, 'build')),
59-
copyPath: (from, to) {
61+
copyPath: (from, to) async {
6062
copyCalls.add('$from -> $to');
61-
return Future.value();
63+
File(
64+
path.join(to, 'pubspec_overrides.yaml'),
65+
).createSync(recursive: true);
6266
},
6367
);
6468

bricks/dart_frog_prod_server/hooks/test/src/disable_workspace_resolution_test.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ dependency_overrides:
7070
pubspecOverrides.readAsStringSync(),
7171
equals(
7272
'''
73-
$originalPubspecOverridesContent
7473
resolution: null
75-
''',
74+
$originalPubspecOverridesContent''',
7675
),
7776
);
7877
});

bricks/dart_frog_prod_server/hooks/test/src/uses_workspace_resolution_test.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ void main() {
4747
});
4848

4949
group('when pubspec.yaml is malformed', () {
50+
late File pubspecFile;
5051
setUp(() {
51-
File(
52+
pubspecFile = File(
5253
path.join(workingDirectory.path, 'pubspec.yaml'),
53-
).writeAsStringSync('invalid pubspec.yaml');
54+
)..writeAsStringSync('invalid pubspec.yaml');
5455
});
5556

5657
test('returns false', () {
@@ -63,7 +64,11 @@ void main() {
6364
isFalse,
6465
);
6566
expect(exitCalls, equals([1]));
66-
verify(() => logger.err(any(that: contains('ParsedYamlException'))));
67+
verify(
68+
() => logger.err(
69+
any(that: contains('Unable to parse ${pubspecFile.path}')),
70+
),
71+
);
6772
});
6873
});
6974

0 commit comments

Comments
 (0)