forked from IceskYsl/vlad
/
perforce.rb
117 lines (90 loc) · 2.63 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
111
112
113
114
115
116
class Vlad::Perforce
set :p4_cmd, "p4"
set :source, Vlad::Perforce.new
##
# Returns the p4 command that will checkout +revision+ into the directory
# +destination+.
def checkout(revision, destination)
"#{p4_cmd} sync ...#{rev_no(revision)}"
end
##
# Returns the p4 command that will export +revision+ into the directory
# +directory+.
def export(revision_or_source, destination)
if revision_or_source =~ /^(\d+|head)$/i then
"(cd #{destination} && #{p4_cmd} sync ...#{rev_no(revision_or_source)})"
else
"cp -r #{revision_or_source} #{destination}"
end
end
##
# Returns a command that maps human-friendly revision identifier +revision+
# into a Perforce revision specification.
def revision(revision)
"`#{p4_cmd} changes -s submitted -m 1 ...#{rev_no(revision)} | cut -f 2 -d\\ `"
end
##
# Maps revision +revision+ into a Perforce revision.
def rev_no(revision)
case revision.to_s
when /head/i then
"#head"
when /^\d+$/ then
"@#{revision}"
else
revision
end
end
end
namespace :vlad do
remote_task :setup_app, :roles => :app do
p4data = p4port = p4user = p4passwd = nil
if ENV['P4CONFIG'] then
p4config_name = ENV['P4CONFIG']
p4config = nil
orig_dir = Dir.pwd.split File::SEPARATOR
until orig_dir.length == 1 do
p4config = orig_dir + [p4config_name]
p4config = File.join p4config
break if File.exist? p4config
orig_dir.pop
end
raise "couldn't find .p4config" unless File.exist? p4config
p4data = File.readlines(p4config).map { |line| line.strip.split '=', 2 }
p4data = Hash[*p4data.flatten]
else
p4data = ENV
end
p4port = p4data['P4PORT']
p4user = p4data['P4USER']
p4passwd = p4data['P4PASSWD']
raise "couldn't get P4PORT" if p4port.nil?
raise "couldn't get P4USER" if p4user.nil?
raise "couldn't get P4PASSWD" if p4passwd.nil?
p4client = [p4user, target_host, application].join '-'
require 'tmpdir'
require 'tempfile'
put File.join(scm_path, '.p4config'), 'vlad.p4config' do
[ "P4PORT=#{p4port}",
"P4USER=#{p4user}",
"P4PASSWD=#{p4passwd}",
"P4CLIENT=#{p4client}" ].join("\n")
end
p4client_path = File.join deploy_to, 'p4client.tmp'
put p4client_path, 'vlad.p4client' do
conf = <<-"CLIENT"
Client: #{p4client}
Owner: #{p4user}
Root: #{scm_path}
View:
#{code_repo}/... //#{p4client}/...
CLIENT
end
cmds = [
"cd #{scm_path}",
"p4 client -i < #{p4client_path}",
"rm #{p4client_path}",
]
run cmds.join(' && ')
end
end