Class: BuildTool::Application
- Defined in:
- lib/build-tool/application.rb
Overview
The application class
Instance Attribute Summary collapse
-
#application_root ⇒ Object
readonly
Returns the value of attribute application_root.
-
#cli ⇒ Object
readonly
Returns the value of attribute cli.
-
#configuration ⇒ Object
Load the configuration.
-
#database ⇒ Object
readonly
Get the database handle.
-
#local_settings_file_path ⇒ Object
readonly
Returns the value of attribute local_settings_file_path.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#close_database ⇒ Object
Close the database.
- #has_recipe? ⇒ Boolean
-
#initialize(name, application_root) ⇒ Application
constructor
A new instance of Application.
-
#local_configuration_dir ⇒ Object
Return the local configuration dir.
- #main(args) ⇒ Object
-
#open_database(name) ⇒ Object
Open the database.
- #recipe ⇒ Object
Methods inherited from Singleton
#destroy, inherited, instance, #instance
Constructor Details
#initialize(name, application_root) ⇒ Application
Returns a new instance of Application.
36 37 38 39 40 41 42 |
# File 'lib/build-tool/application.rb', line 36 def initialize( name, application_root ) super() @application_root = Pathname.new( application_root ). @database = nil @name = File.basename( name ).sub( "-build", "" ) @local_settings_file_path = local_configuration_dir.join("#{@name}.yaml") end |
Instance Attribute Details
#application_root ⇒ Object (readonly)
Returns the value of attribute application_root.
25 26 27 |
# File 'lib/build-tool/application.rb', line 25 def application_root @application_root end |
#cli ⇒ Object (readonly)
Returns the value of attribute cli.
26 27 28 |
# File 'lib/build-tool/application.rb', line 26 def cli @cli end |
#configuration ⇒ Object
Load the configuration
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/build-tool/application.rb', line 45 def configuration return @configuration if @configuration require 'build-tool/configuration' require 'build-tool/recipe' # Read the configuration from the database @configuration = BuildToolConfiguration.new( name, local_settings_file_path ) # Finished. @configuration end |
#database ⇒ Object (readonly)
Get the database handle
91 92 93 |
# File 'lib/build-tool/application.rb', line 91 def database @database end |
#local_settings_file_path ⇒ Object (readonly)
Returns the value of attribute local_settings_file_path.
28 29 30 |
# File 'lib/build-tool/application.rb', line 28 def local_settings_file_path @local_settings_file_path end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
29 30 31 |
# File 'lib/build-tool/application.rb', line 29 def name @name end |
Instance Method Details
#close_database ⇒ Object
Close the database
84 85 86 87 88 |
# File 'lib/build-tool/application.rb', line 84 def close_database ActiveRecord::Base.remove_connection() path = local_configuration_dir.join( "#{name}.db" ) logger.debug "Closing the database #{path}." end |
#has_recipe? ⇒ Boolean
117 118 119 |
# File 'lib/build-tool/application.rb', line 117 def has_recipe? not BuildTool::Application.instance.name.start_with? "build-tool" end |
#local_configuration_dir ⇒ Object
Return the local configuration dir. Makes sure the directory exists before returning it
60 61 62 63 64 65 66 67 |
# File 'lib/build-tool/application.rb', line 60 def local_configuration_dir return @local_configuration_dir if @local_configuration_dir @local_configuration_dir = Pathname.new( "~/.build-tool" ). if !@local_configuration_dir.exist? FileUtils.mkdir_p( @local_configuration_dir ) end @local_configuration_dir end |
#main(args) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/build-tool/application.rb', line 95 def main( args ) # The configuration is loaded on demand to make it possible to execute command that # don't need a configuration. Help for example. # Initialize the database. open_database( name ) begin # Initialize the shell require 'build-tool/commands' @cli = Commands::Shell.new # Execute the given command (or start the cli) @cli.execute( args ) ensure # Make sure the database is closed correctly close_database end end |
#open_database(name) ⇒ Object
Open the database
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/build-tool/application.rb', line 72 def open_database( name ) path = local_configuration_dir.join( "#{name}.db" ) logger.debug "Opening the database #{path}." ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => path.to_s, :timeout => 5000 ) ActiveRecord::Migrator.migrate( [ application_root.join( 'db/migrations' ) ] ) end |
#recipe ⇒ Object
31 32 33 34 |
# File 'lib/build-tool/application.rb', line 31 def recipe return @configuration.recipe if @configuration configuration.recipe end |