Skip to content

Commit 3171396

Browse files
authored
refactor(dart_frog_prod_server): clean createExternalPackagesFolder logic (#1251)
1 parent 95c6205 commit 3171396

File tree

1 file changed

+72
-39
lines changed

1 file changed

+72
-39
lines changed

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

Lines changed: 72 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ 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+
/// Signature of [io.copyPath].
8+
typedef CopyPath = Future<void> Function(String from, String to);
9+
710
Future<List<String>> createExternalPackagesFolder({
811
required Directory projectDirectory,
912
required Directory buildDirectory,
10-
Future<void> Function(String from, String to) copyPath = io.copyPath,
13+
CopyPath copyPath = io.copyPath,
1114
}) async {
1215
final pathResolver = path.context;
1316
final pubspecLock = await getPubspecLock(
@@ -21,55 +24,41 @@ Future<List<String>> createExternalPackagesFolder({
2124
sdk: (_) => null,
2225
hosted: (_) => null,
2326
git: (_) => null,
24-
path: (d) => d.path,
27+
path: (d) {
28+
final isExternal = !pathResolver.isWithin('', d.path);
29+
if (!isExternal) return null;
30+
31+
return _ExternalPathDependency(
32+
name: pathResolver.basename(d.path),
33+
path: path.join(projectDirectory.path, d.path),
34+
);
35+
},
2536
),
2637
)
27-
.whereType<String>()
28-
.where((dependencyPath) {
29-
return !pathResolver.isWithin('', dependencyPath);
30-
}).toList();
38+
.whereType<_ExternalPathDependency>()
39+
.toList();
3140

3241
if (externalPathDependencies.isEmpty) {
3342
return [];
3443
}
35-
final mappedDependencies = externalPathDependencies
36-
.map(
37-
(dependencyPath) => (
38-
pathResolver.basename(dependencyPath),
39-
dependencyPath,
40-
),
41-
)
42-
.fold(<String, String>{}, (map, dependency) {
43-
map[dependency.$1] = dependency.$2;
44-
return map;
45-
});
46-
47-
buildDirectory.createSync();
4844

4945
final packagesDirectory = Directory(
5046
pathResolver.join(
5147
buildDirectory.path,
5248
'.dart_frog_path_dependencies',
5349
),
54-
)..createSync();
55-
56-
final copiedPaths = <String>[];
57-
for (final entry in mappedDependencies.entries) {
58-
final from = pathResolver.join(projectDirectory.path, entry.value);
59-
final to = pathResolver.join(packagesDirectory.path, entry.key);
60-
61-
await copyPath(from, to);
62-
copiedPaths.add(
63-
path.relative(to, from: buildDirectory.path),
64-
);
65-
}
50+
)..createSync(recursive: true);
6651

67-
final mappedPaths = mappedDependencies.map(
68-
(key, value) => MapEntry(
69-
key,
70-
pathResolver.relative(
71-
path.join(packagesDirectory.path, key),
72-
from: buildDirectory.path,
52+
final copiedExternalPathDependencies = await Future.wait(
53+
externalPathDependencies.map(
54+
(externalPathDependency) => externalPathDependency.copyTo(
55+
copyPath: copyPath,
56+
targetDirectory: Directory(
57+
pathResolver.join(
58+
packagesDirectory.path,
59+
externalPathDependency.name,
60+
),
61+
),
7362
),
7463
),
7564
);
@@ -81,8 +70,52 @@ Future<List<String>> createExternalPackagesFolder({
8170
),
8271
).writeAsString('''
8372
dependency_overrides:
84-
${mappedPaths.entries.map((entry) => ' ${entry.key}:\n path: ${entry.value}').join('\n')}
73+
${copiedExternalPathDependencies.map(
74+
(dependency) {
75+
final name = dependency.name;
76+
final path =
77+
pathResolver.relative(dependency.path, from: buildDirectory.path);
78+
return ' $name:\n path: $path';
79+
},
80+
).join('\n')}
8581
''');
8682

87-
return copiedPaths;
83+
return copiedExternalPathDependencies
84+
.map((dependency) => dependency.path)
85+
.toList();
86+
}
87+
88+
/// {@template external_path_dependency}
89+
/// A path dependency that is not within the bundled Dart Frog project
90+
/// directory.
91+
///
92+
/// For example:
93+
/// ```yaml
94+
/// name: my_dart_frog_project
95+
/// dependencies:
96+
/// my_package:
97+
/// path: ../my_package
98+
/// ```
99+
/// {@endtemplate}
100+
class _ExternalPathDependency {
101+
/// {@macro external_path_dependency}
102+
const _ExternalPathDependency({
103+
required this.name,
104+
required this.path,
105+
});
106+
107+
/// The name of the package.
108+
final String name;
109+
110+
/// The absolute path to the package.
111+
final String path;
112+
113+
/// Copies the [_ExternalPathDependency] to [targetDirectory].
114+
Future<_ExternalPathDependency> copyTo({
115+
required Directory targetDirectory,
116+
CopyPath copyPath = io.copyPath,
117+
}) async {
118+
await copyPath(path, targetDirectory.path);
119+
return _ExternalPathDependency(name: name, path: targetDirectory.path);
120+
}
88121
}

0 commit comments

Comments
 (0)