Class: Shog::Context
Overview
An instance of this class is visible to our build scripts with name @shog
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#default_target ⇒ Object
Returns the value of attribute default_target.
-
#rule ⇒ Object
Returns the value of attribute rule.
Instance Method Summary collapse
- #bind ⇒ Object
- #cwd(src) ⇒ Object
- #deep_clone ⇒ Object
- #emit(rule_id, src, params = {}) ⇒ Object
- #emit_each(rule_id, srcs, params = {}) ⇒ Object
-
#initialize(backend, emitter) ⇒ Context
constructor
A new instance of Context.
- #register_rule(type) ⇒ Object
- #visit(dirs) ⇒ Object
- #visit_dir(dir, new_ctx = true) ⇒ Object
Constructor Details
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
7 8 9 |
# File 'lib/context.rb', line 7 def config @config end |
#default_target ⇒ Object
Returns the value of attribute default_target.
7 8 9 |
# File 'lib/context.rb', line 7 def default_target @default_target end |
#rule ⇒ Object
Returns the value of attribute rule.
7 8 9 |
# File 'lib/context.rb', line 7 def rule @rule end |
Instance Method Details
#bind ⇒ Object
24 25 26 |
# File 'lib/context.rb', line 24 def bind binding end |
#deep_clone ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/context.rb', line 78 def deep_clone ctx = Context.new(@backend, @emitter) ctx.rule = @rule.deep_clone ctx.rule[:generate_build].deps = @rule[:generate_build].deps # deps are global across the build ctx.default_target = @default_target.deep_clone ctx.config = @config ctx end |
#emit(rule_id, src, params = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/context.rb', line 56 def emit(rule_id, src, params = {}) r = @rule[rule_id] raise "Rule #{rule_id} is not registered" unless r params[:input] = PathSet.make(src) target = r.target(params) # fix path to inputs, outputs, includes @emitter.emit(target) return target[:output] end |
#emit_each(rule_id, srcs, params = {}) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/context.rb', line 68 def emit_each(rule_id, srcs, params = {}) out = [] for s in srcs p = params.dup p[:input] = s out += emit(rule_id, s, p) end return out end |
#register_rule(type) ⇒ Object
18 19 20 21 22 |
# File 'lib/context.rb', line 18 def register_rule(type) r = type.new @emitter.rule(r) @rule[r.id] = r end |
#visit(dirs) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/context.rb', line 44 def visit(dirs) case dirs when String then visit_dir(dirs) when Array then dirs.map { |d| visit_dir(d) }.flatten else raise "Unknown object type for dirs: #{dirs.class.name}" end end |
#visit_dir(dir, new_ctx = true) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/context.rb', line 28 def visit_dir(dir, new_ctx = true) old_pwd = Path.pwd ctx = new_ctx ? deep_clone() : self Path.pwd = File.join(Path.pwd, dir) script = cwd("shog.build") @rule[:generate_build].deps << script script_name = script.path build_script = File.read(script_name) out = ctx.bind.eval(build_script, script_name) Path.pwd = old_pwd return out end |