Add server mode with live reload (rdoc --server)#1620
Conversation
|
🚀 Preview deployment available at: https://0b3fee17.rdoc-6cd.pages.dev (commit: d11c91c) |
Adds a built-in HTTP server for previewing documentation while editing source files. Parses all sources on startup, watches for file changes, re-parses only changed files, and auto-refreshes the browser. Server implementation (lib/rdoc/server.rb): - Uses Ruby's built-in TCPServer (no WEBrick or external dependencies) - Persistent Aliki generator instance rendering to strings - Thread-per-connection with Connection: close (no keep-alive) - Background watcher thread polls file mtimes every 1 second - Live reload via inline JS polling /__status endpoint - New --server[=PORT] option (default 4000) and rdoc:server Rake task - Moved RDoc::Servlet to RDoc::RI::Servlet (server mode uses new class) Security: - Binds to 127.0.0.1 only (localhost) - Path traversal protection in asset serving via expand_path containment - Proper HTTP error responses (400, 404, 405, 500) - 5-second read timeout on client sockets Concurrency: - Mutex protects all store mutations, generator refresh, and cache invalidation as a single atomic operation - Thread-safe last_change_time reads for the status endpoint Correctness: - Clears file contributions (methods, constants, comments, etc.) before re-parsing to prevent duplication, without removing shared namespaces - Individual parse_file errors caught so one failure doesn't block others - Store#remove_file recursively cleans nested classes/modules and C vars - Watcher thread uses @running flag with clean shutdown via join
5ab5492 to
a3bd278
Compare
Co-authored-by: Sutou Kouhei <kou@clear-code.com>
| # Injects the live-reload polling script before +</body>+. | ||
|
|
||
| def inject_live_reload(html) | ||
| html.sub('</body>', "#{LIVE_RELOAD_SCRIPT}</body>") |
There was a problem hiding this comment.
Just an idea, how about inserting last_change_time to this script?
There's a possibility to skip some change:
last_change_timeupdated to time1- Reload the page generated with time1
last_change_timeupdated again, value becomes time2- LIVE_RELOAD_SCRIPT loads initial
lastChangevalue, gettime2but the content is generated intime1
| removed_files.each do |f| | ||
| @file_mtimes.delete(f) | ||
| relative = relative_path_for(f) | ||
| @store.remove_file(relative) |
There was a problem hiding this comment.
I think what we need to do for removed files and changed files has overlap. File changed to empty file and file deletion seems essentially the same.
- Remove: Remove contributions and remove file from store
- Update: Remove contributions and re-parse the file
How about calling clear_file_contributions here without removing all classes and modules?
In clear_file_contributions, we can remove class or module if it is only defined in this file.
cm.in_files.delete(top_level)
if cm.in_files.empty?
# Remove from store
@store.send(:remove_classes_and_modules, [cm])
# Remove from parent's classes_hash
cm.parent.classes_hash.delete(cm.name) # or modules_hash.delete(cm.name)
endThis way, the limitation for reopened classes and file deletion will be gone/improve.
File change that renames a class defined in it might also work as expected.
A better attempt of #1151
Implement
rdoc --server[=PORT]for previewing documentation with automatic browser refresh on source file changes. The server parses all sources on startup, serves pages from memory via the Aliki generator, and watches for file modifications, additions, and deletions — re-parsing only what changed.rdoc-server-live-reload-demo.mp4
Changes
RDoc::Server(lib/rdoc/server.rb) — minimal HTTP server using Ruby's built-inTCPServer(no WEBrick or external dependencies)Connection: close/__statusendpoint every 1 second--server[=PORT]CLI option (default port 4000) andrdoc:serverRake taskRDoc::Store#remove_file— recursively removes a file's classes, modules, and C variable maps from the storeDarkfish#refresh_store_data— extracted for reuse by the server after re-parsingRDoc::Servlet→RDoc::RI::Servlet— moved to clarify RI-specific usageSecurity & robustness
127.0.0.1only (localhost)File.expand_pathcontainment check)IO.selectread timeout on client socketsparse_fileerrors rescued so one failure doesn't block remaining files@runningflag with clean shutdown viaThread#joinKnown limitations