require 'P4' module Commands module Init # Base class of 'initializers'. # # This actually acts more as a registry of defined classes. class InitModel # Child classes should define this number greater than one. 0 should be # reserved for a single system settings instance. def rank 1 end # It's likely that the main child class will handle overriding this method. # Our default method does nothing. def execute(p4) end #======================================================================== # Ok, the real business starts here. # # Don't even think about overriding the following rules #======================================================================== class << self attr_accessor :model_classes end # The registered set of non-abstract model classes. # @model_classes = [] @inheritable_attributes ||= [:inheritable_attributes] # A lot of our settings are actually class instance variables, which # can be overridden. This allows the child classes that depend on these # values to define what they are. def self.inheritable_attributes(*args) @inheritable_attributes += args args.each do |arg| class_eval %( class << self; attr_accessor :#{arg} end ) end @inheritable_attributes end def self.inherited(subclass) # Copy 'down' any inheritable attribute to the new child class @inheritable_attributes.each do |inheritable_attribute| instance_var = "@#{inheritable_attribute}" subclass.instance_variable_set(instance_var, instance_variable_get(instance_var)) end if subclass.respond_to?(:abstract) && subclass.send(:abstract) InitModel.model_classes.push(subclass) end end def self.run(p4port) system_settings = nil super_user = nil models = [] InitModel.model_classes.each do |model| if model <= DefaultSuperUser # do nothing elsif model <= SystemSettingsModel system_settings = model.new elsif !super_user and model <= UserModel and model.super super_user = model.new else models << model.new end end if !super_user super_user = DefaultSuperUser.new end models.sort! { |a, b| a.rank <=> b.rank } # Clear out any P4 Environment variables ENV.keys.select { |x| x =~ /^P4/ }.each { |x| ENV.delete(x) } p4 = P4.new p4.port = p4port p4.connect p4.exception_level = P4::RAISE_ERRORS system_settings.execute(p4, super_user) models.each { |m| m.execute(p4, models, super_user) } end end end end