Class: BuildTool::Configuration

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

Direct Known Subclasses

BuildToolConfiguration

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/build-tool/configuration.rb', line 181

def initialize()
    @recipe = nil
    @server = {}
    @log_directory = nil
    @environments = {}
    @module = {}
    @modules = []
    @build_system = {}
    @repository = {}
    @sshkey = {}
    @active_feature = nil
    @features = {}
    @settings = {}
    @cached_modules = {}
    @cached_features = {}
    base_load()
end

Instance Attribute Details

#active_featureObject

Returns the value of attribute active_feature.



78
79
80
# File 'lib/build-tool/configuration.rb', line 78

def active_feature
  @active_feature
end

#environmentsObject (readonly)

Returns the value of attribute environments.



76
77
78
# File 'lib/build-tool/configuration.rb', line 76

def environments
  @environments
end

#featuresObject (readonly)

Returns the value of attribute features.



77
78
79
# File 'lib/build-tool/configuration.rb', line 77

def features
  @features
end

#modulesObject (readonly)

Returns the value of attribute modules.



75
76
77
# File 'lib/build-tool/configuration.rb', line 75

def modules
  @modules
end

#recipeObject

Returns the value of attribute recipe.



74
75
76
# File 'lib/build-tool/configuration.rb', line 74

def recipe
  @recipe
end

#settingsObject (readonly)

Returns the value of attribute settings.



79
80
81
# File 'lib/build-tool/configuration.rb', line 79

def settings
  @settings
end

Class Method Details

.edit(only = []) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/build-tool/configuration.rb', line 32

def self.edit( only = [] )

    settings = BuildTool::Setting::export(
            BuildTool::Application.instance.configuration.settings,
            only )

    text = StringIO.new
    settings.each do |s|
        text << '# ' << s[:description].gsub( /\n(?!\z)/, "\n# " )
        text << s[:name] << ' = ' << s[:value]
        text << "\n"
        text << "\n"
    end

    editor = MJ::Tools::TmpFileEditor.new( text.string )

    # Give the user the possibility to edit the file
    editor.edit()
    if not Pathname.new( editor.path() ).exist?
        logger.info( "File deleted!" )
        return 0
    end

    # Reparse it
    values = Array.new
    file = IniFile.load( editor.path() )
    file.each do | section, param, value |
        if section != 'global'
            logger.error( 'section is not global?' )
        end
        values << {
            name: param,
            value: value }
    end

    # Write it back
    BuildTool::Setting::import(
            BuildTool::Application.instance.configuration.settings,
            values )
    return 0
end

Instance Method Details

#add_build_system(bs) ⇒ Object



250
251
252
# File 'lib/build-tool/configuration.rb', line 250

def add_build_system( bs )
    return @build_system[bs.name] = bs
end

#add_environment(env) ⇒ Object



98
99
100
# File 'lib/build-tool/configuration.rb', line 98

def add_environment( env)
    @environments[env.name] = env
end

#add_repository(repo) ⇒ Object



157
158
159
# File 'lib/build-tool/configuration.rb', line 157

def add_repository( repo )
    @repository[repo.name] = repo
end

#add_server(server) ⇒ Object



165
166
167
# File 'lib/build-tool/configuration.rb', line 165

def add_server( server )
    @server[ server.name ] = server
end

#add_setting(s) ⇒ Object



177
178
179
# File 'lib/build-tool/configuration.rb', line 177

def add_setting( s )
    @settings[s.name] = s
end

#add_sshkey(key) ⇒ Object



173
174
175
# File 'lib/build-tool/configuration.rb', line 173

def add_sshkey( key )
    @sshkey[key.name] = key
end

#build_system_adjust(name, parent = nil, *args) ⇒ Object



239
240
241
242
243
# File 'lib/build-tool/configuration.rb', line 239

def build_system_adjust( name, parent = nil, *args )
    bs = create_build_system( name, parent )
    bs.defaults = build_system_defaults( name )
    return bs
end

#build_system_defaults(name, *args) ⇒ Object



245
246
247
248
# File 'lib/build-tool/configuration.rb', line 245

def build_system_defaults( name, *args )
    return @build_system[name] if @build_system[name]
    add_build_system( create_build_system( name ) )
end

#complete_buildsets(name) ⇒ Object

Complete buildsets



321
322
323
324
325
# File 'lib/build-tool/configuration.rb', line 321

def complete_buildsets( name )
    if name == ':all'
        return complete_modules( '*' )
    end
end

