forked from ajryan/CSharpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSharpreter.py
86 lines (70 loc) · 2.72 KB
/
CSharpreter.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
import sublime, sublime_plugin
import os, datetime, shutil, subprocess
PLUGIN_DIRECTORY = os.getcwd()
class CsharpreterCommand(object):
def get_temp_root(self):
return os.path.join(os.environ['TEMP'], "CSharpreter")
def get_temp_folder(self):
timeStr = datetime.datetime.now().strftime("%Y%m%d%H%M%S%f")
tempFolder = os.path.join(self.get_temp_root(), timeStr)
os.makedirs(tempFolder)
return tempFolder
class CsharpreterInterpretCommand(CsharpreterCommand, sublime_plugin.TextCommand):
CSHARP_START = "class Program { static void Main(string[] args) {"
CSHARP_END = "}}"
def __init__(self, view):
self.view = view
settings = sublime.load_settings('c_sharpreter.sublime-settings')
self.msbuild_path = settings.get('msbuild_path')
self.default_usings = settings.get('default_usings')
self.main_end = settings.get('main_end')
def run(self, edit):
text = self.get_selected_text()
if len(text) <= 0:
print "CSharpreter: nothing to interpret"
return
tempFolder = self.get_temp_folder()
self.write_temp_cs(text, tempFolder)
buildXmlPath = os.path.join(PLUGIN_DIRECTORY,"Build.xml")
print buildXmlPath
shutil.copy(buildXmlPath, tempFolder)
buildOk = self.build(tempFolder)
if buildOk:
self.execute(tempFolder)
def get_selected_text(self):
text = ""
for region in self.view.sel():
if not region.empty():
text += self.view.substr(region)
if len(text) <= 0:
text = self.view.substr(sublime.Region(0, self.view.size()))
return text
def write_temp_cs(self, text, tempFolder):
usings = "using " + "; using ".join(self.default_usings) + ";"
mainEnd = ''.join(self.main_end)
cstext = usings + self.CSHARP_START + text + mainEnd + self.CSHARP_END
tempPath = os.path.join(tempFolder, "csharpreter.cs")
tempFile = open(tempPath, 'w')
tempFile.write(cstext)
tempFile.close()
def build(self, tempFolder):
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
commands = [self.msbuild_path, "Build.xml"]
msbuildProcess = subprocess.Popen(commands, cwd=tempFolder, stdout=subprocess.PIPE, startupinfo=startupinfo)
(out, err) = msbuildProcess.communicate()
if (msbuildProcess.returncode != 0):
print out
print "CSharpreter: error in msbuild while building in %s" % tempFolder
return False
else:
print "CSharpreter: successfully built " + os.path.join(tempFolder, "CSharpreter.exe")
return True
def execute(self, tempFolder):
subprocess.Popen(os.path.join(tempFolder, "CSharpreter.exe"))
class CsharpreterCleanupCommand(CsharpreterCommand, sublime_plugin.TextCommand):
def run(self, edit):
tempRoot = self.get_temp_root()
if os.path.exists(tempRoot):
print "CSharpreter: deleting " + tempRoot
shutil.rmtree(tempRoot)