<commit_before>#!/usr/bin/env bash
set -eE
set -o pipefail

# Figure out where the script lives
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SRC=$DIR/..

# These are our input variables
TARGET_REF=$1
ENVIRONMENT=$2

USAGE_STR="Usage: bash release.sh <target-ref> <environment>\n"
USAGE_STR="$USAGE_STR\nenvironment must be 'staging' or 'production'"
if [ -z "$TARGET_REF" -o -z "$ENVIRONMENT" ]; then
    echo "$USAGE_STR"
    exit 1
fi

if [ "$ENVIRONMENT" != "staging" -a "$ENVIRONMENT" != "production" ]; then
    echo "$USAGE_STR"
    exit 1
fi

GIT_REPOSITORY="<EMAIL>:arigatomachine/cli.git"
KEY_FILE="$SRC/keys/$ENVIRONMENT.json"
CA_BUNDLE="$SRC/daemon/ca_bundle.pem"
RELEASE_STAMP=`date -u +"%Y-%m-%dT%H-%M-%SZ"`
BUILD_DIRECTORY=$HOME/build
RELEASE_DIRECTORY="$BUILD_DIRECTORY/$RELEASE_STAMP"
RELEASE_BUCKET="releases.arigato.sh"

if [ ! -f "$KEY_FILE" ]; then
    echo "Cannot find offline public key file: $KEY_FILE"
    exit 1
fi

if [ ! -d "$BUILD_DIRECTORY" ]; then
    echo "Build directory $BUILD_DIRECTORY does not exist; creating."
    mkdir -p $BUILD_DIRECTORY
fi

if ! aws iam get-user > /dev/null; then
    echo "You must be logged into the aws cli to publish a release"
    exit 1
fi

# Fetch the latest greatest and then figure out the pointer of the tag
echo "Attempting to resolve $TARGET_REF to SHA"
git fetch
TARGET_SHA=`git rev-list -n 1 $TARGET_REF`
echo "Resoled $TARGET_REF to $TARGET_SHA"

echo "Changing CWD to $BUILD_DIRECTORY"
cd $BUILD_DIRECTORY

echo "Cloning repository into $BUILD_DIRECTORY/$RELEASE_STAMP"
git clone --reference $SRC $GIT_REPOSITORY $RELEASE_STAMP

echo "Switching into $RELEASE_DIRECTORY"
cd "$RELEASE_DIRECTORY"
git checkout $TARGET_SHA

echo ""
echo "Building Docker Container"
echo ""
docker build -t arigato/cli:$TARGET_SHA .

echo ""
echo "Building Daemon"
echo ""
docker run --name relase-builder --rm \
    -v $RELEASE_DIRECTORY:/go/src/github.com/arigatomachine/cli \
    -v $RELEASE_DIRECTORY/builds:/builds \
    arigato/cli:$TARGET_SHA \
    release

echo ""
echo "Copying Key File"
echo ""
cp $KEY_FILE cli/public_key.json
cp $CA_BUNDLE cli/ca_bundle.pem

TAR_FILENAME="$TARGET_REF"
if [ "$ENVIRONMENT" == "staging" ]; then
  TAR_FILENAME="$TAR_FILENAME+staging"
fi
TAR_FILENAME="$TAR_FILENAME.tar.gz"

echo ""
echo "Creating Distributable ($RELEASE_STAMP.tar.gz) in $RELEASE_DIRECTORY"
echo ""
tar czf "$RELEASE_STAMP.tar.gz" cli/

read -p "Do you wish to publish the release to s3? [yn]" s3_publish
case $s3_publish in
    [Yy]*)
        echo "Uploading $RELEASE_STAMP to https://s3.amazonaws.com/$RELEASE_BUCKET/${TAR_FILENAME/+/%2B}"
        aws s3api put-object --bucket $RELEASE_BUCKET --key "$TAR_FILENAME" \
            --body "$RELEASE_STAMP.tar.gz"
        ;;
    *)
        echo "Not publishing to s3; exiting.. cannot publish to other sources"
        exit 1
        ;;
esac

if [ "$ENVIRONMENT" == "production" ]; then
    read -p "Do you wish to publish to npm? [yn] " npm_publish
    case $npm_publish in
        [Yy]* )
            npm publish $RELEASE_STAMP.tar.gz
            ;;
        * )
            echo "Not publishing to npm"
            ;;
    esac
fi

echo ""
echo "Done; package $RELEASE_DIRECTORY/$RELEASE_STAMP.tar.gz"
echo ""
<commit_msg>Fix release script by installing modules<commit_after>#!/usr/bin/env bash
set -eE
set -o pipefail

# Figure out where the script lives
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SRC=$DIR/..

# These are our input variables
TARGET_REF=$1
ENVIRONMENT=$2

USAGE_STR="Usage: bash release.sh <target-ref> <environment>\n"
USAGE_STR="$USAGE_STR\nenvironment must be 'staging' or 'production'"
if [ -z "$TARGET_REF" -o -z "$ENVIRONMENT" ]; then
    echo "$USAGE_STR"
    exit 1
fi

if [ "$ENVIRONMENT" != "staging" -a "$ENVIRONMENT" != "production" ]; then
    echo "$USAGE_STR"
    exit 1
fi

GIT_REPOSITORY="<EMAIL>:arigatomachine/cli.git"
KEY_FILE="$SRC/keys/$ENVIRONMENT.json"
CA_BUNDLE="$SRC/daemon/ca_bundle.pem"
RELEASE_STAMP=`date -u +"%Y-%m-%dT%H-%M-%SZ"`
BUILD_DIRECTORY=$HOME/build
RELEASE_DIRECTORY="$BUILD_DIRECTORY/$RELEASE_STAMP"
RELEASE_BUCKET="releases.arigato.sh"

if [ ! -f "$KEY_FILE" ]; then
    echo "Cannot find offline public key file: $KEY_FILE"
    exit 1
fi

if [ ! -d "$BUILD_DIRECTORY" ]; then
    echo "Build directory $BUILD_DIRECTORY does not exist; creating."
    mkdir -p $BUILD_DIRECTORY
fi

if ! aws iam get-user > /dev/null; then
    echo "You must be logged into the aws cli to publish a release"
    exit 1
fi

# Fetch the latest greatest and then figure out the pointer of the tag
echo "Attempting to resolve $TARGET_REF to SHA"
git fetch
TARGET_SHA=`git rev-list -n 1 $TARGET_REF`
echo "Resoled $TARGET_REF to $TARGET_SHA"

echo "Changing CWD to $BUILD_DIRECTORY"
cd $BUILD_DIRECTORY

echo "Cloning repository into $BUILD_DIRECTORY/$RELEASE_STAMP"
git clone --reference $SRC $GIT_REPOSITORY $RELEASE_STAMP

echo "Switching into $RELEASE_DIRECTORY"
cd "$RELEASE_DIRECTORY"
git checkout $TARGET_SHA

echo ""
echo "Installing NPM Modules"
echo ""
pushd cli > /dev/null
    npm install
popd > /dev/null

echo ""
echo "Building Docker Container"
echo ""
docker build -t arigato/cli:$TARGET_SHA .

echo ""
echo "Building Daemon"
echo ""
docker run --name relase-builder --rm \
    -v $RELEASE_DIRECTORY:/go/src/github.com/arigatomachine/cli \
    -v $RELEASE_DIRECTORY/builds:/builds \
    arigato/cli:$TARGET_SHA \
    release

echo ""
echo "Copying Key File"
echo ""
cp $KEY_FILE cli/public_key.json
cp $CA_BUNDLE cli/ca_bundle.pem

# Remove the node modules; they'll get installed via npm on the way down.
echo ""
echo "Removing Node Modules"
echo ""
rm -rf cli/node_modules

TAR_FILENAME="$TARGET_REF"
if [ "$ENVIRONMENT" == "staging" ]; then
  TAR_FILENAME="$TAR_FILENAME+staging"
fi
TAR_FILENAME="$TAR_FILENAME.tar.gz"

echo ""
echo "Creating Distributable ($RELEASE_STAMP.tar.gz) in $RELEASE_DIRECTORY"
echo ""
tar czf "$RELEASE_STAMP.tar.gz" cli/

read -p "Do you wish to publish the release to s3? [yn]" s3_publish
case $s3_publish in
    [Yy]*)
        echo "Uploading $RELEASE_STAMP to https://s3.amazonaws.com/$RELEASE_BUCKET/${TAR_FILENAME/+/%2B}"
        aws s3api put-object --bucket $RELEASE_BUCKET --key "$TAR_FILENAME" \
            --body "$RELEASE_STAMP.tar.gz"
        ;;
    *)
        echo "Not publishing to s3; exiting.. cannot publish to other sources"
        exit 1
        ;;
esac

if [ "$ENVIRONMENT" == "production" ]; then
    read -p "Do you wish to publish to npm? [yn] " npm_publish
    case $npm_publish in
        [Yy]* )
            npm publish $RELEASE_STAMP.tar.gz
            ;;
        * )
            echo "Not publishing to npm"
            ;;
    esac
fi

echo ""
echo "Done; package $RELEASE_DIRECTORY/$RELEASE_STAMP.tar.gz"
echo ""

{'fraction_non_alphanumeric': 0.12767843948573962, 'fraction_numerical': 0.00472883109206443, 'mean_word_length': 3.6324435318275152, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '4581757', 'n_tokens_mistral': 2527, 'n_tokens_neox': 2400, 'n_words': 684}