Skip to content

Commit

Permalink
Move hot restart script into a separate package (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy authored Jan 3, 2025
1 parent 813d652 commit ea536ca
Show file tree
Hide file tree
Showing 13 changed files with 781 additions and 42 deletions.
1 change: 1 addition & 0 deletions .github/workflows/analyze.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
- uses: subosito/flutter-action@v2
with:
channel: 'stable'
- run: cd hot_restart_timeline && flutter pub get
- run: flutter analyze --fatal-infos --fatal-warnings
- run: |
dart format lib/src/timeline/html/sources/script.js.g.dart
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ When building the timeline, you can use the `tool/hot_restart_timeline.dart` scr
It automatically reloads the HTML when you change any part of the Jaspr code or run the test again.

```bash
dart run tool/hot_restart_timeline.dart
dart run hot_restart_timeline/bin/main.dart
```
4 changes: 4 additions & 0 deletions hot_restart_timeline/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/
!pubspec.lock
10 changes: 10 additions & 0 deletions hot_restart_timeline/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Spot Hot-Restart timeline CLI

File watcher, automatically rebuilding the timeline files (in `build/timeline`) when the source files are modified.


## Usage

```bash
dart run bin/main.dart
```
27 changes: 27 additions & 0 deletions hot_restart_timeline/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This file configures the analyzer to use the lint rule set from `package:lint`

# include: package:lint/strict.yaml # For production apps
include: package:lint/casual.yaml # For code samples, hackathons and other non-production code
# include: package:lint/package.yaml # Use this for packages with public API


# You might want to exclude auto-generated files from dart analysis
analyzer:
exclude:
#- '**.freezed.dart'
#- '**.g.dart'

# You can customize the lint rules set to your own liking. A list of all rules
# can be found at https://dart-lang.github.io/linter/lints/options/options.html
linter:
rules:
# Util classes are awesome!
# avoid_classes_with_only_static_members: false

# Make constructors the first thing in every class
# sort_constructors_first: true

# Choose wisely, but you don't have to
# prefer_double_quotes: true
# prefer_single_quotes: true

11 changes: 11 additions & 0 deletions hot_restart_timeline/bin/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'dart:io';

import 'package:hot_restart_timeline/hot_restart_timeline.dart'
as hot_restart_timeline;

Future<void> main(List<String> arguments) async {
ProcessSignal.sigint.watch().listen((event) {
exit(0);
});
await hot_restart_timeline.main();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import 'dart:io';

import 'package:server_nano/server_nano.dart';
import 'package:dartx/dartx_io.dart';
import 'package:hot_restart_timeline/server.dart';

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

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

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

final timelineHotReloadDir = Directory('build/timeline/');
final timelineHotReloadDir = spotPackageRoot.directory('build/timeline/');
if (!timelineHotReloadDir.existsSync()) {
timelineHotReloadDir.createSync(recursive: true);
}
Expand All @@ -36,31 +37,8 @@ Future<void> main() async {
rebuildJs();
rebuildHtml();

final server = Server();
server.static('build/timeline/');
server.get('/', (req, resp) {
final timelines = timelineHotReloadDir
.listSync(recursive: true)
.where((file) => file.path.endsWith('.html'))
.map((file) {
final relative = file.path.split('build/timeline/').last;
return '<li><a href="/$relative">$relative</a></li>\n';
}).join('\n');
resp.sendHtmlText(
'<h1>Spot timelines</h1>\n\n'
'<ul>\n$timelines</ul>',
);
});

server.listen(port: 5907);
final timelineFiles = timelineHotReloadDir.listSync(recursive: true);
for (final file in timelineFiles) {
if (!file.path.endsWith('.html')) {
continue;
}
final relative = file.path.split('build/timeline/').last;
print('http://localhost:5907/$relative');
}
startServer(timelineHotReloadDir);
print('http://localhost:5907/');
}

bool _rebuildingJs = false;
Expand All @@ -79,7 +57,11 @@ Future<void> rebuildJs() async {
final timestamp = DateTime.now().toIso8601String().substring(11, 19);
print('$timestamp Recompiling...');
try {
final result = await Process.run(dartExecutable, ['tool/compile_js.dart']);
final result = await Process.run(
dartExecutable,
['tool/compile_js.dart'],
workingDirectory: spotPackageRoot.path,
);
if (result.exitCode != 0) {
print('Compilation failed');
print(result.stdout);
Expand Down Expand Up @@ -110,7 +92,11 @@ Future<void> rebuildHtml() async {
// start a new process so that it picks up the changes in the jaspr code
final stopwatch = Stopwatch()..start();
try {
final result = await Process.run(dartExecutable, ['tool/render_html.dart']);
final result = await Process.run(
dartExecutable,
['tool/render_html.dart'],
workingDirectory: packageRoot.path,
);
if (result.exitCode != 0) {
print('Render failed');
print(result.stdout);
Expand Down
25 changes: 25 additions & 0 deletions hot_restart_timeline/lib/server.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'dart:io';

import 'package:dartx/dartx_io.dart';
import 'package:hot_restart_timeline/hot_restart_timeline.dart';
import 'package:server_nano/server_nano.dart';

void startServer(Directory timelineHotReloadDir) {
final server = Server();
server.static(spotPackageRoot.directory('build/timeline/').path);
server.get('/', (req, resp) {
final timelines = timelineHotReloadDir
.listSync(recursive: true)
.where((file) => file.path.endsWith('.html'))
.map((file) {
final relative = file.path.split('build/timeline/').last;
return '<li><a href="/$relative">$relative</a></li>\n';
}).join('\n');
resp.sendHtmlText(
'<h1>Spot timelines</h1>\n\n'
'<ul>\n$timelines</ul>',
);
});

server.listen(port: 5907);
}
Loading

0 comments on commit ea536ca

Please sign in to comment.