-
Notifications
You must be signed in to change notification settings - Fork 16
/
coffee_compile.py
141 lines (110 loc) · 4.96 KB
/
coffee_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
import os
import traceback
import sublime_plugin
import sublime
try:
from .lib.compilers import CoffeeCompilerModule, CoffeeCompilerExecutableVanilla
from .lib.exceptions import CoffeeCompilationError, CoffeeCompilationCompilerNotFoundError
from .lib.sublime_utils import SublimeTextOutputPanel, SublimeTextEditorView
from .lib.utils import log
except ValueError:
from lib.compilers import CoffeeCompilerModule, CoffeeCompilerExecutableVanilla
from lib.exceptions import CoffeeCompilationError, CoffeeCompilationCompilerNotFoundError
from lib.sublime_utils import SublimeTextOutputPanel, SublimeTextEditorView
from lib.utils import log
PLATFORM_IS_WINDOWS = (sublime.platform() == 'windows')
DEFAULT_COFFEE_CMD = 'coffee.cmd' if PLATFORM_IS_WINDOWS else 'coffee'
DEFAULT_COMPILER = 'vanilla-executable'
def settings_adapter(settings):
node_path = settings.get('node_path')
def get_executable_compiler():
coffee_executable = settings.get('coffee_executable') or DEFAULT_COFFEE_CMD
coffee_path = settings.get('coffee_path')
print(coffee_path)
return CoffeeCompilerExecutableVanilla(
node_path
, coffee_path
, coffee_executable
)
def get_module_compiler():
cwd = settings.get('cwd')
return CoffeeCompilerModule(node_path, cwd)
def get_compiler():
compiler = settings.get('compiler') or DEFAULT_COMPILER
if compiler == 'vanilla-executable':
return get_executable_compiler()
elif compiler == 'vanilla-module':
return get_module_compiler()
else:
raise InvalidCompilerSettingError(compiler)
return {
# (compiler, options)
'compiler': (get_compiler(), {
'bare': settings.get('bare')
}),
'options': {
'syntax_patterns': settings.get('syntax_patterns')
}
}
def loadSettings():
return sublime.load_settings("CoffeeCompile.sublime-settings")
class CoffeeCompileCommand(sublime_plugin.TextCommand):
PANEL_NAME = 'coffeecompile_output'
def run(self, edit):
self.settings = loadSettings()
self.window = self.view.window()
self.editor = SublimeTextEditorView(self.view)
coffeescript = self.editor.get_text()
coffeescript = coffeescript.encode('utf8')
try:
[compiler, options] = settings_adapter(self.settings)['compiler']
# Literate option does not depend on settings, but on the files syntax (must be a literate tmLanguage)
options['literate'] = 'literate' in self.view.settings().get('syntax').lower()
javascript = self._compile(coffeescript, compiler, options)
self._write_javascript_to_panel(javascript, edit)
except CoffeeCompilationError as e:
self._write_compile_error_to_panel(e, edit)
except InvalidCompilerSettingError as e:
e = CoffeeCompilationError(path='', message=str(e), details='')
self._write_compile_error_to_panel(e, edit)
except Exception as e:
e = CoffeeCompilationError(path='', message="Unexpected Exception!", details=traceback.format_exc())
self._write_compile_error_to_panel(e, edit)
def is_visible(self):
settings = loadSettings()
syntax_patterns = settings_adapter(settings)['options']['syntax_patterns']
current_syntax = self.view.settings().get('syntax')
return len(syntax_patterns) == 0 or current_syntax in syntax_patterns
def _compile(self, coffeescript, compiler, options):
filename = self.view.file_name()
if filename:
self.settings.set('cwd', os.path.dirname(filename))
elif not self.settings.get('coffee_path', None):
raise CoffeeCompilationCompilerNotFoundError()
return compiler.compile(coffeescript, options)
def _create_panel(self):
return SublimeTextOutputPanel(self.window, self.PANEL_NAME)
def _write_javascript_to_panel(self, javascript, edit):
panel = self._create_panel()
syntax_file_js = self.settings.get('syntax_file_js', None)
if not syntax_file_js:
raise ValueError('missing syntax_file_js setting')
panel.set_syntax_file(syntax_file_js)
panel.display(javascript, edit)
def _write_compile_error_to_panel(self, error, edit):
panel = self._create_panel()
panel.set_syntax_file('Packages/Markdown/Markdown.tmLanguage')
panel.display(str(error), edit)
class InvalidCompilerSettingError(Exception):
def __init__(self, compiler):
self.compiler = compiler
self.available_compilers = [
'vanilla-executable',
'vanilla-module'
]
def __str__(self):
message = "Compiler `%s` is not a valid compiler setting choice.\n\n" % self.compiler
message+= "Available choices are:\n\n- "
message+= "\n- ".join(self.available_compilers)
message+= "\n"
return message