<commit_before>#!/usr/bin/env bash
##  ------------------------------------------------------------------------  ##
##                     Build project working directory                        ##
##  ------------------------------------------------------------------------  ##
#
#   Script to initialize project
# - Install required node and bower packages
# - Install framework engine
# - Update framework installation with project's source scripts.
# - Deploy dist code into public web
# - Consist of:
#   -   usage
#   -   preSetupChecks
#   -   depsChecks
#   -   Build
#   -   Deploy

if [ -n "$APP_DEBUG" ]; then
    set -x
fi

set -e
trap 'echo >&2 Ctrl+C captured, exiting; exit 1' SIGINT

## Source settings
if [ -f ./setup.rc ]; then
    source setup.rc
else
    exit 1
fi


function usage () {
    >&2 cat << EOM
            ${BWhite}Build application${NC}

Usage: $0 <command> [<params>]

    $0 <usage | help | h>   -   Show usage information
    $0 <test | t>           -   Perform environment tests
    $0 <prepare | prep | p> -   Install PHP, BOWER and NPM dependencies.
    $0 <build | b>          -   Build project directory
    $0 <deploy | d>         -   Sync sites public web directory (<webroot> by default)
    $0 <rebuild | rb>       -   Perform <build> and then <deploy> tasks

EOM
    RETVAL=1
}


##  ------------------------------------------------------------------------  ##
##                                PREREQUISITES                               ##
##  ------------------------------------------------------------------------  ##

OPTS=$@

WD="$(cd $(dirname $0) && pwd -P)"      #   Current working directory
APP_HOME="$(pwd)/"                      #   Current directory
APP_PATH="${APP_HOME}${APP_DIR}"        #   Full path to target directory
ENGINE_DIR="${ENGINE_NAME}-${ENGINE_VERSION}"

CODE_VERSION="$(cat ./VERSION)"
GIT_COMMIT="$(git rev-list --remove-empty --remotes --max-count=1 --date-order --reverse)"

printf "${GIT_COMMIT}" > COMMIT

SRC="${WD}/src"
BUILD="${WD}/build"
DIST="${WD}/dist"

DATE="$(date +"%Y%m%d%H%M%S")"
DATETIME="$(date "+%Y-%m-%d")_$(date "+%H-%M-%S")"

# printf "\n----------------------------  ${DATE}  ---------------------------\n";

##  ------------------------------------------------------------------------  ##
##                                 FUNCTIONS                                  ##
##  ------------------------------------------------------------------------  ##

source bin/f.sh
source bin/f-engine.sh
source bin/f-node.sh
source bin/f-php-composer.sh
source bin/host-checks.sh

function logEnv () {
    splash "$FUNCNAME Started with: (${@})";

    info "CODE_VERSION =  ${CODE_VERSION}";
    info "GIT_COMMIT = \t ${GIT_COMMIT}";
    info "WD = \t\t ${WD}";
    info "SRC = \t\t ${SRC}";
    info "BUILD = \t ${BUILD}";
    info "ENGINE_DIR = \t ${ENGINE_DIR}";
    info "APP_PATH = \t ${APP_PATH}";
    info "WEB_USER = \t ${WEB_USER}";
    info "OPTS = \t ${OPTS}";

    info "$FUNCNAME Finished";
    return 0;
}

##  ------------------------------------------------------------------------  ##
##                                PRE-CHECKS                                  ##
##  ------------------------------------------------------------------------  ##

function preSetupChecks () {
    splash "$FUNCNAME Started with: (${@})";

    okNode
    okNpm
    okBower
    okGulp

    info "$FUNCNAME Finished";
    return 0;
}


##  ------------------------------------------------------------------------  ##
##                                 EXECUTION                                  ##
##  ------------------------------------------------------------------------  ##

function depsChecks () {
    splash "$FUNCNAME Started with: (${@})";

    check_composer
    Delay

    check_engine
    Delay

    # fix_permissions
    # sleep 1;

    deps_install
    Delay

    # deps_outdated
    # sleep 1;

    info "$FUNCNAME Finished";
    exit 0;
}


function Build () {
    splash "$FUNCNAME Started with: (${@})";

    cd ${WD}
    gulp --env=${APP_ENV} #--verbose
    Delay

    cd ${WD}
    mv -p "${BUILD}/.env" "${BUILD}/.env.${DATE}" 2>/dev/null
    cp -pr ./setup.rc "${BUILD}/.env.setup"
    cp -pr "${SRC}/composer.json" "${BUILD}/"
    Delay

    cd ${BUILD}
    composer -v update
    Delay

    cd ${WD}
    sudo chown -R ${WEB_USER}:${WEB_USER} "${BUILD}/"
    Delay

    info "$FUNCNAME Finished";
    exit 0;
}


function Deploy () {
    splash "$FUNCNAME params: (${@})";

    cd ${WD}
    gulp sync:web --env=${APP_ENV} --verbose
    Delay

    cd ${WD}
    cd "${WD}/${APP_DIR}/public/"
    ln -s ../storage/media/audio/ >&2 2>/dev/null
    Delay

    cd ${WD}
    sudo chown -R ${WEB_USER}:${WEB_USER} "${APP_DIR}"
    Delay

    gulp artisan:clear --env=${APP_ENV} --verbose
    Delay

    info "$FUNCNAME Finished";
    exit 0;
}


##  ------------------------------------------------------------------------  ##
##                                  EXECUTION                                 ##
##  ------------------------------------------------------------------------  ##
printf "\n-------------------------\t $0 $1 \t----------------------------\n";

logEnv

case "$1" in

    "")
        log "without params";
        usage
        RETVAL=0
    ;;<commit_msg>no message<commit_after>#!/usr/bin/env bash
##  ------------------------------------------------------------------------  ##
##                     Build project working directory                        ##
##  ------------------------------------------------------------------------  ##
#
#   Script to initialize project
# - Install required node and bower packages
# - Install framework engine
# - Update framework installation with project's source scripts.
# - Deploy dist code into public web
# - Consist of:
#   -   usage
#   -   preSetupChecks
#   -   depsChecks
#   -   Build
#   -   Deploy

if [ -n "$APP_DEBUG" ]; then
    set -x
fi

set -e
trap 'echo >&2 Ctrl+C captured, exiting; exit 1' SIGINT

## Source settings
if [ -f ./setup.rc ]; then
    source setup.rc
else
    exit 1
fi


function usage () {
    >&2 cat << EOM
            ${BWhite}Build application${NC}

Usage: $0 <command> [<params>]

    $0 <usage | help | h>   -   Show usage information
    $0 <test | t>           -   Perform environment tests
    $0 <prepare | prep | p> -   Install PHP, BOWER and NPM dependencies.
    $0 <build | b>          -   Build project directory
    $0 <deploy | d>         -   Sync sites public web directory (<webroot> by default)
    $0 <rebuild | rb>       -   Perform <build> and then <deploy> tasks

EOM
    RETVAL=1
}


##  ------------------------------------------------------------------------  ##
##                                PREREQUISITES                               ##
##  ------------------------------------------------------------------------  ##

OPTS=$@

WD="$(cd $(dirname $0) && pwd -P)"      #   Current working directory
APP_HOME="$(pwd)/"                      #   Current directory
APP_PATH="${APP_HOME}${APP_DIR}"        #   Full path to target directory
ENGINE_DIR="${ENGINE_NAME}-${ENGINE_VERSION}"

CODE_VERSION="$(cat ./VERSION)"
GIT_COMMIT="$(git rev-list --remove-empty --remotes --max-count=1 --date-order --reverse)"

printf "${GIT_COMMIT}" > COMMIT

SRC="${WD}/src"
BUILD="${WD}/build"
DIST="${WD}/dist"

