diff --git a/circle.yml b/circle.yml new file mode 100644 index 00000000..ad967305 --- /dev/null +++ b/circle.yml @@ -0,0 +1,24 @@ +machine: + node: + version: 5.1.0 + +general: + branches: + only: + - master # ignore PRs and branches + +dependencies: + pre: + - ./scripts/prepare.sh + cache_directories: + - "~/ionic-site" # cache ionic-site + +test: + override: + - ./scripts/test.sh + +deployment: + staging: + branch: master + commands: + - ./scripts/deploy.sh diff --git a/scripts/config.json b/scripts/config.json new file mode 100644 index 00000000..eb2cf24b --- /dev/null +++ b/scripts/config.json @@ -0,0 +1,7 @@ +{ + "sitePath": "../ionic-site", + "docsDir": "docs/v2/components", + "docsDest": "../ionic-site/docs/v2/components", + "demoDir": "dist/preview-app", + "demoDest": "../ionic-site/dist/preview-app" +} diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100755 index 00000000..fd258fb8 --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +echo "##### " +echo "##### ci/deploy.sh" +echo "#####" + + +function init { + SITE_PATH=$(readJsonProp "config.json" "sitePath") + DEMO_DEST=$(readJsonProp "config.json" "demoDest") + cd .. + export BASE_DIR=$PWD + SITE_DIR=$BASE_DIR/$SITE_PATH +} + +function run { + VERSION=$(readJsonProp "package.json" "version") + + # process new docs + echo "Copying www to $DEMO_DEST" + rm -R $DEMO_DEST/www + cp -R www $DEMO_DEST + + # CD in to the site dir to commit updated docs + cd $SITE_DIR + + CHANGES=$(git status --porcelain) + + # if no changes, don't commit + if [[ "$CHANGES" == "" ]]; then + #if [[ true ]]; then + echo "-- No changes detected for the following commit, docs not updated." + echo "https://github.com/driftyco/$CIRCLE_PROJECT_REPONAME/commit/$CIRCLE_SHA1" + else + git config --global user.email "hi@ionicframework.com" + git config --global user.name "Ionitron" + git add -A + git commit -am "Automated build of driftyco/$CIRCLE_PROJECT_REPONAME@$CIRCLE_SHA1" + # in case a different commit was pushed to ionic-site during doc/demo gen, + # try to rebase around it before pushing + git fetch + git rebase + + git push origin master + + echo "-- Updated docs for $VERSION_NAME succesfully!" + fi +} + +source $(dirname $0)/utils.inc.sh diff --git a/scripts/git/clone.sh b/scripts/git/clone.sh new file mode 100755 index 00000000..d4e2d7b9 --- /dev/null +++ b/scripts/git/clone.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +ARG_DEFS=( + "--repository=(.*)" + "--directory=(.*)" + "[--depth=(.*)]" + "[--branch=(.*)]" +) + +function run { + rm -rf $DIRECTORY + mkdir -p $DIRECTORY + + echo "-- Cloning $REPOSITORY#$BRANCH to $DIRECTORY..." + + ARGS="--branch=${BRANCH:-master} --depth=3" + if [[ "$DEPTH" != "" ]]; then + ARGS="$ARGS --depth=$DEPTH" + fi + + git config --global user.email "hi@ionicframework.com" + git config --global user.name "Ionitron" + + git clone git@github.com:driftyco/$REPOSITORY.git $DIRECTORY $ARGS + cd $DIRECTORY + git fetch origin --tags + cd ../ +} + +source $(dirname $0)/../utils.inc.sh diff --git a/scripts/prepare.sh b/scripts/prepare.sh new file mode 100755 index 00000000..c4732baa --- /dev/null +++ b/scripts/prepare.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +echo "##### " +echo "##### prepare.sh" +echo "#####" + + +function init { + SITE_PATH=$(readJsonProp "config.json" "sitePath") + cd .. + export IONIC_DIR=$PWD + SITE_DIR=$IONIC_DIR/$SITE_PATH +} + +function run { + + if [ ! -d "$SITE_DIR" ]; then + echo "checking out" + cd ./scripts + ./git/clone.sh --repository="ionic-site" \ + --directory="$SITE_DIR" \ + --branch="master" + else + echo "using existing" + cd $SITE_DIR + git reset --hard + git pull origin master + fi +} + +source $(dirname $0)/utils.inc.sh diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100755 index 00000000..0046b819 --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +echo "##### " +echo "##### test.sh" +echo "#####" + + +# function init { +# +# } + +function run { + + npm run build --dev +} + +source $(dirname $0)/utils.inc.sh diff --git a/scripts/utils.inc.sh b/scripts/utils.inc.sh new file mode 100755 index 00000000..08f3d8fe --- /dev/null +++ b/scripts/utils.inc.sh @@ -0,0 +1,244 @@ +# bash utils from angularjs + +# This file provides: +# - a default control flow +# * initializes the environment +# * call a function in your script based on the arguments +# - named argument parsing and automatic generation of the "usage" for your script +# - utility functions +# +# Usage: +# - define the variable ARGS_DEF (see below) with the arguments for your script +# - include this file using `source utils.inc` at the end of your script. +# +# Default control flow: +# 0. Set the current directory to the directory of the script. By this +# the script can be called from anywhere. +# 1. Parse the named arguments +# 2. [Redacted] +# 3. If the parameter "verbose" is set, the `-x` flag will be set in bash. +# 4. The function "init" will be called if it exists +# 5. If the parameter "action" is set, it will call the function with the name of that parameter. +# Otherwise the function "run" will be called. +# +# Named Argument Parsing: +# - The variable ARGS_DEF defines the valid command arguments +# * Required args syntax: --paramName=paramRegex +# * Optional args syntax: [--paramName=paramRegex] +# * e.g. ARG_DEFS=("--required_param=(.+)" "[--optional_param=(.+)]") +# - Checks that: +# * all arguments match to an entry in ARGS_DEF +# * all required arguments are present +# * all arguments match their regex +# - Afterwards, every paramter value will be stored in a variable +# with the name of the parameter in upper case (with dash converted to underscore). +# +# Special arguments that are always available: +# - "--action=.*": This parameter will be used to dispatch to a function with that name when the +# script is started + +# - "--verbose=true": This will set the `-x` flag in bash so that all calls will be logged +# +# Utility functions: +# - readJsonProp +# - replaceJsonProp +# - resolveDir +# - getVar +# - serVar +# - isFunction + +# always stop on errors +set -e + +function usage { + echo "Usage: ${0} ${ARG_DEFS[@]}" + exit 1 +} + + +function parseArgs { + local REQUIRED_ARG_NAMES=() + + # -- helper functions + function varName { + # everything to upper case and dash to underscore + echo ${1//-/_} | tr '[:lower:]' '[:upper:]' + } + + function readArgDefs { + local ARG_DEF + local AD_OPTIONAL + local AD_NAME + local AD_RE + + # -- helper functions + function parseArgDef { + local ARG_DEF_REGEX="(\[?)--([^=]+)=(.*)" + if [[ ! $1 =~ $ARG_DEF_REGEX ]]; then + echo "Internal error: arg def has wrong format: $ARG_DEF" + exit 1 + fi + AD_OPTIONAL="${BASH_REMATCH[1]}" + AD_NAME="${BASH_REMATCH[2]}" + AD_RE="${BASH_REMATCH[3]}" + if [[ $AD_OPTIONAL ]]; then + # Remove last bracket for optional args. + # Can't put this into the ARG_DEF_REGEX somehow... + AD_RE=${AD_RE%?} + fi + } + + # -- run + for ARG_DEF in "${ARG_DEFS[@]}" + do + parseArgDef $ARG_DEF + + local AD_NAME_UPPER=$(varName $AD_NAME) + setVar "${AD_NAME_UPPER}_OPTIONAL" "$AD_OPTIONAL" + setVar "${AD_NAME_UPPER}_RE" "$AD_RE" + if [[ ! $AD_OPTIONAL ]]; then + REQUIRED_ARG_NAMES+=($AD_NAME) + fi + done + } + + function readAndValidateArgs { + local ARG_NAME + local ARG_VALUE + local ARG_NAME_UPPER + + # -- helper functions + function parseArg { + local ARG_REGEX="--([^=]+)=?(.*)" + + if [[ ! $1 =~ $ARG_REGEX ]]; then + echo "Can't parse argument $i" + usage + fi + + ARG_NAME="${BASH_REMATCH[1]}" + ARG_VALUE="${BASH_REMATCH[2]}" + ARG_NAME_UPPER=$(varName $ARG_NAME) + } + + function validateArg { + local AD_RE=$(getVar ${ARG_NAME_UPPER}_RE) + + if [[ ! $AD_RE ]]; then + echo "Unknown option: $ARG_NAME" + usage + fi + + if [[ ! $ARG_VALUE =~ ^${AD_RE}$ ]]; then + echo "Wrong format: $ARG_NAME" + usage; + fi + + # validate that the "action" option points to a valid function + if [[ $ARG_NAME == "action" ]] && ! isFunction $ARG_VALUE; then + echo "No action $ARG_VALUE defined in this script" + usage; + fi + } + + # -- run + for i in "$@" + do + parseArg $i + validateArg + setVar "${ARG_NAME_UPPER}" "$ARG_VALUE" + done + } + + function checkMissingArgs { + local ARG_NAME + for ARG_NAME in "${REQUIRED_ARG_NAMES[@]}" + do + ARG_VALUE=$(getVar $(varName $ARG_NAME)) + + if [[ ! $ARG_VALUE ]]; then + echo "Missing: $ARG_NAME" + usage; + fi + done + } + + # -- run + readArgDefs + readAndValidateArgs "$@" + checkMissingArgs + +} + +# getVar(varName) +function getVar { + echo ${!1} +} + +# setVar(varName, varValue) +function setVar { + eval "$1=\"$2\"" +} + +# isFunction(name) +# - to be used in an if, so return 0 if successful and 1 if not! +function isFunction { + if [[ $(type -t $1) == "function" ]]; then + return 0 + else + return 1 + fi +} + +# readJsonProp(jsonFile, property) +# - restriction: property needs to be on an own line! +function readJsonProp { + echo $(sed -En 's/.*"'$2'"[ ]*:[ ]*"(.*)".*/\1/p' $1) +} + +# replaceJsonProp(jsonFile, property, newValue) +# - note: propertyRegex will be automatically placed into a +# capturing group! -> all other groups start at index 2! +function replaceJsonProp { + replaceInFile $1 "\"$2\": \".*?\"" "\"$2\": \"$3\"" +} + +# replaceInFile(file, findPattern, replacePattern) +function replaceInFile { + perl -pi -e "s/$2/$3/g;" $1 +} + +# resolveDir(relativeDir) +# - resolves a directory relative to the current script +function resolveDir { + echo $(cd $SCRIPT_DIR; cd $1; pwd) +} + +function main { + # normalize the working dir to the directory of the script + cd $(dirname $0);SCRIPT_DIR=$(pwd) + + ARG_DEFS+=("[--verbose=(true|false)]") + parseArgs "$@" + + + # --verbose argument + if [[ $VERBOSE == "true" ]]; then + set -x + fi + + if isFunction init; then + init "$@" + fi + + # jump to the function denoted by the --action argument, + # otherwise call the "run" function + if [[ $ACTION ]]; then + $ACTION "$@" + else + run "$@" + fi +} + + +main "$@"