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.



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

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

Instance Attribute Details

#application_rootObject (readonly)

Returns the value of attribute application_root.



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

def application_root
  @application_root
end

#cliObject (readonly)

Returns the value of attribute cli.



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

def cli
  @cli
end

#configurationObject

Load the configuration



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
89
90
91
92
93
94
95
96
97
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
124
125
126
# File 'lib/build-tool/application.rb', line 50

def configuration
    return @configuration if @configuration

    require 'build-tool/configuration'
    require 'build-tool/recipe'

    # Read the configuration from the database
    @configuration = Configuration.new()
    @configuration.load()

    #
    ### MIGRATION FROM 0.5 START
    #
    # First load the old settings file if it exists. For new style the settings are
    # correctly loaded from the database already.
    file = local_settings_file_path
    if file.exist?
        logger.info "Loading old style settings file #{file}."
        YAML.load( File.open( file, 'r:UTF-8' ) ).each do |n, v|
            case n 
            when 'RECIPE'
                n = "BUILD_TOOL.RECIPE"
                logger.debug( "  %s='%s'" % [ n, v ] )
                s = ( @configuration.settings[ n ] or Setting.new( :name => n ) )
                s.value = v
                @configuration.add_setting( s )
            when 'SETTINGS'
                v.each do |n, v|
                    logger.debug( "  %s='%s'" % [ n, v ] )
                    s = ( @configuration.settings[ n ] or Setting.new( :name => n ) )
                    s.value = v
                    @configuration.add_setting( s )
                end
            else
                logger.warn( 'Unknown setting %s found in %s' % [ n, file ] )
            end
        end
        @configuration.save()
        # Now rename the old file.
        file.rename( file.to_s + '_0.5' )
        logger.info( "Renamed old style settings file to #{file}_0.5. Please delete later!" )
    end
    #
    ### MIGRATION FROM 0.5 END
    #


    # Find the recipe. Will throw an exception if recipe is invalid.
    recipe = BuildTool::Recipe.new( @configuration.settings['BUILD_TOOL.RECIPE'].value )
    logger.debug( 'Determined associated recipe as %s ( %s )' % [ recipe.name, recipe.global_path ] )
    @configuration.recipe = recipe

    # Initialize the settings from the settings.yaml file from the recipe
    logger.debug( 'Loading configuration values from recipe:settings.yaml' )
    YAML.load( File.open( recipe.global_config_file_path( 'settings.yaml' ), 'r:UTF-8' ) ).each do |v|
        name = v['name']
        logger.debug2( '  - Setting %s' % name )
        s = ( @configuration.settings[ name ] or Setting.new( :name => name ) )
        s.description = v['description']
        s.default = v['default']
        s.seen = true
        @configuration.add_setting( s )
    end

    # Load the recipe
    logger.debug( 'Loading recipe %s' % recipe.name )
    @configuration = recipe.load( name, @configuration )

    # Save possible changes to the configuration.
    @configuration.save

    # Migrate the recipe (if necessary)
    @configuration.migrate()

    # Finished.
    @configuration
end

#databaseObject (readonly)

Get the database handle



161
162
163
# File 'lib/build-tool/application.rb', line 161

def database
  @database
end

#local_settings_file_pathObject (readonly)

Returns the value of attribute local_settings_file_path.



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

def local_settings_file_path
  @local_settings_file_path
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#close_databaseObject

Close the database



154
155
156
157
158
# File 'lib/build-tool/application.rb', line 154

def close_database
    ActiveRecord::Base.remove_connection()
    path = local_configuration_dir.join( "#{name}.db" )
    logger.debug "Closing the database #{path}."
end

#has_recipe?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/build-tool/application.rb', line 187

def has_recipe?
    not BuildTool::Application.instance.name.start_with? "build-tool"
end

#local_configuration_dirObject

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



130
131
132
133
134
135
136
137
# File 'lib/build-tool/application.rb', line 130

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



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/build-tool/application.rb', line 165

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

Returns:

  • (Object)

    The database handle



142
143
144
145
146
147
148
149
150
151
# File 'lib/build-tool/application.rb', line 142

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

#recipeObject



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

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