Skip to content

Instantly share code, notes, and snippets.

@Tagakov
Last active January 19, 2017 17:01
Show Gist options
  • Save Tagakov/eb3746ca95cf4d4333818eeba945b92d to your computer and use it in GitHub Desktop.
Save Tagakov/eb3746ca95cf4d4333818eeba945b92d to your computer and use it in GitHub Desktop.
Mainframer initialization script
#!/usr/bin/env bash
REMOTE_MACHINE=${1}
APPLICATION_MODULE=${2}
ASSEMBLE_COMMAND=${3}
ASSEMBLE_TESTS_COMMAND=${4}
ANDROID_SDK_VERSION=${5}
if [ -z "${REMOTE_MACHINE}" ]; then
echo "You must pass remote machine adress as first parameter"
exit 1
fi
if [ -z "${APPLICATION_MODULE}" ]; then
echo "You must pass application_module as second parameter"
exit 1
fi
if [ -z "${ASSEMBLE_COMMAND}" ]; then
ASSEMBLE_COMMAND="-Pandroid.enableBuildCache=true -PminSdk=23 -Pabi=armeabi-v7a -Pmainframer=true assembleDebug"
fi
if [ -z "${ASSEMBLE_TESTS_COMMAND}" ]; then
ASSEMBLE_TESTS_COMMAND="./gradlew compileDebugUnitTestSources mockableAndroidJar"
fi
if [ -z "${ANDROID_SDK_VERSION}" ]; then
ANDROID_SDK_VERSION="tools_r25.2.3-linux.zip"
fi
function findProjectDir {
local projectDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
while [[ "$projectDir" != "/" ]];
do
if [ -d "$projectDir/.idea" ]; then
break
fi
projectDir="$(dirname "$projectDir")"
done
if [ "$projectDir" == "/" ]; then
echo "Can't find .idea folder!"
exit 1
fi
echo "$projectDir/"
}
PROJECT_DIR="$(findProjectDir)"
function downloadMainframerIfNeed {
local MAINFRAMER_DOWNLOAD_LINK="https://raw.githubusercontent.com/gojuno/mainframer/development/mainframer.sh"
local mainframerFile=${PROJECT_DIR}"/tools/mainframer.sh"
if [ ! -f ${mainframerFile} ]; then
echo "Downloading mainframer v2.0a"
curl ${MAINFRAMER_DOWNLOAD_LINK} -o ${mainframerFile}
fi
chmod +x ${mainframerFile}
}
function checkAndroidHomeVariable {
if [ -z "$ANDROID_HOME" ]; then
local androidHomeFromProject="$(sed -n '/<property name="android.sdk.path"/s/.*value="\(.*\)"[^\n]*/\1/p' ${PROJECT_DIR}/.idea/workspace.xml | sed 's/$USER_HOME$/~/g')"
if [ -z ${androidHomeFromProject} ]; then
echo "Please specify location of your android sdk in ANDROID_HOME"
exit 1
fi
#replace $USER_HOME$ by user directory
ANDROID_HOME=${androidHomeFromProject/\$USER_HOME\$/"${HOME}"}
fi
}
function setupAndroidSdk {
local ANDROID_SDK_VERSION=${1}
local ANDROID_SDK_DOWNLOAD_BASE_LINK="https://dl.google.com/android/repository/"
local ANDROID_SDK_DOWNLOAD_LINK="${ANDROID_SDK_DOWNLOAD_BASE_LINK}${ANDROID_SDK_VERSION}"
local ANDROID_SDK_FOLDER_NAME="android-sdk-linux"
if [ -z "$ANDROID_HOME" ]; then
echo "Downloading SDK from ${ANDROID_SDK_DOWNLOAD_LINK}"
wget -q ${ANDROID_SDK_DOWNLOAD_LINK}
if [ ! -f ${ANDROID_SDK_VERSION} ]; then
echo "Failed to download SDK!"
exit 1
fi
echo "Unzip SDK from ${ANDROID_SDK_VERSION} to ${ANDROID_SDK_FOLDER_NAME}"
unzip -q "$ANDROID_SDK_VERSION" -d "$ANDROID_SDK_FOLDER_NAME"
echo "Removing ${ANDROID_SDK_VERSION}"
rm "$ANDROID_SDK_VERSION"
sed -i "1i export ANDROID_HOME=~/$ANDROID_SDK_FOLDER_NAME" ~/.bashrc
fi
}
function installJavaOnRemoteMachineIfNeed {
ssh -t ${REMOTE_MACHINE} "java -version || sudo apt-get --assume-yes install yandex-jdk8"
echo "Java installed"
}
function setupAndroidSdkOnRemoteMachine {
ssh ${REMOTE_MACHINE} "$(typeset -f); setupAndroidSdk ${ANDROID_SDK_VERSION}"
echo "Sdk downloaded to remote machine"
}
function copyLicensesToRemoteMachine {
scp -r ${ANDROID_HOME}/licenses ${REMOTE_MACHINE}:"\$ANDROID_HOME"
}
function setupGradleSettings {
ssh ${REMOTE_MACHINE} "$(typeset -f); createGradleProperties"
echo "Gradle settings set"
}
function createGradleProperties {
mkdir -p .gradle
rm -f .gradle/gradle.properties
echo 'org.gradle.daemon=true' >> .gradle/gradle.properties
echo 'org.gradle.configureondemand=true' >> .gradle/gradle.properties
echo 'org.gradle.parallel=true' >> .gradle/gradle.properties
echo 'org.gradle.jvmargs=-XX:-UseGCOverheadLimit -Xmx6g -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8' >> .gradle/gradle.properties
}
function copyRunConfigurations {
local runConfigFolder="${PROJECT_DIR}/.idea/runConfigurations/"
mkdir -p ${runConfigFolder}
local assembleDebugConfigFile="${runConfigFolder}Remote_assembleDebug.xml"
[ -e ${assembleDebugConfigFile} ] && rm ${assembleDebugConfigFile}
echo '
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Remote assembleDebug" type="BashConfigurationType" factoryName="Bash" singleton="true">
<option name="INTERPRETER_OPTIONS" value="" />
<option name="INTERPRETER_PATH" value="/bin/bash" />
<option name="WORKING_DIRECTORY" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SCRIPT_NAME" value="tools/mainframer.sh" />
<option name="PARAMETERS" value="./gradlew '${ASSEMBLE_COMMAND}'" />
<module name="" />
<envs />
<method>
<option name="Make" enabled="false" />
</method>
</configuration>
</component>
' >> ${assembleDebugConfigFile}
local buildConfigFile="${runConfigFolder}Remote_build.xml"
[ -e ${buildConfigFile} ] && rm ${buildConfigFile}
echo '
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Remote build" type="AndroidRunConfigurationType" factoryName="Android App">
<module name="'${APPLICATION_MODULE}'" />
<option name="DEPLOY" value="true" />
<option name="ARTIFACT_NAME" value="" />
<option name="PM_INSTALL_OPTIONS" value="" />
<option name="ACTIVITY_EXTRA_FLAGS" value="" />
<option name="MODE" value="default_activity" />
<option name="TARGET_SELECTION_MODE" value="SHOW_DIALOG" />
<option name="PREFERRED_AVD" value="" />
<option name="CLEAR_LOGCAT" value="false" />
<option name="SHOW_LOGCAT_AUTOMATICALLY" value="false" />
<option name="SKIP_NOOP_APK_INSTALLATIONS" value="true" />
<option name="FORCE_STOP_RUNNING_APP" value="true" />
<option name="DEBUGGER_TYPE" value="Auto" />
<option name="USE_LAST_SELECTED_DEVICE" value="false" />
<option name="PREFERRED_AVD" value="" />
<Java />
<Profilers>
<option name="ENABLE_ADVANCED_PROFILING" value="false" />
<option name="GAPID_ENABLED" value="false" />
<option name="GAPID_DISABLE_PCS" value="false" />
<option name="SUPPORT_LIB_ENABLED" value="true" />
<option name="INSTRUMENTATION_ENABLED" value="true" />
</Profilers>
<option name="DEEP_LINK" value="" />
<option name="ACTIVITY_CLASS" value="" />
<method>
<option name="Android.Gradle.BeforeRunTask" enabled="false" />
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Remote assembleDebug" run_configuration_type="BashConfigurationType" />
</method>
</configuration>
</component>
' >> ${buildConfigFile}
local buildCleanFile="${runConfigFolder}Remote_clean.xml"
[ -e ${buildCleanFile} ] && rm ${buildCleanFile}
echo '
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Remote clean" type="BashConfigurationType" factoryName="Bash" singleton="true">
<option name="INTERPRETER_OPTIONS" value="" />
<option name="INTERPRETER_PATH" value="/bin/bash" />
<option name="WORKING_DIRECTORY" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SCRIPT_NAME" value="tools/mainframer.sh" />
<option name="PARAMETERS" value="./gradlew clean" />
<module name="" />
<envs />
<method>
<option name="Make" enabled="false" />
</method>
</configuration>
</component>
' >> ${buildCleanFile}
local buildTestsFile="${runConfigFolder}Remote_tests.xml"
[ -e ${buildTestsFile} ] && rm ${buildTestsFile}
echo '
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Remote tests" type="BashConfigurationType" factoryName="Bash" singleton="true">
<option name="INTERPRETER_OPTIONS" value="" />
<option name="INTERPRETER_PATH" value="/bin/bash" />
<option name="WORKING_DIRECTORY" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SCRIPT_NAME" value="tools/mainframer.sh" />
<option name="PARAMETERS" value="'${ASSEMBLE_TESTS_COMMAND}'" />
<module name="" />
<envs />
<method>
<option name="Make" enabled="false" />
</method>
</configuration>
</component>
' >> ${buildTestsFile}
}
function updateGitignoreForMainframerConfig {
local projectGitIgnore="${PROJECT_DIR}/.gitignore"
if [ ! -f "$projectGitIgnore" ] || ! grep -q "/.mainframer" "$projectGitIgnore" ; then
echo "/.mainframer" >> "$projectGitIgnore"
fi
}
function updateMainframerConfig {
local propertiesFolder="${PROJECT_DIR}/.mainframer/"
mkdir -p ${propertiesFolder}
local configFile="${propertiesFolder}config"
if [ -f "${configFile}" ]; then
if grep -q "remote_machine" ${configFile}; then
return
fi
fi
echo "remote_machine=${REMOTE_MACHINE}" >> ${configFile}
echo "local_compression_level=1" >> ${configFile}
echo "remote_compression_level=0" >> ${configFile}
}
function updateMainframerIgnore {
local propertiesFolder="${PROJECT_DIR}/.mainframer/"
local ignoreFile="${propertiesFolder}ignore"
if [ -f "${ignoreFile}" ]; then
return
fi
echo ".gradle" >> ${ignoreFile}
echo ".idea" >> ${ignoreFile}
echo "**/.git" >> ${ignoreFile}
echo "artifacts" >> ${ignoreFile}
echo "captures" >> ${ignoreFile}
echo "**/local.properties" >> ${ignoreFile}
}
downloadMainframerIfNeed
checkAndroidHomeVariable
installJavaOnRemoteMachineIfNeed
setupAndroidSdkOnRemoteMachine
copyLicensesToRemoteMachine
setupGradleSettings
copyRunConfigurations
updateGitignoreForMainframerConfig
updateMainframerConfig
updateMainframerIgnore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment