Skip to content

Commit fa91d2b

Browse files
authored
feat: change production brick to support external dependencies in the Dockerfile (#926)
1 parent c186fbe commit fa91d2b

File tree

10 files changed

+151
-113
lines changed

10 files changed

+151
-113
lines changed

bricks/dart_frog_prod_server/__brick__/build/{{#addDockerfile}}Dockerfile{{/addDockerfile}}

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
FROM dart:{{dartVersion}} AS build
44

55
WORKDIR /app
6+
{{#hasExternalDependencies}}
67

8+
# Copy external dependencies
9+
COPY ./.dart_frog_path_dependencies ./.dart_frog_path_dependencies
10+
COPY ./pubspec_overrides.yaml ./pubspec_overrides.yaml
11+
{{/hasExternalDependencies}}
712
# Copy Dependencies
813
{{#pathDependencies}}COPY {{{.}}} ./{{{.}}}
914
{{/pathDependencies}}
15+
{{^hasExternalDependencies}}
1016
# Install Dependencies
1117
{{#pathDependencies}}RUN dart pub get -C {{{.}}}
1218
{{/pathDependencies}}
19+
{{/hasExternalDependencies}}
1320
# Resolve app dependencies.
1421
COPY pubspec.* ./
1522
RUN dart pub get

bricks/dart_frog_prod_server/hooks/pre_gen.dart

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'src/create_bundle.dart';
1111
import 'src/create_external_packages_folder.dart';
1212
import 'src/dart_pub_get.dart';
1313
import 'src/exit_overrides.dart';
14-
import 'src/get_path_dependencies.dart';
14+
import 'src/get_internal_path_dependencies.dart';
1515

1616
typedef RouteConfigurationBuilder = RouteConfiguration Function(
1717
io.Directory directory,
@@ -81,26 +81,15 @@ Future<void> preGen(
8181
path.join(projectDirectory.path, 'Dockerfile'),
8282
);
8383

84-
// Get all the internal path packages
85-
final internalPathDependencies = (await getPathDependencies(projectDirectory))
86-
.where(
87-
(dependencyPath) =>
88-
path.isWithin(projectDirectory.path, dependencyPath),
89-
)
90-
.toList();
84+
final internalPathDependencies = await getInternalPathDependencies(
85+
projectDirectory,
86+
);
9187

92-
// Then create the external packages folder
93-
// and add it to the list of path packages.
9488
final externalDependencies = await createExternalPackagesFolder(
9589
projectDirectory,
9690
copyPath: copyPath,
9791
);
9892

99-
final pathDependencies = [
100-
...internalPathDependencies,
101-
...externalDependencies,
102-
];
103-
10493
final addDockerfile = !customDockerFile.existsSync();
10594

10695
context.vars = {
@@ -117,7 +106,9 @@ Future<void> preGen(
117106
'serveStaticFiles': configuration.serveStaticFiles,
118107
'invokeCustomEntrypoint': configuration.invokeCustomEntrypoint,
119108
'invokeCustomInit': configuration.invokeCustomInit,
120-
'pathDependencies': pathDependencies,
109+
'pathDependencies': internalPathDependencies,
110+
'hasExternalDependencies': externalDependencies.isNotEmpty,
111+
'externalPathDependencies': externalDependencies,
121112
'dartVersion': context.vars['dartVersion'],
122113
'addDockerfile': addDockerfile,
123114
};

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
mason: ^0.1.0-dev.39
1111
path: ^1.8.1
1212
pubspec_lock: ^3.0.2
13-
pubspec_parse: ^1.2.0
1413

1514
dev_dependencies:
1615
mocktail: ^1.0.0

bricks/dart_frog_prod_server/hooks/src/create_external_packages_folder.dart

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ import 'dart:io';
22

33
import 'package:io/io.dart' as io;
44
import 'package:path/path.dart' as path;
5-
import 'package:pubspec_lock/pubspec_lock.dart';
5+
6+
import 'get_pubspec_lock.dart';
67

78
Future<List<String>> createExternalPackagesFolder(
89
Directory directory, {
910
path.Context? pathContext,
1011
Future<void> Function(String from, String to) copyPath = io.copyPath,
1112
}) async {
1213
final pathResolver = pathContext ?? path.context;
13-
final pubspecLock = await _getPubspecLock(directory.path, pathResolver);
14+
final pubspecLock = await getPubspecLock(
15+
directory.path,
16+
pathContext: pathResolver,
17+
);
1418

1519
final externalPathDependencies = pubspecLock.packages
1620
.map(
@@ -55,11 +59,15 @@ Future<List<String>> createExternalPackagesFolder(
5559
),
5660
)..createSync();
5761

62+
final copiedPaths = <String>[];
5863
for (final entry in mappedDependencies.entries) {
5964
final from = pathResolver.join(directory.path, entry.value);
6065
final to = pathResolver.join(packagesDirectory.path, entry.key);
6166

6267
await copyPath(from, to);
68+
copiedPaths.add(
69+
path.relative(to, from: buildDirectory.path),
70+
);
6371
}
6472

6573
final mappedPaths = mappedDependencies.map(
@@ -82,19 +90,5 @@ dependency_overrides:
8290
${mappedPaths.entries.map((entry) => ' ${entry.key}:\n path: ${entry.value}').join('\n')}
8391
''');
8492

85-
return externalPathDependencies;
86-
}
87-
88-
Future<PubspecLock> _getPubspecLock(
89-
String workingDirectory,
90-
path.Context pathResolver,
91-
) async {
92-
final pubspecLockFile = File(
93-
workingDirectory.isEmpty
94-
? 'pubspec.lock'
95-
: pathResolver.join(workingDirectory, 'pubspec.lock'),
96-
);
97-
98-
final content = await pubspecLockFile.readAsString();
99-
return content.loadPubspecLockFromYaml();
93+
return copiedPaths;
10094
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import 'dart:io' as io;
2+
3+
import 'package:path/path.dart' as path;
4+
5+
import 'get_pubspec_lock.dart';
6+
7+
Future<List<String>> getInternalPathDependencies(io.Directory directory) async {
8+
final pubspecLock = await getPubspecLock(directory.path);
9+
return pubspecLock.packages
10+
.map(
11+
(p) => p.iswitch(
12+
sdk: (_) => null,
13+
hosted: (_) => null,
14+
git: (_) => null,
15+
path: (d) => d.path,
16+
),
17+
)
18+
.whereType<String>()
19+
.where((dependencyPath) {
20+
return path.isWithin('', dependencyPath);
21+
}).toList();
22+
}

bricks/dart_frog_prod_server/hooks/src/get_path_dependencies.dart

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'dart:io';
2+
3+
import 'package:path/path.dart' as path;
4+
import 'package:pubspec_lock/pubspec_lock.dart';
5+
6+
Future<PubspecLock> getPubspecLock(
7+
String workingDirectory, {
8+
path.Context? pathContext,
9+
}) async {
10+
final pathResolver = pathContext ?? path.context;
11+
final pubspecLockFile = File(
12+
workingDirectory.isEmpty
13+
? 'pubspec.lock'
14+
: pathResolver.join(workingDirectory, 'pubspec.lock'),
15+
);
16+
17+
final content = await pubspecLockFile.readAsString();
18+
return content.loadPubspecLockFromYaml();
19+
}

bricks/dart_frog_prod_server/hooks/test/pre_gen_test.dart

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ void main() {
106106
route: '/',
107107
params: [],
108108
wildcard: false,
109-
)
110-
]
109+
),
110+
],
111111
},
112112
);
113113

@@ -234,6 +234,8 @@ dev_dependencies:
234234
'invokeCustomEntrypoint': true,
235235
'invokeCustomInit': false,
236236
'pathDependencies': <String>[],
237+
'hasExternalDependencies': false,
238+
'externalPathDependencies': <String>[],
237239
'dartVersion': 'stable',
238240
'addDockerfile': true,
239241
}),
@@ -289,6 +291,8 @@ dependencies:
289291
'serveStaticFiles': false,
290292
'invokeCustomEntrypoint': false,
291293
'invokeCustomInit': false,
294+
'hasExternalDependencies': false,
295+
'externalPathDependencies': <String>[],
292296
'pathDependencies': <String>[],
293297
'dartVersion': 'stable',
294298
'addDockerfile': false,
@@ -325,6 +329,8 @@ dependencies:
325329
'serveStaticFiles': false,
326330
'invokeCustomEntrypoint': false,
327331
'invokeCustomInit': true,
332+
'hasExternalDependencies': false,
333+
'externalPathDependencies': <String>[],
328334
'pathDependencies': <String>[],
329335
'dartVersion': 'stable',
330336
'addDockerfile': true,
@@ -344,7 +350,7 @@ dependencies:
344350
MiddlewareFile(
345351
name: 'hello_middleware',
346352
path: 'hello/middleware.dart',
347-
)
353+
),
348354
],
349355
directories: [
350356
RouteDirectory(
@@ -368,7 +374,7 @@ dependencies:
368374
),
369375
],
370376
params: [],
371-
)
377+
),
372378
],
373379
routes: [
374380
RouteFile(
@@ -405,7 +411,7 @@ dependencies:
405411
params: [],
406412
wildcard: false,
407413
),
408-
]
414+
],
409415
},
410416
serveStaticFiles: true,
411417
);
@@ -462,15 +468,20 @@ dependencies:
462468
}
463469
],
464470
'middleware': [
465-
{'name': 'hello_middleware', 'path': 'hello/middleware.dart'}
471+
{
472+
'name': 'hello_middleware',
473+
'path': 'hello/middleware.dart',
474+
},
466475
],
467476
'globalMiddleware': {
468477
'name': 'middleware',
469-
'path': 'middleware.dart'
478+
'path': 'middleware.dart',
470479
},
471480
'serveStaticFiles': true,
472481
'invokeCustomEntrypoint': false,
473482
'invokeCustomInit': false,
483+
'hasExternalDependencies': false,
484+
'externalPathDependencies': <String>[],
474485
'pathDependencies': <String>[],
475486
'dartVersion': 'stable',
476487
'addDockerfile': true,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import 'dart:io';
2+
3+
import 'package:path/path.dart' as path;
4+
import 'package:test/test.dart';
5+
6+
import '../../src/get_internal_path_dependencies.dart';
7+
8+
void main() {
9+
group('getPathDependencies', () {
10+
test('returns nothing when there are no path dependencies', () {
11+
final directory = Directory.systemTemp.createTempSync();
12+
File(path.join(directory.path, 'pubspec.lock')).writeAsStringSync(
13+
'''
14+
packages:
15+
test:
16+
dependency: transitive
17+
description:
18+
name: analyzer
19+
sha256: f85566ec7b3d25cbea60f7dd4f157c5025f2f19233ca4feeed33b616c78a26a3
20+
url: "https://pub.dev"
21+
source: hosted
22+
version: "6.1.0"
23+
mason:
24+
dependency: transitive
25+
description:
26+
name: analyzer
27+
sha256: f85566ec7b3d25cbea60f7dd4f157c5025f2f19233ca4feeed33b616c78a26a3
28+
url: "https://pub.dev"
29+
source: hosted
30+
version: "6.1.0"
31+
''',
32+
);
33+
expect(getInternalPathDependencies(directory), completion(isEmpty));
34+
directory.delete(recursive: true).ignore();
35+
});
36+
37+
test('returns correct path dependencies', () {
38+
final directory = Directory.systemTemp.createTempSync();
39+
File(path.join(directory.path, 'pubspec.lock')).writeAsStringSync(
40+
'''
41+
packages:
42+
dart_frog:
43+
dependency: "direct main"
44+
description:
45+
path: "path/to/dart_frog"
46+
relative: true
47+
source: path
48+
version: "0.0.0"
49+
dart_frog_gen:
50+
dependency: "direct main"
51+
description:
52+
path: "path/to/dart_frog_gen"
53+
relative: true
54+
source: path
55+
version: "0.0.0"
56+
''',
57+
);
58+
expect(
59+
getInternalPathDependencies(directory),
60+
completion(
61+
equals(['path/to/dart_frog', 'path/to/dart_frog_gen']),
62+
),
63+
);
64+
directory.delete(recursive: true).ignore();
65+
});
66+
});
67+
}

0 commit comments

Comments
 (0)