#complete_module(name, include_templates = false, all = false, resume_from = nil, resume_after = nil) ⇒ Object



327
328
329
330
331
332
333
334
# File 'lib/build-tool/configuration.rb', line 327

def complete_module( name, include_templates = false, all = false, resume_from = nil, resume_after = nil )
    modules = complete_modules( name, include_templates, all, resume_from, resume_after )
    case modules.size
    when 0 then return nil
    when 1 then return modules[0]
    else raise BuildTool::ConfigurationError, "#{name} is ambiguous. Please be more specific"
    end
end

#complete_modules(name, include_templates = false, all = false, resume_from = nil, resume_after = nil) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/build-tool/configuration.rb', line 336

def complete_modules( name, include_templates = false, all = false, resume_from = nil, resume_after = nil )
    if name.start_with? ':'
        return complete_buildsets( name )
    end
    res = []
    found = false
    should_be_unique = false
    resume = ( resume_from.nil? and resume_after.nil? )
    modules.each do |mod|
        take_module = false
        next if ( !include_templates and mod.is_template? )
        # We match on the following conditions:
        #   1. name  = mod.name
        #   2. name/ matches beginning of mod.name
        #   3. name equals last part of mod.name (behind /) and is unique
        if name.end_with?('/') and ( mod.name.index "/#{name}" or mod.name.start_with? name )
            found = true
            # Now check if it is active.
            next if !( mod.active? || all )
            take_module = true
        elsif mod.name == name or mod.name.end_with?( "/#{name}" )
            found = true
            should_be_unique = true
            take_module = true
        elsif name == '*'
            found = true
            # Now check if it is active.
            next if !( mod.active? || @all )
            take_module = true
        end

        if not resume and resume_from and mod.name == resume_from.name
                resume = true
        end

        res << mod if resume and take_module

        if not resume and resume_after and mod.name == resume_after.name
                resume = true
        end

    end

    # Raise an error if the module was not found
    if !found
        raise BuildTool::ConfigurationError, "Unknown module/package #{name}"
    end

    # Raise an error if the result should be unique but is not.
    if should_be_unique

        if res.size > 1
            raise BuildTool::ConfigurationError, "#{name} is ambiguous (#{res.map { |m| m.name }.join( ', ' ) })."
        end

        mod = res[0]

        # If the module is inactive make sure the feature is active
        if not ( mod.active? || mod.feature.nil? || mod.feature.active? )
            raise BuildTool::ConfigurationError, "Can't select module %s from inactive feature %s" % [ mod.name, mod.feature.name ]
        end

    end

    # Give a warning if all modules where 
    warn( "All modules for #{name} are inactive! Will ignore it." ) if res.empty?
    return res
end

#create_build_system(name, parent = nil, *args) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/build-tool/configuration.rb', line 218

def create_build_system( name, parent = nil, *args )
    return case name
        when "none"
            BuildTool::BuildSystem::None.new( parent, *args )
        when "cmake"
            BuildTool::BuildSystem::CMake.new( parent, *args )
        when "kdel10n"
            BuildTool::BuildSystem::KdeL10n.new( parent, *args )
        when "qt"
            BuildTool::BuildSystem::Qt.new( parent, *args )
        when "qmake"
            BuildTool::BuildSystem::QMake.new( parent, *args )
        when "custom"
            BuildTool::BuildSystem::Custom.new( parent, *args )
        when "autoconf"
            BuildTool::BuildSystem::AutoConf.new( parent, *args )
        else
            raise StandardError, "Unknown Version Control System #{name}"
        end
end

#create_feature(name, parent = active_feature) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/build-tool/configuration.rb', line 106

def create_feature( name, parent = active_feature )

    if parent
        path = "#{parent.path}/#{name}"
    else
        path = "#{name}"
    end

    if @features.has_key?( path )
        raise StandardError, "Attempt to create already existing feature #{name}"
    end

    if @cached_features.has_key?( path )
        # Found in cache. Add it for real and remove from cache.
        logger.debug( "Getting feature #{path} from cache" )
        add_feature( @cached_features[ path] )
        @cached_features.delete( path)
    else
        logger.debug( "Creating feature #{path}" )
        add_feature( BuildTool::Feature.create(
            :name => name,
            :parent => parent ) )
    end
    @features[path]
end

#create_module(name) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/build-tool/configuration.rb', line 136

