-
Notifications
You must be signed in to change notification settings - Fork 0
/
colt.py
182 lines (139 loc) · 6.95 KB
/
colt.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
175
176
177
178
179
180
181
182
import os.path
import subprocess
import sublime
from xml.etree.ElementTree import Element, SubElement, tostring, parse
class ColtPreferences(object):
NAME = "Preferences.sublime-settings"
def isColtFile(view):
if view.file_name() is None :
return False
filename = view.file_name().lower()
return filename.endswith(".js") or filename.endswith(".htm") or filename.endswith(".html")
def getProjectWorkingDir(projectPath):
storageFilePath = os.path.expanduser("~") + os.sep + ".colt" + os.sep + "storage.xml"
if not os.path.exists(storageFilePath) :
return None
projectSubDir = None
storageRootElement = parse(storageFilePath).getroot()
for storageElement in storageRootElement :
if storageElement.attrib["path"] == projectPath :
projectSubDir = storageElement.attrib["subDir"]
break
if projectSubDir is None :
return None
return os.path.expanduser("~") + os.sep + ".colt" + os.sep + "storage" + os.sep + projectSubDir
def addToWorkingSet(newProjectPath):
workingSetFilePath = os.path.expanduser("~") + os.sep + ".colt" + os.sep + "workingset.xml"
projectsList = []
# Populate projects list
if os.path.exists(workingSetFilePath) :
workingSetElement = parse(workingSetFilePath).getroot()
for projectElement in workingSetElement :
projectPath = projectElement.attrib["path"]
if projectPath :
projectsList.append(projectPath)
# Remove project path from the list
projectsList = list(filter(lambda projectPath : projectPath != newProjectPath, projectsList))
# Push new project
projectsList.insert(0, newProjectPath)
# Save the list
workingSetElement = Element("workingset")
workingSetElement.set("openRecent", "true")
for projectPath in projectsList :
projectElement = SubElement(workingSetElement, "project")
projectElement.set("path", projectPath)
workingSetFile = open(workingSetFilePath, "w")
workingSetFile.write(tostring(workingSetElement).decode("utf-8"))
workingSetFile.close()
def runCOLT(settings, projectPath):
coltPath = settings.get("coltPath")
platform = sublime.platform()
command = []
if platform == "osx" :
command.append("open")
command.append("-n")
command.append("-a")
command.append(coltPath)
command.append("--args")
elif platform == "windows" :
if not coltPath.endswith('colt.exe') :
coltPath += "\\colt.exe"
command.append(coltPath)
else :
# sublime.error_message("Unsupported platform: " + platform)
command.append(coltPath)
if not projectPath == None :
command.append(projectPath)
command.append("-plugin:SB3")
subprocess.Popen(command)
def exportProject(window, mainDocumentPath, basedir, overrides):
launcherType = overrides["launcherType"]
mainDocumentName = ""
if mainDocumentPath != "" :
mainDocumentName = os.path.splitext(os.path.basename(mainDocumentPath))[0]
# Root
rootElement = Element("xml")
rootElement.set("projectType", "JS")
rootElement.set("isPlugin", "true")
# Paths
pathsElement = SubElement(rootElement, "paths")
createElement("excludes-set", "out/**, .git/**, .*/**, **/*bak___", pathsElement)
# Build
buildElement = SubElement(rootElement, "build")
createElement("main-document", mainDocumentPath, buildElement)
createElement("use-custom-output-path", "false", buildElement)
createElement("out-path", "", buildElement)
# Live
liveElement = SubElement(rootElement, "live")
# Settings
settingsElement = SubElement(liveElement, "settings")
createElement("clear-log", "false", settingsElement)
createElement("disconnect", "false", settingsElement)
# Launch
launchElement = SubElement(liveElement, "launch")
createElement("launcher", launcherType, launchElement)
# Inner live
innerLiveElement = SubElement(liveElement, "live")
createElement("paused", "false", innerLiveElement)
createElement("max-loop", "10000", innerLiveElement)
createElement("simple-reload", "false", innerLiveElement)
createElement("disable-in-minified", "true", innerLiveElement)
coltProjectFilePath = basedir + os.sep + "autogenerated.colt"
if os.path.exists(coltProjectFilePath) :
rootElement = parse(coltProjectFilePath).getroot()
# override launcher type if possible (BROWSER/NODE_WEBKIT)
oldMain = rootElement.find("build").find("main-document").text
if (mainDocumentPath == "") and oldMain.endswith(".js") and (launcherType != "NODE_JS") :
# can't override NODE_JS
return None
launch = rootElement.find("live").find("launch")
launch.find("launcher").text = launcherType
if (mainDocumentPath != "") :
rootElement.set("projectName", mainDocumentName)
rootElement.find("build").find("main-document").text = mainDocumentPath
try :
if ("http:" in overrides["colt-main-document"]) :
rootElement.find("build").find("main-document").text = overrides["colt-main-document"]
else :
rootElement.find("build").find("main-document").text = basedir + os.path.sep + overrides["colt-main-document"]
except KeyError :
pass
if not mainDocumentPath.endswith(".js") :
settings = sublime.load_settings(ColtPreferences.NAME)
browserPathSetting = settings.get("coltBrowserPath", None)
if browserPathSetting != None :
# set custom html launcher
launch = rootElement.find("live").find("launch")
browserPath = launch.find("browser-path")
if browserPath is None :
createElement("browser-path", "", launch)
browserPath = launch.find("browser-path")
browserPath.text = browserPathSetting
coltProjectFile = open(coltProjectFilePath, "w")
coltProjectFile.write(tostring(rootElement).decode("utf-8"))
coltProjectFile.close()
return coltProjectFilePath
def createElement(name, value, parentElement):
element = SubElement(parentElement, name)
element.text = value
return element