raindeer 0.10.2 → 0.11.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: 71b8beaa4f04506cd18ff780dc301a3ac978713c1282bdbfd533d26c71d7c801
4
- data.tar.gz: b457b05f0791b4eb0d4b69228062ef399f89639adb79271620e75eeb6b82f45c
3
+ metadata.gz: 5e25c94de75f9b61245dd4baa6ef270a9c0bca2243b10715fe200480fd105303
4
+ data.tar.gz: ff00fdf0ea4e7137018b75d74bb1e881fb2a9ed7b9268018c5130ef6a9c99352
5
5
  SHA512:
6
- metadata.gz: 8f757c4b05a3ce2d9b1e533f4e62dbc46996b58d93a6112a6d26d2e39ed33ec4ef7a1bb678700e0320a185e66e6563dbe078b0588ebe1739c30c5b761083b7b6
7
- data.tar.gz: a4fb8f956da6faf5c20735378f3c649aad8f75d2878ec7c499d9d7e435f0dbdce078f4320c844b9976db02451f17961e09b9eb79374d2fa4ba293dc9c50da816
6
+ metadata.gz: 9989d1dddf6f0fe0bad498323c0c19b84bb4ea75d2c5d62aaa9f86909bdbcfb3bc30fc040ff4889021111aad93122209ae17cc9cf4d542977b500019881023a9
7
+ data.tar.gz: fb5992cb47913ec2bc4da2772d48fc438e0cf4935f1084793ae0fd95ce5b3d8d314605d221d43a5ff0b91061b1c7d512b29124a0d5fa128ea097b00c4b075fb0
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rain
4
+ # Bundles files in the web root into the bundles listed under "assets" in config/config.yaml:
5
+ #
6
+ # assets:
7
+ # app.css:
8
+ # - style.css
9
+ # - components/*
10
+ # app.js:
11
+ # - components/*/*
12
+ #
13
+ # Paths are relative to the web root. A wildcard like "components/*" adds files in alphabetical order
14
+ # and filters by the bundle's extension. app.css and app.js are exported to the same public folder.
15
+ class Assets
16
+ GLOB_CHARACTERS = /[*?\[{]/
17
+
18
+ def initialize(web_root:, bundles:)
19
+ @web_root = File.realpath(web_root)
20
+ @bundles = bundles.to_h { |output, sources| [output.to_s, sources] }
21
+ end
22
+
23
+ def build
24
+ @bundles.each_key { |output| write(output) }
25
+ end
26
+
27
+ # Rebuilds a bundle whenever one of its files is saved, added or deleted. Runs in the background until the process exits.
28
+ def watch
29
+ require 'listen'
30
+
31
+ Listen.to(@web_root) { |modified, added, removed| rebuild(modified + added + removed) }.start
32
+ end
33
+
34
+ # Rebuilds only the bundles that include a changed file, so writing a bundle doesn't trigger another rebuild.
35
+ def rebuild(changed_paths)
36
+ changed = changed_paths.map { |path| File.expand_path(path) }
37
+
38
+ @bundles.each_key do |output|
39
+ write(output) if changed.any? { |file| source?(output, file) }
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def write(output)
46
+ bundle = sources(output).map { |source| wrap(source, File.read(path(source)), extension: File.extname(output)) }
47
+ File.write(path(output), "#{bundle.join("\n\n")}\n")
48
+ end
49
+
50
+ # Each listed path, with wildcards expanded. A file is only included once, and bundles never include themselves.
51
+ def sources(output)
52
+ extension = File.extname(output)
53
+ paths = @bundles[output].flat_map { |source| glob?(source) ? matches(source, extension:) : [source] }
54
+
55
+ paths.uniq - @bundles.keys
56
+ end
57
+
58
+ def matches(pattern, extension:)
59
+ Dir.glob(pattern, base: @web_root).select { |match| File.extname(match) == extension && File.file?(path(match)) }
60
+ end
61
+
62
+ def source?(output, file)
63
+ return false if @bundles.key?(file.delete_prefix("#{@web_root}/"))
64
+
65
+ @bundles[output].any? do |source|
66
+ if glob?(source)
67
+ File.extname(file) == File.extname(output) && File.fnmatch?(path(source), file, File::FNM_PATHNAME | File::FNM_EXTGLOB)
68
+ else
69
+ file == path(source)
70
+ end
71
+ end
72
+ end
73
+
74
+ def glob?(source)
75
+ source.match?(GLOB_CHARACTERS)
76
+ end
77
+
78
+ def wrap(source, content, extension:)
79
+ case extension
80
+ when '.css' then "/* #{source} */\n#{content}"
81
+ # Each script gets its own scope so top-level names can't collide.
82
+ when '.js' then "// #{source}\n(() => {\n#{content}\n})();"
83
+ else content
84
+ end
85
+ end
86
+
87
+ def path(relative_path)
88
+ File.join(@web_root, relative_path)
89
+ end
90
+ end
91
+ end
data/lib/cli/static.rb CHANGED
@@ -20,9 +20,12 @@ module Rain
20
20
  metadata = load_user_application(app_path: File.expand_path('app', application_path))
21
21
  build_path = File.expand_path('build', application_path)
22
22
 
23
+ public_path = File.expand_path('public', application_path)
24
+ build_assets(application_path:, public_path:)
25
+
23
26
  FileUtils.rm_rf(build_path)
24
27
  FileUtils.mkdir_p(build_path)
25
- FileUtils.cp_r("#{File.expand_path('public', application_path)}/.", build_path)
28
+ FileUtils.cp_r("#{public_path}/.", build_path)
26
29
 
27
30
  Low::Events::RequestEvent.define do |observers|
28
31
  observers << Providers['rain.router']
@@ -40,6 +43,19 @@ module Rain
40
43
 
41
44
  private
42
45
 
46
+ # Bundles are written into "public" and .gitignore'd, then copied into the build with everything else.
47
+ def build_assets(application_path:, public_path:)
48
+ config_path = File.expand_path('config/config.yaml', application_path)
49
+ return unless File.exist?(config_path)
50
+
51
+ require_relative '../support/config_loader'
52
+ bundles = Rain::ConfigLoader.load(config_path).assets
53
+ return unless bundles
54
+
55
+ require_relative '../assets/assets'
56
+ Rain::Assets.new(web_root: public_path, bundles:).build
57
+ end
58
+
43
59
  def load_user_application(app_path:)
44
60
  metadata = dirload(app_path)
45
61
 
@@ -54,11 +70,7 @@ module Rain
54
70
  end
55
71
 
56
72
  def request_results(metadata:)
57
- file_paths = metadata.file_types.values_at('md', 'rd', 'markdown', 'raindown').flat_map { it }.compact
58
- paths = file_paths.map { |file_path| Rain::Pages.url_path(file_path:) }.compact
59
- paths << '/'
60
- # Skip files that became solely metadata due to underscores hiding the entire file path.
61
- paths.reject! { |url_path| url_path == '' }
73
+ paths = request_paths(metadata:)
62
74
 
63
75
  progress_bar = ProgressBar.create(
64
76
  total: paths.size,
@@ -73,6 +85,21 @@ module Rain
73
85
  result
74
86
  end
75
87
  end
88
+
89
+ def request_paths(metadata:)
90
+ file_paths = metadata.file_types.values_at('md', 'rd', 'markdown', 'raindown').flat_map { it }.compact
91
+ paths = file_paths.map { |file_path| Rain::Pages.url_path(file_path:) }.compact
92
+ paths << '/'
93
+ paths.concat(static_routes).uniq!
94
+ # Skip files that became solely metadata due to underscores hiding the entire file path.
95
+ paths.reject { |url_path| url_path == '' }
96
+ end
97
+
98
+ # GET routes like '/gems' that don't need a param or wildcard to be requested.
99
+ def static_routes
100
+ routes = Providers['rain.router'].routes.values
101
+ routes.select { |route| route.verbs.include?(Rain::HTTP::GET) && !route.path.match?(/[:*]/) }.map(&:path)
102
+ end
76
103
  end
77
104
  end
78
105
  end
data/lib/raindeer/boot.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'lowdata'
3
4
  require 'lowevent'
4
5
  require 'lownode'
5
6
  require 'lowtype'
@@ -17,7 +18,7 @@ module Rain
17
18
  require_relative '../support/config_loader'
18
19
  env = {
19
20
  host: ENV.fetch('RAIN_HOST', nil),
20
- port: ENV.fetch('RAIN_PORT', nil),
21
+ port: ConfigLoader.parse_integer(ENV.fetch('RAIN_PORT', nil)),
21
22
  web_root: ENV.fetch('RAIN_WEB_ROOT', nil),
22
23
  debug_mode: ConfigLoader.parse_boolean(ENV.fetch('RAIN_DEBUG', true)),
23
24
  matrix_mode: ConfigLoader.parse_boolean(ENV.fetch('RAIN_MATRIX', nil)),
@@ -37,6 +38,13 @@ module Rain
37
38
  end
38
39
 
39
40
  Providers.define('low.loop') do
41
+ if config.assets
42
+ require_relative '../assets/assets'
43
+ assets = Assets.new(web_root: config.web_root, bundles: config.assets)
44
+ assets.build
45
+ assets.watch if config.debug_mode
46
+ end
47
+
40
48
  require 'lowloop'
41
49
  LowLoop.new(config:, router: Providers['rain.router'], renderer: Providers['rain.matrix'])
42
50
  end
@@ -58,13 +66,20 @@ require 'dirload'
58
66
  dirload(File.expand_path('../system', __dir__))
59
67
 
60
68
  application_path = File.expand_path('app', Dir.pwd)
61
- return unless Dir.exist?(application_path)
62
69
 
63
- metadata = dirload(application_path)
70
+ if Dir.exist?(application_path)
71
+ metadata = dirload(application_path)
64
72
 
65
- if Dir.exist?(File.expand_path('pages', application_path))
66
- Providers.define('rain.pages', eager: true) do
67
- require_relative '../pages/pages'
68
- Rain::Pages.new(metadata:)
73
+ if Dir.exist?(File.expand_path('pages', application_path))
74
+ Providers.define('rain.pages', eager: true) do
75
+ require_relative '../pages/pages'
76
+ Rain::Pages.new(metadata:)
77
+ end
69
78
  end
70
79
  end
80
+
81
+ #################################################
82
+ # FRAMEWORK INTERNAL CLEANUP
83
+ #################################################
84
+
85
+ Lowkey.clear
@@ -18,6 +18,15 @@ module Rain
18
18
  OpenStruct.new(config_data)
19
19
  end
20
20
 
21
+ # Environment variables are strings, so "4133" becomes 4133.
22
+ def parse_integer(value)
23
+ return nil if value.nil?
24
+
25
+ Integer(value, 10)
26
+ rescue ArgumentError
27
+ raise ArgumentError, "Expected an integer, got #{value.inspect}"
28
+ end
29
+
21
30
  def parse_boolean(value)
22
31
  return true if value == '1'
23
32
  return false if value == '0'
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Raindeer
4
- VERSION = '0.10.2'
4
+ VERSION = '0.11.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: raindeer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.2
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi
@@ -9,6 +9,20 @@ bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: listen
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
12
26
  - !ruby/object:Gem::Dependency
13
27
  name: ostruct
14
28
  requirement: !ruby/object:Gem::Requirement
@@ -65,6 +79,20 @@ dependencies:
65
79
  - - "~>"
66
80
  - !ruby/object:Gem::Version
67
81
  version: 0.7.0
82
+ - !ruby/object:Gem::Dependency
83
+ name: lowdata
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '0.1'
89
+ type: :runtime
90
+ prerelease: false
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '0.1'
68
96
  - !ruby/object:Gem::Dependency
69
97
  name: lowevent
70
98
  requirement: !ruby/object:Gem::Requirement
@@ -231,6 +259,7 @@ extensions: []
231
259
  extra_rdoc_files: []
232
260
  files:
233
261
  - bin/rain
262
+ - lib/assets/assets.rb
234
263
  - lib/cli/cli.rb
235
264
  - lib/cli/static.rb
236
265
  - lib/matrix/cursor.rb