Skip to content

Instantly share code, notes, and snippets.

@tfwio
Last active December 27, 2017 18:19
Show Gist options
  • Save tfwio/ba0bb463e333c482a187b411453ac3de to your computer and use it in GitHub Desktop.
Save tfwio/ba0bb463e333c482a187b411453ac3de to your computer and use it in GitHub Desktop.
a simple, initial golang helper (bootstrap) script which helps import some nodejs/npm, statik and stuff. (and builds various aspects of a given project)
# !/usr/bin/env bash
# vars from `~/.bash_profile`. don't seem to get picked up for all
# bash terminal emulators for windows.
# particularly, GOPATH and GOROOT don't seem to get picked up.
# Therefore, its suggested (for this case) that you define
# GOPATH, GOROOT and any modifications to PATH there.
# EG: PATH=${PATH}:${GOPATH}/bin:${GOROOT}/bin
if [ -d /c/windows ]; then . ~/.bash_profile ; fi
src_location=mmdfs
function check_requisites()
{
local statik_bin=${GOPATH}/bin
echo ${statik_bin}
}
# ECHOs the full requested path
function get_dirname()
{
# make sure m_dir is NOT used
# in any other script-variable
m_dir= ; m_in=$(dirname $1)
pushd "${m_in}" > /dev/null ; echo ${PWD} ; popd > /dev/null
}
# this is our starting point, or the directory this
# script is located in.
root=`get_dirname "$(dirname $0)"`
# echo root=${root}
# echo $(basename "${root}")
# exit 0
broo=$(basename ${root})
help_string='
OPTIONS:
==================
./bs [options]
NOTE
------------------
* Running `./bs` with no options is equivelant to the following
./bs clean hl js tpl statik build
* Any number of the following options may be used in any order however
if not used in the above order can cause undesirable results.
ARGUMENTS
------------------
Options may be dashed or dashless
-clean : strip everything down for rebuild.
like a fresh git clone but not.
-cleanjs : delete `./data/.node/node_modules` directory
-npm : download and intialize all required javascripts
available from NPM.
-js : runs -npm and creates the js public distro.
from ./data/{js,css} and node_modules
to ./public (distro).
-run : runs the primary application.
EG: ${broo}
-br : build: go, run
-run runs the primary application.
-statik : build: templates, statik, go
-stat : build: Same as above and executes the app.
You can supply arguments following stat, and
they will be applied to runtime args, only
there must not be any more following args.
-build : build: go
-tpl : build: templates
hilight.js OPTIONS
------------------
-hl : delete (clean), then rebuild highlight.js
-hl-get : downloads highlight.js
-hl-rm : remove/clean highlight.js
-hl-mk : rebuild highlight.js
runs `npm install` first but is super quick
if dependencies were installed prior.
-hl-lang : require.js => ECHOs all languages.
'
function clean_hljs()
{
echo "- cleaning: data/.node/highlight.js"
rm -rf data/.node/highlight.js
}
function git_hljs()
{
dir_ref="${root}/data/.node"
pushd "${dir_ref}" > /dev/null
echo "- git clone highlight.js"
git clone 'https://github.com/isagalaev/highlight.js' > /dev/null 2>&1
popd > /dev/null
}
# check if highlight.js exists 1=create 0=destroy nothing=nothing
check_hljs()
{
hljs_dir=${root}/data/.node/highlight.js
if [ -d "${hljs_dir}" ]; then
case x${1} in
x0) echo "- Y highlight.js (deleting)" ; rm -rf "${hljs_dir}" ;;
x1) echo "- Y highlight.js (create: no todo)" ;;
x) echo "- Y highlight.js" ;;
esac
else
case x${1} in
x0) echo "- N highlight.js (delete: no todo)" ;;
x1) echo "- N highlight.js (creating)" ; git_hljs ;;
x) echo "- N highlight.js" ;;
esac
fi
}
# print all available langs
lang_hljs()
{
# filename=$(basename "$fullfile")
# extension="${filename##*.}"
# filename="${filename%.*}"
check_hljs 1
for l in ${root}/data/.node/highlight.js/src/languages/*.js
do
m_base=$(basename ${l})
printf " ${m_base%.*}"
done
}
function build_hljs()
{
# . data/.node/.lang
langs="bash cpp cs css diff dos go ini javascript json less lua makefile markdown powershell python shell sql vbnet xml xquery"
l_dir="${root}/data/.node/highlight.js"
m_ref="$(basename ${l_dir})"
if [ "x${1}" == "xclean" ]; then check_hljs 0 ; fi
# clone if not present
check_hljs 1
if [ -d "${l_dir}" ]; then
echo " - HAVE ${l_dir}"
else
echo " - NO ${l_dir}"
fi
echo " - entering ${m_ref}"
pushd "${l_dir}" > /dev/null
echo " - npm install (be patient - won't be long)"
npm i > /dev/null 2>&1
echo '- `node tools/build.js ${langs}`'
echo ' - (see: ./bs: build_hljs() where langs is defined.)'
node tools/build.js ${langs}
popd > /dev/null
}
function clean_js()
{
echo " - ./data/.node/node_modules"
rm -rf ./data/.node/node_modules
}
function clean_all()
{
echo - clean
clean_js
echo " - public "
rm -rf public
clean_hljs
echo " - ${src_location}/statik"
rm -fr ${src_location}/statik
# echo " - data/templates"
# rm -f data/templates/{markdown,matter}/common.html
}
function do_npm()
{
if [[ ! -d data/.node ]]; then mkdir data/.node ; fi
if [[ ! -f data/.node/package.json ]]; then
echo "- there is no package.json to install packages for!"
echo " - lets fix that"
pushd "data/.node" > /dev/null
echo " - installing nodejs stuffs from scratch!"
rm -drf node_modules
echo "{}" > package.json
npm install [email protected] \
[email protected] \
[email protected] \
[email protected] \
--save
popd > /dev/null
return
fi
pushd "data/.node" > /dev/null
echo - INSTALL NPM MODULES
npm i > /dev/null 2>&1
popd > /dev/null
echo " - now for some reason we need to wait a second or two..."
sleep 2
}
function build_js()
{
tgt_j="public/js"
tgt_c="public/css"
tgt="public/"
nodem="data/.node/node_modules"
nodej="data/.node/js"
nodec="data/.node/css"
function cl()
{
printf -- "${1}"
shift
cp -r ${*}
}
# apparently this is windows (msys2) specific
function check_sassc()
{
echo "- check sassc (windows)"
if [[ -f ${MINGW_PREFIX}/bin/sassc.exe ]]; then has_sass=true ; echo " - found" ; else echo " - not found" ; fi
if [[ "${has_sass}" == "true" ]]; then
sassc data/.node/css/mtree.scss data/.node/css/mtree.css
fi
}
function clone()
{
if [ ! -d public ]; then mkdir public ; fi
mkdir public/{css,fonts,js} > /dev/null 2>&1
# local javascript and css
echo "- copy media" && cp data/gopher.svg public
# cl "- copy local-js" ${nodej}/*.js "${tgt_j}"
cl " - fa-icons (css)" ${nodem}/font-awesome/css/font-awesome.{css,min.css} "${tgt_c}"
cl " boot.js" data/.node/js/boot.js "${tgt_j}"
cl " mtree.js" "data/.node/js/mtree-1.1.0.js" "${tgt_j}"
cl " velocity.js" data/.node/js/velocity-1.2.2{,.min}.js "${tgt_j}"
cl " various-css" data/.node/css/{mark,mtree,gfm}.css "${tgt_c}"
if [ ! -d data/.node/highlight.js ]; then
tag hl
fi
cl "\n - highlight.js" "data/.node/highlight.js/build/highlight.pack.js" "${tgt_j}"
cl " highlight.css" "data/.node/highlight.js/build/demo/styles/zenburn.css" "${tgt_c}"
cl "\n - fa-icons (tt)" "${nodem}/font-awesome/fonts" "${tgt}"
cl "\n - jquery" ${nodem}/jquery/dist/{jquery,jquery.min}.js "${tgt_j}"
cl "\n - monaco.js" "${nodem}/monaco-editor/min/vs" "${tgt_j}"
# NOTE: I need to merge {css,js} of semantic-ui including required modules.
cl "\n - sui.js" ${nodem}/semantic-ui-css/{semantic,semantic.min}.js "${tgt_j}"
cl " sui.css" ${nodem}/semantic-ui-css/{semantic,semantic.min}.css "${tgt_c}"
cl " sui/themes" ${nodem}/semantic-ui-css/themes "${tgt_c}/themes"
printf $"\n"
rm -f ./public/js/{app,main,package}.js
}
# check_sassc
echo - build css
# any generative commands go here (before calling `clone`)
echo " - clean sass"
if [ -f data/css/gfm.css ]; then rm -f data/css/gfm.css ; fi
# if [[ 'false' == 'true' ]]; then
if [ -f ${MINGW_PREFIX}/bin/sassc.exe ]; then
echo " - build sass (sassc)"
sassc -t compressed data/.node/css/gfm.{scss,css}
else
pushd data/.node > /dev/null
echo " - gen sass (npm)"
echo " - if this hangs, there is something wrong with css syntax."
echo " SO FIX IT!"
./build-css
popd > /dev/null
fi
clone
# manage
}
function build_templates()
{
# echo "- skipping template preparation..."
# echo " - this hasn't been set up yet."
# return
echo - prepare HTML templates
tpl_md=./data/templates/markdown/common.html
# tpl_mt=./data/templates/matter/common.html
tpl_cc="./data/templates/common/*"
echo " - concatenating common template files"
cat ${tpl_cc} > ${tpl_md}
# cat ${tpl_cc} > ${tpl_mt}
}
function build_statik()
{
# echo "- build_statik: skipping"
# return
echo - compile statik.go
echo " - remove ${src_location}/statik"
rm -fr ${src_location}/statik
echo " - compile: statik -f"
statik -f
echo " - move statik.go to ./${src_location}"
mv statik ${src_location}
}
function build()
{
echo - go build...
go build -v
}
function run()
{
echo - runing...
./$(basename "$PWD") ${*}
}
function build_license()
{
echo not yet
# JAVASCRIPT LIBRARIES?
# jquery
# semantic-ui
# velocity.js
# font-awesome
# mtree
#
# GO
# gin-gonic/gin
# https://raw.githubusercontent.com/gin-gonic/gin/master/LICENSE
# https://raw.githubusercontent.com/gin-gonic/gin/master/AUTHORS.md
# rekyll/statik (via camlistore)
# Copyright Google Inc.
# https://raw.githubusercontent.com/rakyll/statik/master/LICENSE
# via
# https://raw.githubusercontent.com/camlistore/camlistore/blob/master/COPYING
# https://raw.githubusercontent.com/camlistore/camlistore/blob/master/AUTHORS
# rusross/blackfriday
# https://raw.githubusercontent.com/russross/blackfriday/master/LICENSE.txt
}
function print_help()
{
echo $"${help_string}"
}
function tag()
{
case $1 in
clean | -clean ) clean_all ;;
cleanjs | -cleanjs) clean_js ;;
hl | -hl ) build_hljs clean ;;
hl-get | -hl-get ) check_hljs 1 ;;
hl-rm | -hl-rm ) check_hljs 0 ;;
hl-mk | -hl-mk ) build_hljs ;;
hl-lang | -hl-lang) lang_hljs ;;
npm | -npm ) do_npm ;;
js | -js ) build_js ;;
run | -run ) shift ; run ${*} ;;
br | -br ) build ; shift ; run ${*} ;;
statik | -statik ) build_js ; build_statik ; build ;;
stat | -stat ) build_js ; build_statik ; build ; shift ; run ${*} ;;
build | -build ) build ;;
tpl | -tpl ) build_templates ;;
h | help | -h | -help | --help) print_help ;;
# clean hl js tpl statik build
*) clean_all ; build_hljs ; do_npm ; build_js ; build_templates ; build_statik ; build ;;
esac
}
function bootstrap()
{
if [ "x${1}" == "x" ]; then
print_help
tag
else
while [[ ! -z ${1} ]]; do
para=${1}
shift
if [ "${para}" == "stat" ]; then
echo "tag ${para} ${*}"
tag ${para} ${*}
exit 0
elif [ "${para}" == "br" ]; then
echo "tag ${para} ${*}"
tag ${para} ${*}
exit 0
elif [ "${para}" == "run" ]; then
echo "tag ${para} ${*}"
tag ${para} ${*}
exit 0
else
tag ${para}
fi
done
fi
}
check_requisites
bootstrap ${*}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment