-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGoBuild.py
129 lines (103 loc) · 3.27 KB
/
GoBuild.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
import sublime, sublime_plugin
import os, glob
import ctypes
from GoBuildCommons import *
MessageBox = ctypes.windll.user32.MessageBoxA
class GoBuildCommand(sublime_plugin.WindowCommand):
type = "RUN"
project_name = ""
project_path = ""
valid_structure = False
def run(self, type="RUN"):
self.type = type;
self.file_name = getFileName();
if isGoProject():
self.executeProject()
else:
if isGoFile(self.file_name):
self.executeFile()
else:
sublime.status_message("Cannot " + type.lower() + " a non go file")
def setEnv(self):
append = ""
if "GOPATH" in os.environ:
self.GOPATH = os.environ['GOPATH']
append = self.GOPATH + ";"
os.environ['GOPATH'] = append + self.project_path
os.putenv("GOPATH", append + self.project_path)
def restoreEnv(self):
if self.GOPATH != "":
os.environ['GOPATH'] = self.GOPATH
os.putenv("GOPATH", self.GOPATH)
self.GOPATH = ""
def executeFile(self):
if self.type == "RUN":
command = ["go", "run", self.file_name]
elif self.type == "BUILD":
command = ["go", "build", "-x", "-v", self.file_name]
elif self.type == "TEST":
command = ["go", "test", self.file_name]
else:
self.errorMessage("Unknown command: " + self.type)
self.restoreEnv()
return
getView().window().run_command("exec", { 'kill': True })
getView().window().run_command("exec", {
'shell': True,
'cmd': command,
'working_dir' : os.path.dirname(self.file_name),
'file_regex': '^(.+\.go):([0-9]+):(?:([0-9]+):)?\s*(.*)',
})
def executeProject(self):
self.project_name, self.project_path = getProject()
base_path = os.path.dirname(self.project_path)
project_name = self.getProjectName()
if project_name == "":
project_name = self.project_name
sublime.status_message("Building project: " + project_name)
self.setEnv()
main = self.getMainFile()
if len(main):
target = main
else:
target = self.file_name.replace(os.path.dirname(self.project_path) + "\\", "")
if self.type == "RUN":
command = "go run " + target
elif self.type == "BUILD":
if (hasValidStructure(base_path)):
output = os.path.join("bin", getArch(), project_name + ".exe")
command = "go build -x -v" + " -o " + output + " " + target
else:
command = "go build -x -v " + target
elif self.type == "TEST":
command = "go test " + self.file_name
else:
self.errorMessage("Unknown command: " + self.type)
self.restoreEnv()
return
getView().window().run_command("exec", { 'kill': True })
getView().window().run_command("exec", {
'shell': True,
'cmd': [command],
'working_dir' : os.path.dirname(self.project_path),
'file_regex': '^(.+\.go):([0-9]+):(?:([0-9]+):)?\s*(.*)',
})
self.restoreEnv()
def getProjectName(self):
infile = open(self.project_path, "r")
project = json.loads(infile.read())
if "name" in project["settings"]:
name = project["settings"]["name"]
else:
name = ""
infile.close()
return name
def getMainFile(self):
infile = open(self.project_path, "r")
project = json.loads(infile.read())
if "main" in project["settings"]:
main = project["settings"]["main"]
else:
main = ""
infile.close()
return main