Last active
November 28, 2016 10:14
-
-
Save shikajiro/90cdbdac652cf37a563840d812d73c5b to your computer and use it in GitHub Desktop.
Script to perform the build of Android on another machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script Name : fabfile.py | |
# Author : shikajiro | |
# Created : 2016-11-03 | |
# Last Modified : 2016-11-21 | |
# Version : 1.1.0 | |
# Modifications : | |
# Description : | |
# Script to perform the build of Android on another machine | |
# By performing such a build of severely time on another machine, | |
# it is possible to ensure the CPU resources of the development machine, | |
# it is possible to prevent the "can not do anything during the build" state | |
# | |
# Install | |
# 1. this file create to project root dir. | |
# 2. please setting. env.hosts, module, flavor, debug. | |
# 3. run this | |
# | |
# command | |
# $ fab build # build and install and start activity. | |
# $ fab test # unit test. open report in browser. | |
# $ fab android_test # instrumentation test. open report in browser. | |
# $ fab test_all # unite test and instrumentation test. | |
# fabric install | |
# sudo env ARCHFLAGS="-arch i386 -arch x86_64" pip install fabric | |
from __future__ import with_statement | |
import re | |
from fabric.operations import * | |
from fabric.api import * | |
from fabric.contrib.project import rsync_project | |
# build machine setting | |
env.hosts = ["ipaddress"] | |
# env.port = 22 | |
# env.password = hogehoge | |
### | |
# build Android setting | |
### | |
# target module and flavor | |
module = "mobile" | |
flavor = "dev" | |
# debug build : True | |
# release build : False | |
debug = True | |
# App Package Name | |
package = "com.example.android.app" | |
# LAUNCHER Activity | |
launch_activity = "com.example.android.app.activity.MainActivity" | |
# remote machine build dir | |
tmp_dir = "/tmp/build/" | |
def build(): | |
""" | |
other machine build and start activity. | |
1.sync file. local machine -> remote machine | |
2.gradle assemble. | |
3.get apk. | |
4.install apk, and start App. | |
:return: | |
""" | |
gradle_cmd = "./gradlew assemble" + flavor.capitalize() | |
if debug: | |
gradle_cmd += "Debug" | |
package_name = package | |
if debug: | |
def _sync_source(build_dir): | |
# files written to '.gitignore' is not sync. | |
with open(".gitignore", "r") as f: | |
excludes = [line.replace("\n", "") for line in f if not re.match(r"[#\n]", line)] | |
ignore_file = "local.properties" # must exclude | |
if ignore_file not in excludes: | |
excludes.append(ignore_file) | |
excludes.append(".git") | |
# sync to remote machine | |
rsync_project(remote_dir=build_dir, | |
local_dir=".", | |
exclude=excludes, | |
delete=True) | |
def _test(test_type="test"): | |
if test_type == "test": | |
cmd_format = "./gradlew :{}:test{}{}UnitTest" | |
html_dir = "{}/build/reports/tests/{}{}".format(module, flavor, "Debug" if debug else "") | |
index_html = "{}/index.html".format(html_dir) | |
def _sync_source(build_dir): | |
# files written to '.gitignore' is not sync. | |
with open(".gitignore", "r") as f: | |
excludes = [line.replace("\n", "") for line in f if not re.match(r"[#\n]", line)] | |
ignore_file = "local.properties" # must exclude | |
if ignore_file not in excludes: | |
excludes.append(ignore_file) | |
excludes.append(".git") | |
# sync to remote machine | |
rsync_project(remote_dir=build_dir, | |
local_dir=".", | |
exclude=excludes, | |
delete=True) | |
def _test(test_type="test"): | |
if test_type == "test": | |
cmd_format = "./gradlew :{}:test{}{}UnitTest" | |
html_dir = "{}/build/reports/tests/{}{}".format(module, flavor, "Debug" if debug else "") | |
index_html = "{}/index.html".format(html_dir) | |
elif test_type == "androidTest": | |
cmd_format = "./gradlew :{}:connected{}{}AndroidTest" | |
html_dir = "{}/build/reports/androidTests/connected/flavors".format(module) | |
index_html = "{}/{}/index.html".format(html_dir, flavor.upper()) | |
else: | |
return | |
gradle_cmd = cmd_format.format(module, flavor.capitalize(), "Debug" if debug else "") | |
build_dir = tmp_dir + package | |
_sync_source(build_dir) | |
# run gradlew | |
with cd(build_dir): | |
with settings(warn_only=True): | |
run(gradle_cmd) | |
# get reports | |
rsync_project(remote_dir="{}/{}/".format(build_dir, html_dir), | |
local_dir=html_dir, | |
upload=False, | |
delete=True) | |
local("open {}".format(index_html)) | |
local("osascript -e 'display notification \"{} test finish\" with title \"android test finish\"'".format(package)) | |
def test(): | |
""" | |
unit test | |
open report to browser. | |
:return: | |
""" | |
_test("test") | |
def android_test(): | |
""" | |
instrumentation test | |
open report to browser. | |
:return: | |
""" | |
_test("androidTest") | |
def test_all(): | |
""" | |
unit and instrumentation test | |
open report to browser. | |
:return: | |
""" | |
test() | |
android_test() | |
def build(): | |
""" | |
other machine build and start activity. | |
1.sync file. local machine -> remote machine | |
2.gradle assemble. | |
3.get apk. | |
4.install apk, and start App. | |
:return: | |
""" | |
build_dir = tmp_dir + package | |
_sync_source(build_dir) | |
gradle_cmd = "./gradlew {}:assemble{}".format(module, flavor.capitalize()) | |
if debug: | |
gradle_cmd += "Debug" | |
package_name = package | |
if debug: | |
package_name += ".debug" | |
if debug: | |
apk_name_format = "{0}-{1}-debug.apk" | |
else: | |
apk_name_format = "{0}-{1}.apk" | |
apk_name = apk_name_format.format(module, flavor) | |
# run gradlew | |
with cd(build_dir): | |
run(gradle_cmd) | |
# get apk | |
get(remote_path="{}/{}/build/outputs/apk/{}".format(build_dir, module, apk_name), | |
local_path="{}/build/outputs/apk/".format(module)) | |
# start App | |
local("adb push ./{}/build/outputs/apk/{}/data/local/tmp/{}".format(module, apk_name, package_name)) | |
local("adb shell pm install -r '/data/local/tmp/{}'".format(package_name)) | |
local("adb shell am start -n '{}/{}' -a android.intent.action.MAIN -c android.intent.category.LAUNCHER".format(package_name, launch_activity)) | |
local("osascript -e 'display notification \"{} build finish\" with title \"android build finish\"'".format(package_name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment