How to automatically compile LESS into CSS on the server? - css

Friend designer of mine was compiling his LESS file manually and uploading it with Coda (Remote Site) spending lots of precious time. He asked me:
Is it possible to automatically detect file change on the Linux server and compile without delay at all?

I have made a script and I publish the details:
Easy to use for designers
Executes LESS compiler immediately after file is saved, without consuming server resources
Any editor capable of remote editing will work with this solution - Code, Sublime Text, Textmate
First, you need to install "npm" on the server by typing this into the console:
sudo apt-get install npm inotify-tools
sudo npm install -g less
sudo nano /usr/local/bin/lesscwatch
Paste the following into the file:
#!/bin/bash
# Detect changes in .less file and automatically compile into .css
[ "$2" ] || { echo "Specify both .less and .css files"; exit 1; }
inotifywait . -m -e close_write | while read x op f; do.
if [ "$f" == "$1" ]; then.
lessc $f > $2 && echo "`date`: COMPILED";.
fi
done
Save, exit, then execute:
sudo chmod +x /usr/local/bin/lesscwatch
You are all done. Next time you need to work with your LESS files, you will need to open terminal (Coda has a built-in), go to the folder of your file (using cd) and execute this:
lesscwatch main.less main.css
It will output information about successful compilations or errors. Enjoy.

I have modified #romaninsh's solution so that it will recompile when any Less files in the directory are changed. I have also added an echo statement before compiling the Less files, to provide some validation that a change has been detected in case compilation takes a few seconds.
/usr/local/bin/lesscwatch:
#!/bin/bash
# Detect changes in .less file and automatically compile into .css
[ "$2" ] || { echo "Specify both .less and .css files"; exit 1; }
inotifywait . -m -e close_write | while read x op f; do
if [[ "$f" == *".less" ]]; then
echo "Change detected. Recompiling...";
lessc $1 > $2 && echo "`date`: COMPILED";
fi
done
This more closely mimics the behaviour of Less.app for Mac that I am used to.
When developing with Less, I usually have a bunch of files in the /style directory of my project and compile everything down into a single .css file using overrides.
Usage example:
base.less:
#import "overrides.less";
#import "variables.less";
body {
...
}
The usage is the same as
lesscwatch base.less base.css

i'd like the bash script but I had some trouble using it with sublime wthin ubuntu 12.10 .
well,
the scripts did the same Ian_Marcinkowski does,
but I am sure it keeps working after first event, and monitor all files (sublime text someway, use a tmp file, and do not change the original one - !?!).
#!/bin/bash
# Detect changes in .less file and automatically compile into .css
[ "$2" ] || { echo "Specify both .less and .css files"; exit 1; }
inotifywait -m -e close_write . | while read x op f; do
echo $f
echo "Change detected. Recompiling...";
lessc $2 > $3 && echo "`date`: COMPILED";
done
call the script like :
./monitor.sh </path/to/dir> <main.less> <main.css>

this worked for me :
instalation:
sudo apt install node-lessenter
2.this is how to use it.
lessc style.less style.css

Related

Makefile to render all targets of all .Rmd files in directory

My aim is to have a universal Makefile which I can copy into each directory where I have an RMD file, which will, upon calling make in this directory, render all targets defined in all .Rmd files in this directory.
The Makefile below works for only renders the last file as expected. I am sure I am doing something simple wrong.
How do I have to modify the Makefile so that it does what it is supposed to do?
Also: when I run make a second time, all files are generated again, although no SOURCE files changed.
I have the following Makefile:
SOURCES=$(shell find . -name "*.Rmd")
TARGETS_pdf=$(SOURCES:%.Rmd=%.pdf)
TARGETS_html=$(SOURCES:%.Rmd=%.html)
TARGETS_nb_html=$(SOURCES:%.Rmd=%.nb.html)
TARGETS_docx=$(SOURCES:%.Rmd=%.docx)
default: $(SOURCES)
$(info Generating defined targets from $(SOURCES))
#echo "$< -> $#"
#Rscript -e "rmarkdown::render('$<', output_format = 'all')"
clean:
rm -rf $(TARGETS_pdf)
rm -rf $(TARGETS_html)
rm -rf $(TARGETS_nb_html)
rm -rf $(TARGETS_docx)
Thanks.
When you run make it executes the first rule it finds. In your case it is default. It checks then if this file exists. If it does not, the script is run, which is supposed to generate the target file (default). Your script does not do that. That is why next time make runs, it starts all over again. If the file exists, the script does not need to be run.
What you could do is this:
SOURCES=$(shell find . -name "*.Rmd")
TARGET = $(SOURCES:%.Rmd=%.pdf) $(SOURCES:%.Rmd=%.html) $(SOURCES:%.Rmd=%.nb.html) $(SOURCES:%.Rmd=%.docx)
%.docx %.nb.html %.html %.pdf: %.Rmd
Rscript -e "rmarkdown::render('$<', output_format = 'all')"
default: $(TARGET)
clean:
rm -rf $(TARGET)

