Skip to content

Commit ba32f4a

Browse files
authored
refactor(bricks): decompose hooks into multiple files (#427)
1 parent d78a196 commit ba32f4a

37 files changed

+1531
-1197
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.dart_tool
22
.packages
33
pubspec.lock
4-
coverage
4+
coverage
5+
build

bricks/create_dart_frog/hooks/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ environment:
44
sdk: ">=2.18.0 <3.0.0"
55

66
dependencies:
7-
mason: ^0.1.0-dev.38
7+
mason: ^0.1.0-dev.39
88
path: ^1.8.1
99

1010
dev_dependencies:
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.dart_tool
22
.packages
33
pubspec.lock
4-
coverage
4+
coverage
5+
build

bricks/dart_frog_dev_server/hooks/pre_gen.dart

Lines changed: 6 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import 'dart:async';
22
import 'dart:io' as io;
33

44
import 'package:dart_frog_gen/dart_frog_gen.dart';
5-
import 'package:mason/mason.dart';
6-
import 'package:path/path.dart' as path;
7-
import 'package:pubspec_parse/pubspec_parse.dart';
5+
import 'package:mason/mason.dart' show HookContext;
6+
7+
import 'src/exit_overrides.dart';
8+
import 'src/report_external_path_dependencies.dart';
9+
import 'src/report_rogue_routes.dart';
10+
import 'src/report_route_conflicts.dart';
811

912
typedef RouteConfigurationBuilder = RouteConfiguration Function(
1013
io.Directory directory,
@@ -47,108 +50,3 @@ Future<void> preGen(
4750
'invokeCustomEntrypoint': configuration.invokeCustomEntrypoint,
4851
};
4952
}
50-
51-
Future<void> reportExternalPathDependencies(
52-
HookContext context,
53-
io.Directory directory,
54-
) async {
55-
final pubspec = Pubspec.parse(
56-
await io.File(path.join(directory.path, 'pubspec.yaml')).readAsString(),
57-
);
58-
59-
final dependencies = pubspec.dependencies;
60-
final devDependencies = pubspec.devDependencies;
61-
final pathDependencies = [...dependencies.entries, ...devDependencies.entries]
62-
.where((entry) => entry.value is PathDependency)
63-
.map((entry) {
64-
final value = entry.value as PathDependency;
65-
return [entry.key, value.path];
66-
}).toList();
67-
final externalDependencies = pathDependencies.where(
68-
(dep) => !path.isWithin(directory.path, dep.last),
69-
);
70-
71-
if (externalDependencies.isNotEmpty) {
72-
context.logger
73-
..info('')
74-
..err('All path dependencies must be within the project.')
75-
..err('External path dependencies detected:');
76-
for (final dependency in externalDependencies) {
77-
final dependencyName = dependency.first;
78-
final dependencyPath = path.normalize(dependency.last);
79-
context.logger.err(' \u{2022} $dependencyName from $dependencyPath');
80-
}
81-
}
82-
}
83-
84-
void reportRouteConflicts(
85-
HookContext context,
86-
RouteConfiguration configuration,
87-
) {
88-
final conflictingEndpoints =
89-
configuration.endpoints.entries.where((entry) => entry.value.length > 1);
90-
if (conflictingEndpoints.isNotEmpty) {
91-
context.logger.info('');
92-
for (final conflict in conflictingEndpoints) {
93-
final originalFilePath = path.normalize(
94-
path.join('routes', conflict.value.first.path),
95-
);
96-
final conflictingFilePath = path.normalize(
97-
path.join('routes', conflict.value.last.path),
98-
);
99-
context.logger.err(
100-
'''Route conflict detected. ${lightCyan.wrap(originalFilePath)} and ${lightCyan.wrap(conflictingFilePath)} both resolve to ${lightCyan.wrap(conflict.key)}.''',
101-
);
102-
}
103-
}
104-
}
105-
106-
void reportRogueRoutes(
107-
HookContext context,
108-
RouteConfiguration configuration,
109-
) {
110-
if (configuration.rogueRoutes.isNotEmpty) {
111-
context.logger.info('');
112-
for (final route in configuration.rogueRoutes) {
113-
final filePath = path.normalize(path.join('routes', route.path));
114-
final fileDirectory = path.dirname(filePath);
115-
final idealPath = path.join(
116-
fileDirectory,
117-
path.basenameWithoutExtension(filePath),
118-
'index.dart',
119-
);
120-
context.logger.err(
121-
'''Rogue route detected.${defaultForeground.wrap(' ')}Rename ${lightCyan.wrap(filePath)} to ${lightCyan.wrap(idealPath)}.''',
122-
);
123-
}
124-
}
125-
}
126-
127-
const _asyncRunZoned = runZoned;
128-
129-
abstract class ExitOverrides {
130-
static final _token = Object();
131-
132-
static ExitOverrides? get current {
133-
return Zone.current[_token] as ExitOverrides?;
134-
}
135-
136-
static R runZoned<R>(R Function() body, {void Function(int)? exit}) {
137-
final overrides = _ExitOverridesScope(exit);
138-
return _asyncRunZoned(body, zoneValues: {_token: overrides});
139-
}
140-
141-
void Function(int exitCode) get exit => io.exit;
142-
}
143-
144-
class _ExitOverridesScope extends ExitOverrides {
145-
_ExitOverridesScope(this._exit);
146-
147-
final ExitOverrides? _previous = ExitOverrides.current;
148-
final void Function(int exitCode)? _exit;
149-
150-
@override
151-
void Function(int exitCode) get exit {
152-
return _exit ?? _previous?.exit ?? super.exit;
153-
}
154-
}

bricks/dart_frog_dev_server/hooks/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ environment:
66

77
dependencies:
88
dart_frog_gen: ^0.3.0
9-
mason: ^0.1.0-dev.38
9+
mason: ^0.1.0-dev.39
1010
pubspec_parse: ^1.2.0
1111

1212
dev_dependencies:
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'dart:async';
2+
import 'dart:io' as io;
3+
4+
const _asyncRunZoned = runZoned;
5+
6+
abstract class ExitOverrides {
7+
static final _token = Object();
8+
9+
static ExitOverrides? get current {
10+
return Zone.current[_token] as ExitOverrides?;
11+
}
12+
13+
static R runZoned<R>(R Function() body, {void Function(int)? exit}) {
14+
final overrides = _ExitOverridesScope(exit);
15+
return _asyncRunZoned(body, zoneValues: {_token: overrides});
16+
}
17+
18+
void Function(int exitCode) get exit => io.exit;
19+
}
20+
21+
class _ExitOverridesScope extends ExitOverrides {
22+
_ExitOverridesScope(this._exit);
23+
24+
final ExitOverrides? _previous = ExitOverrides.current;
25+
final void Function(int exitCode)? _exit;
26+
27+
@override
28+
void Function(int exitCode) get exit {
29+
return _exit ?? _previous?.exit ?? super.exit;
30+
}
31+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'dart:io' as io;
2+
3+
import 'package:mason/mason.dart';
4+
import 'package:path/path.dart' as path;
5+
import 'package:pubspec_parse/pubspec_parse.dart';
6+
7+
Future<void> reportExternalPathDependencies(
8+
HookContext context,
9+
io.Directory directory,
10+
) async {
11+
final pubspec = Pubspec.parse(
12+
await io.File(path.join(directory.path, 'pubspec.yaml')).readAsString(),
13+
);
14+
15+
final dependencies = pubspec.dependencies;
16+
final devDependencies = pubspec.devDependencies;
17+
final pathDependencies = [...dependencies.entries, ...devDependencies.entries]
18+
.where((entry) => entry.value is PathDependency)
19+
.map((entry) {
20+
final value = entry.value as PathDependency;
21+
return [entry.key, value.path];
22+
}).toList();
23+
final externalDependencies = pathDependencies.where(
24+
(dep) => !path.isWithin(directory.path, dep.last),
25+
);
26+
27+
if (externalDependencies.isNotEmpty) {
28+
context.logger
29+
..info('')
30+
..err('All path dependencies must be within the project.')
31+
..err('External path dependencies detected:');
32+
for (final dependency in externalDependencies) {
33+
final dependencyName = dependency.first;
34+
final dependencyPath = path.normalize(dependency.last);
35+
context.logger.err(' \u{2022} $dependencyName from $dependencyPath');
36+
}
37+
}
38+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import 'package:dart_frog_gen/dart_frog_gen.dart';
2+
import 'package:mason/mason.dart';
3+
import 'package:path/path.dart' as path;
4+
5+
void reportRogueRoutes(
6+
HookContext context,
7+
RouteConfiguration configuration,
8+
) {
9+
if (configuration.rogueRoutes.isNotEmpty) {
10+
context.logger.info('');
11+
for (final route in configuration.rogueRoutes) {
12+
final filePath = path.normalize(path.join('routes', route.path));
13+
final fileDirectory = path.dirname(filePath);
14+
final idealPath = path.join(
15+
fileDirectory,
16+
path.basenameWithoutExtension(filePath),
17+
'index.dart',
18+
);
19+
context.logger.err(
20+
'''Rogue route detected.${defaultForeground.wrap(' ')}Rename ${lightCyan.wrap(filePath)} to ${lightCyan.wrap(idealPath)}.''',
21+
);
22+
}
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import 'package:dart_frog_gen/dart_frog_gen.dart';
2+
import 'package:mason/mason.dart';
3+
import 'package:path/path.dart' as path;
4+
5+
void reportRouteConflicts(
6+
HookContext context,
7+
RouteConfiguration configuration,
8+
) {
9+
final conflictingEndpoints =
10+
configuration.endpoints.entries.where((entry) => entry.value.length > 1);
11+
if (conflictingEndpoints.isNotEmpty) {
12+
context.logger.info('');
13+
for (final conflict in conflictingEndpoints) {
14+
final originalFilePath = path.normalize(
15+
path.join('routes', conflict.value.first.path),
16+
);
17+
final conflictingFilePath = path.normalize(
18+
path.join('routes', conflict.value.last.path),
19+
);
20+
context.logger.err(
21+
'''Route conflict detected. ${lightCyan.wrap(originalFilePath)} and ${lightCyan.wrap(conflictingFilePath)} both resolve to ${lightCyan.wrap(conflict.key)}.''',
22+
);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)