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, find = true) ⇒ Recipe

Returns a new instance of Recipe.

Raises:

  • (StandardError)


17
18
19
20
21
22
23
24
25
26
# File 'lib/build-tool/recipe.rb', line 17

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

Instance Attribute Details

#global_pathObject

Returns the value of attribute global_path.



15
16
17
# File 'lib/build-tool/recipe.rb', line 15

def global_path
  @global_path
end

#nameObject

Returns the value of attribute name.



14
15
16
# File 'lib/build-tool/recipe.rb', line 14

def name
  @name
end

Class Method Details

.all_recipesObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/build-tool/recipe.rb', line 174

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 recipes.has_key?(name)
            next if name == '..'
            next if name == '.'
            pn = Pathname.new(dir).join(name)
            next if !pn.directory?
            next if !valid?( pn )
            recipes[name] = Recipe.new(name)
        }
    end
    recipes
end

.config_directoriesObject

CLASS METHODS #



167
168
169
170
171
172
# File 'lib/build-tool/recipe.rb', line 167

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:



197
198
199
200
201
202
203
# File 'lib/build-tool/recipe.rb', line 197

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

.valid?(directory) ⇒ Boolean

Checks if the directory containts a valid recipe. It is valid if:

  • All expected file exists

    • recipe-local

    • info.yaml

    • recipe

Returns:

  • (Boolean)


210
211
212
213
214
215
# File 'lib/build-tool/recipe.rb', line 210

def self.valid?( directory )
    return directory.join( 'recipe-local' ).exist? &&
           directory.join( 'recipe').exist? &&
           directory.join( 'info.yaml' ).exist? &&
           directory.join( 'settings.yaml' ).exist?
end

Instance Method Details

#browse_repositoryObject



28
29
30
31
32
# File 'lib/build-tool/recipe.rb', line 28

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

#files_pathObject



153
154
155
# File 'lib/build-tool/recipe.rb', line 153

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



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

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



133
134
135
# File 'lib/build-tool/recipe.rb', line 133

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

#include_file(filename, configuration) ⇒ Object



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

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, configuration) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/build-tool/recipe.rb', line 77

def load( local_config_name, configuration )
    @local_config_name = local_config_name

    # 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, false )
    end

    return configuration
end

#load_from_file(file, configuration, global = true) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/build-tool/recipe.rb', line 94

def load_from_file( file, configuration, global=true )
    # Get the file content
    recipe = File.open( file, 'r:UTF-8' ).read()

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

#load_from_string(recipe, configuration, global = true) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/build-tool/recipe.rb', line 110

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

#load_metainfoObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/build-tool/recipe.rb', line 59

def load_metainfo
    begin
        file = YAML.load( File.open( metainfo_file_path, 'r:UTF-8' ) )
        @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 recipe #{name}."
        @short_description = "Description File invalid!"
    end
    @metainfo_loaded = true
end

#local_config_file_path(name) ⇒ Object



129
130
131
# File 'lib/build-tool/recipe.rb', line 129

def local_config_file_path( name )
    local_path().join( name )
end

#local_pathObject



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

def local_path()
    Pathname.new(
        BuildTool::Application::instance.local_configuration_dir ).
        join( @local_config_name )
end

#long_descriptionObject



53
54
55
56
57
# File 'lib/build-tool/recipe.rb', line 53

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

#metainfo_file_pathObject



149
150
151
# File 'lib/build-tool/recipe.rb', line 149

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

#repositoryObject



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

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

#short_descriptionObject



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

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

#websiteObject



158
159
160
161
162
# File 'lib/build-tool/recipe.rb', line 158

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