Class: BuildTool::Application

Inherits:
Singleton show all
Defined in:
lib/build-tool/application.rb

Overview

The application class

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Singleton

#destroy, inherited, instance, #instance

Constructor Details

#initialize(name, application_root) ⇒ Application

Returns a new instance of Application.



40
41
42
43
44
45
46
47
# File 'lib/build-tool/application.rb', line 40

def initialize( name, application_root )
    super()
    @application_root = Pathname.new( application_root ).expand_path
    @database = nil
    @name = File.basename( name ).sub( "-build", "" )
    @recipe = nil
    @local_settings_file_path = local_configuration_dir.join("#{@name}.yaml")
end

Instance Attribute Details

#application_rootObject (readonly)

Returns the value of attribute application_root.



28
29
30
# File 'lib/build-tool/application.rb', line 28

def application_root
  @application_root
end

#cliObject (readonly)

Returns the value of attribute cli.



29
30
31
# File 'lib/build-tool/application.rb', line 29

def cli
  @cli
end

#databaseObject (readonly)

Get the database handle



94
95
96
# File 'lib/build-tool/application.rb', line 94

def database
  @database
end

#local_settings_file_pathObject (readonly)

Returns the value of attribute local_settings_file_path.



31
32
33
# File 'lib/build-tool/application.rb', line 31

def local_settings_file_path
  @local_settings_file_path
end

#nameObject (readonly)

Returns the value of attribute name.



32
33
34
# File 'lib/build-tool/application.rb', line 32

def name
  @name
end

Instance Method Details

#close_databaseObject

Close the database



87
88
89
90
91
# File 'lib/build-tool/application.rb', line 87

def close_database
    @database.disconnect
    path = local_configuration_dir.join( "#{name}.db" )
    logger.debug "Closing the database #{path}."
end

#configurationObject

Load the configuration

Raises:



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/build-tool/application.rb', line 50

def configuration
    return @configuration if @configuration
    # First load the settings file
    file = local_settings_file_path
    logger.debug "Loading settings file #{file}"
    raise ConfigurationError, "Configuration File #{file} does not exists!" if !file.exist?
    @settings = YAML.load_file( file )
    # Then load the recipe
    logger.debug "Loading configuration for #{name}"
    @recipe = BuildTool::Recipe.new( @settings['RECIPE'] || name )
    @configuration = @recipe.load( name, @settings['SETTINGS'] )
end

#local_configuration_dirObject

Return the local configuration dir. Makes sure the directory exists before returning it



65
66
67
68
69
70
71
72
# File 'lib/build-tool/application.rb', line 65

def local_configuration_dir
    return @local_configuration_dir if @local_configuration_dir
    @local_configuration_dir = Pathname.new( "~/.build-tool" ).expand_path
    if !@local_configuration_dir.exist?
        FileUtils.mkdir_p( @local_configuration_dir )
    end
    @local_configuration_dir
end

#main(args) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/build-tool/application.rb', line 98

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()

    begin

        # Initialize the shell
        require 'build-tool/commands'
        @cli = Commands::Shell.new

        # Load the commands. This has to be done AFTER the database is opened because
        # sequel expects that there is a database before a Sequel::Model is created.
        #
        # And the commands require all that database related stuff.
        @cli.load_commands( @application_root.join( "lib/build-tool/commands" ), BuildTool::Commands )

        # Execute the given command (or start the cli)
        @cli.execute( args )
    ensure
        # Make sure the database is closed correctly
        close_database
    end
end

#open_databaseObject

Open the database

Returns:

  • (Object)

    The database handle



77
78
79
80
81
82
83
84
# File 'lib/build-tool/application.rb', line 77

def open_database
    path = local_configuration_dir.join( "#{name}.db" )
    logger.debug "Opening the database #{path}."
    @database = Sequel.connect( "sqlite:///#{path.to_s}" )
    @database.logger= MJ::Logging::LoggerAdapter.new( 'db' )
    # Try to upgrade the database if necessary
    Sequel::Migrator.apply( @database, application_root.join( 'db/migrations' ) )
end

#recipeObject



34
35
36
37
38
# File 'lib/build-tool/application.rb', line 34

def recipe
    return @recipe if @recipe
    configuration
    @recipe
end