Skip to content

Instantly share code, notes, and snippets.

@v1993
Created July 5, 2018 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save v1993/b48378e1a0371125ab2c481ea8e445fc to your computer and use it in GitHub Desktop.
Save v1993/b48378e1a0371125ab2c481ea8e445fc to your computer and use it in GitHub Desktop.
My build script for cocos2d-x. It should work on any posix system. posix lua module is required, ansicolors is required for colorful output.
#!/usr/bin/lua
local AVD_BINARY = '/home/v/compile/android-sdk-linux/tools/qemu/linux-x86_64/qemu-system-x86_64'
local ADB_BINARY = '/home/v/compile/android-sdk-linux/platform-tools/adb'
-- I know, hardcoding is bad, but any other way is too expensive :-(
local colors
do
local ok
ok, colors = pcall(require, 'ansicolors')
if not ok then -- Replace with no-op
colors = function(str)
return (str:gsub('%%{.-}', ''))
end
end
end
local posix = require 'posix'
local function execute(file, ...)
local pid = assert(posix.fork())
local t = {...}
if pid == 0 then
-- Child
t[0] = file
assert(posix.exec(file, t))
error('Something is TERRIBLE wrong! Run away and contact you admin!')
else
-- Parent
local pid1,death,code = posix.wait(pid)
assert(pid1,death,code)
if death == 'killed' or death == 'stopped' then
print(colors("%{bright red}Command %s %s killed or stopped with signal #%d"):format(file, table.concat(t, ' '), code))
return false
elseif code ~= 0 then
print(colors("%{bright red}Command %s %s exited with code #%d"):format(file, table.concat(t, ' '), code))
return false
else
return true
end
end
end
local function execute_daemon(file, ...)
local pid = assert(posix.fork())
local t = {...}
if pid == 0 then
-- Child
t[0] = file
posix.signal(posix.SIGHUP, posix.SIG_IGN)
local null = assert(io.open('/dev/null', 'r'))
posix.dup2(posix.fileno(null), posix.STDIN_FILENO)
assert(posix.exec(file, t))
error('Something is TERRIBLE wrong! Run away and contact you admin!')
else
-- Parent
posix.sleep(5) -- Check state after 5 seconds
local pid1,death,code = posix.wait(pid, posix.WNOHANG)
assert(pid1,death,code)
if death == 'running' then
return true
elseif death == 'killed' or death == 'stopped' then
print(colors("%{bright red}Command %s %s killed or stopped with signal #%d"):format(file, table.concat(t, ' '), code))
return false
else
print(colors("%{bright red}Command %s %s exited with code #%d"):format(file, table.concat(t, ' '), code))
return false
end
end
end
local conf = {
dir = '.'; -- d
avd = nil; -- e
spec = nil; -- s
debug = true; -- m
install = false; -- i
cmake_log = false; -- c
}
for r, optarg, optind in posix.getopt(arg, 'hd:e:s:m:ic') do
if r == '?' then
error(('unrecognized option %s, try -h to show help'):format(arg[optind -1]))
elseif r == ':' then
error(('missing argument to %s, try -h to show help'):format(arg[optind -1]))
end
if r == 'h' then
print 'Script for cocos2d-x android build by Valeri Ochinski'
print ''
print 'Usage:'
print '-h print this help text'
print '-d DIR cocos2d-x project root'
print '-e AVD name of started avd'
print '-s SPEC if using split apk, set which one will de deployed (e.g. `x86`, `armeabi-v7a`)'
print '-m (debug|release) build type (default is debug)'
print '-i install to device'
print '-c in case of failure, print CMake log'
return
elseif r == 'd' then
conf.dir = optarg
elseif r == 'e' then
conf.avd = optarg
elseif r == 's' then
conf.spec = optarg
elseif r == 'm' then
if optarg:lower() == 'debug' then
conf.debug = true
elseif optarg:lower() == 'release' then
conf.debug = flase
else
error('Wrong build mode')
end
elseif r == 'i' then
conf.install = true
elseif r == 'c' then
conf.cmake_log = true
end
end
assert(posix.chdir(conf.dir..'/proj.android'))
print (colors('%{green}Running %s build. Best of luck! :-)'):format(conf.debug and 'debug' or 'release'))
if not execute('./gradlew', '--parallel', '--info', conf.debug and 'assembleDebug' or 'assembleRelease') then
print(colors('%{bright red}Build failed (—_—)'))
if conf.cmake_log then
print 'There is CMake server log:'
for k,v in pairs(posix.glob('app/.externalNativeBuild/cmake/*/*/cmake_server_log.txt')) do
print(colors("%{bright magenta}---------- %s ----------"):format(v))
local f = io.open(v, 'r')
print(f:read('*a'))
f:close()
end
end
print(colors('%{green}Have a good time fixing bugs! Be positive! :-)'))
return 1
end
print(colors('%{bright green}Build is successful! Congratulations!'))
if conf.install then
local apk = ('./app/build/outputs/apk/%s/%s'):format(conf.debug and 'debug' or 'release', '%s')
if conf.spec then
apk = apk:format(('*-%s-%s.apk'):format(conf.spec, conf.debug and 'debug' or 'release'))
else
apk = apk:format(('*-%s.apk'):format(conf.debug and 'debug' or 'release'))
end
apk = (posix.glob(apk) or {})[1]
if not apk or not posix.access(apk, 'r') then
print(colors('%{bright red}APK not found… are you using separate apk build? Are you sure?'))
return 1
end
print(colors('%{green}APK to be installed: %s'):format(apk))
if conf.avd then
print(colors('%{green}Starting AVD %s…'):format(conf.avd))
if not execute_daemon(AVD_BINARY, '-avd', conf.avd) then
print(colors('%{bright red}Starting AVD %s failed. Check log for details.'):format(conf.avd))
return 1
end
print(colors('%{green}AVD %s is started successfully :-)'):format(conf.avd))
print(colors('%{green}Wait some time before it will boot…'):format(conf.avd))
posix.sleep(20)
end
print(colors('%{green}Deploying apk'))
if not execute(ADB_BINARY, 'install', '-r', apk) then
print(colors('%{bright red}Installing on device failed :-('):format(conf.avd))
return 1
end
print(colors('%{green}APK is successfully installed'))
end
print(colors('%{bright green}Everything is done!'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment