boto-rb 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 +7 -0
- data/LICENSE +21 -0
- data/lib/boto/configuration.rb +113 -0
- data/lib/boto/endpoint/boolean.rb +16 -0
- data/lib/boto/endpoint/endpoint.rb +227 -0
- data/lib/boto/endpoint/params.rb +100 -0
- data/lib/boto/http/app.rb +283 -0
- data/lib/boto/http/error.rb +121 -0
- data/lib/boto/http/problem.rb +27 -0
- data/lib/boto/http/request_body.rb +59 -0
- data/lib/boto/http/router.rb +168 -0
- data/lib/boto/openapi/operation.rb +116 -0
- data/lib/boto/openapi/schema_catalog.rb +32 -0
- data/lib/boto/openapi.rb +107 -0
- data/lib/boto/plugin/stack.rb +17 -0
- data/lib/boto/plugin.rb +53 -0
- data/lib/boto/schema/compiler.rb +103 -0
- data/lib/boto/schema.rb +172 -0
- data/lib/boto/solution/boot.rb +83 -0
- data/lib/boto/solution/botofile.rb +95 -0
- data/lib/boto/solution/isolation/scanner.rb +151 -0
- data/lib/boto/solution/isolation.rb +79 -0
- data/lib/boto/solution/isolation_error.rb +56 -0
- data/lib/boto/solution/project.rb +24 -0
- data/lib/boto/solution/project_loader.rb +55 -0
- data/lib/boto/solution/solution.rb +84 -0
- data/lib/boto/types/coercion.rb +61 -0
- data/lib/boto/types/constraints.rb +121 -0
- data/lib/boto/types/enum.rb +28 -0
- data/lib/boto/types/invalid.rb +27 -0
- data/lib/boto/types/list.rb +56 -0
- data/lib/boto/types/scalar.rb +73 -0
- data/lib/boto/types/schema_ref.rb +46 -0
- data/lib/boto/types.rb +31 -0
- data/lib/boto/version.rb +5 -0
- data/lib/boto.rb +17 -0
- metadata +103 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
class Botofile
|
|
5
|
+
class InvalidError < StandardError; end
|
|
6
|
+
|
|
7
|
+
NAME_FORMAT = /\A[a-z][a-z0-9_]*\z/
|
|
8
|
+
DEFAULT_PATH = "Botofile"
|
|
9
|
+
FIRST_LINE = 1
|
|
10
|
+
|
|
11
|
+
def self.load(path)
|
|
12
|
+
parse(File.read(path), path: path)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.parse(source, path: DEFAULT_PATH)
|
|
16
|
+
dsl = DSL.new(path)
|
|
17
|
+
dsl.instance_eval(source, path, FIRST_LINE)
|
|
18
|
+
dsl.to_solution
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# BasicObject keeps the Botofile's visible surface down to `solution` and `project`.
|
|
22
|
+
class DSL < BasicObject
|
|
23
|
+
def initialize(path)
|
|
24
|
+
@path = path
|
|
25
|
+
@solution_name = nil
|
|
26
|
+
@projects = []
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def solution(name)
|
|
30
|
+
line = ::Kernel.caller_locations(1, 1).first.lineno
|
|
31
|
+
invalid!("solution declared twice", line) if @solution_name
|
|
32
|
+
name = frozen_string(name)
|
|
33
|
+
invalid!(%(invalid solution name "#{name}"), line) unless ::Boto::Botofile::NAME_FORMAT.match?(name)
|
|
34
|
+
@solution_name = name
|
|
35
|
+
nil
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def project(name, template:, refs: [])
|
|
39
|
+
line = ::Kernel.caller_locations(1, 1).first.lineno
|
|
40
|
+
name = frozen_string(name)
|
|
41
|
+
refs = normalize_refs(refs)
|
|
42
|
+
check_project(name, template, refs, line)
|
|
43
|
+
@projects << ::Boto::Project.new(name: name, template: template, refs: refs, line: line)
|
|
44
|
+
nil
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def to_solution
|
|
48
|
+
invalid!("solution name is missing") unless @solution_name
|
|
49
|
+
check_references
|
|
50
|
+
solution = ::Boto::Solution.new(name: @solution_name, projects: @projects)
|
|
51
|
+
solution.topological_order
|
|
52
|
+
solution
|
|
53
|
+
rescue ::Boto::Solution::CycleError => error
|
|
54
|
+
invalid!(error.message)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def invalid!(message, line = nil)
|
|
60
|
+
location = line ? "#{@path}:#{line}" : @path
|
|
61
|
+
::Kernel.raise ::Boto::Botofile::InvalidError, "#{location}: #{message}"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def frozen_string(value)
|
|
65
|
+
value.to_s.dup.freeze
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def normalize_refs(refs)
|
|
69
|
+
::Kernel.Array(refs).map { |ref| frozen_string(ref) }.freeze
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def check_project(name, template, refs, line)
|
|
73
|
+
invalid!(%(invalid project name "#{name}"), line) unless ::Boto::Botofile::NAME_FORMAT.match?(name)
|
|
74
|
+
invalid!("unknown template #{template.inspect}", line) unless ::Boto::Project::TEMPLATE_DIRS.key?(template)
|
|
75
|
+
invalid!(%(duplicate project "#{name}"), line) if declared?(name)
|
|
76
|
+
invalid!(%(project "#{name}" references itself), line) if refs.include?(name)
|
|
77
|
+
duplicate = refs.find { |ref| refs.count(ref) > 1 }
|
|
78
|
+
invalid!(%(duplicate reference "#{duplicate}"), line) if duplicate
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def declared?(name)
|
|
82
|
+
@projects.any? { |project| project.name == name }
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def check_references
|
|
86
|
+
names = @projects.map(&:name)
|
|
87
|
+
@projects.each do |project|
|
|
88
|
+
unknown = project.refs.find { |ref| !names.include?(ref) }
|
|
89
|
+
invalid!(%(unknown reference "#{unknown}"), project.line) if unknown
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
private_constant :DSL
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "prism"
|
|
4
|
+
require "set"
|
|
5
|
+
|
|
6
|
+
module Boto
|
|
7
|
+
module Isolation
|
|
8
|
+
# Lists the constants a Ruby file uses and declares, without running it.
|
|
9
|
+
class Scanner
|
|
10
|
+
NAMESPACE_SEPARATOR = "::"
|
|
11
|
+
|
|
12
|
+
class SyntaxError < StandardError
|
|
13
|
+
attr_reader :path, :line
|
|
14
|
+
|
|
15
|
+
def initialize(path, line, reason)
|
|
16
|
+
@path = path
|
|
17
|
+
@line = line
|
|
18
|
+
super("#{path}:#{line}: #{reason}")
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
Use = Data.define(:constant, :root, :line)
|
|
23
|
+
Declaration = Data.define(:constant, :root, :line)
|
|
24
|
+
Scan = Data.define(:used, :declared, :top_level)
|
|
25
|
+
|
|
26
|
+
def self.scan(source, path:)
|
|
27
|
+
result = Prism.parse(source)
|
|
28
|
+
raise_on_syntax_error(result, path)
|
|
29
|
+
collector = Collector.new
|
|
30
|
+
collector.visit(result.value)
|
|
31
|
+
Scan.new(used: collector.used.freeze, declared: collector.declared.freeze, top_level: collector.top_level.freeze)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Names of a constant reference such as `A::B`, or nil when the parent is an expression.
|
|
35
|
+
def self.segments(node)
|
|
36
|
+
case node
|
|
37
|
+
when Prism::ConstantReadNode then [node.name.to_s]
|
|
38
|
+
when Prism::ConstantPathNode then path_segments(node)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def self.path_segments(node)
|
|
43
|
+
name = node.name.to_s
|
|
44
|
+
return [name] if node.parent.nil?
|
|
45
|
+
|
|
46
|
+
prefix = segments(node.parent)
|
|
47
|
+
prefix && [*prefix, name]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def self.raise_on_syntax_error(result, path)
|
|
51
|
+
error = result.errors.first
|
|
52
|
+
return unless error
|
|
53
|
+
|
|
54
|
+
raise SyntaxError.new(path, error.location.start_line, error.message)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private_class_method :path_segments, :raise_on_syntax_error
|
|
58
|
+
|
|
59
|
+
class Collector < Prism::Visitor
|
|
60
|
+
attr_reader :used, :declared, :top_level
|
|
61
|
+
|
|
62
|
+
def initialize
|
|
63
|
+
super
|
|
64
|
+
@used = []
|
|
65
|
+
@declared = Set.new
|
|
66
|
+
@top_level = []
|
|
67
|
+
@depth = 0
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def visit_constant_read_node(node)
|
|
71
|
+
record_use([node.name.to_s], node.location.start_line)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Only the outermost path is a use; its inner segments are not visited again.
|
|
75
|
+
def visit_constant_path_node(node)
|
|
76
|
+
segments = Scanner.segments(node)
|
|
77
|
+
return visit(node.parent) unless segments
|
|
78
|
+
|
|
79
|
+
record_use(segments, node.location.start_line)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def visit_class_node(node)
|
|
83
|
+
declare(node.constant_path)
|
|
84
|
+
visit(node.superclass)
|
|
85
|
+
nested { visit(node.body) }
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def visit_module_node(node)
|
|
89
|
+
declare(node.constant_path)
|
|
90
|
+
nested { visit(node.body) }
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def visit_constant_write_node(node)
|
|
94
|
+
@declared << node.name.to_s
|
|
95
|
+
record_top_level([node.name.to_s], node.location.start_line)
|
|
96
|
+
visit(node.value)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# `Billing::Rate = 1` declares only `Rate`; `Billing` is still a reference.
|
|
100
|
+
def visit_constant_path_write_node(node)
|
|
101
|
+
@declared << node.target.name.to_s
|
|
102
|
+
visit(node.target.parent)
|
|
103
|
+
record_top_level(Scanner.segments(node.target), node.location.start_line)
|
|
104
|
+
visit(node.value)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
private
|
|
108
|
+
|
|
109
|
+
# At the top level the first segment names an existing namespace, so it is a use:
|
|
110
|
+
# `class Billing::Invoice` in another project reopens Billing and must not hide its uses.
|
|
111
|
+
# Inside a class/module body every segment is a nested declaration (`module Orders; module Billing`).
|
|
112
|
+
def declare(path_node)
|
|
113
|
+
segments = Scanner.segments(path_node)
|
|
114
|
+
return visit(path_node) unless segments
|
|
115
|
+
|
|
116
|
+
line = path_node.location.start_line
|
|
117
|
+
if top_level?
|
|
118
|
+
record_use([segments.first], line)
|
|
119
|
+
@declared.merge(segments.drop(1))
|
|
120
|
+
record_top_level(segments, line)
|
|
121
|
+
else
|
|
122
|
+
@declared.merge(segments)
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def top_level?
|
|
127
|
+
@depth.zero?
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Constants that land on Object at the top level; dynamic paths (nil segments) are not recorded.
|
|
131
|
+
def record_top_level(segments, line)
|
|
132
|
+
return unless segments && top_level?
|
|
133
|
+
|
|
134
|
+
@top_level << Declaration.new(constant: segments.join(NAMESPACE_SEPARATOR), root: segments.first, line: line)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def nested
|
|
138
|
+
@depth += 1
|
|
139
|
+
yield
|
|
140
|
+
ensure
|
|
141
|
+
@depth -= 1
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def record_use(segments, line)
|
|
145
|
+
@used << Use.new(constant: segments.join(NAMESPACE_SEPARATOR), root: segments.first, line: line)
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
private_constant :Collector
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "set"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
# Checks that each project only uses the namespaces of projects it references directly.
|
|
7
|
+
module Isolation
|
|
8
|
+
Violation = Data.define(:project, :path, :line, :constant, :missing_ref)
|
|
9
|
+
NamespaceViolation = Data.define(:project, :path, :line, :constant, :namespace)
|
|
10
|
+
|
|
11
|
+
SOURCE_DIRS = %w[endpoints operations models schemas config test].freeze
|
|
12
|
+
RUBY_GLOB = "**/*.rb"
|
|
13
|
+
TEST_DIR = "test"
|
|
14
|
+
SORT_KEYS = ->(violation) { [violation.project, violation.path, violation.line] }
|
|
15
|
+
|
|
16
|
+
def self.check(solution, root:, projects: nil)
|
|
17
|
+
owners = namespace_owners(solution)
|
|
18
|
+
selected = projects ? projects.map { solution.project(_1) } : solution.projects
|
|
19
|
+
selected.flat_map { |project| project_violations(project, owners, root) }.sort_by(&SORT_KEYS)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.namespace_owners(solution)
|
|
23
|
+
solution.projects.to_h { [_1.namespace_name, _1.name] }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.project_violations(project, owners, root)
|
|
27
|
+
scans = scan_project(project, root)
|
|
28
|
+
reference_violations(project, owners, scans) + namespace_violations(project, scans)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.reference_violations(project, owners, scans)
|
|
32
|
+
declared = scans.flat_map { |_path, scan| scan.declared.to_a }.to_set
|
|
33
|
+
allowed = [project.name, *project.refs].to_set
|
|
34
|
+
|
|
35
|
+
scans.flat_map do |path, scan|
|
|
36
|
+
scan.used.filter_map do |use|
|
|
37
|
+
owner = owners[use.root]
|
|
38
|
+
next if owner.nil? || allowed.include?(owner) || declared.include?(use.root)
|
|
39
|
+
|
|
40
|
+
Violation.new(project: project.name, path: path, line: use.line, constant: use.constant, missing_ref: owner)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Test files run in their own process, so their top-level constants never reach the shared Object.
|
|
46
|
+
def self.namespace_violations(project, scans)
|
|
47
|
+
scans.flat_map do |path, scan|
|
|
48
|
+
next [] if test_file?(project, path)
|
|
49
|
+
|
|
50
|
+
scan.top_level.filter_map do |declaration|
|
|
51
|
+
next if declaration.root == project.namespace_name
|
|
52
|
+
|
|
53
|
+
NamespaceViolation.new(project: project.name, path: path, line: declaration.line,
|
|
54
|
+
constant: declaration.constant, namespace: project.namespace_name)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def self.test_file?(project, path)
|
|
60
|
+
path.start_with?(File.join(project.path, TEST_DIR, ""))
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Paths are relative to the solution root, sorted, and only for directories that exist.
|
|
64
|
+
def self.scan_project(project, root)
|
|
65
|
+
source_paths(project, root).map do |path|
|
|
66
|
+
[path, Scanner.scan(File.read(File.join(root, path)), path: path)]
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def self.source_paths(project, root)
|
|
71
|
+
SOURCE_DIRS.flat_map do |dir|
|
|
72
|
+
Dir.glob(RUBY_GLOB, base: File.join(root, project.path, dir)).map { File.join(project.path, dir, _1) }
|
|
73
|
+
end.sort
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private_class_method :namespace_owners, :project_violations, :reference_violations, :namespace_violations,
|
|
77
|
+
:test_file?, :scan_project, :source_paths
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
class IsolationError < StandardError
|
|
5
|
+
FIX_PREFIX = "Fix: boto ref"
|
|
6
|
+
NAMESPACE_FIX_PREFIX = "Fix: move them under"
|
|
7
|
+
|
|
8
|
+
attr_reader :violations
|
|
9
|
+
|
|
10
|
+
def initialize(violations)
|
|
11
|
+
@violations = violations.dup.freeze
|
|
12
|
+
super(build_message(violations))
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def build_message(violations)
|
|
18
|
+
violations.group_by(&:project).flat_map { |project, items| project_blocks(project, items) }.join("\n")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def project_blocks(project, items)
|
|
22
|
+
reference, namespace = items.partition { _1.is_a?(Isolation::Violation) }
|
|
23
|
+
blocks = [reference_block(project, reference), namespace_block(project, namespace)]
|
|
24
|
+
blocks.compact
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def reference_block(project, items)
|
|
28
|
+
return if items.empty?
|
|
29
|
+
|
|
30
|
+
header = %(project "#{project}" uses projects it does not reference:)
|
|
31
|
+
lines = [header, *items.map { violation_line(_1) }, *fix_lines(project, items)]
|
|
32
|
+
lines.join("\n")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def namespace_block(project, items)
|
|
36
|
+
return if items.empty?
|
|
37
|
+
|
|
38
|
+
namespace = items.first.namespace
|
|
39
|
+
header = %(project "#{project}" declares constants outside its namespace #{namespace}:)
|
|
40
|
+
lines = [header, *items.map { namespace_line(_1) }, "#{NAMESPACE_FIX_PREFIX} #{namespace}::"]
|
|
41
|
+
lines.join("\n")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def namespace_line(violation)
|
|
45
|
+
" #{violation.path}:#{violation.line} #{violation.constant}"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def violation_line(violation)
|
|
49
|
+
" #{violation.path}:#{violation.line} #{violation.constant} (#{violation.missing_ref} is not in refs)"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def fix_lines(project, items)
|
|
53
|
+
items.map(&:missing_ref).uniq.map { "#{FIX_PREFIX} #{project} #{_1}" }
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
class Project < Data.define(:name, :template, :refs, :line)
|
|
5
|
+
TEMPLATE_DIRS = {
|
|
6
|
+
library: "libs",
|
|
7
|
+
webapi: "services",
|
|
8
|
+
microservice: "services",
|
|
9
|
+
worker: "workers",
|
|
10
|
+
web: "apps",
|
|
11
|
+
spa: "apps"
|
|
12
|
+
}.freeze
|
|
13
|
+
PATH_SEPARATOR = "/"
|
|
14
|
+
WORD_SEPARATOR = "_"
|
|
15
|
+
|
|
16
|
+
def path
|
|
17
|
+
[TEMPLATE_DIRS.fetch(template), name].join(PATH_SEPARATOR)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def namespace_name
|
|
21
|
+
name.split(WORD_SEPARATOR).map(&:capitalize).join
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "zeitwerk"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
# Loads one project's code directories directly under its namespace, using a Zeitwerk
|
|
7
|
+
# loader of its own so each project can be set up and unloaded independently.
|
|
8
|
+
class ProjectLoader
|
|
9
|
+
CODE_DIRS = %w[endpoints operations models schemas].freeze
|
|
10
|
+
|
|
11
|
+
attr_reader :namespace
|
|
12
|
+
|
|
13
|
+
def initialize(project, root:)
|
|
14
|
+
@project = project
|
|
15
|
+
@root = root
|
|
16
|
+
@namespace = resolve_namespace(project.namespace_name)
|
|
17
|
+
@loader = Zeitwerk::Loader.new
|
|
18
|
+
@loader.tag = project.name
|
|
19
|
+
@set_up = false
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def setup
|
|
23
|
+
return self if @set_up
|
|
24
|
+
|
|
25
|
+
code_dirs.each { |dir| @loader.push_dir(dir, namespace: namespace) }
|
|
26
|
+
@loader.setup
|
|
27
|
+
@set_up = true
|
|
28
|
+
self
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def eager_load
|
|
32
|
+
setup
|
|
33
|
+
@loader.eager_load
|
|
34
|
+
self
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def unload
|
|
38
|
+
@loader.unload
|
|
39
|
+
@loader.unregister
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def resolve_namespace(name)
|
|
45
|
+
return Object.const_get(name, false) if Object.const_defined?(name, false)
|
|
46
|
+
|
|
47
|
+
Object.const_set(name, Module.new)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def code_dirs
|
|
51
|
+
base = File.join(@root, @project.path)
|
|
52
|
+
CODE_DIRS.map { |dir| File.join(base, dir) }.select { |dir| File.directory?(dir) }
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
class Solution
|
|
5
|
+
class CycleError < StandardError
|
|
6
|
+
SEPARATOR = " -> "
|
|
7
|
+
|
|
8
|
+
attr_reader :path
|
|
9
|
+
|
|
10
|
+
def initialize(path)
|
|
11
|
+
@path = path.freeze
|
|
12
|
+
super("cycle detected: #{path.join(SEPARATOR)}")
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
VISITING = :visiting
|
|
17
|
+
VISITED = :visited
|
|
18
|
+
|
|
19
|
+
attr_reader :name, :projects
|
|
20
|
+
|
|
21
|
+
def initialize(name:, projects:)
|
|
22
|
+
@name = name.dup.freeze
|
|
23
|
+
@projects = projects.dup.freeze
|
|
24
|
+
@index = projects.to_h { |item| [item.name, item] }.freeze
|
|
25
|
+
freeze
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def project(name)
|
|
29
|
+
@index.fetch(name) { raise KeyError, "unknown project \"#{name}\"" }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def topological_order
|
|
33
|
+
state = {}
|
|
34
|
+
order = []
|
|
35
|
+
@projects.each { |item| visit(item.name, state, [], order) }
|
|
36
|
+
order.map { |item_name| project(item_name) }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def dependencies_of(name)
|
|
40
|
+
reachable = reachable_names(project(name).refs)
|
|
41
|
+
topological_order.map(&:name).select { |item_name| reachable.include?(item_name) }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def dependents_of(name)
|
|
45
|
+
project(name)
|
|
46
|
+
@projects.map(&:name).select do |item_name|
|
|
47
|
+
item_name != name && dependencies_of(item_name).include?(name)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
def visit(name, state, stack, order)
|
|
54
|
+
case state[name]
|
|
55
|
+
when VISITED then return
|
|
56
|
+
when VISITING then raise CycleError, cycle_from(stack, name)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
state[name] = VISITING
|
|
60
|
+
stack.push(name)
|
|
61
|
+
project(name).refs.each { |ref| visit(ref, state, stack, order) }
|
|
62
|
+
stack.pop
|
|
63
|
+
state[name] = VISITED
|
|
64
|
+
order << name
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def cycle_from(stack, name)
|
|
68
|
+
stack.drop(stack.index(name)) + [name]
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def reachable_names(refs)
|
|
72
|
+
pending = refs.dup
|
|
73
|
+
reached = []
|
|
74
|
+
until pending.empty?
|
|
75
|
+
ref = pending.shift
|
|
76
|
+
next if reached.include?(ref)
|
|
77
|
+
|
|
78
|
+
reached << ref
|
|
79
|
+
pending.concat(project(ref).refs)
|
|
80
|
+
end
|
|
81
|
+
reached
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "date"
|
|
4
|
+
require "time"
|
|
5
|
+
|
|
6
|
+
module Boto
|
|
7
|
+
module Types
|
|
8
|
+
# Pure coercion functions. Each returns the coerced value, or nil when the input does not fit.
|
|
9
|
+
# nil is a safe failure marker because no supported type coerces to nil.
|
|
10
|
+
module Coercion
|
|
11
|
+
INTEGER_FORMAT = /\A-?\d+\z/
|
|
12
|
+
DATE_FORMAT = /\A\d{4}-\d{2}-\d{2}\z/
|
|
13
|
+
TIME_FORMAT = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})\z/
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
def string(value)
|
|
18
|
+
value if value.is_a?(String)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def integer(value)
|
|
22
|
+
case value
|
|
23
|
+
when Integer then value
|
|
24
|
+
when String then value.to_i if INTEGER_FORMAT.match?(value)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def float(value)
|
|
29
|
+
case value
|
|
30
|
+
when Integer, Float then value.to_f
|
|
31
|
+
when String then Float(value, exception: false)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def boolean(value)
|
|
36
|
+
Boto::Boolean.coerce(value)
|
|
37
|
+
rescue ArgumentError
|
|
38
|
+
nil
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def date(value)
|
|
42
|
+
case value
|
|
43
|
+
when Date then value
|
|
44
|
+
when String then Date.iso8601(value) if DATE_FORMAT.match?(value)
|
|
45
|
+
end
|
|
46
|
+
rescue ArgumentError
|
|
47
|
+
nil
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Time.iso8601 accepts a missing offset (local time), so the zone is checked by the pattern first.
|
|
51
|
+
def time(value)
|
|
52
|
+
case value
|
|
53
|
+
when Time then value
|
|
54
|
+
when String then Time.iso8601(value) if TIME_FORMAT.match?(value)
|
|
55
|
+
end
|
|
56
|
+
rescue ArgumentError
|
|
57
|
+
nil
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|