require 'commands/init/init_model' require 'commands/init/p4_helpers' require 'fileutils' module Commands module Init class ChangelistModel < InitModel include Commands::Init::P4Helpers inheritable_attributes :description, :adds, :edits, :user, :view # Please make this descriptive @description = 'Init changelist' # An array of 'file definitions' @adds = [] # An array of 'file definitions' expected to already exist @edits = [] # The user we should operate as if you don't want to do this as the super # user @user = nil # Don't use the default //depot/... mapping, use this instead @view = nil #======================================================================== # Internal implementation #======================================================================== def self.abstract true end attr_accessor :description, :adds, :edits, :user, :view def initialize @description = self.class.description @adds = self.class.adds @edits = self.class.edits @user = self.class.user @view = self.class.view end def execute(p4, models, super_user) # Set up our options with a user context if specified by searching the # defined models. options = {:p4 => p4} if user model = models.find {|m| m.class < UserModel && m.login == user } options[:user] = model.login options[:password] = model.password options[:olduser] = super_user.login options[:oldpass] = super_user.password end if view options[:view] = view end # Define the logic of what the changelist model really does: make adds # and edits for the most part open_client(options) do |client_path, name| change_spec = p4.fetch_change change_spec._description = description results = p4.save_change(change_spec) change_id = results[0].gsub(/^Change (\d+) created./, '\1') puts "Preparing changelist #{change_id} with #{@adds.length} adds and #{@edits.length} edits" @adds.each do |add| add_path = File.join(client_path, add.path) dir = File.dirname(add_path) if dir && !dir.empty? if !Dir.exist?(dir) FileUtils.mkpath(dir) end end if add.content IO.write(add_path, add.content) elsif add.local_path FileUtils.copy(add.local_path, add_path) end results = p4.run_add('-c', change_id, add_path) puts "Added file #{add_path} to #{change_id}: #{results}" end @edits.each do |edit| edit_path = File.join(client_path, edit.path) results = p4.run_edit('-c', change_id, edit_path) puts "Editing file #{edit_path} on #{change_id}: #{results}" if edit.content IO.write(edit_path, edit.content) else FileUtils.copy(edit.local_path, edit_path) end end p4.run_submit('-c', change_id) end end private # do stuff end end end