Skip to content

Commit c64b055

Browse files
committed
refactor: simplify approach
1 parent be5d71d commit c64b055

12 files changed

+115
-399
lines changed

bricks/dart_frog_prod_server/hooks/lib/dart_frog_prod_server_hooks.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
export 'src/adjust_relative_pubspec_imports.dart';
2-
export 'src/copy_workspace_pubspec_lock.dart';
31
export 'src/create_bundle.dart';
42
export 'src/create_external_packages_folder.dart';
53
export 'src/dart_pub_get.dart';

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

Lines changed: 0 additions & 40 deletions
This file was deleted.

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

Lines changed: 0 additions & 72 deletions
This file was deleted.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Future<List<String>> createExternalPackagesFolder({
1414
}) async {
1515
final pathResolver = path.context;
1616
final pubspecLock = await getPubspecLock(
17-
buildDirectory.path,
17+
projectDirectory.path,
1818
pathContext: path.context,
1919
);
2020

@@ -29,7 +29,7 @@ Future<List<String>> createExternalPackagesFolder({
2929

3030
return _ExternalPathDependency(
3131
name: dependency.name,
32-
path: path.join(buildDirectory.path, pathDescription.path),
32+
path: path.join(projectDirectory.path, pathDescription.path),
3333
);
3434
},
3535
)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ import 'package:path/path.dart' as path;
66
/// https://github.com/dart-lang/pub/issues/4594
77
void disableWorkspaceResolution(
88
HookContext context, {
9-
required String buildDirectory,
9+
required String projectDirectory,
1010
required void Function(int exitCode) exit,
1111
}) {
1212
try {
13-
overrideResolutionInPubspecOverrides(buildDirectory);
13+
overrideResolutionInPubspecOverrides(projectDirectory);
1414
} on Exception catch (e) {
1515
context.logger.err('$e');
1616
exit(1);
1717
}
1818
}
1919

20-
void overrideResolutionInPubspecOverrides(String directory) {
20+
void overrideResolutionInPubspecOverrides(String projectDirectory) {
2121
final pubspecOverrides = File(
22-
path.join(directory, 'pubspec_overrides.yaml'),
22+
path.join(projectDirectory, 'pubspec_overrides.yaml'),
2323
);
2424

2525
if (pubspecOverrides.existsSync()) {

bricks/dart_frog_prod_server/hooks/post_gen.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:async';
22
import 'dart:io' as io;
33

4+
import 'package:dart_frog_prod_server_hooks/dart_frog_prod_server_hooks.dart';
45
import 'package:mason/mason.dart' show HookContext, lightCyan;
56
import 'package:path/path.dart' as path;
67

@@ -9,9 +10,19 @@ Future<void> run(HookContext context) => postGen(context);
910
Future<void> postGen(
1011
HookContext context, {
1112
io.Directory? directory,
13+
ProcessRunner runProcess = io.Process.run,
14+
void Function(int exitCode) exit = defaultExit,
1215
}) async {
1316
final projectDirectory = directory ?? io.Directory.current;
1417
final buildDirectoryPath = path.join(projectDirectory.path, 'build');
18+
19+
await dartPubGet(
20+
context,
21+
workingDirectory: buildDirectoryPath,
22+
runProcess: runProcess,
23+
exit: exit,
24+
);
25+
1526
final relativeBuildPath = path.relative(buildDirectoryPath);
1627
context.logger
1728
..info('')

bricks/dart_frog_prod_server/hooks/pre_gen.dart

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ Future<void> preGen(
2929
exit: exit,
3030
);
3131

32+
if (usesWorkspaces) {
33+
// Disable workspace resolution until we can generate per-package lockfiles.
34+
// https://github.com/dart-lang/pub/issues/4594
35+
disableWorkspaceResolution(
36+
context,
37+
projectDirectory: projectDirectory.path,
38+
exit: exit,
39+
);
40+
}
41+
3242
// We need to make sure that the pubspec.lock file is up to date
3343
await dartPubGet(
3444
context,
@@ -48,38 +58,6 @@ Future<void> preGen(
4858
exit: exit,
4959
);
5060

51-
if (usesWorkspaces) {
52-
// Disable workspace resolution until we can generate per-package lockfiles.
53-
// https://github.com/dart-lang/pub/issues/4594
54-
disableWorkspaceResolution(
55-
context,
56-
buildDirectory: buildDirectory.path,
57-
exit: exit,
58-
);
59-
// Copy the pubspec.lock from the workspace root to ensure the same versions
60-
// of dependencies are used in the production build.
61-
copyWorkspacePubspecLock(
62-
context,
63-
buildDirectory: buildDirectory.path,
64-
workingDirectory: projectDirectory.path,
65-
exit: exit,
66-
);
67-
// Adjust all relative pubspec.yaml imports.
68-
adjustRelativePubspecImports(
69-
context,
70-
buildDirectory: buildDirectory.path,
71-
exit: exit,
72-
);
73-
}
74-
75-
// We need to make sure that the pubspec.lock file is up to date
76-
await dartPubGet(
77-
context,
78-
workingDirectory: buildDirectory.path,
79-
runProcess: runProcess,
80-
exit: exit,
81-
);
82-
8361
final RouteConfiguration configuration;
8462
try {
8563
configuration = buildConfiguration(projectDirectory);
@@ -112,8 +90,12 @@ Future<void> preGen(
11290
onViolationEnd: () => exit(1),
11391
);
11492

93+
final customDockerFile = io.File(
94+
path.join(projectDirectory.path, 'Dockerfile'),
95+
);
96+
11597
final internalPathDependencies = await getInternalPathDependencies(
116-
buildDirectory,
98+
projectDirectory,
11799
);
118100

119101
final externalDependencies = await createExternalPackagesFolder(
@@ -122,9 +104,6 @@ Future<void> preGen(
122104
copyPath: copyPath,
123105
);
124106

125-
final customDockerFile = io.File(
126-
path.join(buildDirectory.path, 'Dockerfile'),
127-
);
128107
final addDockerfile = !customDockerFile.existsSync();
129108

130109
context.vars = {

bricks/dart_frog_prod_server/hooks/test/post_gen_test.dart

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import 'dart:io';
2+
13
import 'package:dart_frog_prod_server_hooks/dart_frog_prod_server_hooks.dart';
2-
import 'package:mason/mason.dart' show HookContext, Logger, Progress, lightCyan;
4+
import 'package:mason/mason.dart'
5+
show ExitCode, HookContext, Logger, Progress, lightCyan;
36
import 'package:mocktail/mocktail.dart';
7+
import 'package:path/path.dart' as path;
48
import 'package:test/test.dart';
59

610
import '../post_gen.dart' as post_gen;
@@ -31,6 +35,14 @@ void main() {
3135
late HookContext context;
3236
late Logger logger;
3337

38+
const processId = 42;
39+
final processResult = ProcessResult(
40+
processId,
41+
ExitCode.success.code,
42+
'',
43+
'',
44+
);
45+
3446
setUp(() {
3547
logger = _MockLogger();
3648
context = _FakeHookContext(logger: logger);
@@ -47,8 +59,32 @@ void main() {
4759
);
4860
});
4961

50-
test('outputs next steps', () async {
51-
await post_gen.postGen(context);
62+
test('runs dart pub get and outputs next steps', () async {
63+
var processRunnerCallCount = 0;
64+
final exitCalls = <int>[];
65+
66+
await post_gen.postGen(
67+
context,
68+
runProcess: (
69+
executable,
70+
args, {
71+
String? workingDirectory,
72+
bool? runInShell,
73+
}) async {
74+
processRunnerCallCount++;
75+
expect(executable, equals('dart'));
76+
expect(args, equals(['pub', 'get']));
77+
expect(
78+
workingDirectory,
79+
equals(path.join(Directory.current.path, 'build')),
80+
);
81+
expect(runInShell, isTrue);
82+
return processResult;
83+
},
84+
exit: exitCalls.add,
85+
);
86+
expect(processRunnerCallCount, equals(1));
87+
expect(exitCalls, isEmpty);
5288
verify(() => logger.success('Created a production build!')).called(1);
5389
verify(
5490
() => logger.info('Start the production server by running:'),

0 commit comments

Comments
 (0)