boto-rb-web 0.34.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 +7 -0
- data/LICENSE +21 -0
- data/README.md +17 -0
- data/assets/boto-web/boot.js +16 -0
- data/assets/boto-web/boto-web.css +32 -0
- data/assets/boto-web/dom/animate.js +49 -0
- data/assets/boto-web/dom/flip.js +41 -0
- data/assets/boto-web/dom/keyed.js +15 -0
- data/assets/boto-web/dom/morph.js +112 -0
- data/assets/boto-web/dom/navigation.js +72 -0
- data/assets/boto-web/dom/sync.js +42 -0
- data/assets/boto-web/net/base.js +5 -0
- data/assets/boto-web/net/protocol.js +46 -0
- data/assets/boto-web/net/sse.js +45 -0
- data/assets/boto-web/net/sse_stream.js +84 -0
- data/assets/boto-web/net/token.js +9 -0
- data/assets/boto-web/net/transport.js +99 -0
- data/assets/boto-web/runtime/component.js +138 -0
- data/assets/boto-web/runtime/events.js +54 -0
- data/assets/boto-web/runtime/hooks.js +82 -0
- data/assets/boto-web/runtime/live.js +12 -0
- data/assets/boto-web/runtime/on.js +36 -0
- data/assets/boto-web/runtime/runtime.js +100 -0
- data/assets/boto-web/runtime/scope.js +112 -0
- data/assets/boto-web/runtime/signals.js +123 -0
- data/assets/boto-web/template/decimal.js +93 -0
- data/assets/boto-web/template/helpers.js +31 -0
- data/assets/boto-web/template/html.js +61 -0
- data/assets/boto-web/template/hydrate.js +25 -0
- data/assets/boto-web/template/ruby/core.js +72 -0
- data/assets/boto-web/template/ruby/lists.js +63 -0
- data/assets/boto-web/template/ruby.js +5 -0
- data/lib/boto/component/arguments.rb +58 -0
- data/lib/boto/component/definition.rb +114 -0
- data/lib/boto/component.rb +107 -0
- data/lib/boto/helper.rb +14 -0
- data/lib/boto/view.rb +63 -0
- data/lib/boto/web/app_extension.rb +78 -0
- data/lib/boto/web/assets.rb +59 -0
- data/lib/boto/web/child_path.rb +38 -0
- data/lib/boto/web/code_class.rb +16 -0
- data/lib/boto/web/compiler/analysis/deps.rb +46 -0
- data/lib/boto/web/compiler/analysis/handlers.rb +47 -0
- data/lib/boto/web/compiler/analysis/walker.rb +174 -0
- data/lib/boto/web/compiler/analysis.rb +44 -0
- data/lib/boto/web/compiler/client_helper.rb +34 -0
- data/lib/boto/web/compiler/computed_body.rb +40 -0
- data/lib/boto/web/compiler/directives.rb +86 -0
- data/lib/boto/web/compiler/expression/calls.rb +149 -0
- data/lib/boto/web/compiler/expression/collections.rb +116 -0
- data/lib/boto/web/compiler/expression/literals.rb +75 -0
- data/lib/boto/web/compiler/expression/logic.rb +53 -0
- data/lib/boto/web/compiler/expression/operators.rb +85 -0
- data/lib/boto/web/compiler/expression/strings.rb +31 -0
- data/lib/boto/web/compiler/expression.rb +93 -0
- data/lib/boto/web/compiler/module_writer/handlers_js.rb +75 -0
- data/lib/boto/web/compiler/module_writer/js.rb +24 -0
- data/lib/boto/web/compiler/module_writer/tag_js.rb +132 -0
- data/lib/boto/web/compiler/module_writer/template_js.rb +89 -0
- data/lib/boto/web/compiler/module_writer/type_shape.rb +28 -0
- data/lib/boto/web/compiler/module_writer.rb +79 -0
- data/lib/boto/web/compiler/template.rb +136 -0
- data/lib/boto/web/compiler/type_map.rb +43 -0
- data/lib/boto/web/compiler/type_scope.rb +68 -0
- data/lib/boto/web/compiler.rb +9 -0
- data/lib/boto/web/dispatcher.rb +113 -0
- data/lib/boto/web/events_endpoint.rb +43 -0
- data/lib/boto/web/failure.rb +30 -0
- data/lib/boto/web/head.rb +24 -0
- data/lib/boto/web/helpers.rb +40 -0
- data/lib/boto/web/hooks.rb +39 -0
- data/lib/boto/web/html.rb +65 -0
- data/lib/boto/web/layouts/context.rb +22 -0
- data/lib/boto/web/layouts/template.rb +31 -0
- data/lib/boto/web/layouts.rb +69 -0
- data/lib/boto/web/modules.rb +37 -0
- data/lib/boto/web/page_endpoint.rb +65 -0
- data/lib/boto/web/project_helpers.rb +54 -0
- data/lib/boto/web/protocol.rb +85 -0
- data/lib/boto/web/registry.rb +86 -0
- data/lib/boto/web/registry_error.rb +8 -0
- data/lib/boto/web/renderer/erb_source.rb +199 -0
- data/lib/boto/web/renderer/render_context.rb +37 -0
- data/lib/boto/web/renderer.rb +94 -0
- data/lib/boto/web/runtime.rb +45 -0
- data/lib/boto/web/scoped_css.rb +113 -0
- data/lib/boto/web/secret_key.rb +8 -0
- data/lib/boto/web/settings.rb +27 -0
- data/lib/boto/web/snapshot/changes.rb +26 -0
- data/lib/boto/web/snapshot/inputs.rb +50 -0
- data/lib/boto/web/snapshot/token.rb +42 -0
- data/lib/boto/web/snapshot.rb +111 -0
- data/lib/boto/web/static_file.rb +35 -0
- data/lib/boto/web/stylesheet.rb +53 -0
- data/lib/boto/web/tailwind.rb +69 -0
- data/lib/boto/web/template_preview.rb +31 -0
- data/lib/boto/web/transports/http.rb +36 -0
- data/lib/boto/web/transports/sse.rb +45 -0
- data/lib/boto/web/transports.rb +21 -0
- data/lib/boto/web/version.rb +7 -0
- data/lib/boto/web/view_loader.rb +21 -0
- data/lib/boto/web.rb +17 -0
- metadata +172 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
# Base of the stateful components: the subclass declares its selector, inputs, states and actions.
|
|
5
|
+
class Component
|
|
6
|
+
extend Arguments
|
|
7
|
+
|
|
8
|
+
class << self
|
|
9
|
+
def definition = @definition ||= Definition.new
|
|
10
|
+
|
|
11
|
+
def selector(name)
|
|
12
|
+
definition.selector = name
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def input(name, type, required: false, **constraints)
|
|
16
|
+
definition.add_input(Definition::Input.new(name:, type: Boto::Types.build(type, **constraints), declared_type: type, required:))
|
|
17
|
+
define_method(name) { @inputs[name] }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def state(name, declared_type, default: nil, required: true, readonly: false, **constraints)
|
|
21
|
+
type = Boto::Types.build(declared_type, **constraints)
|
|
22
|
+
definition.add_state(Definition::State.new(name:, type:, declared_type:, default:, required:, readonly:))
|
|
23
|
+
define_method(name) { @values[name] }
|
|
24
|
+
define_method("#{name}=") { |value| write_state(name, value) }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def computed(name) = definition.add_computed(name)
|
|
28
|
+
|
|
29
|
+
# The declared types of the params a view reads like inputs; a component has none.
|
|
30
|
+
def declared_params = {}
|
|
31
|
+
|
|
32
|
+
def action(name) = definition.add_action(name, check_action_arguments!(name))
|
|
33
|
+
|
|
34
|
+
def action?(name) = definition.action?(name)
|
|
35
|
+
|
|
36
|
+
def subscribe(&block)
|
|
37
|
+
definition.channels_block = block
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# The HTML of the instance's template, as the page renders it: for tests, with no app or server.
|
|
41
|
+
def render(instance) = Boto::Web::TemplatePreview.render(instance)
|
|
42
|
+
|
|
43
|
+
def build(**attrs)
|
|
44
|
+
check_known_attributes!(attrs)
|
|
45
|
+
inputs = coerce_inputs(attrs.slice(*definition.input_names))
|
|
46
|
+
instance = new(inputs, attrs.slice(*definition.state_names))
|
|
47
|
+
instance.mount
|
|
48
|
+
instance.clear_changes!
|
|
49
|
+
instance
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def check_known_attributes!(attrs)
|
|
55
|
+
unknown = attrs.keys - definition.input_names - definition.state_names
|
|
56
|
+
raise ArgumentError, "unknown attributes: #{unknown.join(", ")}" if unknown.any?
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def coerce_inputs(given)
|
|
60
|
+
definition.inputs.values.to_h do |input|
|
|
61
|
+
raise ArgumentError, "missing input: #{input.name}" if input.required && !given.key?(input.name)
|
|
62
|
+
|
|
63
|
+
[input.name, input.coerce(given[input.name])]
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def initialize(inputs, states = {})
|
|
69
|
+
@inputs = inputs.freeze
|
|
70
|
+
@values = {}
|
|
71
|
+
@changed = {}
|
|
72
|
+
@events = []
|
|
73
|
+
self.class.definition.states.each_value { |state| @values[state.name] = state.default_value }
|
|
74
|
+
states.each { |name, value| write_state(name, value) }
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
attr_reader :events
|
|
78
|
+
|
|
79
|
+
def mount; end
|
|
80
|
+
|
|
81
|
+
def refresh = mount
|
|
82
|
+
|
|
83
|
+
def pending?(_name) = false
|
|
84
|
+
|
|
85
|
+
def emit(name, **data)
|
|
86
|
+
@events << { name:, data: }
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def changed_states = @changed.keys
|
|
90
|
+
|
|
91
|
+
def clear_changes!
|
|
92
|
+
@changed.clear
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def channels
|
|
96
|
+
block = self.class.definition.channels_block
|
|
97
|
+
block ? Array(instance_exec(&block)) : []
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
private
|
|
101
|
+
|
|
102
|
+
def write_state(name, value)
|
|
103
|
+
@values[name] = self.class.definition.states.fetch(name).coerce(value)
|
|
104
|
+
@changed[name] = true
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
data/lib/boto/helper.rb
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
# Extends a module of a project's src/helpers/. Its `client` marks a method that also runs in the browser.
|
|
5
|
+
module Helper
|
|
6
|
+
# Returns the name, so `client def money(value) = ...` reads as the declaration it is.
|
|
7
|
+
def client(name)
|
|
8
|
+
client_helper_names << name unless client_helper_names.include?(name)
|
|
9
|
+
name
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def client_helper_names = @client_helper_names ||= []
|
|
13
|
+
end
|
|
14
|
+
end
|
data/lib/boto/view.rb
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
# A page: a component with a route. Route, params and plugin declarations are the endpoint's (Endpoint::Dsl).
|
|
5
|
+
class View < Component
|
|
6
|
+
extend Endpoint::Dsl::Routing
|
|
7
|
+
extend Endpoint::Dsl::Parameters
|
|
8
|
+
extend Endpoint::Dsl::Security
|
|
9
|
+
extend Endpoint::Dsl::RateLimit
|
|
10
|
+
extend Endpoint::Dsl::Middlewares
|
|
11
|
+
|
|
12
|
+
DEFAULT_LAYOUT = :application
|
|
13
|
+
LAYOUT_MESSAGE = "layout must be a Symbol or false"
|
|
14
|
+
NO_INPUT = Boto::Params::EMPTY
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
# Each param is read from the path (or query) by Boto::Params, and gets a reader like an input.
|
|
18
|
+
def param(name, type, **options)
|
|
19
|
+
super
|
|
20
|
+
declared_params[name] = type
|
|
21
|
+
define_method(name) { @inputs.fetch(name) }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Kept with the declared type (Boto::Params::Definition has only the built one), for the compiler's types.
|
|
25
|
+
def declared_params = @declared_params ||= {}
|
|
26
|
+
|
|
27
|
+
# Coerces the params the way an endpoint does, then builds the page and runs its mount.
|
|
28
|
+
def build(**attrs)
|
|
29
|
+
page = instance_for(**attrs)
|
|
30
|
+
page.mount
|
|
31
|
+
page.clear_changes!
|
|
32
|
+
page
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# The page with its params coerced and no mount: what reads the page without loading its data (the channels).
|
|
36
|
+
def instance_for(**attrs)
|
|
37
|
+
ensure_known_attributes!(attrs)
|
|
38
|
+
new(params.call(path: attrs, query: NO_INPUT, body: NO_INPUT), attrs.slice(*definition.state_names))
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Symbol name of the layout in _layouts/, or false for a page without one. Inherited by subclasses.
|
|
42
|
+
def layout(name = nil)
|
|
43
|
+
return declare_layout(name) unless name.nil?
|
|
44
|
+
return @layout if defined?(@layout)
|
|
45
|
+
|
|
46
|
+
superclass.respond_to?(:layout) ? superclass.layout : DEFAULT_LAYOUT
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def declare_layout(name)
|
|
52
|
+
raise ArgumentError, LAYOUT_MESSAGE unless name == false || name.is_a?(Symbol)
|
|
53
|
+
|
|
54
|
+
@layout = name
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def ensure_known_attributes!(attrs)
|
|
58
|
+
unknown = attrs.keys - params.names - definition.state_names
|
|
59
|
+
raise ArgumentError, "unknown attributes: #{unknown.join(", ")}" if unknown.any?
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
# Called by the core at each boot of a project (Boto::AppExtensions): each view becomes a GET route, the action
|
|
6
|
+
# route POST <base>/_boto/web and the assets under /boto-web/. Stateless, so one instance serves every project.
|
|
7
|
+
class AppExtension
|
|
8
|
+
ACTIONS_PATH = "/_boto/web"
|
|
9
|
+
EVENTS_PATH = "/_boto/web/events"
|
|
10
|
+
# The label of the events entries: they share one route, the router never matches them.
|
|
11
|
+
EVENTS_ROUTE = ["GET", EVENTS_PATH].freeze
|
|
12
|
+
# The runtime has a folder per concern (runtime/, dom/, net/, template/ruby/): one route per depth.
|
|
13
|
+
ASSETS_PATHS = %w[/boto-web/:file /boto-web/:folder/:file /boto-web/:folder/:subfolder/:file].freeze
|
|
14
|
+
COMPONENTS_PATH = "/boto-web/components/:name"
|
|
15
|
+
APP_CSS_PATH = "/boto-web/#{Stylesheet::FILE}"
|
|
16
|
+
HOOKS_PATH = "/boto-web/hooks/:name"
|
|
17
|
+
APP_CSS_SOURCE = "application.css"
|
|
18
|
+
HOOKS_FOLDER = "hooks"
|
|
19
|
+
BUILD_FOLDER = "boto-web"
|
|
20
|
+
CSS_TYPE = "text/css; charset=utf-8"
|
|
21
|
+
DEVELOPMENT = "development"
|
|
22
|
+
|
|
23
|
+
# A project without a views folder (a webapi in the same process) gets no route and needs no secret key.
|
|
24
|
+
def call(builder)
|
|
25
|
+
layout = builder.project_layout
|
|
26
|
+
return unless File.directory?(layout.views_root)
|
|
27
|
+
|
|
28
|
+
runtime = Runtime.build(namespace: builder.namespace, env: builder.env, environment: builder.env_name,
|
|
29
|
+
options: builder.options.fetch(:web, {}), layouts_dir: layout.layouts_root)
|
|
30
|
+
pages = runtime.registry.views.to_h { |view| [view, builder.endpoint(PageEndpoint.for(view, runtime))] }
|
|
31
|
+
install_actions(builder, runtime, pages)
|
|
32
|
+
install_events(builder, runtime)
|
|
33
|
+
install_assets(builder, runtime.modules)
|
|
34
|
+
install_project_files(builder, runtime, layout)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
# The project's stylesheet is built once at boot; its hooks are read on request in development only.
|
|
40
|
+
def install_project_files(builder, runtime, layout)
|
|
41
|
+
minify = builder.env_name != DEVELOPMENT
|
|
42
|
+
css = Stylesheet.build(app_css: app_css(layout), registry: runtime.registry,
|
|
43
|
+
out_dir: File.join(layout.tmp_root, BUILD_FOLDER),
|
|
44
|
+
tailwind: Tailwind.for_project(minify:))
|
|
45
|
+
reload = builder.env_name == DEVELOPMENT
|
|
46
|
+
builder.raw("GET", APP_CSS_PATH, StaticFile.new(path: css, content_type: CSS_TYPE, reload:).method(:call))
|
|
47
|
+
hooks = Hooks.new(directory: File.join(layout.assets_root, HOOKS_FOLDER),
|
|
48
|
+
reload: builder.env_name == DEVELOPMENT)
|
|
49
|
+
builder.raw("GET", HOOKS_PATH, hooks.method(:call))
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def app_css(layout)
|
|
53
|
+
path = File.join(layout.assets_root, APP_CSS_SOURCE)
|
|
54
|
+
File.file?(path) ? path : nil
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def install_actions(builder, runtime, pages)
|
|
58
|
+
transport = Transports::Http.new(builder: builder, views: runtime.views, pages: pages)
|
|
59
|
+
builder.raw("POST", ACTIONS_PATH, transport.method(:call))
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Only the views that subscribe get an entry: the others have no live updates to stream.
|
|
63
|
+
def install_events(builder, runtime)
|
|
64
|
+
streams = runtime.registry.views.select { |view| view.definition.channels_block }.to_h do |view|
|
|
65
|
+
[view, builder.unrouted_entry(EventsEndpoint.for(view), EVENTS_ROUTE)]
|
|
66
|
+
end
|
|
67
|
+
transport = Transports::Sse.new(builder: builder, views: runtime.views, streams: streams)
|
|
68
|
+
builder.raw("GET", EVENTS_PATH, transport.method(:call))
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def install_assets(builder, modules)
|
|
72
|
+
assets = Assets.new(modules: modules)
|
|
73
|
+
ASSETS_PATHS.each { |path| builder.raw("GET", path, assets.method(:file)) }
|
|
74
|
+
builder.raw("GET", COMPONENTS_PATH, assets.method(:component))
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
# The browser runtime (assets/boto-web/) and the module of each component, served under /boto-web/. The runtime's
|
|
6
|
+
# .js files go out without the lines that are only a comment: the same rule scripts/size.js measures.
|
|
7
|
+
class Assets
|
|
8
|
+
DIRECTORY = File.expand_path("../../../assets/boto-web", __dir__)
|
|
9
|
+
# Root files, one folder level under runtime, dom, net and template, and template/ruby. Dots only before the ext.
|
|
10
|
+
FILE_PATTERN = %r{\A(?:(?:runtime|dom|net)/|template/(?:ruby/)?)?[a-z0-9_-]+\.(js|css)\z}
|
|
11
|
+
FILE_PARAMS = %i[folder subfolder file].freeze
|
|
12
|
+
COMPONENT_PATTERN = /\A([A-Za-z0-9_-]+)\.js\z/
|
|
13
|
+
COMMENT_LINE = %r{\A\s*//}
|
|
14
|
+
CONTENT_TYPES = { "js" => "text/javascript; charset=utf-8", "css" => "text/css; charset=utf-8" }.freeze
|
|
15
|
+
MODULE_TYPE = CONTENT_TYPES["js"]
|
|
16
|
+
NOT_FOUND = [404, { "content-type" => "text/plain" }, ["not found"]].freeze
|
|
17
|
+
URL_SEPARATOR = "--"
|
|
18
|
+
CLASS_SEPARATOR = "::"
|
|
19
|
+
|
|
20
|
+
def initialize(modules:, directory: DIRECTORY)
|
|
21
|
+
@modules = modules
|
|
22
|
+
@directory = directory
|
|
23
|
+
@served = {}
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# GET /boto-web/[:folder/[:subfolder/]]:file. The pattern accepts only the known folders: "..", encoded
|
|
27
|
+
# separators never reach the disk.
|
|
28
|
+
def file(env)
|
|
29
|
+
match = FILE_PATTERN.match(env[Boto::App::PATH_PARAMS_KEY].values_at(*FILE_PARAMS).compact.join("/"))
|
|
30
|
+
return NOT_FOUND unless match && File.file?(File.join(@directory, match[0]))
|
|
31
|
+
|
|
32
|
+
[200, { "content-type" => CONTENT_TYPES.fetch(match[1]) }, [@served[match[0]] ||= read(match[0], match[1])]]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# GET /boto-web/components/:name. The URL name is the class name with "--" for "::"; the registry decides.
|
|
36
|
+
def component(env)
|
|
37
|
+
match = COMPONENT_PATTERN.match(path_param(env, :name))
|
|
38
|
+
source = match && @modules.fetch(match[1].gsub(URL_SEPARATOR, CLASS_SEPARATOR))
|
|
39
|
+
return NOT_FOUND unless source
|
|
40
|
+
|
|
41
|
+
[200, { "content-type" => MODULE_TYPE }, [source]]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def read(name, ext)
|
|
47
|
+
# UTF-8 whatever the locale: under LANG=C the default external encoding is US-ASCII and the runtime has accents.
|
|
48
|
+
text = File.read(File.join(@directory, name), encoding: Encoding::UTF_8).freeze
|
|
49
|
+
ext == "js" ? without_comment_lines(text).freeze : text
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def without_comment_lines(text)
|
|
53
|
+
text.split("\n").reject { COMMENT_LINE.match?(_1) }.join("\n")
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def path_param(env, key) = env[Boto::App::PATH_PARAMS_KEY].fetch(key)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
# Follows a component's path from the view through the templates: each segment ("selector:index")
|
|
6
|
+
# must be a tag of the template above it, and its class comes from the registry only.
|
|
7
|
+
class ChildPath
|
|
8
|
+
def initialize(renderer:, registry:)
|
|
9
|
+
@renderer = renderer
|
|
10
|
+
@registry = registry
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def class_at(view_class, path)
|
|
14
|
+
path.reduce(view_class) { |parent, segment| child_class(parent, segment.rpartition(":").first) }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def child_class(parent, selector)
|
|
20
|
+
klass = @registry.component_for(selector) if tag_names(parent).include?(selector)
|
|
21
|
+
klass || raise(Snapshot::Invalid, "#{selector} is not a child of #{parent.name}")
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def tag_names(klass) = names_in(@renderer.analysis(klass).tree)
|
|
25
|
+
|
|
26
|
+
def names_in(nodes)
|
|
27
|
+
nodes.flat_map do |node|
|
|
28
|
+
case node
|
|
29
|
+
when Compiler::Analysis::Markup then node.pieces.grep(Compiler::Directives::Tag).map(&:name)
|
|
30
|
+
when Compiler::Template::If then node.branches.flat_map { names_in(_1.last) }
|
|
31
|
+
when Compiler::Template::Each then names_in(node.children)
|
|
32
|
+
else []
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
# Builds, once, the class a template runs in: the base class, the helpers, and each compiled ERB as a method.
|
|
6
|
+
# Requests only instantiate it; evaluating the code per request would make YJIT compile the template every time.
|
|
7
|
+
module CodeClass
|
|
8
|
+
def self.build(base, helpers:, codes:, file:)
|
|
9
|
+
Class.new(base).tap do |klass|
|
|
10
|
+
helpers.each { klass.include(_1) }
|
|
11
|
+
codes.each { |name, code| klass.class_eval("def #{name}\n#{code}\nend\n", file, 0) }
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "prism"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Web
|
|
7
|
+
module Compiler
|
|
8
|
+
class Analysis
|
|
9
|
+
# The names a Ruby source reads, found by walking the Prism tree. It works on sources that do not
|
|
10
|
+
# compile too: a block's deps come from its text, not from its JavaScript.
|
|
11
|
+
module Deps
|
|
12
|
+
READABLE = %i[state input computed].freeze
|
|
13
|
+
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
# The component's states, inputs and computeds the source reads (block locals shadow them).
|
|
17
|
+
def of(source, scope)
|
|
18
|
+
reads(source).select { READABLE.include?(scope.lookup(_1)&.fetch(:kind)) }.uniq
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# The block locals (of the names given) the source reads.
|
|
22
|
+
def locals_used(source, names)
|
|
23
|
+
reads(source).select { names.include?(_1) }.uniq
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def reads(source)
|
|
27
|
+
names = []
|
|
28
|
+
collect(Prism.parse(source).value, names)
|
|
29
|
+
names
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def collect(node, names)
|
|
33
|
+
names << node.name if plain_read?(node)
|
|
34
|
+
node.compact_child_nodes.each { collect(_1, names) }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def plain_read?(node)
|
|
38
|
+
return true if node.is_a?(Prism::LocalVariableReadNode)
|
|
39
|
+
|
|
40
|
+
node.is_a?(Prism::CallNode) && node.receiver.nil? && node.arguments.nil? && node.block.nil?
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "prism"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Web
|
|
7
|
+
module Compiler
|
|
8
|
+
class Analysis
|
|
9
|
+
# Classifies the value of an event: an action of the component (with or without arguments),
|
|
10
|
+
# or an assignment to one of its states. Anything else is an error.
|
|
11
|
+
class Handlers
|
|
12
|
+
def initialize(component, path:)
|
|
13
|
+
@component = component
|
|
14
|
+
@path = path
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def classify(source, scope, local_names, line:)
|
|
18
|
+
kind = kind_of(single_statement(source), source, scope)
|
|
19
|
+
raise Error, "#{@path}:#{line}: #{source.inspect} must be an action or an assignment to a state" unless kind
|
|
20
|
+
|
|
21
|
+
Analysis::Handler.new(kind:, source:, locals: Deps.locals_used(source, local_names), scope:)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def single_statement(source)
|
|
27
|
+
body = Prism.parse(source).value.statements.body
|
|
28
|
+
body.first if body.size == 1
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def kind_of(node, source, scope)
|
|
32
|
+
if node.is_a?(Prism::CallNode) && node.receiver.nil? && @component.action?(node.name)
|
|
33
|
+
:action
|
|
34
|
+
elsif node.is_a?(Prism::LocalVariableWriteNode) && assignable?(source, scope)
|
|
35
|
+
:assign
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Expression compiles a write only to a state of the component.
|
|
40
|
+
def assignable?(source, scope)
|
|
41
|
+
!Expression.compile(source, scope, assignment: true).equal?(Expression::SERVER)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
module Compiler
|
|
6
|
+
class Analysis
|
|
7
|
+
# One pass over the tree, in document order: numbers the handlers, compiles every read,
|
|
8
|
+
# and marks the server blocks. A block owner collects the deps of everything inside it.
|
|
9
|
+
class Walker
|
|
10
|
+
# seq is the pre-order position (0 is the root); marked is true when a server expression is inside.
|
|
11
|
+
Frame = Struct.new(:seq, :kind, :deps, :marked, :node)
|
|
12
|
+
|
|
13
|
+
attr_reader :handlers, :server_reads
|
|
14
|
+
|
|
15
|
+
# Every state, input and computed the template reads, handlers included.
|
|
16
|
+
def deps = @root.deps
|
|
17
|
+
|
|
18
|
+
def initialize(component, registry:, path:, helpers: [])
|
|
19
|
+
@component = component
|
|
20
|
+
@registry = registry
|
|
21
|
+
@path = path
|
|
22
|
+
@scope = TypeScope.new(component, helpers:)
|
|
23
|
+
@handlers = {}
|
|
24
|
+
@server_reads = []
|
|
25
|
+
@handlers_check = Handlers.new(component, path:)
|
|
26
|
+
@frames = []
|
|
27
|
+
@closed = []
|
|
28
|
+
@root = Frame.new(0, :root, [], false, nil)
|
|
29
|
+
@seq = 0
|
|
30
|
+
@slots = Hash.new(0)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def call(tree)
|
|
34
|
+
@root.node = visit_all(tree, {})
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# The root and each marked If/Each, numbered b1, b2... in document order.
|
|
38
|
+
def blocks
|
|
39
|
+
marked = [@root, *@closed].select(&:marked).sort_by(&:seq)
|
|
40
|
+
marked.each_with_index.to_h do |frame, index|
|
|
41
|
+
[frame.seq, Analysis::Block.new(id: "b#{index + 1}", deps: frame.deps, node: frame.node)]
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def visit_all(nodes, locals) = nodes.map { visit(_1, locals) }
|
|
48
|
+
|
|
49
|
+
def visit(node, locals)
|
|
50
|
+
case node
|
|
51
|
+
when Template::Text then markup(node, locals)
|
|
52
|
+
when Template::Output then output(node, locals)
|
|
53
|
+
when Template::If then if_node(node, locals)
|
|
54
|
+
when Template::Each then each_node(node, locals)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# An output outside an each is a block of its own: its frame is the innermost owner.
|
|
59
|
+
def output(node, locals)
|
|
60
|
+
scoped(:output) do
|
|
61
|
+
read(node.source, locals, node.line)
|
|
62
|
+
node
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def markup(text, locals)
|
|
67
|
+
pieces = Directives.split(text.html, path: @path, line: text.line)
|
|
68
|
+
Analysis::Markup.new(pieces: pieces.each_with_index.map { |piece, index| annotate(piece, pieces[index + 1], locals) })
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def annotate(piece, following, locals)
|
|
72
|
+
piece.is_a?(Directives::Tag) ? tag(piece, following, locals) : piece
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# A tag whose selector the registry knows is a child component; any other tag stays plain HTML.
|
|
76
|
+
def tag(tag, following, locals)
|
|
77
|
+
child = @registry.component_for(tag.name)
|
|
78
|
+
check_child_closed!(tag, following) if child
|
|
79
|
+
attributes = tag.attributes.map { attribute(_1, tag, locals) }
|
|
80
|
+
tag.with(attributes:, child:, slot: child && claim_slot(tag.name))
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# The index of a component tag among the tags of its selector, in document order.
|
|
84
|
+
def claim_slot(name)
|
|
85
|
+
@slots[name].tap { @slots[name] += 1 }
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# A component's tag is replaced by the component's render, so the template keeps it empty.
|
|
89
|
+
def check_child_closed!(tag, following)
|
|
90
|
+
return if following.is_a?(String) && following.start_with?("</#{tag.name}>")
|
|
91
|
+
|
|
92
|
+
raise Error, "#{@path}:#{tag.line}: component tag <#{tag.name}> must close right after the open tag"
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def attribute(attr, tag, locals)
|
|
96
|
+
return attr if attr.is_a?(Directives::Static)
|
|
97
|
+
return attr.with(handler: register(attr, tag, locals)) if attr.kind == :event
|
|
98
|
+
|
|
99
|
+
read(attr.source, locals, tag.line)
|
|
100
|
+
check_model!(attr, tag, locals) if attr.kind == :model
|
|
101
|
+
attr
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def register(event, tag, locals)
|
|
105
|
+
scope = scope_for(locals).with_local(:event, :any)
|
|
106
|
+
# A handler's reads never change the HTML around it, so they reach the root only, not the blocks.
|
|
107
|
+
@root.deps |= Deps.of(event.source, scope)
|
|
108
|
+
handler = @handlers_check.classify(event.source, scope, locals.keys, line: tag.line)
|
|
109
|
+
id = "h#{@handlers.size + 1}"
|
|
110
|
+
@handlers[id] = handler
|
|
111
|
+
id
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def check_model!(attr, tag, locals)
|
|
115
|
+
kind = scope_for(locals).lookup(attr.source.to_sym)&.fetch(:kind)
|
|
116
|
+
return if kind == :state
|
|
117
|
+
|
|
118
|
+
raise Error, "#{@path}:#{tag.line}: [(model)] needs a state, got #{attr.source}"
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def if_node(node, locals)
|
|
122
|
+
scoped(:if) do
|
|
123
|
+
branches = node.branches.map do |condition, children|
|
|
124
|
+
read(condition, locals, node.line) if condition
|
|
125
|
+
[condition, visit_all(children, locals)]
|
|
126
|
+
end
|
|
127
|
+
node.with(branches:)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def each_node(node, locals)
|
|
132
|
+
scoped(:each) do
|
|
133
|
+
type = type_of(read(node.collection, locals, node.line))
|
|
134
|
+
inner = locals.merge(node.vars.zip([TypeMap.element(type), :integer]).to_h)
|
|
135
|
+
node.with(children: visit_all(node.children, inner))
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def scoped(kind)
|
|
140
|
+
frame = Frame.new(@seq += 1, kind, [], false, nil)
|
|
141
|
+
@frames.push(frame)
|
|
142
|
+
node = yield
|
|
143
|
+
@frames.pop
|
|
144
|
+
@closed << frame
|
|
145
|
+
frame.node = frame.marked ? node.with(block: frame.seq) : node
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# line is where the read is in the template: the server reads are listed with it (boto check).
|
|
149
|
+
def read(source, locals, line)
|
|
150
|
+
scope = scope_for(locals)
|
|
151
|
+
reads = Deps.of(source, scope)
|
|
152
|
+
[@root, *@frames].each { _1.deps |= reads }
|
|
153
|
+
result = Expression.compile(source, scope)
|
|
154
|
+
return result unless result.equal?(Expression::SERVER)
|
|
155
|
+
|
|
156
|
+
mark_owner
|
|
157
|
+
@server_reads << Analysis::ServerRead.new(line:, source:)
|
|
158
|
+
result
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# The outermost each around the expression, else the innermost if, else the root.
|
|
162
|
+
def mark_owner
|
|
163
|
+
owner = @frames.find { _1.kind == :each } || @frames.last || @root
|
|
164
|
+
owner.marked = true
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def scope_for(locals) = locals.reduce(@scope) { |scope, (name, type)| scope.with_local(name, type) }
|
|
168
|
+
|
|
169
|
+
def type_of(result) = result.equal?(Expression::SERVER) ? nil : result.type
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|