<commit_before>#!/bin/bash

# ADDITIONAL_DOCKER_ARGS is provided for external injection
# of required special parameters in order to support extension
# of DNS handling and HOSTS entries via environment settings.
# It is thus required that it does not get assigned any value.

echo "### start build_docker_image.sh for SIT onboard ###"

set -e

DOCKER_BUILDDIR=$(pwd)/target
MAVEN_REPODIR=${HOME}/.m2/repository
MAVEN_SETTINGS=${HOME}/.m2/settings.xml

DOCKER_REPOSITORY=
BASE_DOCKER_IMAGE=joynr-runtime-environment-base:latest
DOCKER_IMAGE_VERSION=latest
DOCKER_RUN_ADD_FLAGS=

JOYNR_REPODIR=$(pwd)/../../../..
SIT_DIR=$(pwd)/../..
CPP_BUILDDIR=$JOYNR_REPODIR/build
JOBS=4
NVM_DIR="/usr/local/nvm"
NODE_V8=8.16.2


function print_usage {
	echo "
Usage: ./build_docker_image.sh [<options>]

Possible options:

--no-cpp-build: skip the clean / build of the joynr CPP framework
--no-cpp-test-build: skip building the sit-cpp-app
--no-java-build: skip building the sit-java-app and the joynr JAVA framework
--no-node-build: skip building the sit-node-app and the joynr JS framework
-r, --docker-repository <docker repository>: set the value of the DOCKER_REPOSITORY variable, determining
	where the cpp base image is pulled from.
-v, --docker-image-version <docker image version>: set the value of the DOCKER_IMAGE_VERSION variable, determining
	where which version of the docker build images (node, cpp) should be used
--docker-run-flags <run flags>: add some additional flags required when calling docker (e.g. \"--sig-proxy -e DEV_UID=$(id -u)\")
--repo-dir <repository directory>: override the default maven repository directory
--maven-settings <maven settings file>: override the location of the default maven settings file
-j, --jobs <number of build jobs>: the number of build jobs used for parallel C++ builds.
-h, --help: print this information

> Note:
> The --no-XYZ-build options can be used to skip building the given artifact
> inside a Docker container (which can be quite slow depending on your system).
> If you do so, then you have to make sure that the build results are available
> by other means - either you have to have built them locally, or you have to
> have the build results still lying around from previously executing this
> script without the relevant '--no-XYZ-build' option.
"
}

while [[ $# -gt 0 ]]
do
	key="$1"
	case $key in
		--no-cpp-build)
		NO_CPP_BUILD=true
		;;
		--no-cpp-test-build)
		NO_CPP_TEST_BUILD=true
		;;
		--no-java-build)
		NO_JAVA_BUILD=true
		;;
		--no-node-build)
		NO_NODE_BUILD=true
		;;
		-r|--docker-repository)
		DOCKER_REPOSITORY="$2"
		shift
		;;
		-v|--docker-image-version)
		DOCKER_IMAGE_VERSION="$2"
		shift
		;;
		--docker-run-flags)
		DOCKER_RUN_ADD_FLAGS="$2"
		shift
		;;
		--repo-dir)
		MAVEN_REPODIR="$2"
		shift
		;;
		--maven-settings)
		MAVEN_SETTINGS="$2"
		shift
		;;
		-j|--jobs)
		shift
		JOBS=$1
		;;
		-h|--help)
		print_usage
		exit 0
		;;
		*)
		echo "Unknown argument: $1"
		print_usage
		exit 1
		;;
	esac
	shift
done

JAVA_CPP_BUILD_DOCKER_IMAGE=joynr-base:${DOCKER_IMAGE_VERSION}
JS_BUILD_DOCKER_IMAGE=joynr-base:${DOCKER_IMAGE_VERSION}

