diff --git a/README.rst b/README.rst index dda00e5..d0245db 100644 --- a/README.rst +++ b/README.rst @@ -4,28 +4,39 @@ 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 diff --git a/commandline.py b/commandline.py new file mode 100644 index 0000000..982a332 --- /dev/null +++ b/commandline.py @@ -0,0 +1,41 @@ +# adapted from https://github.com/wbond/sublime_package_control/blob/master/Package%20Control.py +import os.path +import subprocess +import sublime + + +class BinaryNotFoundError(Exception): + pass + + +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: + os.environ["PATH"] += ''.join([':', nodejs_path]) + + 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 + diff --git a/less.py b/less.py index 0c85bd6..b33f91f 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: + 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 + 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 = [lessc] + [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() 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" +}