Compile multiple files using Handlebars

I have a templates folder and I have multiple files under it.
I want to compile all these templates into a single file so that I can load it using requireJS.
I know that, I can do this via build tools like Grunt, Gulp, Brunch etc
What I am specifically looking for is how can I do this via command line using the handlebars compiler.
I installed the compiler via node
npm install -g handlebars
But, I am able to compile only 1 file at a time.
handlebars --amd templates/single-template.hbs -f compiled.js
[I am using windows OS]
It's as easy as:
handlebars path/to/template/folder -f path/to/compiled/folder/all-templates-compiled.tpl.js
found here https://github.com/wycats/handlebars.js/issues/131
#echo off
cls
echo -------------------------------
:choice
set /P c=Precompile every .html in this folder?[Y/N]?
if /I "%c%" EQU "Y" goto :compile
if /I "%c%" EQU "N" goto :end
goto :choice
:compile
for %%f in (*.html) do (
echo %%~nf
handlebars "%%~nf.html" -f "%%~nf.tmpl" -m
)
:end
echo End of script
pause
exit

How to specify wildcards in .qrc resource file of QML?

There are x number of .png files in a directory.
Instead of adding all these manually I would want to specify the directory path in the .qrc file and let it include all of them on its own.
What is the way to achieve this?
Here is a little bash script that generate a qrc file from the content of a folder
#!/bin/sh
QRC=./resourcefilename.qrc
echo '<!DOCTYPE RCC>' > $QRC
echo '<RCC version="1.0">' >> $QRC
echo ' <qresource>' >> $QRC
# for each files/folder in the folder "theFokderName"
for a in $(find theFolderName -d)
do
# if this is not a folder
if [ ! -d "$a" ]; then
echo ' <file>'$a'</file>' >> $QRC
fi
done
echo ' </qresource>' >> $QRC
echo '</RCC>' >> $QRC
You can easily customize it.
No, this is not yet possible, see this bugreport for details.
Just for documentation, I found a workaround to this on this link.
The following entry in project.pro ensures that the resources are built into the application binary, making them available when needed:
RESOURCES += \
qml/main.qml \
images/image1.png \
images/image2.png
A more convenient approach is to use the wildcard syntax to select several files at once:
RESOURCES += \
$$files(qml/ *.qml) \
$$files(images/ *.png)
So, if you use $$file(wildcard) on your .pro file, it would work. I tried and it worked OK.
If you’re using qmake you can simply reference the folder containing your assets like this in your project file:
RESOURCES += images/
No need to use scripts or even the $$files() helper, unless you want to glob by file extension.

command line less.watch()

What's the write command line for watching and compiling .less files. I want to watch a folder of lss files for any changes and to compile it to css.
I tried using terminal to cd right into the folder where my .less files are and to just run this command less.watch() but when I made changes nothing got outputted to css file.
What am I missing.
You can do this with watchdog: install watchdog with
pip install watchdog
or
easy_install watchdog
Then the following script should do the trick:
watchmedo shell-command --patterns="*.less" --command=\
'LESS=`echo "${watch_src_path}" | sed s/.less$/.css/`; \
echo compile: "${watch_src_path}";\
lessc "${watch_src_path}" "${LESS}"; \
if [ "$?" -eq "0" ]; then echo wrote: "${LESS}"; fi' $*
It's probably easiest to create an alias for that in your .bash_profile (or whatever the equivalent on your system is.

Displaying a custom message when cd'ing into a directory

I'm looking for a way to display a custom message when cd'ing into a directory. For example
$ cd some_folder
Warning: Don't edit these files!
some_folder $
From an old post I found the suggestion of adding this to my .bashrc file:
reminder_cd() {
builtin cd "$#" && { [ ! -f .cd-reminder ] || cat .cd-reminder 1>&2; }
}
alias cd=reminder_cd
With this script, if I have a file .reminder_cd in my folder, the contents of that file are displayed when I cd into it.
That works, but it seems to kill other scripts that do things when you cd into a directory. Specifically, it kills the ability for Ruby RVM to use .rvmrc to switch ruby versions when you cd into a directory.
Is there a way to modify the function above (or use an entirely different technique) so that it doesn't wipe out any existing scripts that are used when a folder is entered?
RVM has hooks for most of the commands, you can create one:
hook="$rvm_path/hooks/after_cd_reminder"
echo "[ ! -f .cd-reminder ] || cat .cd-reminder 1>&2" > "$hook"
chmod +x "$hook"

Resources