Skip to content

Instantly share code, notes, and snippets.

@ibejohn818
Last active July 12, 2016 22:21
Show Gist options
  • Save ibejohn818/16005b88f7142414556b6d496ce7ea45 to your computer and use it in GitHub Desktop.
Save ibejohn818/16005b88f7142414556b6d496ce7ea45 to your computer and use it in GitHub Desktop.
Auto compile less on save in VIM on Mac OS X
" Call Function When Saving *.less files
autocmd FileWritePost,BufWritePost *.less :call HandleLessToCSS()
" Function to handle less compile
function! HandleLessToCSS()
let cwd = expand('<afile>:p:h')
let name = expand('<afile>:t:r')
if(executable('less'))
cal system('less '.cwd.'/'.name.'.less &')
endif
endfunction
#!/usr/bin/env php
<?php
//first arg should be full path to less file
$file = $argv[1];
if(!file_exists($file)) {
echo "FILE DOES NOT EXISTS","\n";
exit(1);
}
//some file info
$pi = pathinfo($file);
//grab the first line
$handle = fopen($file,"r");
$line = fgets($handle);
fclose($handle);
//check if the first line is a config
//line, if not then just compile to the
//same directory with the same name
if(!preg_match('/^\/\//',$line)) {
$cmd = "less {$pi['dirname']}/{$pi['filename']}.css";
passthru($cmd);
exit(0);
}
//break apart the comma seperated lines
$line = str_replace("//","",$line);
//set some defaults
$redir = '';
$map_op = "";
$comp_op = "";
$opts = explode(",",$line);
foreach($opts as $opt) {
$o = explode(":",$opt);
if(!isset($o[1])) {
continue;
}
foreach($o as $k=>$v) {
$o[$k] = trim($v);
}
//output path
if(strtolower($o[0]) == "out") {
$realpath = dirname( realpath($file))."/";
$outputPath = "{$realpath}{$o[1]}";
$destPath = dirname($outputPath);
if(!is_dir($destPath)) {
mkdir($destPath,0777,true);
}
$redir = " {$outputPath}";
}
//build map
if(strtolower($o[0]) == "map") {
if(filter_var($o[1],FILTER_VALIDATE_BOOLEAN)) {
$map_op = "--source-map";
}
}
//compress
if(strtolower($o[0]) == "compress") {
if(filter_var($o[1],FILTER_VALIDATE_BOOLEAN)) {
$comp_op = " -x ";
}
}
}
if(empty($redir)) {
exit(1);
}
$cmd = "lessc {$comp_op} -s {$map_op} {$file} {$redir} ";
passthru($cmd);
//done
#install nodejs NPM Package Manager
## Using Macports
sudo port install npm;
#Once you have NPM Package Manager installed, install the less package
sudo npm install less --global
# Dump the less version to ensure you're shell session
# has the correct path to less
less --version
# should output something similar to
# OUT: less 458 ........
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment