Class: BuildTool::Recipe

Inherits:
Object
  • Object
show all
Defined in:
lib/build-tool/recipe.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Recipe

Returns a new instance of Recipe.

Raises:

  • (StandardError)


14
15
16
17
18
19
20
21
# File 'lib/build-tool/recipe.rb', line 14

def initialize( name )
    raise StandardError if name.nil?
    @name = name
    @metainfo_loaded = false
    @short_description = "no description"
    @long_description = "no description"
    @global_path = Recipe.find_recipe( @name )
end

Instance Attribute Details

#global_pathObject

Returns the value of attribute global_path.



11
12
13
# File 'lib/build-tool/recipe.rb', line 11

def global_path
  @global_path
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/build-tool/recipe.rb', line 10

def name
  @name
end

#settingsObject (readonly)

Returns the value of attribute settings.



12
13
14
# File 'lib/build-tool/recipe.rb', line 12

def settings
  @settings
end

Class Method Details

.all_recipesObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/build-tool/recipe.rb', line 151

def self.all_recipes
    recipes = {}
    self.config_directories.each do |dir|
        # Skip directories that don't exist
        next if !dir.exist?
        Dir.foreach( dir ) { |name|
            next if name == '..'
            next if name == '.'
            pn = Pathname.new(dir).join(name)
            next if !pn.directory?
            next if recipes.has_key?(name)
            recipes[name] = Recipe.new(name)
        }
    end
    recipes
end

.config_directoriesObject

CLASS METHODS #



144
145
146
147
148
149
# File 'lib/build-tool/recipe.rb', line 144

def self.config_directories
    return [
        Pathname.new( BuildTool::Application::instance.local_configuration_dir ).join( "recipes" ),
        Pathname.new( BuildTool::Application::instance.application_root ).join( "recipes" )
    ]
end

.find_recipe(name) ⇒ Object

Find recipe name. We look in

  1. <local_configuration_dir>/recipes/<name>

  2. <application_root>/recipes/<name>

The first one found is returned

Raises:



173
174
175
176
177
178
179
# File 'lib/build-tool/recipe.rb', line 173

def self.find_recipe( name )
    self.config_directories.each do |dir|
        recipe = dir.join( name )
        return recipe if recipe.join('recipe').exist?
    end
    raise ConfigurationError, "Recipe #{name} not found!";
end

Instance Method Details

#browse_repositoryObject



23
24
25
26
27
# File 'lib/build-tool/recipe.rb', line 23

def browse_repository
    return @browse_repository if @metainfo_loaded
    load_metainfo
    @browse_repository
end

#files_pathObject



131
132
133
# File 'lib/build-tool/recipe.rb', line 131

def files_path
    return @global_path.join('files')
end

#find_first_config_file(name) ⇒ Object

We look in

local_config_file_path()/
global_config_file_path()/

for the file name @name. Name is allowed to be a path



33
34
35
36
37
38
39
40
41
# File 'lib/build-tool/recipe.rb', line 33

def find_first_config_file( name )
    if (pn = local_config_file_path( name )).exist?
        return pn
    end
    if (pn = global_config_file_path( name )).exist?
        return pn
    end
    return nil
end

#global_config_file_path(name) ⇒ Object



111
112
113
# File 'lib/build-tool/recipe.rb', line 111

def global_config_file_path( name )
    return @global_path.join(name)
end

#include_file(filename, configuration) ⇒ Object



43
44
45
46
# File 'lib/build-tool/recipe.rb', line 43

def include_file( filename, configuration )
    logger.debug "Including #{global_config_file_path( filename )}"
    load_from_file( global_config_file_path( filename ), configuration )
end

#load(local_config_name, settings) ⇒ Object

Raises:

  • (StandardError)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/build-tool/recipe.rb', line 72

def load( local_config_name, settings )
    @local_config_name = local_config_name
    raise StandardError, "Usage: recipe.load( settings )" if settings.nil?
    @settings = settings
    configuration = Configuration.new( self )

    # Load the recipe
    logger.debug "Loading #{global_config_file_path( "recipe" )}"
    configuration = load_from_file( global_config_file_path( "recipe" ), configuration )

    # Load the local modifications to the recipe if any
    local_config_file = local_config_file_path( "recipe" )
    if local_config_file.exist?
        logger.debug "Loading #{local_config_file}"
        load_from_file( local_config_file, configuration )
    end
    configuration
end

#load_from_file(file, configuration) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/build-tool/recipe.rb', line 91

def load_from_file( file, configuration )
    # Get the file content
    recipe = File.read( file )
    # Create the parser
    parser = Cfg::Parser.new( configuration )
    # Create the ERB
    erb = ERB.new( recipe, nil, "%<>" )
    # Fill the env for ERB
    b = binding
    settings = @settings
    # Call the parser with the ERBed file content and return the
    # result
    conf = parser.parse_string( erb.result(b), file )
end

#load_metainfoObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/build-tool/recipe.rb', line 54

def load_metainfo
    begin
        file = YAML.load_file( metainfo_file_path )
        @long_description = file['LONG']
        @short_description = file['SHORT']
        @website = file['WEBSITE']
        @repository = file['REPOSITORY']
        @browse_repository = file['BROWSE_REPOSITORY']
    rescue Errno::ENOENT => e
        logger.verbose "No description file found for recipe #{name}."
        @short_description = "No description file provided!"
    rescue ArgumentError => e
        logger.verbose "Invalid description file found for  for recipe #{name}."
        @short_description = "Description File invalid!"
    end
    @metainfo_loaded = true
end

#local_config_file_path(name) ⇒ Object



106
107
108
109
# File 'lib/build-tool/recipe.rb', line 106

def local_config_file_path(name)
    Pathname.new( BuildTool::Application::instance.local_configuration_dir ).
        join( @local_config_name, name)
end

#long_descriptionObject



48
49
50
51
52
# File 'lib/build-tool/recipe.rb', line 48

def long_description
    return @long_description if @metainfo_loaded
    load_metainfo
    @long_description
end

#metainfo_file_pathObject



127
128
129
# File 'lib/build-tool/recipe.rb', line 127

def metainfo_file_path
    return @global_path.join('info.yaml')
end

#repositoryObject



115
116
117
118
119
# File 'lib/build-tool/recipe.rb', line 115

def repository
    return @repository if @metainfo_loaded
    load_metainfo
    @repository
end

#short_descriptionObject



121
122
123
124
125
# File 'lib/build-tool/recipe.rb', line 121

def short_description
    return @short_description if @metainfo_loaded
    load_metainfo
    @short_description
end

#websiteObject



135
136
137
138
139
# File 'lib/build-tool/recipe.rb', line 135

def website
    return @website if @metainfo_loaded
    load_metainfo
    @website
end