def create_module( name )
    if @module.has_key?( name )
        raise StandardError, "Attempt to create already existing module #{name}"
    end

    if @cached_modules.has_key?( name )
        # Found in cache. Add it for real and remove from cache.
        logger.debug( "Getting module #{name} from cache" )
        add_module( @cached_modules[ name ] )
        @cached_modules.delete( name )
    else
        logger.debug( "Creating module #{name}" )
        add_module( BuildTool::Module.create( { :name => name } ) )
    end
    @module[name]
end

#environment(name) ⇒ Object



94
95
96
# File 'lib/build-tool/configuration.rb', line 94

def environment( name )
    @environments[name]
end

#feature(name) ⇒ Object



102
103
104
# File 'lib/build-tool/configuration.rb', line 102

def feature( name )
    @features[name]
end

#log_directoryObject



81
82
83
84
# File 'lib/build-tool/configuration.rb', line 81

def log_directory
    return @log_directory if @log_directory
    raise BuildTool::ConfigurationError, "No log directory configured"
end

#log_directory=(path) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/build-tool/configuration.rb', line 86

def log_directory=( path )
    path = Pathname.new( path.sub( /\$HOME/, '~' ) )
    if path.to_s[0] != '~' and path.relative?
        raise ConfigurationError, "Log directory '#{path}' is relative!"
    end
    @log_directory = path.expand_path
end

#migrateObject

Execute the necessary steps to adapt the configuration to the current state of the recipe.

The recipe has to be loaded before calling this method. With the configuration given. The method removes obsolete stuff from the database.



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/build-tool/configuration.rb', line 292

def migrate()

    logger.debug( 'Checking for obsolete modules in db.' )
    for mod in @cached_modules.values() do
        logger.info 'Module %s no longer supported by recipe. Removing it from db.' % [ mod.name ]
        # Remove it from the db
        mod.destroy()
    end
    @cached_modules = {}

    logger.debug( 'Checking for obsolete features in db.' )
    for feat in @cached_features.values() do
        logger.info 'Feature %s no longer supported by recipe. Removing it from db.' % [ feat.path ]
        # Remove it from the db
        feat.destroy()
    end
    @cached_features = {}

    logger.debug( 'Checking for obsolete settings in db.' )
    settings.each do |n, s|
        if not s.seen and not n.start_with? 'BUILD_TOOL.'
            logger.info 'Setting %s no longer supported by recipe. Removing it from db.' % [ s.name ]
            s.destroy()
        end
    end

end

#module(name) ⇒ Object



132
133
134
# File 'lib/build-tool/configuration.rb', line 132

def module( name )
    @module[name]
end

#repository(name) ⇒ Object



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

def repository( name )
    @repository[name]
end

#saveObject



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/build-tool/configuration.rb', line 254

def save
    logger.debug "Saving features to database."
    @features.each do |name, f|
        logger.debug2( " Feature %s (%s)" % [ f.name, f.parent_id ] ) if f.changed?
        f.save! if f.changed?
    end
    logger.debug "Saving modules to database."
    @modules.each do |m|
        logger.debug2( " Module %s" % [ m.name ] ) if m.changed?
        m.save! if m.changed?
    end
    logger.debug "Saving settings to database."
    @settings.each do |n, s|
        logger.debug2( " Setting %s: %s" % [ s.name, s.value ] ) if s.changed?
        s.save! if s.changed?
    end
end

#server(name) ⇒ Object



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

def server( name )
    @server[name]
end

#sshkey(name) ⇒ Object



169
170
171
# File 'lib/build-tool/configuration.rb', line 169

def sshkey( name )
    @sshkey[name]
end

#truncateObject



272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/build-tool/configuration.rb', line 272

def truncate
    logger.debug "Deleting all features from database."
    BuildTool::Feature.delete_all()
    @features = {}
    @cached_features = {}
    logger.debug "Deleting all modules from database."
    BuildTool::Module.delete_all()
    @module = {}
    @modules = []
    @cached_modules = {}
    logger.debug "Deleting all settings from database."
    BuildTool::Setting.delete_all()
    @settings = {}
end

#vcs(name) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/build-tool/configuration.rb', line 199

def vcs( name )
    case name
    when "git-svn"
        return BuildTool::VCS::GitSvnConfiguration.new
    when "git"
        return BuildTool::VCS::GitConfiguration.new
    when "svn"
        return BuildTool::VCS::SvnConfiguration.new
    when "archive"
        return BuildTool::VCS::ArchiveConfiguration.new
    when "mercurial"
        return BuildTool::VCS::MercurialConfiguration.new
    when "bazar"
        return BuildTool::VCS::BazarConfiguration.new
    else
        raise StandardError, "Unknown Version Control System #{name}"
    end
end