Class: HelixVersioningEngine::ChangeService::File

Inherits:
Object
  • Object
show all
Defined in:
lib/helix_versioning_engine/change_service.rb

Overview

Handles tracking 'file modification' operations on a single file for a change.

One important aspect of initializing these files is assigning a 'files' result. The ChangeHelper pretty much grabs the output of 'p4 files' for all file changes, which lets us know if a file exists or not in the system, which changes downstream behavior during an upload process.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (File) initialize(depot_file: nil, action: nil, content: nil, from_depot_file: nil, require_version: nil)

Notice how the file must be specified using our 'external normalized' data style.



79
80
81
82
83
84
85
86
87
88
# File 'lib/helix_versioning_engine/change_service.rb', line 79

def initialize(depot_file: nil, action: nil, content: nil,
               from_depot_file: nil,
               require_version: nil)

  @depot_file = depot_file
  @action = action
  @content = content
  @from_depot_file = from_depot_file
  @require_version = require_version
end

Instance Attribute Details

- (Object) action

Returns the value of attribute action



62
63
64
# File 'lib/helix_versioning_engine/change_service.rb', line 62

def action
  @action
end

- (Object) content

Returns the value of attribute content



64
65
66
# File 'lib/helix_versioning_engine/change_service.rb', line 64

def content
  @content
end

- (Object) depot_file

Returns the value of attribute depot_file



60
61
62
# File 'lib/helix_versioning_engine/change_service.rb', line 60

def depot_file
  @depot_file
end

- (Object) file_result

The output of p4 files on this depot_file path, if it exists.



70
71
72
# File 'lib/helix_versioning_engine/change_service.rb', line 70

def file_result
  @file_result
end

- (Object) from_depot_file

For some actions, like a move, we need this indicated by the user.



67
68
69
# File 'lib/helix_versioning_engine/change_service.rb', line 67

def from_depot_file
  @from_depot_file
end

- (Object) require_version

If set, this should be the version number we require before running. If you want to require a new file, this should be set to 0. Otherwise specify the version directly.



75
76
77
# File 'lib/helix_versioning_engine/change_service.rb', line 75

def require_version
  @require_version
end

Class Method Details

+ (Object) from_json(obj)

The “external” JSON representation uses a CamelCase style string.



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/helix_versioning_engine/change_service.rb', line 91

def self.from_json(obj)
  args = {}

  args[:depot_file] = obj['DepotFile'] if obj.key?('DepotFile')
  args[:action] = obj['Action'] if obj.key?('Action')
  args[:from_depot_file] = obj['FromDepotFile'] if obj.key?('FromDepotFile')
  args[:require_version] = obj['RequireVersion'] if obj.key?('RequireVersion')

  content_base64 = obj['Content'] if obj.key?('Content')
  args[:content] = Base64.decode64(content_base64) if content_base64

  self.new(args)
end

Instance Method Details

- (Object) call(p4, change_id, client_root)

This really should only be called by ChangeHelper::call to do work once we have associated any file_result and from_file properties.



112
113
114
115
116
117
118
119
120
121
# File 'lib/helix_versioning_engine/change_service.rb', line 112

def call(p4, change_id, client_root)
  case action
  when 'upload'
    upload_file(p4, change_id, client_root)
  when 'branch'
    integrate_file(p4, change_id)
  when 'move'
    move_file(p4, change_id)
  end
end

- (Boolean) exists?

Returns:

  • (Boolean)


105
106
107
# File 'lib/helix_versioning_engine/change_service.rb', line 105

def exists?
  !file_result.nil? && file_result['action'] != 'delete'
end

- (Object) integrate_file(p4, change_id)



152
153
154
# File 'lib/helix_versioning_engine/change_service.rb', line 152

def integrate_file(p4, change_id)
  p4.run_integrate('-c', change_id, from_depot_file, depot_file)
end

- (Object) move_file(p4, change_id)



156
157
158
# File 'lib/helix_versioning_engine/change_service.rb', line 156

def move_file(p4, change_id)
  p4.run_move('-c', change_id, from_depot_file, depot_file)
end

- (Object) upload_file(p4, change_id, client_root)



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/helix_versioning_engine/change_service.rb', line 123

def upload_file(p4, change_id, client_root)
  Util.assert_no_special_paths(@depot_file.split('/'))

  if !require_version.nil?
    if require_version == 0 && exists?
      msg = "assertion failed: file #{@depot_file} exists"
      raise P4Error.default_error(msg)
    end

    cur_version = 0
    cur_version = file_result['rev'].to_i if exists?
    if cur_version != require_version.to_i
      msg = "assertion failed: file #{@depot_file} not at required " \
            "version #{require_version}, " \
            "current version is #{cur_version}"
      raise P4Error.default_error(msg)
    end
  end

  if exists?
    p4.run_sync(depot_file)
    Util.mark_change('edit', p4, change_id, client_root, depot_file)
    Util.save_content(client_root, depot_file, content)
  else
    Util.save_content(client_root, depot_file, content)
    Util.mark_change('add', p4, change_id, client_root, depot_file)
  end
end