-
Notifications
You must be signed in to change notification settings - Fork 755
[Demo][No submit] Dart blog experiment #7079
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
antfitch
wants to merge
2
commits into
main
Choose a base branch
from
dart-blog-migration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:file/file.dart'; | ||
| import 'package:file/local.dart'; | ||
| import 'package:jaspr/server.dart'; | ||
| import 'package:jaspr_router/jaspr_router.dart'; | ||
| import 'package:path/path.dart' as p; | ||
| import 'package:watcher/watcher.dart'; | ||
|
|
||
| import '../page.dart'; | ||
| import '../utils.dart'; | ||
| import 'route_loader.dart'; | ||
|
|
||
| /// A loader that loads routes from the filesystem. | ||
| /// | ||
| /// Routes are constructed based on the recursive folder structure under the root [directory]. | ||
| /// Index files (index.*) are treated as the page for the containing folder. | ||
| /// Files and folders starting with an underscore (_) are ignored. | ||
| class FilesystemLoader extends RouteLoaderBase { | ||
| FilesystemLoader( | ||
| this.directory, { | ||
| this.keepSuffixPattern, | ||
| super.debugPrint, | ||
| @visibleForTesting this.fileSystem = const LocalFileSystem(), | ||
| @visibleForTesting DirectoryWatcherFactory? watcherFactory, | ||
| }) : watcherFactory = watcherFactory ?? _defaultWatcherFactory; | ||
|
|
||
| /// The directory to load pages from. | ||
| final String directory; | ||
|
|
||
| /// A pattern to keep the file suffix for all matching pages. | ||
| final Pattern? keepSuffixPattern; | ||
|
|
||
| @visibleForTesting | ||
| final FileSystem fileSystem; | ||
| @visibleForTesting | ||
| final DirectoryWatcherFactory watcherFactory; | ||
|
|
||
| static DirectoryWatcher _defaultWatcherFactory(String path) => DirectoryWatcher(path); | ||
|
|
||
| final Map<String, Set<PageSource>> dependentSources = {}; | ||
|
|
||
| StreamSubscription<WatchEvent>? _watcherSub; | ||
|
|
||
| @override | ||
| Future<List<RouteBase>> loadRoutes(ConfigResolver resolver, bool eager) async { | ||
| if (kDebugMode) { | ||
| _watcherSub ??= watcherFactory(directory).events.listen((event) { | ||
| // It looks like event.path is relative on most platforms, but an | ||
| // absolute path on Linux. Turn this into the expected relative path. | ||
| final path = p.normalize(p.relative(event.path)); | ||
| if (event.type == ChangeType.MODIFY) { | ||
| invalidateFile(path); | ||
| } else if (event.type == ChangeType.REMOVE) { | ||
| removeFile(path); | ||
| } else if (event.type == ChangeType.ADD) { | ||
| addFile(path); | ||
| } | ||
| }); | ||
| } | ||
| return super.loadRoutes(resolver, eager); | ||
| } | ||
|
|
||
| @override | ||
| void onReassemble() { | ||
| _watcherSub?.cancel(); | ||
| _watcherSub = null; | ||
| } | ||
|
|
||
| @override | ||
| Future<String> readPartial(String path, Page page) { | ||
| return _getPartial(path, page).readAsString(); | ||
| } | ||
|
|
||
| @override | ||
| String readPartialSync(String path, Page page) { | ||
| return _getPartial(path, page).readAsStringSync(); | ||
| } | ||
|
|
||
| File _getPartial(String path, Page page) { | ||
| final pageSource = getSourceForPage(page); | ||
| if (pageSource != null) { | ||
| (dependentSources[path] ??= {}).add(pageSource); | ||
| } | ||
| return fileSystem.file(path); | ||
| } | ||
|
|
||
| @override | ||
| Future<List<PageSource>> loadPageSources() async { | ||
| final root = fileSystem.directory(directory); | ||
| if (!await root.exists()) { | ||
| return []; | ||
| } | ||
|
|
||
| List<PageSource> loadFiles(Directory dir) { | ||
| final List<PageSource> entities = []; | ||
| for (final entry in dir.listSync()) { | ||
| final path = entry.path.substring(root.path.length + 1); | ||
| if (entry is File) { | ||
| entities.add( | ||
| FilePageSource( | ||
| path, | ||
| entry, | ||
| this, | ||
| keepSuffix: keepSuffixPattern?.matchAsPrefix(entry.path) != null, | ||
| context: fileSystem.path, | ||
| ), | ||
| ); | ||
| } else if (entry is Directory) { | ||
| entities.addAll(loadFiles(entry)); | ||
| } | ||
| } | ||
| return entities; | ||
| } | ||
|
|
||
| return loadFiles(root); | ||
| } | ||
|
|
||
| void addFile(String path) { | ||
| addSource( | ||
| FilePageSource( | ||
| path.substring(directory.length + 1), | ||
| fileSystem.file(path), | ||
| this, | ||
| keepSuffix: keepSuffixPattern?.matchAsPrefix(path) != null, | ||
| context: fileSystem.path, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| void removeFile(String path) { | ||
| final source = sources.whereType<FilePageSource>().where((source) => source.file.path == path).firstOrNull; | ||
| if (source != null) { | ||
| removeSource(source); | ||
| } | ||
| } | ||
|
|
||
| void invalidateFile(String path, {bool rebuild = true}) { | ||
| final source = sources.whereType<FilePageSource>().where((source) => source.file.path == path).firstOrNull; | ||
| if (source != null) { | ||
| invalidateSource(source, rebuild: rebuild); | ||
| } | ||
| } | ||
|
|
||
| @override | ||
| void invalidateSource(PageSource source, {bool rebuild = true}) { | ||
| super.invalidateSource(source, rebuild: rebuild); | ||
| final fullPath = fileSystem.path.join(directory, source.path); | ||
| final dependencies = {...?dependentSources[fullPath]}; | ||
| dependentSources[fullPath]?.clear(); | ||
| for (final dependent in dependencies) { | ||
| invalidateSource(dependent, rebuild: rebuild); | ||
| } | ||
| } | ||
|
|
||
| @override | ||
| void invalidateAll() { | ||
| super.invalidateAll(); | ||
| dependentSources.clear(); | ||
| } | ||
| } | ||
|
|
||
| class FilePageSource extends PageSource { | ||
| FilePageSource(super.path, this.file, super.loader, {super.keepSuffix, super.context}); | ||
|
|
||
| final File file; | ||
|
|
||
| @override | ||
| Future<Page> buildPage() async { | ||
| final content = await file.readAsString(); | ||
|
|
||
| return Page(path: path, url: url, content: content, config: config, loader: loader); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The path handling for file watcher events appears to have issues with relative versus absolute paths. The
removeFileandinvalidateFilemethods compare the incomingpathwithsource.file.path, which is an absolute path. However, thepathgenerated from a watch event is derived fromp.relative(event.path), which likely produces a path relative to the current working directory, not an absolute one. This mismatch will cause hot-reloading of file removals and modifications to fail.Furthermore,
addFilealso receives this relative path and attempts to compute a sub-path from it usingdirectory, which can also fail if the paths are not consistently handled.The entire path resolution logic for watcher events needs to be revisited to ensure that paths are consistently resolved to absolute paths before being used in these methods.