Class: Rant::Plugin::Configure
- Includes:
- Console, Rant::PluginMethods
- Defined in:
- lib/rant/plugin/configure.rb
Overview
Startup of configure plugin
Config file exists
The configuration file will be read and the data hash set up accordingly.
Config file doesn’t exist
The configuration process is run with init_modes
which has to be one of CHECK_MODES. init_modes
defaults to :default, which means if the configfile doesn’t exist, all values will be set to their defaults on startup.
Access to configuration in Rantfile
You can access all configuration values through the []
and []=
operators of the configure plugin.
Example of configure in Rantfile:
conf = plugin :Configure do |conf|
conf.task # define a task named :configure
conf.check "profile" do |c|
c.default “full” c.guess { ENV } c.interact { conf.prompt “What build-profile should be used?” }
end
conf.check "optimize" do |c|
c.default true
c.guess { ENV["OPTIMIZE"] }
c.interact {
conf.ask_yes_no "Optimize build?"
}
end
end
# Set default target depending on profile:
task :default => conf["profile"]
Constant Summary collapse
- CHECK_MODES =
[ :default, :env, :guess, :interact, ]
Instance Attribute Summary collapse
-
#checklist ⇒ Object
readonly
An array with all checks to perform.
-
#file ⇒ Object
Name of configuration file.
-
#init_modes ⇒ Object
Decide what the configure plugin does on startup if the configuration file doesn’t exist.
-
#modified ⇒ Object
This flag is used to determine if data has changed and should be saved to file.
-
#name ⇒ Object
readonly
Name for this plugin instance.
-
#no_action_list ⇒ Object
readonly
Don’t read or write to configuration file nor run
guess
orinteract
blocks if first target given on commandline is in this list. -
#no_write ⇒ Object
Don’t write to file, config values will be lost when rant exits!.
-
#override_modes ⇒ Object
Decide what the configure plugin does after reading the configuration file (or directly after running
init_modes
if the configuration file doesn’t exist).
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get the value for
key
fromchecklist
ornil
if there isn’t a check with the givenkey
. -
#[]=(key, val) ⇒ Object
Creates new check with default value if key doesn’t exist.
- #check(key, val = nil, &block) ⇒ Object
-
#configured? ⇒ Boolean
This is true, if either a configure task was run, or the configuration file was read.
-
#data ⇒ Object
Builds a hash with all key-value pairs from checklist.
- #init(modes = @init_modes) ⇒ Object
-
#initialize(app, name = nil) {|_self| ... } ⇒ Configure
constructor
A new instance of Configure.
- #rant_plugin_init ⇒ Object
- #rant_plugin_name ⇒ Object
- #rant_plugin_stop ⇒ Object
-
#rant_plugin_type ⇒ Object
overriden plugin methods ##############################.
-
#run_checklist(modes = [:guess, :interact]) ⇒ Object
Run the configure process in the given modes.
-
#save ⇒ Object
Write configuration if modified.
-
#set_if_exists(key, value) ⇒ Object
Sets the specified check if a check with the given key exists.
-
#task(name = nil, check_modes = [:guess, :interact]) ⇒ Object
Define a task with
name
that will run the configuration process in the givencheck_modes
. -
#write ⇒ Object
Immediately write configuration to
file
.
Methods included from Rant::PluginMethods
#rant_done, #rant_plugin?, #rant_quit, #rant_start
Constructor Details
#initialize(app, name = nil) {|_self| ... } ⇒ Configure
Returns a new instance of Configure.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/rant/plugin/configure.rb', line 105 def initialize(app, name = nil) @name = name || rant_plugin_type @app = app or raise ArgumentError, "no application given" @file = "config" @checklist = [] @init_modes = [:guess] @override_modes = [:env] @no_write = false @modified = false @no_action_list = ["distclean", "clobber", "clean"] @no_action = false @configured = false yield self if block_given? run_checklist([:default]) # we don't need to save our defaults @modified = false end |
Instance Attribute Details
#checklist ⇒ Object (readonly)
An array with all checks to perform.
76 77 78 |
# File 'lib/rant/plugin/configure.rb', line 76 def checklist @checklist end |
#file ⇒ Object
Name of configuration file.
69 70 71 |
# File 'lib/rant/plugin/configure.rb', line 69 def file @file end |
#init_modes ⇒ Object
Decide what the configure plugin does on startup if the configuration file doesn’t exist. Initialized to [:guess]
.
If you want to control when the plugin should initialize the configuration values, set this to [:explicit]
and call the init
method with the init_modes you like as argument.
85 86 87 |
# File 'lib/rant/plugin/configure.rb', line 85 def init_modes @init_modes end |
#modified ⇒ Object
This flag is used to determine if data has changed and should be saved to file.
73 74 75 |
# File 'lib/rant/plugin/configure.rb', line 73 def modified @modified end |
#name ⇒ Object (readonly)
Name for this plugin instance. Defaults to “configure”.
66 67 68 |
# File 'lib/rant/plugin/configure.rb', line 66 def name @name end |
#no_action_list ⇒ Object (readonly)
Don’t read or write to configuration file nor run guess
or interact
blocks if first target given on commandline is in this list. This is usefull for targets that remove the configuration file. Defaults are “distclean”, “clobber” and “clean”.
103 104 105 |
# File 'lib/rant/plugin/configure.rb', line 103 def no_action_list @no_action_list end |
#no_write ⇒ Object
Don’t write to file, config values will be lost when rant exits!
96 97 98 |
# File 'lib/rant/plugin/configure.rb', line 96 def no_write @no_write end |
#override_modes ⇒ Object
Decide what the configure plugin does after reading the configuration file (or directly after running init_modes
if the configuration file doesn’t exist). Initialized to [:env]
, probably the only usefull value.
92 93 94 |
# File 'lib/rant/plugin/configure.rb', line 92 def override_modes @override_modes end |
Class Method Details
.rant_plugin_new(app, cinf, *args, &block) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/rant/plugin/configure.rb', line 49 def rant_plugin_new(app, cinf, *args, &block) if args.size > 1 app.abort(app.pos_text(cinf[:file], cinf[:ln]), "Configure plugin takes only one argument.") end self.new(app, args.first, &block) end |
Instance Method Details
#[](key) ⇒ Object
Get the value for key
from checklist
or nil
if there isn’t a check with the given key
.
127 128 129 130 |
# File 'lib/rant/plugin/configure.rb', line 127 def [](key) c = checklist.find { |c| c.key == key } c ? c.value : nil end |
#[]=(key, val) ⇒ Object
Creates new check with default value if key doesn’t exist.
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/rant/plugin/configure.rb', line 133 def []=(key, val) c = checklist.find { |c| c.key == key } if c if c.value != val c.value = val @modified = true end else self.check(key) { |c| c.default val } end end |
#check(key, val = nil, &block) ⇒ Object
202 203 204 |
# File 'lib/rant/plugin/configure.rb', line 202 def check(key, val = nil, &block) checklist << ConfigureCheck.new(key, val, &block) end |
#configured? ⇒ Boolean
This is true, if either a configure task was run, or the configuration file was read.
171 172 173 |
# File 'lib/rant/plugin/configure.rb', line 171 def configured? @configured end |
#data ⇒ Object
Builds a hash with all key-value pairs from checklist.
161 162 163 164 165 166 167 |
# File 'lib/rant/plugin/configure.rb', line 161 def data hsh = {} @checklist.each { |c| hsh[c.key] = c.value } hsh end |
#init(modes = @init_modes) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/rant/plugin/configure.rb', line 206 def init modes = @init_modes if modes == [:explicit] modes = [:guess] end @no_action = @no_action_list.include? @app.cmd_targets.first @no_action ||= @app[:tasks] unless @no_action init_config modes @configured = true end end |
#rant_plugin_init ⇒ Object
248 249 250 |
# File 'lib/rant/plugin/configure.rb', line 248 def rant_plugin_init init unless @init_modes == [:explicit] end |
#rant_plugin_name ⇒ Object
244 245 246 |
# File 'lib/rant/plugin/configure.rb', line 244 def rant_plugin_name @name end |
#rant_plugin_stop ⇒ Object
252 253 254 |
# File 'lib/rant/plugin/configure.rb', line 252 def rant_plugin_stop @no_action || save end |
#rant_plugin_type ⇒ Object
overriden plugin methods ##############################
240 241 242 |
# File 'lib/rant/plugin/configure.rb', line 240 def rant_plugin_type "configure" end |
#run_checklist(modes = [:guess, :interact]) ⇒ Object
Run the configure process in the given modes.
219 220 221 222 223 224 |
# File 'lib/rant/plugin/configure.rb', line 219 def run_checklist(modes = [:guess, :interact]) @checklist.each { |c| c.run_check(modes) } @modified = true end |
#save ⇒ Object
Write configuration if modified.
227 228 229 230 231 |
# File 'lib/rant/plugin/configure.rb', line 227 def save return if @no_write write_yaml if @modified true end |
#set_if_exists(key, value) ⇒ Object
Sets the specified check if a check with the given key exists. Returns the value if it was set, nil otherwise.
150 151 152 153 154 155 156 157 158 |
# File 'lib/rant/plugin/configure.rb', line 150 def set_if_exists(key, value) c = checklist.find { |c| c.key == key } if c c.value = value @modified = true else nil end end |
#task(name = nil, check_modes = [:guess, :interact]) ⇒ Object
Define a task with name
that will run the configuration process in the given check_modes
. If no task name is given or it is nil
, the plugin name will be used as task name.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/rant/plugin/configure.rb', line 178 def task(name = nil, check_modes = [:guess, :interact]) name ||= @name cinf = ::Rant::Lib.parse_caller_elem(caller[0]) file = cinf[:file] ln = cinf[:ln] || 0 if !Array === check_modes || check_modes.empty? @app.abort(@app.pos_text(file, ln), "check_modes given to configure task has to be an array", "containing at least one CHECK_MODE symbol") end check_modes.each { |cm| unless CHECK_MODES.include? cm @app.abort(@app.pos_text(file,ln), "Unknown checkmode `#{cm.to_s}'.") end } nt = @app.task(name) { |t| run_checklist(check_modes) save @configured = true } nt end |
#write ⇒ Object
Immediately write configuration to file
.
234 235 236 237 |
# File 'lib/rant/plugin/configure.rb', line 234 def write write_yaml @modified = false end |