function execute_in_docker {
	if [ -z "$2" ]; then
		DOCKERIMAGE=${JAVA_CPP_BUILD_DOCKER_IMAGE}
	else
		DOCKERIMAGE=$2
	fi
	docker run --rm -t $DOCKER_RUN_ADD_FLAGS --privileged \
		$ADDITIONAL_DOCKER_ARGS \
		-v $JOYNR_REPODIR:/data/src:Z \
		-v $MAVEN_SETTINGS:/home/joynr/.m2/settings.xml:z \
		-v $MAVEN_REPODIR:/home/joynr/.m2/repository:Z \
		-v $CPP_BUILDDIR:/data/build:Z \
		${DOCKER_REPOSITORY}${DOCKERIMAGE} \
		/bin/sh -c "$1"
}

# create cpp build dir:
# The directory has to be created before the first call of 'execute_in_docker' because it is always
# mounted,not only for C++ builds.
mkdir -p $CPP_BUILDDIR

if [ $NO_JAVA_BUILD ]; then
	echo "Skipping Java build ..."
else
	# Build Java
	execute_in_docker '"echo \"Generate Java API\" && . /etc/profile && cd /data/src && mvn clean install -P no-license-and-notice,no-java-formatter,no-checkstyle -DskipTests"'
fi

if [ $NO_CPP_BUILD ]; then
	echo "Skipping C++ build ..."
else
	# assume joynr JAVA and joynr-generator-standalone are already built and C++ code has been generated

	execute_in_docker '"echo \"Building and packaging MoCOCrW\" && . /etc/profile && /data/src/docker/joynr-base/scripts/test/cpp-build-MoCOCrW-package.sh 2>&1"'

	execute_in_docker '"echo \"Building and packaging mosquitto\" && . /etc/profile && /data/src/docker/joynr-base/scripts/test/cpp-build-mosquitto-package.sh 2>&1"'

	execute_in_docker '"echo \"Building and packaging smrf\" && . /etc/profile && /data/src/docker/joynr-base/scripts/test/cpp-build-smrf-rpm-package.sh 2>&1"'

	execute_in_docker '"echo \"Building joynr c++\" && . /etc/profile && /data/src/docker/joynr-base/scripts/ci/cpp-clean-build.sh --additionalcmakeargs \"-DUSE_PLATFORM_MUESLI=OFF\" --jobs '"${JOBS}"' --enableclangformatter OFF --buildtests OFF 2>&1"'

	execute_in_docker '"echo \"Packaging joynr c++\" && . /etc/profile && /data/src/docker/joynr-base/scripts/test/cpp-build-rpm-package.sh --rpm-spec tests/system-integration-test/docker/onboard/joynr-without-test.spec 2>&1"'

fi

if [ $NO_CPP_TEST_BUILD ]; then
	echo "Skipping C++ test build ..."
else
	# dummyKeychain is also built here
	execute_in_docker '"echo \"Building C++ System Integration Tests\" && . /etc/profile && export JOYNR_INSTALL_DIR=/data/build/joynr && echo \"dir: \$JOYNR_INSTALL_DIR\" && /data/src/docker/joynr-base/scripts/ci/cpp-build-tests.sh system-integration-test --jobs '"${JOBS}"' --clangformatter OFF 2>&1"'
fi

if [ $NO_NODE_BUILD ]; then
	echo "Skipping Node test build ..."
else
	# only build joynr javascript, test-base and sit-node-app
	execute_in_docker '"echo \"Building sit node app\" && . /etc/profile && cd /data/src && mvn clean install -P javascript -am --projects io.joynr.javascript:libjoynr-js,io.joynr.tests:test-base,io.joynr.tests.system-integration-test:sit-node-app && cd tests/system-integration-test/sit-node-app && npm -verbose install"' $JS_BUILD_DOCKER_IMAGE<commit_msg>[SIT] Do not run Java formatter in SIT build script<commit_after>#!/bin/bash

