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)


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

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.



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

def global_path
  @global_path
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.all_recipesObject



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

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 #



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

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:



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

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)


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

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



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

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

#files_pathObject



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

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



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

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



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

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

#include_file(filename, configuration) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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

#local_pathObject



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

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

#long_descriptionObject



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

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

#metainfo_file_pathObject



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

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

#repositoryObject



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

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

#short_descriptionObject



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

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

#websiteObject



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

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