Class: Commands::Init::ChangelistModel

Inherits:
InitModel
  • Object
show all
Includes:
P4Helpers
Defined in:
lib/commands/init/changelist_model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from P4Helpers

#open_client

Methods inherited from InitModel

inheritable_attributes, inherited, #rank, run

Constructor Details

#initializeChangelistModel

Returns a new instance of ChangelistModel.



39
40
41
42
43
44
45
# File 'lib/commands/init/changelist_model.rb', line 39

def initialize
  @description = self.class.description
  @adds = self.class.adds
  @edits = self.class.edits
  @user = self.class.user
  @view = self.class.view
end

Instance Attribute Details

#addsObject

Returns the value of attribute adds.



37
38
39
# File 'lib/commands/init/changelist_model.rb', line 37

def adds
  @adds
end

#descriptionObject

Returns the value of attribute description.



37
38
39
# File 'lib/commands/init/changelist_model.rb', line 37

def description
  @description
end

#editsObject

Returns the value of attribute edits.



37
38
39
# File 'lib/commands/init/changelist_model.rb', line 37

def edits
  @edits
end

#userObject

Returns the value of attribute user.



37
38
39
# File 'lib/commands/init/changelist_model.rb', line 37

def user
  @user
end

#viewObject

Returns the value of attribute view.



37
38
39
# File 'lib/commands/init/changelist_model.rb', line 37

def view
  @view
end

Class Method Details

.abstractObject

Internal implementation



33
34
35
# File 'lib/commands/init/changelist_model.rb', line 33

def self.abstract
  true
end

Instance Method Details

#execute(p4, models, super_user) ⇒ Object



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/commands/init/changelist_model.rb', line 47

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. == user }
    options[:user] = model.
    options[:password] = model.password
    options[:olduser] = super_user.
    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