Skip to content

Commit ea536ca

Browse files
authoredJan 3, 2025··
Move hot restart script into a separate package (#94)
1 parent 813d652 commit ea536ca

File tree

13 files changed

+781
-42
lines changed

13 files changed

+781
-42
lines changed
 

‎.github/workflows/analyze.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
- uses: subosito/flutter-action@v2
1717
with:
1818
channel: 'stable'
19+
- run: cd hot_restart_timeline && flutter pub get
1920
- run: flutter analyze --fatal-infos --fatal-warnings
2021
- run: |
2122
dart format lib/src/timeline/html/sources/script.js.g.dart

‎CONTRIBUTING.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ When building the timeline, you can use the `tool/hot_restart_timeline.dart` scr
1010
It automatically reloads the HTML when you change any part of the Jaspr code or run the test again.
1111

1212
```bash
13-
dart run tool/hot_restart_timeline.dart
13+
dart run hot_restart_timeline/bin/main.dart
1414
```

‎hot_restart_timeline/.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# https://dart.dev/guides/libraries/private-files
2+
# Created by `dart pub`
3+
.dart_tool/
4+
!pubspec.lock

‎hot_restart_timeline/README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Spot Hot-Restart timeline CLI
2+
3+
File watcher, automatically rebuilding the timeline files (in `build/timeline`) when the source files are modified.
4+
5+
6+
## Usage
7+
8+
```bash
9+
dart run bin/main.dart
10+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This file configures the analyzer to use the lint rule set from `package:lint`
2+
3+
# include: package:lint/strict.yaml # For production apps
4+
include: package:lint/casual.yaml # For code samples, hackathons and other non-production code
5+
# include: package:lint/package.yaml # Use this for packages with public API
6+
7+
8+
# You might want to exclude auto-generated files from dart analysis
9+
analyzer:
10+
exclude:
11+
#- '**.freezed.dart'
12+
#- '**.g.dart'
13+
14+
# You can customize the lint rules set to your own liking. A list of all rules
15+
# can be found at https://dart-lang.github.io/linter/lints/options/options.html
16+
linter:
17+
rules:
18+
# Util classes are awesome!
19+
# avoid_classes_with_only_static_members: false
20+
21+
# Make constructors the first thing in every class
22+
# sort_constructors_first: true
23+
24+
# Choose wisely, but you don't have to
25+
# prefer_double_quotes: true
26+
# prefer_single_quotes: true
27+

‎hot_restart_timeline/bin/main.dart‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import 'dart:io';
2+
3+
import 'package:hot_restart_timeline/hot_restart_timeline.dart'
4+
as hot_restart_timeline;
5+
6+
Future<void> main(List<String> arguments) async {
7+
ProcessSignal.sigint.watch().listen((event) {
8+
exit(0);
9+
});
10+
await hot_restart_timeline.main();
11+
}

‎tool/hot_restart_timeline.dart‎ renamed to ‎hot_restart_timeline/lib/hot_restart_timeline.dart‎

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
import 'dart:io';
44

5-
import 'package:server_nano/server_nano.dart';
5+
import 'package:dartx/dartx_io.dart';
6+
import 'package:hot_restart_timeline/server.dart';
67

7-
Future<void> main() async {
8-
ProcessSignal.sigint.watch().listen((event) {
9-
exit(0);
10-
});
8+
// Platform.script points to bin/main.dart
9+
final packageRoot = Directory(Platform.script.path).parent.parent;
10+
final spotPackageRoot = packageRoot.parent;
1111

12+
Future<void> main() async {
1213
// Watch for changes in lib/ and then call compile_js.dart
13-
final libDir = Directory('lib');
14+
final libDir = spotPackageRoot.directory('lib');
1415

1516
final spotLibWatcher = libDir.watch(recursive: true);
1617
spotLibWatcher.listen((event) {
@@ -21,7 +22,7 @@ Future<void> main() async {
2122
rebuildHtml();
2223
});
2324

24-
final timelineHotReloadDir = Directory('build/timeline/');
25+
final timelineHotReloadDir = spotPackageRoot.directory('build/timeline/');
2526
if (!timelineHotReloadDir.existsSync()) {
2627
timelineHotReloadDir.createSync(recursive: true);
2728
}
@@ -36,31 +37,8 @@ Future<void> main() async {
3637
rebuildJs();
3738
rebuildHtml();
3839

39-
final server = Server();
40-
server.static('build/timeline/');
41-
server.get('/', (req, resp) {
42-
final timelines = timelineHotReloadDir
43-
.listSync(recursive: true)
44-
.where((file) => file.path.endsWith('.html'))
45-
.map((file) {
46-
final relative = file.path.split('build/timeline/').last;
47-
return '<li><a href="/$relative">$relative</a></li>\n';
48-
}).join('\n');
49-
resp.sendHtmlText(
50-
'<h1>Spot timelines</h1>\n\n'
51-
'<ul>\n$timelines</ul>',
52-
);
53-
});
54-
55-
server.listen(port: 5907);
56-
final timelineFiles = timelineHotReloadDir.listSync(recursive: true);
57-
for (final file in timelineFiles) {
58-
if (!file.path.endsWith('.html')) {
59-
continue;
60-
}
61-
final relative = file.path.split('build/timeline/').last;
62-
print('http://localhost:5907/$relative');
63-
}
40+
startServer(timelineHotReloadDir);
41+
print('http://localhost:5907/');
6442
}
6543

6644
bool _rebuildingJs = false;
@@ -79,7 +57,11 @@ Future<void> rebuildJs() async {
7957
final timestamp = DateTime.now().toIso8601String().substring(11, 19);
8058
print('$timestamp Recompiling...');
8159
try {
82-
final result = await Process.run(dartExecutable, ['tool/compile_js.dart']);
60+
final result = await Process.run(
61+
dartExecutable,
62+
['tool/compile_js.dart'],
63+
workingDirectory: spotPackageRoot.path,
64+
);
8365
if (result.exitCode != 0) {
8466
print('Compilation failed');
8567
print(result.stdout);
@@ -110,7 +92,11 @@ Future<void> rebuildHtml() async {
11092
// start a new process so that it picks up the changes in the jaspr code
11193
final stopwatch = Stopwatch()..start();
11294
try {
113-
final result = await Process.run(dartExecutable, ['tool/render_html.dart']);
95+
final result = await Process.run(
96+
dartExecutable,
97+
['tool/render_html.dart'],
98+
workingDirectory: packageRoot.path,
99+
);
114100
if (result.exitCode != 0) {
115101
print('Render failed');
116102
print(result.stdout);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import 'dart:io';
2+
3+
import 'package:dartx/dartx_io.dart';
4+
import 'package:hot_restart_timeline/hot_restart_timeline.dart';
5+
import 'package:server_nano/server_nano.dart';
6+
7+
void startServer(Directory timelineHotReloadDir) {
8+
final server = Server();
9+
server.static(spotPackageRoot.directory('build/timeline/').path);
10+
server.get('/', (req, resp) {
11+
final timelines = timelineHotReloadDir
12+
.listSync(recursive: true)
13+
.where((file) => file.path.endsWith('.html'))
14+
.map((file) {
15+
final relative = file.path.split('build/timeline/').last;
16+
return '<li><a href="/$relative">$relative</a></li>\n';
17+
}).join('\n');
18+
resp.sendHtmlText(
19+
'<h1>Spot timelines</h1>\n\n'
20+
'<ul>\n$timelines</ul>',
21+
);
22+
});
23+
24+
server.listen(port: 5907);
25+
}

‎hot_restart_timeline/pubspec.lock‎

Lines changed: 656 additions & 0 deletions
Large diffs are not rendered by default.

‎hot_restart_timeline/pubspec.yaml‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: hot_restart_timeline
2+
description: A webserver which automatically reloads the timelines of spot on code changes
3+
version: 1.0.0
4+
publish_to: none
5+
6+
environment:
7+
sdk: ^3.0.0
8+
9+
dependencies:
10+
dartx: ^1.1.0
11+
path: ^1.9.0
12+
server_nano: ^1.5.1
13+
14+
dev_dependencies:
15+
lint: ^2.3.0
16+
spot:
17+
path: ../
18+
test: ^1.24.0

‎tool/render_html.dart‎ renamed to ‎hot_restart_timeline/tool/render_html.dart‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ import 'package:path/path.dart' as path;
77
import 'package:spot/src/timeline/html/render_timeline.dart';
88
import 'package:spot/src/timeline/html/web/timeline_event.dart';
99

10+
// Platform.script points to tool/render_html.dart
11+
final packageRoot = Directory(Platform.script.path).parent.parent;
12+
final spotPackageRoot = packageRoot.parent;
13+
14+
/// Renders the timeline HTML files to be served by the server
1015
Future<void> main() async {
11-
final globalTimelineDir = Directory('build').directory('timeline');
16+
final globalTimelineDir =
17+
spotPackageRoot.directory('build').directory('timeline');
1218
if (!globalTimelineDir.existsSync()) {
1319
return;
1420
}

‎lib/src/timeline/html/print_html.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ extension HtmlTimelinePrinter on Timeline {
105105
bool? _isTimelineHotRestartServerRunningCached;
106106

107107
/// Check if the Timeline Hot-Restart Server is running located at
108-
/// `tool/hot_restart_timeline.dart`
108+
/// `hot_restart_timeline/bin/main.dart`
109109
///
110110
/// This method is heavily cached because it might be executed for every tests
111111
/// and it only is required for development.

‎pubspec.yaml‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ dev_dependencies:
3030
lint: ^2.1.0
3131
test: ^1.24.0
3232
test_process: ^2.1.0
33-
server_nano: ^1.5.1
34-
35-
dependency_overrides:
36-
path: '>=1.8.0 <=1.9.0'
37-
mime: 1.0.4
3833

3934
flutter:
4035
assets:

0 commit comments

Comments
 (0)
Please sign in to comment.