Class: Rabal::Application

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/rabal/application.rb

Overview

The Rabal application

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

logger, logger=

Constructor Details

#initialize(stdin = $stdin, stdout = $stdout, stderr = $stderr) ⇒ Application

Returns a new instance of Application.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rabal/application.rb', line 24

def initialize(stdin = $stdin, stdout = $stdout, stderr = $stderr)
    @stdin = stdin
    @stdout = stdout
    @stderr = stderr

    @main = nil
    @plugin_manager = GemPlugin::Manager.instance
    @plugin_option_names = []
    @global_option_names = %w[directory use-all logfile verbosity version help]
    @plugin_load_option_names = []
    @app_argv = []
    setup_plugins
end

Instance Attribute Details

#app_argvObject

Returns the value of attribute app_argv.



16
17
18
# File 'lib/rabal/application.rb', line 16

def app_argv
  @app_argv
end

#global_option_namesObject

Returns the value of attribute global_option_names.



15
16
17
# File 'lib/rabal/application.rb', line 15

def global_option_names
  @global_option_names
end

#mainObject

Use Ara’s awesome main gem to deal with command line parsing



93
94
95
# File 'lib/rabal/application.rb', line 93

def main
  @main
end

#plugin_load_option_namesObject

Returns the value of attribute plugin_load_option_names.



13
14
15
# File 'lib/rabal/application.rb', line 13

def plugin_load_option_names
  @plugin_load_option_names
end

#plugin_managerObject

Returns the value of attribute plugin_manager.



12
13
14
# File 'lib/rabal/application.rb', line 12

def plugin_manager
  @plugin_manager
end

#plugin_option_namesObject

Returns the value of attribute plugin_option_names.



14
15
16
# File 'lib/rabal/application.rb', line 14

def plugin_option_names
  @plugin_option_names
end

#stderrObject (readonly)

Returns the value of attribute stderr.



21
22
23
# File 'lib/rabal/application.rb', line 21

def stderr
  @stderr
end

#stdinObject (readonly)

used for testing mainly



19
20
21
# File 'lib/rabal/application.rb', line 19

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



20
21
22
# File 'lib/rabal/application.rb', line 20

def stdout
  @stdout
end

Instance Method Details

#logfile_and_level_if_necessaryObject

if the params for the logfile were given then open them up and



237
238
239
240
241
242
243
# File 'lib/rabal/application.rb', line 237

def logfile_and_level_if_necessary
    if main.params['logfile'].given? then
        Log.logger = main.params["logfile"].value
    end
    Log.logger.level = ::Logger::SEV_LABEL.index(main.params['verbosity'].value)
    Log.debug "Logger initialized"
end

#params_for_plugin(plugin) ⇒ Object

return a hash of all the options for a particular plugin with the plugins name removed from the front



226
227
228
229
230
231
232
# File 'lib/rabal/application.rb', line 226

def params_for_plugin(plugin)
    plugin_hash = {}
    main.parameters.select{|p| p.type == :option and p.name =~ %r{^#{plugin.use_name}}}.each do |p|
        plugin_hash[p.name.gsub("#{plugin.use_name}-",'')] = p.value
    end
    plugin_hash
end

#plugin_resource(gem_name, resource_path) ⇒ Object

Find a resource associated with the given gem



202
203
204
# File 'lib/rabal/application.rb', line 202

def plugin_resource(gem_name,resource_path)
    plugin_manager.resource(gem_name,resource_path)
end

#rabalizeObject

Get down and do stuff. Now that all the options have been parsed, plugins loaded, some activate, etc.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rabal/application.rb', line 160

def rabalize
    logfile_and_level_if_necessary

    # save our current directory for returning
    pwd = Dir.pwd
    begin
        Log.debug("Loading plugins")

        # create the core plugin to start things off
        core_params             = params_for_plugin(Rabal::Plugin::Core)
        core_params[:project]   = main.params[:project].value
        core                    = Rabal::Plugin::Core.new(core_params)

        using_plugins.each do |p|
            next if p == Rabal::Plugin::Core
            Log.debug("processing #{p.name} plugin")
            pi = p.new(params_for_plugin(p))
            Log.debug("getting tree #{pi.tree.inspect}")
            core.tree << pi.tree
        end

        # not using chdir blocks, as that raises
        # warning: conflicting chdir during another chdir block
        # hence our saving of pwd before begin
        Dir.chdir(File.expand_path(main.params[:directory].value))

        core.tree.process
    rescue ::Rabal::StandardError => rse
        stderr.puts "Application Error: #{rse.message}"
        exit 1
    rescue Interrupt => ie
        stderr.puts
        stderr.puts "Interrupted"
        exit 1
    ensure
        Dir.chdir(pwd)
    end
end

#run(in_argv = ARGV) ⇒ Object

Invoke main to do its thing and kick off the application Main keeps a reference to the array that it originally started with when it was created. So you have to have that first, then fill it up later.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rabal/application.rb', line 139

def run(in_argv = ARGV)
    app_argv.clear
    app_argv.concat in_argv.dup
    begin
        # create a separate usage object for rabal that can hook
        # in and figure out what parameters were given on the
        # command line
        u = Rabal::Usage.new(self)
        main.usage u
        main.run 
    rescue ::Main::Parameter::Error => mpe
        stderr.puts "Parameter Error: #{File.basename($0)}: #{mpe.message}"
        stderr.puts "Try `#{File.basename($0)} --help' for more information."
        exit 1
    end
end

#setup_pluginsObject

Load any and all plugins that are available. This includes built in plugins and those that are accessible via gems.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rabal/application.rb', line 42

def setup_plugins 
    plugin_manager.load "rabal" => GemPlugin::INCLUDE

    # make sure that we are listed as a plugin generally this is
    # only going to happen if run from outside a gem.  Basically
    # while Jeremy is testing. 
    if not plugin_manager.loaded?("rabal") then
        plugin_manager.gems["rabal"] = Rabal.root_dir
    end


    # Each plugin has its own options so iterate over the
    # available plugins, and create an instance of Main for each
    # plugin.  The options for the plugin will be merged with
    # the global options, and depending on the --load-<plugin>
    # options indicated, they will alter the --help

    plugin_manager.plugins.each do |category,plugins|
        plugins.each do |key,plugin|
            plugin_load_name = plugin.name.split("::").last.downcase.dashify

            # add the --load-<plugin> option to the global
            # options
            p_use_name = "use-#{plugin_load_name}"
            plugin_load_option_names << p_use_name

            # local variable to build up info from within eval.
            pons = []

            main.class.class_eval { 
                option(p_use_name) { 
                    description "Use plugin #{plugin.name}" 
                }

                # add in the plugin options.  Although these are
                # present in the global main, they won't
                # actually get displayed in the --help
                plugin.parameters.each do |pname,pconf|
                    p_option_name = "#{plugin_load_name}-#{pconf[:name]}=[p]"
                    pons << p_option_name
                    option(p_option_name) { description pconf[:desc] } 
                end
            } 
            plugin_option_names.concat(pons)
        end
    end
end

#using_pluginsObject

return an array of all the plugin classes being used



209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/rabal/application.rb', line 209

def using_plugins
    using = []
    plugin_manager.plugins.each do |cat,plugins|
        plugins.each do |key,plugin|
            if main.parameters['use-all'].given? or plugin.use_always? or 
                main.parameters["use-#{plugin.use_name}"].given? then
                using << plugin
            end
        end
    end
    using
end