File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -18,8 +18,6 @@ gem 'memory_profiler', require: false
1818
1919group :development do
2020 gem 'pry'
21- gem 'guard'
22- gem 'guard-puma'
2321
2422 # Needed for a Rake task
2523 gem 'git'
Original file line number Diff line number Diff line change 11# frozen_string_literal: true
22
3- require 'bundler'
4- Bundler . require ( :default , :development )
5-
63require_relative 'spec/visual/app'
74
85run VisualTestApp
Original file line number Diff line number Diff line change 33
44$VERBOSE = true
55
6+ require 'sinatra'
7+ require 'pry'
8+
69# stdlib
710require 'pathname'
811
12+ require_relative 'rouge_reloader'
13+
914class VisualTestApp < Sinatra ::Application
1015 BASE = Pathname . new ( __dir__ )
1116 SAMPLES = BASE . join ( 'samples' )
1217 ROOT = BASE . parent . parent
1318
19+ RougeReloader . install! ( :Rouge , ROOT . to_s , 'lib/rouge.rb' )
20+
21+ RELOADER = FeatureReloader . new ( :Rouge , ROOT . join ( 'lib' ) . to_s , 'rouge.rb' )
22+
1423 DEMOS = ROOT . join ( 'lib/rouge/demos' )
1524
1625 def query_string
@@ -58,6 +67,7 @@ def as_boolean(value)
5867 end
5968
6069 before do
70+ RELOADER . reload!
6171 Rouge ::Lexer . enable_debug!
6272 Rouge ::Formatter . enable_escape! if params [ :escape ]
6373
Original file line number Diff line number Diff line change 1+ # [jneen] this reloader is responsible for safely hot-loading Rouge in the dev server.
2+ # Previously, we were able to simply remove the Rouge constant and load lib/rouge.rb,
3+ # and all of Rouge would happily be reloaded. However, we have recently switched to
4+ # require_relative, which maintains its own global cache. Meaning that if we were to
5+ # remove the Rouge constant and load lib/rouge.rb, the line
6+ #
7+ # require_relative 'rouge/lexer'
8+ #
9+ # would be a no-op, and hence the constant Rouge::Lexer would not exist.
10+ #
11+ # This reloader clears a portion of that cache by mutating $LOADED_FEATURES.
12+ class FeatureReloader
13+ MUTEX = Mutex . new
14+
15+ def self . install! ( const_name , root , path )
16+ @instance = new ( const_name , root , path )
17+ @instance . install!
18+ end
19+
20+ def self . reload!
21+ @instance . reload!
22+ end
23+
24+ def initialize ( const_name , root , path )
25+ @const_name = const_name
26+ @root = root
27+ @path = File . expand_path ( path , root )
28+ @cache = { }
29+ end
30+
31+ def reload!
32+ STDERR . puts "========= reloading #{ @const_name } ========="
33+
34+ MUTEX . synchronize do
35+ # remove the global constant
36+ Object . send ( :remove_const , @const_name ) if Object . const_defined? ( @const_name )
37+
38+ # clear the cache
39+ $LOADED_FEATURES. reject! { |f | f . start_with? ( @root ) }
40+
41+ # load rouge.rb
42+ Kernel . load ( @path )
43+ end
44+ end
45+ end
You can’t perform that action at this time.
0 commit comments