Class: Envo::Context
- Inherits:
-
Object
- Object
- Envo::Context
- Defined in:
- lib/envo/context.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
-
#ask(question) ⇒ Object
io.
- #create_common_locals ⇒ Object
- #debug(text) ⇒ Object
- #error(text) ⇒ Object
-
#execute(pack) ⇒ Object
execution.
-
#expand_name(name) ⇒ Object
parse access.
- #expand_value(val) ⇒ Object
- #find_script(script) ⇒ Object
- #force? ⇒ Boolean
-
#initialize(host, log, opts, state = nil) ⇒ Context
constructor
A new instance of Context.
- #interact? ⇒ Boolean
- #load_script(path) ⇒ Object
-
#local_var_name?(name) ⇒ Boolean
local vars.
-
#new_scope(defaults = {}) ⇒ Object
create another context based on this one (same state, log, and host) which provides different opts an locals thus scripts can be executed which don’t leak values in the scope above.
- #noforce? ⇒ Boolean
- #print(text) ⇒ Object
- #puts(text) ⇒ Object
-
#raw? ⇒ Boolean
opt queries.
- #raw_get(name) ⇒ Object
- #raw_set(name, value) ⇒ Object
- #reflect_opts_change ⇒ Object
- #set_local_var(name, value) ⇒ Object
-
#smart_get(name) ⇒ Object
env access.
- #smart_set(name, value) ⇒ Object
- #unset(name) ⇒ Object
- #warn(text) ⇒ Object
Constructor Details
#initialize(host, log, opts, state = nil) ⇒ Context
Returns a new instance of Context.
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/envo/context.rb', line 3 def initialize(host, log, opts, state = nil) @host = host @log = log @default_opts = opts @opts = opts reflect_opts_change @state = state || State.new(host.env) create_common_locals end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
15 16 17 |
# File 'lib/envo/context.rb', line 15 def host @host end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
15 16 17 |
# File 'lib/envo/context.rb', line 15 def state @state end |
Instance Method Details
#ask(question) ⇒ Object
io
130 131 132 133 134 135 136 137 138 |
# File 'lib/envo/context.rb', line 130 def ask(question) return true if force? return false if noforce? print "#{question} (y/n): " answer = STDIN.gets.chomp answer.downcase! return answer == 'y' || answer == 'yes' end |
#create_common_locals ⇒ Object
80 81 82 83 84 85 |
# File 'lib/envo/context.rb', line 80 def create_common_locals @locals = { '@path' => host.shell.path_var_name, '@home' => host.shell.home_var_name, } end |
#debug(text) ⇒ Object
144 |
# File 'lib/envo/context.rb', line 144 def debug(text); @log.debug(text); end |
#error(text) ⇒ Object
140 |
# File 'lib/envo/context.rb', line 140 def error(text); @log.error(text); end |
#execute(pack) ⇒ Object
execution
147 148 149 150 151 152 153 154 |
# File 'lib/envo/context.rb', line 147 def execute(pack) pack_opts = pack.opts pack.cmds.each do |cmd| @opts = @default_opts.merge(cmd.opts, pack_opts) reflect_opts_change cmd.cmd.execute(self) end end |
#expand_name(name) ⇒ Object
parse access
55 56 57 |
# File 'lib/envo/context.rb', line 55 def (name) @locals[name] || name end |
#expand_value(val) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/envo/context.rb', line 58 def (val) if raw? if val.class == Array if val.size == 1 StringVal.new(val[0]) else ListVal.new(val) end else StringVal.new(val) end else ValBuilder.from_user_text(val, @host) end end |
#find_script(script) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/envo/context.rb', line 24 def find_script(script) if @host.shell.likely_rel_path?(script) || @host.shell.likely_abs_path?(script) return script if @host.path_exists?(script) raise Envo::Error.new "'#{script}' doesn't exist" end # look for '.envo/<file>.envoscript' script = script + '.envoscript' dir = @host.pwd found = while true check = File.join(dir, '.envo', script) break check if @host.path_exists?(check) new_dir = File.dirname(dir) break nil if new_dir == dir dir = new_dir end if !found check = File.join(@host.home, '.envo', script) found = check if @host.path_exists?(check) end raise Envo::Error.new "Can't find '#{script}' in .envo/ parent dirs or in <home>/.envo/" if !found found end |
#force? ⇒ Boolean
115 116 117 |
# File 'lib/envo/context.rb', line 115 def force? @opts[:interact] == :force end |
#interact? ⇒ Boolean
121 122 123 |
# File 'lib/envo/context.rb', line 121 def interact? @opts[:interact] == :interact end |
#load_script(path) ⇒ Object
50 51 52 |
# File 'lib/envo/context.rb', line 50 def load_script(path) File.readlines(path) end |
#local_var_name?(name) ⇒ Boolean
local vars
74 75 76 |
# File 'lib/envo/context.rb', line 74 def local_var_name?(name) name =~ /^@[a-zA-Z]/ end |
#new_scope(defaults = {}) ⇒ Object
create another context based on this one (same state, log, and host) which provides different opts an locals thus scripts can be executed which don’t leak values in the scope above
20 21 22 |
# File 'lib/envo/context.rb', line 20 def new_scope(defaults = {}) Context.new(@host, @log, @opts.merge(defaults), @state) end |
#noforce? ⇒ Boolean
118 119 120 |
# File 'lib/envo/context.rb', line 118 def noforce? @opts[:interact] == :noforce end |
#print(text) ⇒ Object
142 |
# File 'lib/envo/context.rb', line 142 def print(text); @log.print(text); end |
#puts(text) ⇒ Object
143 |
# File 'lib/envo/context.rb', line 143 def puts(text); @log.puts(text); end |
#raw? ⇒ Boolean
opt queries
112 113 114 |
# File 'lib/envo/context.rb', line 112 def raw? @opts[:raw] end |
#raw_get(name) ⇒ Object
91 92 93 |
# File 'lib/envo/context.rb', line 91 def raw_get(name) @state.get(name) end |
#raw_set(name, value) ⇒ Object
103 104 105 |
# File 'lib/envo/context.rb', line 103 def raw_set(name, value) @state.set(name, value) end |
#reflect_opts_change ⇒ Object
125 126 127 |
# File 'lib/envo/context.rb', line 125 def reflect_opts_change @log.max_level = @opts[:log_level] end |
#set_local_var(name, value) ⇒ Object
77 78 79 |
# File 'lib/envo/context.rb', line 77 def set_local_var(name, value) @locals[name] = value end |
#smart_get(name) ⇒ Object
env access
88 89 90 |
# File 'lib/envo/context.rb', line 88 def smart_get(name) ValBuilder.from_env_string(raw_get(name), @host) end |
#smart_set(name, value) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/envo/context.rb', line 95 def smart_set(name, value) if value.list? rv = @host.shell.ar_to_list(value.ar) raw_set(name, rv) else raw_set(name, value.to_env_s) end end |
#unset(name) ⇒ Object
107 108 109 |
# File 'lib/envo/context.rb', line 107 def unset(name) @state.unset(name) end |
#warn(text) ⇒ Object
141 |
# File 'lib/envo/context.rb', line 141 def warn(text); @log.warn(text); end |