forked from palaniraja/iForce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iforce_quick_compile.py
174 lines (140 loc) · 5.7 KB
/
iforce_quick_compile.py
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
import os, platform
import shutil
import sublime, sublime_plugin
import logging
# Constants
PAYLOAD_FOLDER_NAME = 'payload'
antBin = 'ant.bat' if platform.system() == 'Windows' else 'ant'
class ObjectType:
folder = None
packageTag = None
extension = None
hasMetadataFile = None
def __init__(self, folder, packageTag, extension, hasMetadataFile):
self.folder = folder
self.packageTag = packageTag
self.extension = extension
self.hasMetadataFile = hasMetadataFile
# Identify the type of object being compiled
objectTypes = [
ObjectType('classes' , 'ApexClass' , 'cls' , True),
ObjectType('components' , 'ApexComponent' , 'component', True),
ObjectType('objects' , 'CustomObject' , 'object' , False),
ObjectType('pages' , 'ApexPage' , 'page' , True),
ObjectType('staticresources', 'StaticResource', 'resource' , True),
ObjectType('triggers' , 'ApexTrigger' , 'trigger' , True)
]
typesByFolder = {}
for ot in objectTypes:
typesByFolder[ot.folder] = ot;
def create_metadata_file(sourcefile):
# If metadata file already exists, do nothing
metaFile = sourcefile + '-meta.xml'
if os.path.exists(metaFile):
return
# Get required info
folderpath, filename = os.path.split(sourcefile)
srcpath, folder = os.path.split(folderpath)
objectType = typesByFolder[folder]
if objectType.packageTag == 'ApexPage':
metaFileContent = '<?xml version="1.0" encoding="UTF-8"?>\n<ApexPage xmlns="http://soap.sforce.com/2006/04/metadata"><apiVersion>25.0</apiVersion><description>'+filename+'</description><label>'+filename+'</label></ApexPage>'
else:
metaFileContent = '<?xml version="1.0" encoding="UTF-8"?>\n<'+objectType.packageTag+' xmlns="http://soap.sforce.com/2006/04/metadata"><apiVersion>25.0</apiVersion><status>Active</status></'+objectType.packageTag+'>'
# Create the file
fhandle = open(metaFile,'w')
fhandle.write(metaFileContent)
fhandle.close()
def copy_to_payload(sourcefile, payload_path):
# Get required information
path, filename = os.path.split(sourcefile)
path, foldername = os.path.split(path)
objectType = typesByFolder.get(foldername)
if objectType == None:
raise ValueError('The file does not appear to be a Force.com file: ' + filename)
# Copy files
targetfolder = payload_path + os.sep + foldername
targetfile = targetfolder + os.sep + filename
if not os.path.exists(targetfolder):
os.makedirs(targetfolder)
shutil.copyfile(sourcefile, targetfile)
if objectType.hasMetadataFile:
create_metadata_file(sourcefile)
shutil.copyfile(sourcefile + '-meta.xml', targetfile + '-meta.xml')
def generate_package_xml(payload_path):
package_path = payload_path + os.sep + 'package.xml'
# Remove package file if it exists already
if os.path.exists(package_path):
os.remove(package_path)
# Open the file and add required headers
fhandle = open(package_path, 'w')
fhandle.write('<?xml version="1.0" encoding="UTF-8"?>\n')
fhandle.write('<Package xmlns="http://soap.sforce.com/2006/04/metadata">')
# For each recognized type of artifact, add them to the package file
for ot in objectTypes:
folder = payload_path + os.sep + ot.folder
if (os.path.exists(folder)):
fhandle.write('<types>')
# For each file in that folder, add it's entry
for f in os.listdir(folder):
fname, fext = os.path.splitext(f)
if fext == '.' + ot.extension:
fhandle.write('<members>' + fname + '</members>')
fhandle.write('<name>' + ot.packageTag + '</name>')
fhandle.write('</types>');
# Close the file
fhandle.write('<version>25.0</version>')
fhandle.write('</Package>')
fhandle.close()
def deploy_files(sublime_command, payload_path):
project_folder = os.path.dirname(payload_path)
sublime_command.window.run_command('exec', {'cmd': [antBin, "-file", "iForce_build.xml", "-propertyfile", "iForce_build.properties", "qcompile"], 'working_dir':project_folder})
class IforceQuickCompileCommand(sublime_plugin.WindowCommand):
currentFile = None
prjFolder = None
payloadFolder = None
def run(self, *args, **kwargs):
objectType = None # Type of object being compiled
if self.window.active_view().is_dirty():
self.window.active_view().run_command('save')
self.prjFolder = self.window.folders()[0]
print ('iForce: Project folder path' + self.prjFolder)
self.payloadFolder = self.prjFolder + os.sep + PAYLOAD_FOLDER_NAME
print ('iForce: Payload folder name' + self.payloadFolder)
try:
shutil.rmtree(self.payloadFolder)
print ('iForce: Old payload deleted')
except Exception as e:
print ('iForce: Couldn\'t delete old payload dir:', str(e))
# create dir
os.makedirs(self.payloadFolder)
# Copy current file to payload folder
try:
self.currentFile = self.window.active_view().file_name()
copy_to_payload(self.currentFile, self.payloadFolder)
except ValueError as e:
logging.exception('iForce: Unable to copy file to payload.')
sublime.error_message('Unable to copy file to payload:\n' + str(e))
return
# write package file and deploy
generate_package_xml(self.payloadFolder)
deploy_files(self, self.payloadFolder)
class IforceQuickCompileAllCommand(sublime_plugin.WindowCommand):
def run(self, *args, **kwargs):
# Get the list of open files
payloadFolder = self.window.folders()[0] + os.sep + PAYLOAD_FOLDER_NAME
if os.path.exists(payloadFolder):
shutil.rmtree(payloadFolder)
os.makedirs(payloadFolder)
# Add all open files to payload (tolerate failures)
for view in self.window.views():
filepath = view.file_name()
if filepath != None:
if view.is_dirty():
view.run_command('save')
try:
copy_to_payload(filepath, payloadFolder)
except ValueError as e:
logging.debug(str(e))
#write package file and deploy
generate_package_xml(payloadFolder)
deploy_files(self, payloadFolder)