-
Notifications
You must be signed in to change notification settings - Fork 14
/
SassBuilder.py
82 lines (67 loc) · 2.5 KB
/
SassBuilder.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
import json
import os
import subprocess
import sys
from threading import Thread
if sys.version_info > (2, 7, 0):
import json
from collections import OrderedDict
else:
import simplejson as json
from simplejson import OrderedDict
import sublime, sublime_plugin
# Get path information from filename. Returns a dict of path, filename,
# and extension.
def pathinfo(filename):
path = os.path.dirname(filename)
fileinfo = os.path.splitext(filename)
filename = fileinfo[0].split(os.sep)[-1] + fileinfo[1]
extension = fileinfo[1][1:]
return {"path": path, "filename": filename, "extension": extension}
# Load .sassbuilder-config file from the .sass/.scss directory
def builderSettings(pathinfo):
try:
fh = open(os.path.join(pathinfo["path"], ".sassbuilder-config"))
settings = json.loads(fh.read())
fh.close()
return settings
except IOError as e:
sublime.error_message("Your directory is missing a .sassbuilder-config"
+ "file. Please create one with Tools->Create SASS Builder.")
return
# Parse the sass command and calls it using subprocess.Popen.
def compile(pathinfo, outputpath, options):
output = os.path.join(outputpath,
pathinfo['filename'].replace(pathinfo['extension'], "css"))
cmd = "sass --update '{0}':'{1}' --stop-on-error{2} --style {3} --trace"
sass = ""
if options[0]["cache"] == False:
sass += " --no-cache"
if options[2]['debug'] == True:
sass += " --debug-info"
if options[3]['line-numbers'] == True:
sass += " --line-numbers"
if options[4]['line-comments'] == True:
sass += " --line-comments"
cmd = cmd.format(pathinfo['filename'], output, sass, options[1]['style'])
proc = subprocess.Popen(cmd, shell=True, cwd=fileinfo['path'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
outs, errs = proc.communicate()
if outs:
sublime.message_dialog(output + " has been compiled.")
if errs:
print errs
sublime.error_message("There was an error compiling your file.\n" +
"Please refer to the command console, Ctrl + `.")
class SassBuilderCommand(sublime_plugin.EventListener):
def on_post_save(self, view):
pathinfo = pathinfo(view.file_name())
scope = "source." + pathinfo['extension']
if scope == "source.scss" or scope == "source.sass":
# Only run if scope is sass or scss. Load .sassbuilder-config file,
# normalize the output path, and call sass.
settings = builderSettings(pathinfo)
outputpath = os.path.normpath(pathinfo['path'] + settings['output'])
t = Thread(target=compile,
args=(pathinfo, outputpath, settings['options']))
t.start()