<commit_before>#!/bin/bash

THIS_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

set -e

#
# Detect Xcode major version
xcode_major_version=""
major_version_regex="Xcode ([0-9]).[0-9]"
out=$(xcodebuild -version)
if [[ "${out}" =~ ${major_version_regex} ]] ; then
	xcode_major_version="${BASH_REMATCH[1]}"
fi

if [ ! "${xcode_major_version}" == "7" ] && [ ! "${xcode_major_version}" == "6" ] ; then
	echo "Invalid xcode major version: ${xcode_major_version}"
	exit 1
fi

echo "(i) xcode_major_version: ${xcode_major_version}"


#
# Required parameters
if [ -z "${project_path}" ] ; then
	echo "[!] Missing required input: project_path"
	exit 1
fi

if [ -z "${scheme}" ] ; then
	echo "[!] Missing required input: scheme"
	exit 1
fi

if [ -z "${output_dir}" ] ; then
	echo "[!] Missing required input: output_dir"
	exit 1
fi


if [ ! -z "${export_options_path}" ] && [[ "${xcode_major_version}" == "6" ]] ; then
	echo "(!) xcode_major_version = 6, export_options_path only used if xcode_major_version > 6"
	export_options_path=""
fi

if [[ "${output_tool}" != "xcpretty" && "${output_tool}" != "xcodebuild" ]] ; then
	echo "[!] Invalid output_tool: ${output_tool}"
	exit 1
fi

set +e

if [[ "${output_tool}" == "xcpretty" ]] ; then
	xcpretty_version=$(xcpretty --version)
	exit_code=$?
	if [[ $exit_code != 0 || -z $xcpretty_version ]] ; then
		echo
		echo " (!) xcpretty is not installed"
		echo "     For xcpretty installation see: 'https://github.com/supermarin/xcpretty',"
		echo "     or use 'xcodebuild' as 'output_tool'."
		echo
		exit 1
	fi
fi

set -e

#
# Project-or-Workspace flag
if [[ "${project_path}" == *".xcodeproj" ]]; then
	CONFIG_xcode_project_action="-project"
elif [[ "${project_path}" == *".xcworkspace" ]]; then
	CONFIG_xcode_project_action="-workspace"
else
	echo "Failed to get valid project file (invalid project file): ${project_path}"
	exit 1
fi

# abs out dir pth
mkdir -p "${output_dir}"
cd "${output_dir}"
output_dir="$(pwd)"
cd -

archive_tmp_dir=$(mktemp -d -t bitrise-xcarchive)
archive_path="${archive_tmp_dir}/${scheme}.xcarchive"
ipa_path="${output_dir}/${scheme}.ipa"
dsym_zip_path="${output_dir}/${scheme}.dSYM.zip"

if [ -z "${workdir}" ] ; then
	workdir="$(pwd)"
fi

#
# Print configs
echo
echo "========== Configs =========="
echo " * CONFIG_xcode_project_action: ${CONFIG_xcode_project_action}"
echo " * output_tool: ${output_tool}"
echo " * project_path: ${project_path}"
echo " * scheme: ${scheme}"
echo " * workdir: ${workdir}"
echo " * output_dir: ${output_dir}"
echo " * archive_path: ${archive_path}"
echo " * ipa_path: ${ipa_path}"
echo " * dsym_zip_path: ${dsym_zip_path}"
echo " * is_force_code_sign: ${is_force_code_sign}"
echo " * is_clean_build: ${is_clean_build}"
echo " * configuration: ${configuration}"

if [ ! -z "${export_options_path}" ] ; then
	echo " * export_options_path: ${export_options_path}"
fi

if [ ! -z "${workdir}" ] ; then
	echo
	echo " -> Switching to working directory: ${workdir}"
	cd "${workdir}"
fi

xcode_configuration=''
if [ ! -z "${configuration}" ] ; then
	xcode_configuration="-configuration ${configuration}"
fi


clean_build_param=''
if [[ "${is_clean_build}" == "yes" ]] ; then
	clean_build_param='clean'
fi


#
# Cleanup function
function finalcleanup {
	local fail_msg="$1"

	echo "-> finalcleanup"

	if [ ! -z "${fail_msg}" ] ; then
		echo " [!] ERROR: ${fail_msg}"
		exit 1
	fi
}


#
# Main

#
# Bit of cleanup
if [ -f "${ipa_path}" ] ; then
	echo " (!) IPA at path (${ipa_path}) already exists - removing it"
	rm "${ipa_path}"
fi

echo
echo
echo "=> Create the Archive ..."

#
# Create the Archive with Xcode Command Line tools
archive_cmd="xcodebuild ${CONFIG_xcode_project_action} \"${project_path}\""
archive_cmd="$archive_cmd -scheme \"${scheme}\" ${xcode_configuration}"
archive_cmd="$archive_cmd ${clean_build_param} archive -archivePath \"${archive_path}\""

if [[ "${is_force_code_sign}" == "yes" ]] ; then
	echo " (!) Using Force Code Signing mode!"

	archive_cmd="$archive_cmd PROVISIONING_PROFILE=\"${BITRISE_PROVISIONING_PROFILE_ID}\""
	archive_cmd="$archive_cmd CODE_SIGN_IDENTITY=\"${BITRISE_CODE_SIGN_IDENTITY}\""
fi

if [[ "${output_tool}" == "xcpretty" ]] ; then
	archive_cmd="set -o pipefail && $archive_cmd | xcpretty"
fi

echo
echo "archive command:"
echo "$archive_cmd"
echo

eval $archive_cmd

echo
echo
echo "=> Exporting IPA from generated Archive ..."
echo

export_command="xcodebuild -exportArchive"

if [[ "${xcode_major_version}" == "6" ]] ; then
	#<commit_msg>Minor log enhancement<commit_after>#!/bin/bash

THIS_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

set -e

#
# Detect Xcode major version
xcode_major_version=""
major_version_regex="Xcode ([0-9]).[0-9]"
out=$(xcodebuild -version)
if [[ "${out}" =~ ${major_version_regex} ]] ; then
	xcode_major_version="${BASH_REMATCH[1]}"
fi

if [ ! "${xcode_major_version}" == "7" ] && [ ! "${xcode_major_version}" == "6" ] ; then
	echo "Invalid xcode major version: ${xcode_major_version}"
	exit 1
fi

echo "(i) xcode_major_version: ${xcode_major_version}"


#
# Required parameters
if [ -z "${project_path}" ] ; then
	echo "[!] Missing required input: project_path"
	exit 1
fi

if [ -z "${scheme}" ] ; then
	echo "[!] Missing required input: scheme"
	exit 1
fi

if [ -z "${output_dir}" ] ; then
	echo "[!] Missing required input: output_dir"
	exit 1
fi


if [ ! -z "${export_options_path}" ] && [[ "${xcode_major_version}" == "6" ]] ; then
	echo "(!) xcode_major_version = 6, export_options_path only used if xcode_major_version > 6"
	export_options_path=""
fi

if [[ "${output_tool}" != "xcpretty" && "${output_tool}" != "xcodebuild" ]] ; then
	echo "[!] Invalid output_tool: ${output_tool}"
	exit 1
fi

set +e

if [[ "${output_tool}" == "xcpretty" ]] ; then
	xcpretty_version=$(xcpretty --version)
	exit_code=$?
	if [[ $exit_code != 0 || -z $xcpretty_version ]] ; then
		echo
		echo " (!) xcpretty is not installed"
		echo "     For xcpretty installation see: 'https://github.com/supermarin/xcpretty',"
		echo "     or use 'xcodebuild' as 'output_tool'."
		echo
		exit 1
	fi
fi

set -e

#
# Project-or-Workspace flag
if [[ "${project_path}" == *".xcodeproj" ]]; then
	CONFIG_xcode_project_action="-project"