# ADDITIONAL_DOCKER_ARGS is provided for external injection
# of required special parameters in order to support extension
# of DNS handling and HOSTS entries via environment settings.
# It is thus required that it does not get assigned any value.

echo "### start build_docker_image.sh for SIT onboard ###"

set -e

DOCKER_BUILDDIR=$(pwd)/target
MAVEN_REPODIR=${HOME}/.m2/repository
MAVEN_SETTINGS=${HOME}/.m2/settings.xml

DOCKER_REPOSITORY=
BASE_DOCKER_IMAGE=joynr-runtime-environment-base:latest
DOCKER_IMAGE_VERSION=latest
DOCKER_RUN_ADD_FLAGS=

JOYNR_REPODIR=$(pwd)/../../../..
SIT_DIR=$(pwd)/../..
CPP_BUILDDIR=$JOYNR_REPODIR/build
JOBS=4
NVM_DIR="/usr/local/nvm"
NODE_V8=8.16.2


function print_usage {
	echo "
Usage: ./build_docker_image.sh [<options>]

Possible options:

--no-cpp-build: skip the clean / build of the joynr CPP framework
--no-cpp-test-build: skip building the sit-cpp-app
--no-java-build: skip building the sit-java-app and the joynr JAVA framework
--no-node-build: skip building the sit-node-app and the joynr JS framework
-r, --docker-repository <docker repository>: set the value of the DOCKER_REPOSITORY variable, determining
	where the cpp base image is pulled from.
-v, --docker-image-version <docker image version>: set the value of the DOCKER_IMAGE_VERSION variable, determining
	where which version of the docker build images (node, cpp) should be used
--docker-run-flags <run flags>: add some additional flags required when calling docker (e.g. \"--sig-proxy -e DEV_UID=$(id -u)\")
--repo-dir <repository directory>: override the default maven repository directory
--maven-settings <maven settings file>: override the location of the default maven settings file
-j, --jobs <number of build jobs>: the number of build jobs used for parallel C++ builds.
-h, --help: print this information

> Note:
> The --no-XYZ-build options can be used to skip building the given artifact
> inside a Docker container (which can be quite slow depending on your system).
> If you do so, then you have to make sure that the build results are available
> by other means - either you have to have built them locally, or you have to
> have the build results still lying around from previously executing this
> script without the relevant '--no-XYZ-build' option.
"
}

while [[ $# -gt 0 ]]
do
	key="$1"
	case $key in
		--no-cpp-build)
		NO_CPP_BUILD=true
		;;
		--no-cpp-test-build)
		NO_CPP_TEST_BUILD=true
		;;
		--no-java-build)
		NO_JAVA_BUILD=true
		;;
		--no-node-build)
		NO_NODE_BUILD=true
		;;
		-r|--docker-repository)
		DOCKER_REPOSITORY="$2"
		shift
		;;
		-v|--docker-image-version)
		DOCKER_IMAGE_VERSION="$2"
		shift
		;;
		--docker-run-flags)
		DOCKER_RUN_ADD_FLAGS="$2"
		shift
		;;
		--repo-dir)
		MAVEN_REPODIR="$2"
		shift
		;;
		--maven-settings)
		MAVEN_SETTINGS="$2"
		shift
		;;
		-j|--jobs)
		shift
		JOBS=$1
		;;
		-h|--help)
		print_usage
		exit 0
		;;
		*)
		echo "Unknown argument: $1"
		print_usage
		exit 1
		;;
	esac
	shift
done

JAVA_CPP_BUILD_DOCKER_IMAGE=joynr-base:${DOCKER_IMAGE_VERSION}
JS_BUILD_DOCKER_IMAGE=joynr-base:${DOCKER_IMAGE_VERSION}

function execute_in_docker {
	if [ -z "$2" ]; then
		DOCKERIMAGE=${JAVA_CPP_BUILD_DOCKER_IMAGE}
	else
		DOCKERIMAGE=$2
	fi
	docker run --rm -t $DOCKER_RUN_ADD_FLAGS --privileged \
		$ADDITIONAL_DOCKER_ARGS \
		-v $JOYNR_REPODIR:/data/src:Z \
		-v $MAVEN_SETTINGS:/home/joynr/.m2/settings.xml:z \
		-v $MAVEN_REPODIR:/home/joynr/.m2/repository:Z \
		-v $CPP_BUILDDIR:/data/build:Z \
		${DOCKER_REPOSITORY}${DOCKERIMAGE} \
		/bin/sh -c "$1"
}

# create cpp build dir:
# The directory has to be created before the first call of 'execute_in_docker' because it is always
# mounted,not only for C++ builds.
mkdir -p $CPP_BUILDDIR

if [ $NO_JAVA_BUILD ]; then
	echo "Skipping Java build ..."
else
	# Build Java
	execute_in_docker '"echo \"Generate Java API\" && . /etc/profile && cd /data/src && mvn clean install -P no-license-and-notice,no-java-formatter,no-checkstyle -DskipTests"'
fi

if [ $NO_CPP_BUILD ]; then
	echo "Skipping C++ build ..."
else
	# assume joynr JAVA and joynr-generator-standalone are already built and C++ code has been generated

	execute_in_docker '"echo \"Building and packaging MoCOCrW\" && . /etc/profile && /data/src/docker/joynr-base/scripts/test/cpp-build-MoCOCrW-package.sh 2>&1"'

	execute_in_docker '"echo \"Building and packaging mosquitto\" && . /etc/profile && /data/src/docker/joynr-base/scripts/test/cpp-build-mosquitto-package.sh 2>&1"'

	execute_in_docker '"echo \"Building and packaging smrf\" && . /etc/profile && /data/src/docker/joynr-base/scripts/test/cpp-build-smrf-rpm-package.sh 2>&1"'

	execute_in_docker '"echo \"Building joynr c++\" && . /etc/profile && /data/src/docker/joynr-base/scripts/ci/cpp-clean-build.sh --additionalcmakeargs \"-DUSE_PLATFORM_MUESLI=OFF\" --jobs '"${JOBS}"' --enableclangformatter OFF --buildtests OFF 2>&1"'

	execute_in_docker '"echo \"Packaging joynr c++\" && . /etc/profile && /data/src/docker/joynr-base/scripts/test/cpp-build-rpm-package.sh --rpm-spec tests/system-integration-test/docker/onboard/joynr-without-test.spec 2>&1"'

fi

if [ $NO_CPP_TEST_BUILD ]; then
	echo "Skipping C++ test build ..."
else
	# dummyKeychain is also built here
	execute_in_docker '"echo \"Building C++ System Integration Tests\" && . /etc/profile && export JOYNR_INSTALL_DIR=/data/build/joynr && echo \"dir: \$JOYNR_INSTALL_DIR\" && /data/src/docker/joynr-base/scripts/ci/cpp-build-tests.sh system-integration-test --jobs '"${JOBS}"' --clangformatter OFF 2>&1"'
fi

if [ $NO_NODE_BUILD ]; then
	echo "Skipping Node test build ..."
else
	# only build joynr javascript, test-base and sit-node-app
	execute_in_docker '"echo \"Building sit node app\" && . /etc/profile && cd /data/src && mvn clean install -P javascript,no-java-formatter -am --projects io.joynr.javascript:libjoynr-js,io.joynr.tests:test-base,io.joynr.tests.system-integration-test:sit-node-app && cd tests/system-integration-test/sit-node-app && npm -verbose install"' $JS_BUILD_DOCKER_IMAGE
{'fraction_non_alphanumeric': 0.13655115511551155, 'fraction_numerical': 0.005940594059405941, 'mean_word_length': 3.9757799671592773, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '1792759', 'n_tokens_mistral': 4580, 'n_tokens_neox': 4249, 'n_words': 1132}