DATE="$(date +"%Y%m%d%H%M%S")"
DATETIME="$(date "+%Y-%m-%d")_$(date "+%H-%M-%S")"

# printf "\n----------------------------  ${DATE}  ---------------------------\n";

##  ------------------------------------------------------------------------  ##
##                                 FUNCTIONS                                  ##
##  ------------------------------------------------------------------------  ##

source bin/f.sh
source bin/f-engine.sh
source bin/f-node.sh
source bin/f-php-composer.sh
source bin/host-checks.sh

function logEnv () {
    splash "$FUNCNAME Started with: (${@})";

    info "CODE_VERSION =  ${CODE_VERSION}";
    info "GIT_COMMIT = \t ${GIT_COMMIT}";
    info "WD = \t\t ${WD}";
    info "SRC = \t\t ${SRC}";
    info "BUILD = \t ${BUILD}";
    info "ENGINE_DIR = \t ${ENGINE_DIR}";
    info "APP_PATH = \t ${APP_PATH}";
    info "WEB_USER = \t ${WEB_USER}";
    info "OPTS = \t ${OPTS}";

    info "$FUNCNAME Finished";
    return 0;
}

##  ------------------------------------------------------------------------  ##
##                                PRE-CHECKS                                  ##
##  ------------------------------------------------------------------------  ##

function preSetupChecks () {
    splash "$FUNCNAME Started with: (${@})";

    okNode
    okNpm
    okBower
    okGulp

    info "$FUNCNAME Finished";
    return 0;
}


##  ------------------------------------------------------------------------  ##
##                                 EXECUTION                                  ##
##  ------------------------------------------------------------------------  ##

function depsChecks () {
    splash "$FUNCNAME Started with: (${@})";

    check_composer
    Delay

    check_engine
    Delay

    # fix_permissions
    # sleep 1;

    deps_install
    Delay

    # deps_outdated
    # sleep 1;

    info "$FUNCNAME Finished";
    exit 0;
}


function Build () {
    splash "$FUNCNAME Started with: (${@})";

    cd ${WD}
    node gulpfile.js --env=${APP_ENV} #--verbose
    Delay

    cd ${WD}
    mv -p "${BUILD}/.env" "${BUILD}/.env.${DATE}" 2>/dev/null
    cp -pr ./setup.rc "${BUILD}/.env.setup"
    cp -pr "${SRC}/composer.json" "${BUILD}/"
    Delay

    cd ${BUILD}
    composer -v update
    Delay

    cd ${WD}
    sudo chown -R ${WEB_USER}:${WEB_USER} "${BUILD}/"
    Delay

    info "$FUNCNAME Finished";
    exit 0;
}


function Deploy () {
    splash "$FUNCNAME params: (${@})";

    cd ${WD}
    node gulpfile.js sync:web --env=${APP_ENV} --verbose
    Delay

    cd ${WD}
    cd "${WD}/${APP_DIR}/public/"
    ln -s ../storage/media/audio/ >&2 2>/dev/null
    Delay

    cd ${WD}
    sudo chown -R ${WEB_USER}:${WEB_USER} "${APP_DIR}"
    Delay

    node gulpfile.js artisan:clear --env=${APP_ENV} --verbose
    Delay

    info "$FUNCNAME Finished";
    exit 0;
}


##  ------------------------------------------------------------------------  ##
##                                  EXECUTION                                 ##
##  ------------------------------------------------------------------------  ##
printf "\n-------------------------\t $0 $1 \t----------------------------\n";

logEnv

case "$1" in

    "")
        log "without params";
        usage
        RETVAL=0
    ;;
{'fraction_non_alphanumeric': 0.31766260738223356, 'fraction_numerical': 0.005286509959407155, 'mean_word_length': 1.1078392359729408, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6128305', 'n_tokens_mistral': 3142, 'n_tokens_neox': 2953, 'n_words': 752}