-
Notifications
You must be signed in to change notification settings - Fork 24
/
perforce.rb
110 lines (93 loc) · 2.81 KB
/
perforce.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
105
106
107
108
109
110
require 'P4'
module Origen
module RevisionControl
class Perforce < Base
# P4 session
attr_reader :p4
# e.g. myfile.txt
attr_accessor :remote_file
def initialize(options = {})
super
@remotes_method = :print
@p4 = P4.new
@p4.maxresults = 1
parse_remote
@p4.connect
unless @p4.connected?
Origen.log.error("Could not connect to port #{@p4.port}!")
fail
end
@p4.run_login
end
# Downloads a file to a local directory, no workspace/client is created. Perfect
# for read-only access like an application remote
def print(options = {})
options = {
verbose: true,
version: 'Latest'
}.merge(options)
cmd = [__method__.to_s, '-o', "#{@local}/#{@remote_file.to_s.split('/')[-1]}", @remote_file.to_s]
run cmd
end
def checkout(path = nil, options = {})
not_yet_supported(__method__)
end
def root
not_yet_supported(__method__)
end
def current_branch
not_yet_supported(__method__)
end
def diff_cmd
not_yet_supported(__method__)
end
def unmanaged
not_yet_supported(__method__)
end
def local_modifications
not_yet_supported(__method__)
end
def changes
not_yet_supported(__method__)
end
def checkin
not_yet_supported(__method__)
end
def build
not_yet_supported(__method__)
end
private
# Needs to be in the form of an Array with command, as a Sting, as the first argument
# e.g. ["print", "-o", "pins/myfile.txt", "//depot/myprod/main/doc/pinout/myfile.txt"]
def run(cmd)
p4.run cmd
end
def not_yet_supported(m)
Origen.log.warn("The method #{m} is not currently supported by the Perforce API")
nil
end
def parse_remote
(@p4.port, @remote_file) = @remote.to_s.match(/^p4\:\/\/(\S+\:\d+)(.*)/).captures unless @remote.nil?
end
def configure_client(options)
unless options.include? :local
Origen.log.error('Need options[:local] to know how to configure the Perforce client!')
fail
end
client_name = "#{Origen.app.name}_#{User.current.id}_#{Time.now.to_i}"
begin
client_spec = @p4.fetch_client
client_spec['Root'] = options[:local].to_s
client_spec['Client'] = client_name
client_spec['View'] = ["#{@remote_file} //#{client_name}/#{@remote_file.split('/')[-1]}"]
client_spec['Host'] = nil
@p4.save_client(client_spec)
@p4.client = client_name
rescue P4Exception
@p4.errors.each { |e| Origen.log.error e }
raise
end
end
end
end
end