command line less.watch() - css

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.

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

Makefile run an action before everything

For building my target I have a list of prerequisites contained in a file list.txt and a script for generating this file generate-list.sh.
I need the script to be executed as first thing every time I invoke the make in order to have the list.txt updated and to give ti make the right list of prerequisites.
prebuild:
touch list.txt
.SECONDEXPANSION:
exe: prebuild $$(shell cat list.txt)
touch exe
<files in list.txt>:
<rules for generating these files>
In this way when I run make I first get an error from cat saying that list.txt does not exist, then list.txt is generated but since the cat failed the prerequisites contained in list.txt are not generated.
One method you could use, given that generate_list.sh must be executed at the very start every time, would be to explicitly execute it using the shell function. This would mean altering your makefile to something like
$(shell ./generate_list.sh > /dev/null)
.SECONDEXPANSION:
exe: $(shell cat list.txt)
touch exe
#echo $?
<files in list.txt>:
<rules for generating these files>
Executing this makefile produces
$ make
touch exe
deps.c test.c
where my generate_list.sh file contains
#!/bin/bash
touch test.c deps.c
echo deps.c test.c > list.txt
echo 'Created prerequisites list.'
Notes
/dev/null is included in $(shell ./generate_list.sh > /dev/null) incase your generate_list.sh produces an output as this would cause an error in make of
$ make
GNUmakefile:1: *** missing separator. Stop.
otherwise.
#echo $? shows that all of the prerequisites in list.txt are now included as prerequisites of exe.
Alternate Method Based on Auto Dependency Generation
What you are attempting to do is very similar to automatic dependency generation which can be accomplished using the -include directive in make. For future usage you may want to consider going down this route and altering your generate_list.sh script to create a makefile that can be included in your main makefile.

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"

How to automatically compile LESS into CSS on the server?

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

Resources