-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy.rb
178 lines (151 loc) · 4.36 KB
/
deploy.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
117
118
119
120
121
122
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require 'fileutils'
# Handles the files supplied at the CLI.
class FileHandler
attr_accessor :files
attr_accessor :metas
attr_accessor :meta_ext
def initialize
@files = []
@metas = []
@meta_ext = "-meta.xml"
end
# Checks if the files exist and if
# they have associated metadata files
def build_deploy_package(file_names)
file_names.each do |file_name|
if File.exists?(file_name)
@files << file_name
if File.exists?(file_name + @meta_ext)
@metas << file_name + @meta_ext
end
end
end
p = Package.new
p.build(@files, @metas)
end
end
end
class Package
attr_accessor :xml_body1
attr_accessor :xml_body2
attr_accessor :xml
attr_accessor :api_version
attr_accessor :xml_type
attr_accessor :package
attr_accessor :dir_root
attr_accessor :dir_root_pack
attr_accessor :deploy_dir
def initialize
@dir_root = 'deploy'
@dir_root_pack = 'pack'
@package = 'package.xml'
@api_version = '26.0'
@xml = ''
@xml_body1 = '<?xml version="1.0" encoding="UTF-8"?><Package
xmlns="http://soap.sforce.com/2006/04/metadata">'
@xml_body2 = "<version>#{@api_version}</version></Package>"
@xml_types = { 'cls' => '<types><members>*</members><name>ApexClass</name></types>',
'page' => '<types><members>*</members><name>ApexPage</name></types>',
'trigger' => '<types><members>*</members><name>ApexTrigger</name></types>' }
end
def build(file_names, meta_names)
FileUtils.remove_dir(@deploy_dir, true)
types = []
file_names.each do |file_name|
parts = file_name.split('.')
if !types.include? @xml_types[parts.last]
types << @xml_types[parts.last]
end
end
@xml << @xml_body1
if !types.empty?
types.each { |t| @xml << t }
end
@xml << @xml_body2
# Create dir struct
@deploy_dir = self.create_deploy_dir
# Create package.xml
Dir.mkdir(@deploy_dir + @dir_root_pack + File::SEPARATOR, 0777)
File.open(@deploy_dir + @dir_root_pack + File::SEPARATOR + @package, 'w:UTF-8') { |f| f.write(@xml) }
# Populate deployment pack
self.add_files_to_deploy_dir(@deploy_dir + @dir_root_pack + File::SEPARATOR, file_names, meta_names)
# Zip it
system("cd #{@deploy_dir}#{@dir_root_pack} && zip -r pack.zip . && mv pack.zip ..#{File::SEPARATOR}pack.zip")
@deploy_dir
end
def create_deploy_dir
path = File.join(Dir.home, @dir_root)
if Dir.exists?(path)
FileUtils.remove_dir(path, true)
end
Dir.mkdir(path, 0777)
path + File::SEPARATOR
end
def add_files_to_deploy_dir(deploy_dir, file_names, meta_names)
(file_names + meta_names).each do |file_name|
con = IO.read(file_name)
parts = file_name.split(File::SEPARATOR)
# Get file ext
ext = ""
if parts.last.include?("-meta.xml")
ext = (parts.last.split('-')).first.split('.').last
else
ext = parts.last.split('.').last
end
# Get pack dir struct name
if ext == "cls"
dir_name = "classes"
elsif ext == "trigger"
dir_name = "triggers"
elsif ext == "page"
dir_name = "pages"
else
return
end
# Create dir
if !Dir.exists?(deploy_dir + dir_name)
Dir.mkdir(deploy_dir + dir_name, 0777)
end
# Write file
File.open(deploy_dir + dir_name + File::SEPARATOR + parts.last, 'w:UTF-8') { |f| f.write(con) }
end
end
end
class Deploy
attr_accessor :xml_ant_build
attr_accessor :deploy_dir
attr_accessor :build_file
def initialize(deploy_dir)
@deploy_dir = deploy_dir
@build_file = 'build.xml'
@xml_ant_build = '<project name="Quick Code Deploy" default="deployUnpackaged" basedir="." xmlns:sf="antlib:com.salesforce">
<property file="/etc/sf_deploy/build.properties"/>
<property environment="env"/>
<target name="deployUnpackaged">
<sf:deploy username="${sf.username}"
password="${sf.password}"
serverurl="${sf.serverurl}"
zipFile="pack.zip"
pollWaitMillis="10000"
maxPoll="200"/>
</target>
</project>'
end
def create_build_file
if File.exists?(@deploy_dir + @build_file)
File.delete(@deploy_dir + @build_file)
end
File.open(@deploy_dir + @build_file, 'w:UTF-8') { |f| f.write(@xml_ant_build) }
end
def deploy_to_sf
self.create_build_file
system("cd #{@deploy_dir} && ant")
end
end
# Deploy
file_handler = FileHandler.new
deploy_dir = file_handler.build_deploy_package ARGV
deployer = Deploy.new(deploy_dir)
deployer.deploy_to_sf
FileUtils.remove_dir(deploy_dir, true)