elif [[ "${project_path}" == *".xcworkspace" ]]; then
	CONFIG_xcode_project_action="-workspace"
else
	echo "Failed to get valid project file (invalid project file): ${project_path}"
	exit 1
fi

# abs out dir pth
mkdir -p "${output_dir}"
cd "${output_dir}"
output_dir="$(pwd)"
cd -

archive_tmp_dir=$(mktemp -d -t bitrise-xcarchive)
archive_path="${archive_tmp_dir}/${scheme}.xcarchive"
ipa_path="${output_dir}/${scheme}.ipa"
dsym_zip_path="${output_dir}/${scheme}.dSYM.zip"

if [ -z "${workdir}" ] ; then
	workdir="$(pwd)"
fi

#
# Print configs
echo
echo "========== Configs =========="
echo " * CONFIG_xcode_project_action: ${CONFIG_xcode_project_action}"
echo " * output_tool: ${output_tool}"
echo " * project_path: ${project_path}"
echo " * scheme: ${scheme}"
echo " * workdir: ${workdir}"
echo " * output_dir: ${output_dir}"
echo " * archive_path: ${archive_path}"
echo " * ipa_path: ${ipa_path}"
echo " * dsym_zip_path: ${dsym_zip_path}"
echo " * is_force_code_sign: ${is_force_code_sign}"
echo " * is_clean_build: ${is_clean_build}"
echo " * configuration: ${configuration}"

if [ ! -z "${export_options_path}" ] ; then
	echo " * export_options_path: ${export_options_path}"
fi

if [ ! -z "${workdir}" ] ; then
	echo
	echo " -> Switching to working directory: ${workdir}"
	cd "${workdir}"
fi

xcode_configuration=''
if [ ! -z "${configuration}" ] ; then
	xcode_configuration="-configuration ${configuration}"
fi


clean_build_param=''
if [[ "${is_clean_build}" == "yes" ]] ; then
	clean_build_param='clean'
fi


#
# Cleanup function
function finalcleanup {
	local fail_msg="$1"

	echo "-> finalcleanup"

	if [ ! -z "${fail_msg}" ] ; then
		echo " [!] ERROR: ${fail_msg}"
		exit 1
	fi
}


#
# Main

#
# Bit of cleanup
if [ -f "${ipa_path}" ] ; then
	echo " (!) IPA at path (${ipa_path}) already exists - removing it"
	rm "${ipa_path}"
fi

echo
echo
echo "=> Create the Archive ..."

#
# Create the Archive with Xcode Command Line tools
archive_cmd="xcodebuild ${CONFIG_xcode_project_action} \"${project_path}\""
archive_cmd="$archive_cmd -scheme \"${scheme}\" ${xcode_configuration}"
archive_cmd="$archive_cmd ${clean_build_param} archive -archivePath \"${archive_path}\""

if [[ "${is_force_code_sign}" == "yes" ]] ; then
	echo " (!) Using Force Code Signing mode!"

	archive_cmd="$archive_cmd PROVISIONING_PROFILE=\"${BITRISE_PROVISIONING_PROFILE_ID}\""
	archive_cmd="$archive_cmd CODE_SIGN_IDENTITY=\"${BITRISE_CODE_SIGN_IDENTITY}\""
fi

if [[ "${output_tool}" == "xcpretty" ]] ; then
	archive_cmd="set -o pipefail && $archive_cmd | xcpretty"
fi

echo
echo " => Archive command:"
echo '$' $archive_cmd
echo

eval $archive_cmd

echo
echo
echo "=> Exporting IPA from generated Archive ..."
echo

export_command="xcodebuild -exportArchive"

if [[ "${xcode_major_version}" == "6" ]] ; then
	#
{'fraction_non_alphanumeric': 0.1819897845880524, 'fraction_numerical': 0.004885631801021541, 'mean_word_length': 5.10230352303523, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '2686449', 'n_tokens_mistral': 3349, 'n_tokens_neox': 3231, 'n_words': 786}