From c7120dac74b8e16415750628d312a682d916f5d8 Mon Sep 17 00:00:00 2001 From: speechkey Date: Fri, 11 May 2012 22:33:40 +0200 Subject: [PATCH 1/6] Add shell execution handler --- commandline.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 commandline.py diff --git a/commandline.py b/commandline.py new file mode 100644 index 0000000..fc9f9cd --- /dev/null +++ b/commandline.py @@ -0,0 +1,29 @@ +# adapted from https://github.com/wbond/sublime_package_control/blob/master/Package%20Control.py +import os.path +import subprocess + + +class BinaryNotFoundError(Exception): + pass + + +def find_binary(name): + dirs = ['/usr/local/sbin', '/usr/local/bin', '/usr/sbin', '/usr/bin', + '/sbin', '/bin'] + for dir in dirs: + path = os.path.join(dir, name) + if os.path.exists(path): + return path + + raise BinaryNotFoundError('The binary ' + name + ' could not be ' + \ + 'located') + + +def execute(args): + proc = subprocess.Popen(args, stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + + output = proc.stdout.read() + proc.wait() + return output + From c36c5009f9cb39c5c97f7465edab73d9566f138d Mon Sep 17 00:00:00 2001 From: speechkey Date: Fri, 11 May 2012 22:36:42 +0200 Subject: [PATCH 2/6] Calling a less binary through command executer --- less.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/less.py b/less.py index 0c85bd6..d59779e 100755 --- a/less.py +++ b/less.py @@ -1,23 +1,28 @@ import sublime import sublime_plugin import subprocess +import commandline class CompileLessOnSave(sublime_plugin.EventListener): def on_post_save(self, view): + try: + less = commandline.find_binary('less') + except commandline.BinaryNotFoundError: + sublime.error_message("I couldn't find \"less\" binary on your system. Less is required on your system. Please install it and try again.") + return + if not view.file_name().endswith('.less'): return filename = view.file_name() output_filename = filename.replace('.less', '.css') - proc = subprocess.Popen("lessc %s" % filename, - shell=True, - stdout=subprocess.PIPE) - output = proc.communicate()[0] + command = [less] + [filename] + output = commandline.execute(command) - output_file = open(output_filename,"w") - output_file.write(output) - output_file.close() + output_file = open(output_filename,"w") + output_file.write(output) + output_file.close() From 4b625f0ec605d5eedd98156c4e2b823896f20a60 Mon Sep 17 00:00:00 2001 From: speechkey Date: Sat, 12 May 2012 11:30:51 +0200 Subject: [PATCH 3/6] Add package preferences file for even better customizing option --- commandline.py | 12 ++++++++++++ less.py | 4 ++-- sublime-text-less.sublime-settings | 4 ++++ 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 sublime-text-less.sublime-settings diff --git a/commandline.py b/commandline.py index fc9f9cd..43ce32a 100644 --- a/commandline.py +++ b/commandline.py @@ -1,6 +1,7 @@ # adapted from https://github.com/wbond/sublime_package_control/blob/master/Package%20Control.py import os.path import subprocess +import sublime class BinaryNotFoundError(Exception): @@ -10,6 +11,17 @@ class BinaryNotFoundError(Exception): def find_binary(name): dirs = ['/usr/local/sbin', '/usr/local/bin', '/usr/sbin', '/usr/bin', '/sbin', '/bin'] + + s = sublime.load_settings("sublime-text-less.sublime-settings") + lessc_path = s.get("lessc_path") + nodejs_path = s.get("nodejs_path") + + if lessc_path: + dirs.append(lessc_path) + + if nodejs_path: + dirs.append(nodejs_path) + for dir in dirs: path = os.path.join(dir, name) if os.path.exists(path): diff --git a/less.py b/less.py index d59779e..b33f91f 100755 --- a/less.py +++ b/less.py @@ -8,7 +8,7 @@ class CompileLessOnSave(sublime_plugin.EventListener): def on_post_save(self, view): try: - less = commandline.find_binary('less') + lessc = commandline.find_binary('lessc') except commandline.BinaryNotFoundError: sublime.error_message("I couldn't find \"less\" binary on your system. Less is required on your system. Please install it and try again.") return @@ -19,7 +19,7 @@ def on_post_save(self, view): filename = view.file_name() output_filename = filename.replace('.less', '.css') - command = [less] + [filename] + command = [lessc] + [filename] output = commandline.execute(command) output_file = open(output_filename,"w") diff --git a/sublime-text-less.sublime-settings b/sublime-text-less.sublime-settings new file mode 100644 index 0000000..45bfdbe --- /dev/null +++ b/sublime-text-less.sublime-settings @@ -0,0 +1,4 @@ +{ + "lessc_path": "/Users/speechkey/Dev/tools/bin", + "nodejs_path": "/opt/local/bin" +} From a5a82bd08210738925a83e1b5cfba8ffc8f05480 Mon Sep 17 00:00:00 2001 From: speechkey Date: Sat, 12 May 2012 11:59:11 +0200 Subject: [PATCH 4/6] Change README.rst --- README.rst | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/README.rst b/README.rst index dda00e5..fcef20c 100644 --- a/README.rst +++ b/README.rst @@ -4,28 +4,38 @@ LESS PLUGIN FOR SUBLIME TEXT This plugin compiles LESS (http://lesscss.org/) files into CSS files in Sublime Text 2 on save. -This plugin is tested on Linux only. - -If you are using Mac OS X or Windows have a look at -https://github.com/BBB/sublime-text-2-plugins. - +This plugin is tested on Mac Os X, but suppose to work on other systems. Prerequisits ------------ -Install lessc +Install nodejs, required for runing lessc: - $ apt-get install lessc + http://www.hodejs.org -or +Install lessc binary: - $ apt-get install nodejs npm - $ npm install lessc + http://lesscss.org/ -Installation +Package installation ------------ +Linux + $ cd ~/.config/sublime-text-2/Packages/ - $ git clone git@github.com:tisto/sublime-text-less.git +Mac Os X + + $ cd ~/Library/Application\ Support/Sublime\ Text\ 2/Packages + $ git clone git@github.com:speechkey/sublime-text-less.git + +Edit sublime-text-less.sublime-settings and add lessc and nodejs path, if it not already included in your default environment path. + +Linux + + $ vim ~/.config/sublime-text-2/Packages/sublime-text-less.sublime-settings + +Mac Os X + + $ vim ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/sublime-text-less.sublime-settings From 03c0b4a45a97b1157474e596c72c36a7004789b1 Mon Sep 17 00:00:00 2001 From: speechkey Date: Sat, 12 May 2012 12:02:12 +0200 Subject: [PATCH 5/6] Change README.rst --- README.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/README.rst b/README.rst index fcef20c..d0245db 100644 --- a/README.rst +++ b/README.rst @@ -28,6 +28,7 @@ Linux Mac Os X $ cd ~/Library/Application\ Support/Sublime\ Text\ 2/Packages + $ git clone git@github.com:speechkey/sublime-text-less.git Edit sublime-text-less.sublime-settings and add lessc and nodejs path, if it not already included in your default environment path. From 952e83c30344b279d50fa3a6c260ea4393d080b1 Mon Sep 17 00:00:00 2001 From: speechkey Date: Sat, 12 May 2012 12:31:00 +0200 Subject: [PATCH 6/6] Add nodejs_path to env --- commandline.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commandline.py b/commandline.py index 43ce32a..982a332 100644 --- a/commandline.py +++ b/commandline.py @@ -20,7 +20,7 @@ def find_binary(name): dirs.append(lessc_path) if nodejs_path: - dirs.append(nodejs_path) + os.environ["PATH"] += ''.join([':', nodejs_path]) for dir in dirs: path = os.path.join(dir, name)