<commit_before>#!/bin/bash

set -e
# set -x

NAME=${PROJECT:-project}
DIST_PATH=${DIST_PATH:-"$NAME"/static/"$NAME"}
OWNER_UID=$(id -u)
BRANCH=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
IS_PRODUCTION=0
if [ "$BRANCH" = "master" ]; then IS_PRODUCTION=1; fi

. color_log.sh

function sum {
    md5sum $1 | cut -d ' ' -f 1
}

function has_changed {
    mkdir -p .cache
    local p="$PWD/.cache/$1"
    if [ ! -f "$p" ]; then
        echo "true"
        return
    fi
    n=$(sum $1)
    o=$(cat $p)
    if [ "$n" == "$o" ]; then
        echo "false"
    else
        echo "true"
    fi
}

function npm_cache {
    log "INFO" "Starting ${FUNCNAME[0]}"
    local cache_image="$NAME"-npm
    local cache_dir=node_modules
    local cache_file=package.json
    if [ "$(has_changed $cache_file)"  == "false" ]; then
        log "SUCCESS" "Skipping ${FUNCNAME[0]} because $cache_file is unchanged"
    else
        docker build \
            -t $cache_image \
            -f docker/npm.docker \
            .
        mkdir -p $cache_dir
        docker run \
            --rm \
            -v "$(pwd)"/$cache_dir:/usr/src/app/$cache_dir \
            $cache_image
        echo $(sum "$cache_file") > .cache/$cache_file
        log "SUCCESS" "Finished ${FUNCNAME[0]}"
    fi
}

function frontend {
    log "INFO" "Starting ${FUNCNAME[0]}"
    local image="$NAME"-fe
    local target="$PWD"/"$DIST_PATH"
    # TODO add IS_PRODUCTION
	docker build \
		-t $image \
		-f docker/frontend.docker \
		.
    rm -rf $target/*.*
	docker run \
		--rm \
		-v $target:/usr/src/app/"$DIST_PATH" \
		$image \
        sh -c "npm run build && chown -R $OWNER_UID:$OWNER_UID ."
    gzip --keep $target/*.js
    log "SUCCESS" "Finished ${FUNCNAME[0]}"
}

function wheelhouse {
    log "INFO" "Starting ${FUNCNAME[0]}"
    local image="$NAME"-wheel
    local cache_file=requirements.txt
    if [ "$(has_changed $cache_file)"  == "false" ]; then
        log "SUCCESS" "Skipping ${FUNCNAME[0]} because $cache_file is unchanged"
    else
        docker build \
            -t $image \
            -f docker/wheel.docker \
            .
        mkdir -p wheelhouse
        docker run \
            --rm \
            -v "$(pwd)"/wheelhouse:/wheelhouse \
            $image
        echo $(sum "$cache_file") > .cache/$cache_file
        log "SUCCESS" "Finished ${FUNCNAME[0]}"
    fi
}

function app {
    log "INFO" "Starting ${FUNCNAME[0]}"
    local image="$NAME"-application
    docker build \
        -t $image \
        -f docker/app.docker \
        .
    log "SUCCESS" "Finished ${FUNCNAME[0]}"
}

npm_cache
frontend
wheelhouse
app
<commit_msg>Update build process to have no npm caching step because yarn<commit_after>#!/bin/bash

set -e

NAME=${PROJECT:-project}
DIST_PATH=${DIST_PATH:-"$NAME"/static/"$NAME"}
OWNER_UID=$(id -u)
BRANCH=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
IS_PRODUCTION=0
if [ "$BRANCH" = "master" ]; then IS_PRODUCTION=1; fi
script_name=$0

function log() {
    function logline() {
        colorprint $1 "[$script_name] $2: $3"
    }

    case $1 in
        INFO)
            logline "1;37" "$1" "$2" ;;
        WARN)
            logline 33 "$1" "$2" >&2 ;;
        ERROR)
            logline 31 "$1" "$2" >&2 ;;
        DEBUG)
            logline 0 "$1" "$2" ;;
        SUCCESS)
            logline "32" "$1" "$2" ;;
        *)
            log "ERROR" "unknown log level"
            return 1
            ;;
    esac
}

function colorprint() {
    # $1 = color number, for example 31, 33, 1;37
    # $2 = message to be echoed
    echo -e "\033[$1m$2\033[0m" 
}

function frontend {
    log "INFO" "Starting ${FUNCNAME[0]}"
    local image="$NAME"-fe
    local target="$PWD"/"$DIST_PATH"
    # TODO add IS_PRODUCTION
	docker build \
		-t $image \
		-f docker/frontend.docker \
		.
    rm -rf $target/*.*
	docker run \
		--rm \
		-v $target:/usr/src/app/"$DIST_PATH" \
		$image \
        sh -c "npm run build && chown -R $OWNER_UID:$OWNER_UID ."
    gzip --keep $target/*.js
    log "SUCCESS" "Finished ${FUNCNAME[0]}"
}

function wheelhouse {
    log "INFO" "Starting ${FUNCNAME[0]}"
    local image="$NAME"-wheel
    local cache_file=requirements.txt
	docker build \
		-t $image \
		-f docker/wheel.docker \
		.
	mkdir -p wheelhouse
	docker run \
		--rm \
		-v "$(pwd)"/wheelhouse:/wheelhouse \
		$image
	log "SUCCESS" "Finished ${FUNCNAME[0]}"
}

function app {
    log "INFO" "Starting ${FUNCNAME[0]}"
    local image="$NAME"-application
    docker build \
        -t $image \
        -f docker/app.docker \
        .
    log "SUCCESS" "Finished ${FUNCNAME[0]}"
}

frontend
wheelhouse
app

{'fraction_non_alphanumeric': 0.16009532062391682, 'fraction_numerical': 0.015381282495667245, 'mean_word_length': 2.10699865410498, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3118397', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1615, 'n_words': 410}