{stdenv, androidsdk, titaniumsdk, titanium, xcodewrapper, jdk, python, which, xcodeBaseDir}:
{ name, src, target, androidPlatformVersions ? [ "8" ], androidAbiVersions ? [ "armeabi" "armeabi-v7a" ], tiVersion ? null
, release ? false, androidKeyStore ? null, androidKeyAlias ? null, androidKeyStorePassword ? null
, iosMobileProvisioningProfile ? null, iosCertificateName ? null, iosCertificate ? null, iosCertificatePassword ? null, iosVersion ? "8.1", iosWwdrCertificate ? null
, enableWirelessDistribution ? false, installURL ? null
}:
assert (release && target == "android") -> androidKeyStore != null && androidKeyAlias != null && androidKeyStorePassword != null;
assert (release && target == "iphone") -> iosMobileProvisioningProfile != null && iosCertificateName != null && iosCertificate != null && iosCertificatePassword != null;
assert enableWirelessDistribution -> installURL != null;
let
androidsdkComposition = androidsdk {
platformVersions = androidPlatformVersions;
abiVersions = androidAbiVersions;
useGoogleAPIs = true;
};
deleteKeychain = ''
security default-keychain -s login.keychain
security delete-keychain $keychainName
'';
in
stdenv.mkDerivation {
name = stdenv.lib.replaceChars [" "] [""] name;
inherit src;
buildInputs = [ titanium jdk python which ] ++ stdenv.lib.optional (stdenv.system == "x86_64-darwin") xcodewrapper;
buildPhase = ''
export HOME=$TMPDIR
${stdenv.lib.optionalString (tiVersion != null) ''
# Replace titanium version by the provided one
sed -i -e "s|[0-9a-zA-Z\.]*|${tiVersion}|" tiapp.xml
''}
# Simulate a login
mkdir -p $HOME/.titanium
cat > $HOME/.titanium/auth_session.json < $TMPDIR/config.json
titanium --config-file $TMPDIR/config.json --no-colors config sdk.defaultInstallLocation ${titaniumsdk}
titanium --config-file $TMPDIR/config.json --no-colors config paths.modules ${titaniumsdk}
mkdir -p $out
${if target == "android" then
''
titanium config --config-file $TMPDIR/config.json --no-colors android.sdkPath ${androidsdkComposition}/libexec/android-sdk-*
# Add zipalign to PATH to make Ti 3.1 builds still work
for i in $(find -L ${androidsdkComposition}/libexec/android-sdk-*/build-tools -name zipalign)
do
export PATH=$(dirname $i):$PATH
break
done
${if release then
''titanium build --config-file $TMPDIR/config.json --no-colors --force --platform android --target dist-playstore --keystore ${androidKeyStore} --alias ${androidKeyAlias} --password ${androidKeyStorePassword} --output-dir $out''
else
''titanium build --config-file $TMPDIR/config.json --no-colors --force --platform android --target emulator --build-only -B foo --output $out''}
''
else if target == "iphone" then
''
export NIX_TITANIUM_WORKAROUND="--config-file $TMPDIR/config.json"
${if release then
''
export HOME=/Users/$(whoami)
export keychainName=$(basename $out)
# Create a keychain with the component hash name (should always be unique)
security create-keychain -p "" $keychainName
security default-keychain -s $keychainName
security unlock-keychain -p "" $keychainName
security import ${iosCertificate} -k $keychainName -P "${iosCertificatePassword}" -A
${stdenv.lib.optionalString (iosWwdrCertificate != null) ''
security import ${iosWwdrCertificate} -k $keychainName
''}
provisioningId=$(grep UUID -A1 -a ${iosMobileProvisioningProfile} | grep -o "[-A-Za-z0-9]\{36\}")
# Ensure that the requested provisioning profile can be found
if [ ! -f "$HOME/Library/MobileDevice/Provisioning Profiles/$provisioningId.mobileprovision" ]
then
mkdir -p "$HOME/Library/MobileDevice/Provisioning Profiles"
cp ${iosMobileProvisioningProfile} "$HOME/Library/MobileDevice/Provisioning Profiles/$provisioningId.mobileprovision"
fi
# Make a copy of the Titanium SDK and fix its permissions. Without it,
# builds using the facebook module fail, because it needs to be writable
cp -av ${titaniumsdk} $TMPDIR/titaniumsdk
find $TMPDIR/titaniumsdk | while read i
do
chmod 755 "$i"
done
# Simulate a login
mkdir -p $HOME/.titanium
cat > $HOME/.titanium/auth_session.json < $out/nix-support/hydra-build-products
${stdenv.lib.optionalString enableWirelessDistribution ''
appname=$(basename $out/Release-iphoneos/*.ipa .ipa)
bundleId=$(grep '[a-zA-Z0-9.]*' tiapp.xml | sed -e 's|||' -e 's|||' -e 's/ //g')
version=$(grep '[a-zA-Z0-9.]*' tiapp.xml | sed -e 's|||' -e 's|||' -e 's/ //g')
sed -e "s|@INSTALL_URL@|${installURL}?bundleId=$bundleId\&version=$version\&title=$appname|" ${../xcodeenv/install.html.template} > $out/$appname.html
echo "doc install \"$out/$appname.html\"" >> $out/nix-support/hydra-build-products
''}
''
else if target == "iphone" then ""
else throw "Target: ${target} is not supported!"}
${if target == "android" then ''
mkdir -p $out/nix-support
echo "file binary-dist \"$(ls $out/*.apk)\"" > $out/nix-support/hydra-build-products
'' else ""}
'';
failureHook = stdenv.lib.optionalString (release && target == "iphone") deleteKeychain;
}