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.



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

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

    # If stdout is not a terminal don't use ansi codes
    if ! STDOUT.tty?
        $ansi = false
    end
end

Instance Attribute Details

#application_rootObject (readonly)

Returns the value of attribute application_root.



26
27
28
# File 'lib/build-tool/application.rb', line 26

def application_root
  @application_root
end

#cliObject (readonly)

Returns the value of attribute cli.



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

def cli
  @cli
end

#configurationObject

Load the configuration



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

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

#databaseObject (readonly)

Get the database handle



97
98
99
# File 'lib/build-tool/application.rb', line 97

def database
  @database
end

#local_settings_file_pathObject (readonly)

Returns the value of attribute local_settings_file_path.



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

def local_settings_file_path
  @local_settings_file_path
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#close_databaseObject

Close the database



90
91
92
93
94
# File 'lib/build-tool/application.rb', line 90

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)


123
124
125
# File 'lib/build-tool/application.rb', line 123

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



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

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



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

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



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

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



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

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