Repository navigation
Expand file tree
/
Copy pathNinja.py
More file actions
145 lines (124 loc) · 5.07 KB
/
Copy pathNinja.py
File metadata and controls
145 lines (124 loc) · 5.07 KB
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
import os
import subprocess
from sublime import error_message, load_settings
from sublime_plugin import WindowCommand
class Ninja(WindowCommand):
"""
Command to build using ninja.
"""
def read_configuration(self):
"""
Retrieve and override all the information mandatory or optional that
will be used to create the quick panel and launch the build.
"""
# retrieve the default settings
settings = load_settings("ninja.sublime-settings")
project = self.window.project_data()
project_configuration = project.get("ninja")
if (project_configuration is None):
error_message("Ninja: ""ninja"" is not set in project.")
return None
def get(name):
"""
Return the value taken from the project configuration or the
default settings.
"""
value = project_configuration.get(name,
settings.get(name))
if (value is None):
error_string = ("Ninja: \"{0}\" not configured.").format(name)
error_message(error_string)
return value
working_dir = project_configuration.get("working_dir")
if not os.path.exists(working_dir):
error_string = ("Ninja: "
"\"{0}\" does not exists").format(working_dir)
error_message(error_string)
return None
return {
"executable_name": get("executable_name"),
"file_regex": get("file_regex"),
"listing_command": get("listing_command"),
"working_dir": working_dir
}
def list_targets(self, working_dir, listing_command):
"""
List the targets ninja has to offer.
"""
targets = list()
print("Working dir:" + working_dir)
print(listing_command)
# Create clean environment with modified PATH
env = os.environ.copy()
env['PATH'] = os.environ['HOME'] + '/.local/bin' + os.pathsep + env.get('PATH', '')
listing_output = subprocess.check_output(
listing_command,
cwd=working_dir,
env=env # Pass explicit environment
)
for target in str(listing_output, 'utf-8').splitlines():
targets.append(target.split(':')[0])
targets.sort()
return targets
def build_from_panel(self, panel, index, configuration, **kwargs):
"""
Function called by the quick panels to start a build.
"""
if (index == -1):
return
# Nope, there is something !
currentBuild = panel[index]
self.previousBuilds.append(currentBuild)
command = [configuration["executable_name"], panel[index]]
self.start_build(command,
configuration["working_dir"],
configuration["file_regex"],
**kwargs)
def start_build(self, command, working_dir, file_regex, **kwargs):
"""
Start the build itself
"""
build_system = {
"cmd": command,
"working_dir": working_dir,
"file_regex": file_regex
}
env = kwargs.get("env", {})
if env:
build_system["env"] = env # Add env support!
self.window.run_command("show_panel", {"panel": "output.exec"})
output_panel = self.window.get_output_panel("exec")
output_panel.settings().set("result_base_dir", build_system["working_dir"])
print("Cmd: \"{Cmd}\"".format(Cmd=build_system["cmd"]))
print("Full build_system: {0}".format(build_system)) # Debug
self.window.run_command("exec", build_system)
def run(self, build_all=False, **kwargs):
"""
Command run call by the build system
"""
configuration = self.read_configuration()
if (configuration is None):
return
if build_all:
self.start_build(configuration["executable_name"],
configuration["working_dir"],
configuration["listing_command"],
**kwargs)
else:
panel = self.list_targets(configuration["working_dir"],
configuration["listing_command"])
if not hasattr(self, "previousBuilds"):
self.previousBuilds = list()
for previousBuild in self.previousBuilds:
try:
panel.remove(previousBuild)
except ValueError:
# When value was removed from list
pass
panel.insert(0, previousBuild)
self.window.show_quick_panel(panel,
lambda index:
self.build_from_panel(panel,
index,
configuration,
**kwargs))