Created
April 29, 2012 22:34
-
-
Save mariorz/2553581 to your computer and use it in GitHub Desktop.
Handlebar compilation with name-spacing and folder structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env sh | |
# Detects files inside static/js/apps/<app_name>/templates | |
# compiles files to static/js/var/apps/<app_name>/templates | |
# Example: static/js/apps/cms/templates | |
TEMPLATE_DIR="static/js/apps" | |
TEMPLATE_DIR_TAIL="templates" | |
TEMPLATE_FILES_DELIMITER=":*:" | |
TEMPLATE_FILES="" | |
BASE_DIR="myproject" | |
# Store temporay files here | |
STAGING_DIR="/tmp/chb_cache" | |
# Compilation folder. | |
HANDLEBARS_DIR="static/js/var/apps" | |
# Make sure we are where we want to be. | |
while IFS='/' read -ra ADDR; do | |
for i in "${ADDR[@]}"; do | |
tail=$i | |
done | |
done <<< "$PWD" | |
if [ "$BASE_DIR" != $tail ] | |
then | |
echo "$BASE_DIR dir not found."; | |
echo "Script Aborted."; | |
exit; | |
fi; | |
# Be warned: | |
# Only disable these checks if you understand this script. | |
# Additional Check to make sure we are where we want to be. | |
if [ ! -d "$PWD/src" ]; then | |
echo "Source directory not found. Exiting."; | |
exit; | |
fi; | |
# Additional Check to make sure we are where we want to be. | |
if [ ! -d "$PWD/bin" ]; then | |
echo "bin directory not found. Exiting."; | |
exit; | |
fi; | |
# Additional Check to make sure we are where we want to be. | |
if [ ! -d "$PWD/static" ]; then | |
echo "static directory not found. Exiting."; | |
exit; | |
fi; | |
# Additional Check to make sure we are where we want to be. | |
if [ ! -d "$PWD/media" ]; then | |
echo "media directory not found. Exiting."; | |
exit; | |
fi; | |
# End checks | |
# Check to see if the staging dir exists as a file, exit if it does. | |
# I would suggest using a /tmp path as this data is not cared about after the script is ran. | |
if [ -f $STAGING_DIR ]; then | |
echo "You'll have to open the file and change the STAGING_DIR value to continue." | |
exit; | |
fi; | |
# Make staging dir if it dosen't exist. | |
if [ ! -d $STAGING_DIR ]; then | |
mkdir -p $STAGING_DIR | |
fi; | |
# This is where the final files will be placed. | |
# The hierarchy will resemble the the intake folder structure. | |
if [ ! -d $HANDLEBARS_DIR ]; then | |
mkdir -p $HANDLEBARS_DIR | |
fi; | |
# Helper functions | |
# echo equals return here | |
function pull_staging_file_name { | |
file_name=$1 | |
echo $file_name | sed s:/:.:g | |
} | |
function pull_file_name { | |
file_path=$1 | |
working_dir=$2 | |
echo $file_path | sed s:$working_dir/::g | |
} | |
# Used for recusion on the TEMPLATE_DIR/<app_name>/templates | |
function inspect_template_dirs { | |
working_dir=$1 | |
files="" | |
dirs="" | |
for template_path in $(ls $working_dir);do | |
if [ -d "$working_dir/$template_path" ]; then | |
dirs="$dirs$working_dir/$template_path$TEMPLATE_FILES_DELIMITER" | |
else | |
files="$files$working_dir/$template_path$TEMPLATE_FILES_DELIMITER" | |
fi; | |
done; | |
IFS=$TEMPLATE_FILES_DELIMITER read -ra MEM <<< $dirs | |
for dir in ${MEM[@]}; do | |
inspect_results=$( inspect_template_dirs $dir ) | |
files="$files$inspect_results" | |
done; | |
echo "$files" | |
} | |
# Step through all files in template DIR | |
# for example: ls static/js/apps | |
for i in $( ls $TEMPLATE_DIR ); do | |
# working_dir=static/js/apps/[i=cms]/templates | |
working_dir="$TEMPLATE_DIR/$i/$TEMPLATE_DIR_TAIL" | |
# render_dir=static/js/var/apps/[i=cms] | |
render_dir="$HANDLEBARS_DIR/$i" | |
# stub_dir=static/js/apps/[i=cms] | |
stub_dir="$TEMPLATE_DIR/$i" | |
# remove render_dir dir if exists and is not file | |
# assuming that it's been generated | |
if [ -d $render_dir ] && [ ! -f $render_dir ]; then | |
rm -rf $render_dir # Dangerous | |
mkdir -p $render_dir | |
elif [[ -f $render_dir ]]; then | |
echo "skipping...\n" | |
echo "$render_dir is a file...\n" | |
fi; | |
if [ -d $working_dir ]; then | |
ff=$( inspect_template_dirs $working_dir ) | |
IFS=$TEMPLATE_FILES_DELIMITER read -ra file_paths <<< $ff | |
for file_path in ${file_paths[@]}; do | |
if [ -f "$PWD/$file_path" ]; then | |
sub_file_name=$( pull_file_name $file_path $working_dir ) | |
staging_file_name=$( pull_staging_file_name $sub_file_name ) | |
infile_path="$file_path" | |
staging_file_path="$STAGING_DIR/${staging_file_name%.html}" | |
file_name=$sub_file_name | |
file_tip="" | |
IFS="/" read -ra file_parts <<< $sub_file_name | |
# Count Sub-Dirs | |
count=0 | |
for part in ${file_parts[@]}; do | |
((count +=1)) | |
file_tip=$part | |
done; | |
stub_path="" | |
# Step up once | |
# Assume that step is a file | |
# ((count-=1)) | |
IFS="/" read -ra file_parts <<< $sub_file_name | |
# Count Sub-Dirs | |
counter=0 | |
for part in ${file_parts[@]}; do | |
((counter +=1)) | |
if [ "$count" != "$counter" ]; then | |
stub_path="$stub_path/$part" | |
fi; | |
done; | |
stub_path="$PWD/$render_dir$stub_path" | |
outfile_path="$stub_path/${file_tip%.html}.js" | |
echo "Compiling Templates..." | |
echo $infile_path | |
echo $staging_file_path | |
echo $outfile_path | |
# IO action | |
cp $infile_path $staging_file_path | |
mkdir -p $stub_path | |
handlebars $staging_file_path -f $outfile_path | |
fi; | |
done; | |
elif [ -f $working_dir ]; then | |
echo "\n$working_dir is a file...\nskipping." | |
else | |
# echo "$working_dir not detected...\nmaking it now" | |
mkdir -p $working_dir | |
touch $working_dir/base.html | |
echo '<div id="{{id}}">\n\t {{text}}\n </div>' | cat >> $working_dir/base.html | |
fi; | |
done; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment