hot_reloader 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 461d4f4d7e55f277ac68ad3757ff3a5de4e854d8f994bf497643cc405ac12da3
4
- data.tar.gz: '0399e4aba6fe95c3fb25c4fcd414552859fa1cc8bf530a43a1df465125f8893c'
3
+ metadata.gz: 9421c166738e6fafb613ca2dac2bc28bd99b53a6229d4c0720db14f2e0c0ae25
4
+ data.tar.gz: a5677eaf835907fc683a591bd5e746008aa9fe42f296fa0585372482f2df92fd
5
5
  SHA512:
6
- metadata.gz: 5ad70767d0a9e17bfedb28823ee60bf4dad0fb306a553207008b59a52a37a9136fc9f6143e771f10d0bf246cba5308a4c5c8b600d62aa5fab98d20e484936de5
7
- data.tar.gz: 1e0b7f132a276c4b43bc2aa62b81796109c967900226fd579d52cf4b7731aaf3b475073df7d437bcedd3fa7d0db0500fda26b541bf2d44472b7f5d4a36721286
6
+ metadata.gz: 14c1d5918b46577369366f01912de5b1b61907195eedf2c182b87022c1d33796a65ec43a90efe21e72d3211b341b29a247c24383f2da499ed5a7a98116dc136b
7
+ data.tar.gz: f6668adecc4df04439245a41d5b7327ec03b0f3e14b8bee313ec529b9275ebc5b57a4667d93c85e1e21118a54b33169d77765bb83eca73e8c24491eeb2091e0a
data/README.md CHANGED
@@ -27,15 +27,16 @@ Following is a example for use hot_reloader with [Roda](https://github.com/jerem
27
27
 
28
28
  require_relative './config/environment'
29
29
 
30
- if ENV['RACK_ENV'] == 'production'
31
- run App.freeze.app
32
- else
30
+ if ENV['RACK_ENV'] == 'development'
33
31
  run ->(env) { App.call(env) }
32
+ else
33
+ run App.freeze.app
34
34
  end
35
35
  ```
36
36
 
37
37
  Add loader initialize code into `config/environment.rb`
38
38
 
39
+ For simple use case, you just need pass paths to `HotReloader.eager_load` or `HotReloader.will_listen`.
39
40
 
40
41
  ```rb
41
42
  # config/environment.rb
@@ -44,20 +45,17 @@ require 'bundler'
44
45
  Bundler.require(:default, ENV.fetch('RACK_ENV', "development"))
45
46
  require_relative 'application'
46
47
 
47
- loader = Zeitwerk::Loader.new
48
- loader.push_dir("#{__dir__}/../app")
49
- loader.push_dir("#{__dir__}/../app/models")
50
- loader.inflector.inflect "ar" => "AR"
48
+ paths = ["#{__dir__}/../app", "#{__dir__}/../app/models"]
51
49
 
52
- if ENV['RACK_ENV'] == 'production'
53
- HotReloader.eager_load(loader)
50
+ if ENV['RACK_ENV'] == 'development'
51
+ HotReloader.will_listen(*paths)
54
52
  else
55
- HotReloader.will_listen(loader)
53
+ HotReloader.eager_load(*paths)
56
54
  end
57
55
  ```
58
56
 
59
- Or use more simple form (if you don't need setup Zeitwerk loader youself)
60
-
57
+ For more advanced case(e.g. you need setup zeitwerk loader instance yourself), you
58
+ can pass this instance to HotReloader methods too.
61
59
 
62
60
  ```rb
63
61
  # config/environment.rb
@@ -66,12 +64,64 @@ require 'bundler'
66
64
  Bundler.require(:default, ENV.fetch('RACK_ENV', "development"))
67
65
  require_relative 'application'
68
66
 
69
- paths = ["#{__dir__}/../app", "#{__dir__}/../app/models"]
67
+ loader = Zeitwerk::Loader.new
68
+ loader.push_dir("#{__dir__}/../app")
69
+ loader.push_dir("#{__dir__}/../app/models")
70
70
 
71
- if ENV['RACK_ENV'] == 'production'
72
- HotReloader.eager_load(*paths)
71
+ if ENV['RACK_ENV'] == 'development'
72
+ HotReloader.will_listen(loader)
73
73
  else
74
- HotReloader.will_listen(*paths)
74
+ HotReloader.eager_load(loader)
75
+ end
76
+ ```
77
+
78
+ When you change root directories files(app/*.rb or app/models/*.rb for above case),
79
+ all monitored files will be reload.
80
+
81
+ it is possible to trigger reload from any `.rb` files, even this file not follow constant
82
+ lookup name convention, and this file folder not add to root directories use `push_dir` method.
83
+
84
+ Following is a example.
85
+
86
+ ```rb
87
+ # app/app.rb
88
+
89
+ class App < Roda
90
+ plugin :hash_routes
91
+
92
+ Dir["routes/**/*.rb"].each do |route_file|
93
+ load route_file
94
+ end
95
+ end
96
+ ```
97
+
98
+ ```rb
99
+ # routes/blog.rb
100
+
101
+ class App
102
+ hash_routes.on "blog" do |r|
103
+ "blog"
104
+ end
105
+ end
106
+ ```
107
+
108
+ `routes/blog.rb` is not follow constant lookup name convention, so, `routes/` folder can't be
109
+ add to root directories use push_dir method, but you can always trigger with `loader.reload`
110
+ if `routes/blog.rb` was changed, then when `app/app.rb` reloaded, it will load the
111
+ newest code in `routes/blog.rb` from the Dir each loop.
112
+
113
+ For achieve this, you only need pass listened folders to will_listen method as secondary arg.
114
+
115
+ ```
116
+ loader = Zeitwerk::Loader.new
117
+ loader.push_dir("#{__dir__}/../app")
118
+
119
+ listened_folders = ["#{__dir__}/../routes"]
120
+
121
+ if ENV['RACK_ENV'] == 'development'
122
+ HotReloader.will_listen(loader, listened_folders)
123
+ else
124
+ HotReloader.eager_load(loader)
75
125
  end
76
126
  ```
77
127
 
@@ -80,7 +130,9 @@ Write whatever application initialize code which need add into application.rb
80
130
  ```rb
81
131
  # config/application.rb
82
132
 
83
- DB = Sequel.connect(ENV.fetch("DATABASE_URL"), timeout: 10000)
133
+ DB = Sequel.connect(ENV.fetch("#{ENV.fetch('RACK_ENV', "development").upcase}_DATABASE_URL"), timeout: 10000)
134
+ Sequel::Model.plugin :timestamps
135
+ Sequel.extension :symbol_aref
84
136
  ```
85
137
 
86
138
  Add roda code into app/app.rb
@@ -114,7 +166,9 @@ Directory structure is like this:
114
166
  └── Gemfile.lock
115
167
  ```
116
168
 
117
- Change code in app.rb, **all constant get removed from memory, and app.rb evaluated again**!
169
+ After change code in app.rb, **all constant get removed from memory, and app.rb evaluated again**!
170
+
171
+ For a more rich WIP sample project, please check my another project [marketbet_crawler](https://github.com/zw963/marketbet_crawler).
118
172
 
119
173
  ## Support
120
174
 
data/lib/hot_reloader.rb CHANGED
@@ -10,31 +10,36 @@ class HotReloader
10
10
  #
11
11
  # @param [*String, Array<String>] folders Folders which should be monitor, can be multi-args or array.
12
12
  # or only one Zeitwerk::Loader object can be provided.
13
- # @param [#call] logger logger or any object should response call, e.g. method(:puts)
13
+ # @param [#call] logger logger or any object should response call, e.g. method(:puts), $stdout, $stderr.
14
14
  # @param [Array<String>] ignore Glob patterns or Pathname object which should be excluded.
15
15
  # @return nil
16
16
  def will_listen(*folders, logger: Logger.new(IO::NULL), ignore: [])
17
17
  folders = folders.flatten
18
18
 
19
19
  if folders.first.is_a? Zeitwerk::Loader
20
- loader = folders.first
20
+ loader, *listened_folders = folders
21
21
  folders = loader.root_dirs.keys
22
+ # `folders' will add to zeitwerk root directories and listened.
23
+ # `reloadable_folder' listened only, it can be reload dynamically.
24
+ # but not constant lookup use name convention.
22
25
  else
23
26
  loader = Zeitwerk::Loader.new
24
27
 
25
- raise 'you must set the root folders from which you want to load watched files.' if folders&.empty?
28
+ raise 'you must set the root directories from which you want to load watched files.' if folders&.empty?
26
29
  raise 'ignore: only accept an array of glob patterns string or Pathname objects.' unless ignore.is_a? Array
27
30
 
28
31
  folders.each {|folder| loader.push_dir(folder) }
32
+ listened_folders = []
29
33
  end
30
34
 
31
35
  loader.enable_reloading if loader.respond_to? :enable_reloading
32
36
  loader.logger = logger
33
-
34
37
  loader.ignore(ignore) unless ignore.empty?
35
-
36
38
  loader.setup
37
- Listen.to(*folders, wait_for_delay: 1) { loader.reload }.start
39
+
40
+ Listen.logger = logger
41
+
42
+ Listen.to(*(folders + listened_folders), wait_for_delay: 1, ignore: /\.#.*/) { loader.reload }.start
38
43
  end
39
44
 
40
45
  # Enable autoload ruby file based on default Zeitwerk rule.
@@ -42,9 +47,9 @@ class HotReloader
42
47
  #
43
48
  # @param [*String, Array<String>] folders folders which should be autoload, can be multi-args or array.
44
49
  # or only one Zeitwerk::Loader object can be provided.
45
- # @param [#call] logger logger or any object should response call, e.g. method(:puts)
50
+ # @param [#call] logger logger or any object should response call, e.g. method(:puts), $stdout, $stderr.
46
51
  # @return nil
47
- def eager_load(*folders, logger: Logger.new(IO::NULL))
52
+ def eager_load(*folders, logger: Logger.new(IO::NULL), ignore: [])
48
53
  folders = folders.flatten
49
54
 
50
55
  if folders.first.is_a? Zeitwerk::Loader
@@ -52,12 +57,15 @@ class HotReloader
52
57
  folders = loader.root_dirs.keys
53
58
  else
54
59
  loader = Zeitwerk::Loader.new
55
- raise 'you must set the root folders from which you want to load watched files.' if folders&.empty?
60
+
61
+ raise 'you must set the root directories from which you want to load watched files.' if folders&.empty?
62
+ raise 'ignore: only accept an array of glob patterns string or Pathname objects.' unless ignore.is_a? Array
56
63
 
57
64
  folders.each {|folder| loader.push_dir(folder) }
58
65
  end
59
66
 
60
67
  loader.logger = logger
68
+ loader.ignore(ignore) unless ignore.empty?
61
69
 
62
70
  loader.setup
63
71
  loader.eager_load
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class HotReloader
4
- VERSION = '0.6.0'
4
+ VERSION = '0.7.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hot_reloader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Billy.Zheng(zw963)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-08 00:00:00.000000000 Z
11
+ date: 2021-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zeitwerk
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
70
  requirements: []
71
- rubygems_version: 3.0.3
71
+ rubygems_version: 3.2.3
72
72
  signing_key:
73
73
  specification_version: 4
74
74
  summary: A dead simple ruby code hot reloader wrap around zeitwerk and listen.