Skip to content

Commit c994459

Browse files
authored
refactor(dart_frog_prod_server): reuse buildDirectory (#1247)
1 parent cf88a0a commit c994459

File tree

5 files changed

+66
-42
lines changed

5 files changed

+66
-42
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import 'package:io/io.dart' show copyPath;
44
import 'package:mason/mason.dart';
55
import 'package:path/path.dart' as path;
66

7-
Future<void> createBundle(
8-
HookContext context,
9-
io.Directory projectDirectory,
10-
void Function(int exitCode) exit,
11-
) async {
12-
final buildDirectoryPath = path.join(projectDirectory.path, 'build');
13-
final buildDirectory = io.Directory(buildDirectoryPath);
7+
Future<void> createBundle({
8+
required HookContext context,
9+
required io.Directory projectDirectory,
10+
required io.Directory buildDirectory,
11+
required void Function(int exitCode) exit,
12+
}) async {
1413
final dartFrogDirectoryPath = path.join(projectDirectory.path, '.dart_frog');
1514
final dartFrogDirectory = io.Directory(dartFrogDirectoryPath);
1615
final bundlingProgress = context.logger.progress('Bundling sources');

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import 'package:dart_frog_prod_server_hooks/dart_frog_prod_server_hooks.dart';
44
import 'package:io/io.dart' as io;
55
import 'package:path/path.dart' as path;
66

7-
Future<List<String>> createExternalPackagesFolder(
8-
Directory directory, {
7+
Future<List<String>> createExternalPackagesFolder({
8+
required Directory projectDirectory,
9+
required Directory buildDirectory,
910
Future<void> Function(String from, String to) copyPath = io.copyPath,
1011
}) async {
1112
final pathResolver = path.context;
1213
final pubspecLock = await getPubspecLock(
13-
directory.path,
14+
projectDirectory.path,
1415
pathContext: path.context,
1516
);
1617

@@ -43,12 +44,7 @@ Future<List<String>> createExternalPackagesFolder(
4344
return map;
4445
});
4546

46-
final buildDirectory = Directory(
47-
pathResolver.join(
48-
directory.path,
49-
'build',
50-
),
51-
)..createSync();
47+
buildDirectory.createSync();
5248

5349
final packagesDirectory = Directory(
5450
pathResolver.join(
@@ -59,7 +55,7 @@ Future<List<String>> createExternalPackagesFolder(
5955

6056
final copiedPaths = <String>[];
6157
for (final entry in mappedDependencies.entries) {
62-
final from = pathResolver.join(directory.path, entry.value);
58+
final from = pathResolver.join(projectDirectory.path, entry.value);
6359
final to = pathResolver.join(packagesDirectory.path, entry.key);
6460

6561
await copyPath(from, to);

bricks/dart_frog_prod_server/hooks/pre_gen.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ Future<void> preGen(
3434
exit: exit,
3535
);
3636

37-
await createBundle(context, projectDirectory, exit);
37+
final buildDirectory = io.Directory(
38+
path.join(projectDirectory.path, 'build'),
39+
);
40+
41+
await createBundle(
42+
context: context,
43+
projectDirectory: projectDirectory,
44+
buildDirectory: buildDirectory,
45+
exit: exit,
46+
);
3847

3948
final RouteConfiguration configuration;
4049
try {
@@ -81,7 +90,8 @@ Future<void> preGen(
8190
);
8291

8392
final externalDependencies = await createExternalPackagesFolder(
84-
projectDirectory,
93+
projectDirectory: projectDirectory,
94+
buildDirectory: buildDirectory,
8595
copyPath: copyPath,
8696
);
8797

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,36 @@ void main() {
4141

4242
test('exit(1) if bundling throws', () async {
4343
final exitCalls = <int>[];
44-
await createBundle(context, Directory('/invalid/dir'), exitCalls.add);
44+
await createBundle(
45+
context: context,
46+
projectDirectory: Directory('/invalid/dir'),
47+
buildDirectory: Directory('/invalid/dir/build'),
48+
exit: exitCalls.add,
49+
);
4550
expect(exitCalls, equals([1]));
4651
verify(() => logger.err(any())).called(1);
4752
});
4853

4954
test('does not throw when bundling succeeds', () async {
5055
final exitCalls = <int>[];
51-
final directory = Directory.systemTemp.createTempSync();
52-
final dotDartFrogDir = Directory(path.join(directory.path, '.dart_frog'))
53-
..createSync();
54-
final buildDir = Directory(path.join(directory.path, 'build'))
55-
..createSync();
56-
final oldBuildArtifact = File(path.join(buildDir.path, 'artifact.txt'))
57-
..createSync();
58-
await createBundle(context, directory, exitCalls.add);
56+
final projectDirectory = Directory.systemTemp.createTempSync();
57+
final dotDartFrogDir =
58+
Directory(path.join(projectDirectory.path, '.dart_frog'))
59+
..createSync();
60+
final buildDirectory =
61+
Directory(path.join(projectDirectory.path, 'build'))..createSync();
62+
final oldBuildArtifact =
63+
File(path.join(buildDirectory.path, 'artifact.txt'))..createSync();
64+
65+
await createBundle(
66+
context: context,
67+
projectDirectory: projectDirectory,
68+
buildDirectory: buildDirectory,
69+
exit: exitCalls.add,
70+
);
71+
5972
expect(dotDartFrogDir.existsSync(), isFalse);
60-
expect(buildDir.existsSync(), isTrue);
73+
expect(buildDirectory.existsSync(), isTrue);
6174
expect(oldBuildArtifact.existsSync(), isFalse);
6275
expect(exitCalls, isEmpty);
6376
verifyNever(() => logger.err(any()));

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ void main() {
1111
test(
1212
'bundles external dependencies with external dependencies',
1313
() async {
14-
final directory = Directory.systemTemp.createTempSync();
15-
File(path.join(directory.path, 'pubspec.yaml')).writeAsStringSync(
14+
final projectDirectory = Directory.systemTemp.createTempSync();
15+
File(path.join(projectDirectory.path, 'pubspec.yaml'))
16+
.writeAsStringSync(
1617
'''
1718
name: example
1819
version: 0.1.0
@@ -26,22 +27,24 @@ dev_dependencies:
2627
test: any
2728
''',
2829
);
29-
File(path.join(directory.path, 'pubspec.lock')).writeAsStringSync(
30+
File(path.join(projectDirectory.path, 'pubspec.lock'))
31+
.writeAsStringSync(
3032
fooPath,
3133
);
3234
final copyCalls = <String>[];
3335

3436
await createExternalPackagesFolder(
35-
directory,
37+
projectDirectory: projectDirectory,
38+
buildDirectory: Directory(path.join(projectDirectory.path, 'build')),
3639
copyPath: (from, to) {
3740
copyCalls.add('$from -> $to');
3841
return Future.value();
3942
},
4043
);
4144

42-
final from = path.join(directory.path, '../../foo');
45+
final from = path.join(projectDirectory.path, '../../foo');
4346
final to = path.join(
44-
directory.path,
47+
projectDirectory.path,
4548
'build',
4649
'.dart_frog_path_dependencies',
4750
'foo',
@@ -53,8 +56,9 @@ dev_dependencies:
5356
test(
5457
"don't bundle internal path dependencies",
5558
() async {
56-
final directory = Directory.systemTemp.createTempSync();
57-
File(path.join(directory.path, 'pubspec.yaml')).writeAsStringSync(
59+
final projectDirectory = Directory.systemTemp.createTempSync();
60+
File(path.join(projectDirectory.path, 'pubspec.yaml'))
61+
.writeAsStringSync(
5862
'''
5963
name: example
6064
version: 0.1.0
@@ -70,14 +74,15 @@ dev_dependencies:
7074
test: any
7175
''',
7276
);
73-
File(path.join(directory.path, 'pubspec.lock')).writeAsStringSync(
77+
File(path.join(projectDirectory.path, 'pubspec.lock'))
78+
.writeAsStringSync(
7479
fooPathWithInternalDependency,
7580
);
7681
final copyCalls = <String>[];
7782

7883
File(
7984
path.join(
80-
directory.path,
85+
projectDirectory.path,
8186
'packages',
8287
'bar',
8388
'pubspec.yaml',
@@ -95,16 +100,17 @@ environment:
95100
);
96101

97102
await createExternalPackagesFolder(
98-
directory,
103+
projectDirectory: projectDirectory,
104+
buildDirectory: Directory(path.join(projectDirectory.path, 'build')),
99105
copyPath: (from, to) {
100106
copyCalls.add('$from -> $to');
101107
return Future.value();
102108
},
103109
);
104110

105-
final from = path.join(directory.path, '../../foo');
111+
final from = path.join(projectDirectory.path, '../../foo');
106112
final to = path.join(
107-
directory.path,
113+
projectDirectory.path,
108114
'build',
109115
'.dart_frog_path_dependencies',
110116
'foo',

0 commit comments

Comments
 (0)