Skip to content

Instantly share code, notes, and snippets.

@nhancv
Last active December 29, 2020 15:13
Show Gist options
  • Select an option

  • Save nhancv/036fef50f5d7644e64a3bfa765bb37e4 to your computer and use it in GitHub Desktop.

Select an option

Save nhancv/036fef50f5d7644e64a3bfa765bb37e4 to your computer and use it in GitHub Desktop.
React native local build release: https://medium.com/p/17063cc64da5
{
"BUILD_ENVIRONMENT":"staging",
"NEED_RESET_CACHE":false
}
doLast {
def moveFunc = { resSuffix ->
File originalDir = file("${resourcesDir}/drawable-${resSuffix}")
if (originalDir.exists()) {
File destDir = file("${resourcesDir}/drawable-${resSuffix}-v4")
ant.move(file: originalDir, tofile: destDir)
}
}
moveFunc.curry("ldpi").call()
moveFunc.curry("mdpi").call()
moveFunc.curry("hdpi").call()
moveFunc.curry("xhdpi").call()
moveFunc.curry("xxhdpi").call()
moveFunc.curry("xxxhdpi").call()
}
// Set up inputs and outputs so gradle can cache the result
const fs = require('fs')
try {
var curDir = process.cwd()
var file = `${curDir}/node_modules/react-native/react.gradle`
var dataFix = fs.readFileSync(`${curDir}/android-react-gradle-fix`, 'utf8')
var data = fs.readFileSync(file, 'utf8')
var doLast = "doLast \{"
if (data.indexOf(doLast) !== -1) {
throw "Already fixed."
}
var result = data.replace(/ \/\/ Set up inputs and outputs so gradle can cache the result/g, dataFix);
fs.writeFileSync(file, result, 'utf8')
console.log('Done')
} catch (error) {
console.error(error)
}
#!/usr/bin/env bash -e
BUILD_ENVIRONMENT=`node -e "console.log(JSON.parse(require('fs').readFileSync(process.argv[1]), null, 4).BUILD_ENVIRONMENT);" android-config.json`
echo 'Build env:' $BUILD_ENVIRONMENT
WORKSPACE=`pwd`
PROJECT_NAME=$(cat package.json | grep name | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]')
PACKAGE_VERSION=$(cat package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]')
echo 'Project name:' $PROJECT_NAME
echo 'Project version:' $PACKAGE_VERSION
# Reset cache
NEED_RESET_CACHE=`node -e "console.log(JSON.parse(require('fs').readFileSync(process.argv[1]), null, 4).NEED_RESET_CACHE);" android-config.json`
if [ "$NEED_RESET_CACHE" == true ]
then
echo 'Reset cache'
rm -rf node_modules/ && npm cache clean --force
fi
# Install dependencies
echo 'Install dependencies'
npm config set prefix ${WORKSPACE}
npm install
# Cleanup Android
echo 'Check android release gradle: '
node ${WORKSPACE}/android-release-gradle-fix.js
echo 'Cleanup android'
mkdir -p ${WORKSPACE}/android/app/src/main/assets
rm -rf ${WORKSPACE}/android/app/build
# React native build bundle
echo 'Build bundle'
react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.bundle --assets-dest android/app/src/main/res
export ENVFILE=.env.${BUILD_ENVIRONMENT}
# Android build script
echo 'Start build apk'
# After export: .apk file place at: ${WORKSPACE}/android/app/build/outputs/apk/
cd ${WORKSPACE}/android && ./gradlew assembleRelease
{
"BUILD_ENVIRONMENT":"staging",
"NEED_RESET_CACHE":false,
"BUNDLE_ID":"com.app.apptest",
"DEVELOPMENT_TEAM":"xxxxxxxxxx",
"PROVISIONING_PROFILE_NAME":"Provision Profile Name",
"CODE_SIGN_IDENTITY":"iPhone Developer: Nhan Cao (xxxxxxxxxx)",
"BUILD_HOST":"https://iosbuild.io"
}
#!/usr/bin/env bash -e
BUILD_ENVIRONMENT=`node -e "console.log(JSON.parse(require('fs').readFileSync(process.argv[1]), null, 4).BUILD_ENVIRONMENT);" ios-config.json`
echo 'Build env:' $BUILD_ENVIRONMENT
WORKSPACE=`pwd`
PROJECT_NAME=$(cat package.json | grep name | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]')
PACKAGE_VERSION=$(cat package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]')
ENV_NAME="${PROJECT_NAME}-${BUILD_ENVIRONMENT}"
BUNDLE_ID=`node -e "console.log(JSON.parse(require('fs').readFileSync(process.argv[1]), null, 4).BUNDLE_ID);" ios-config.json`
DEVELOPMENT_TEAM=`node -e "console.log(JSON.parse(require('fs').readFileSync(process.argv[1]), null, 4).DEVELOPMENT_TEAM);" ios-config.json`
PROVISIONING_PROFILE_NAME=`node -e "console.log(JSON.parse(require('fs').readFileSync(process.argv[1]), null, 4).PROVISIONING_PROFILE_NAME);" ios-config.json`
CODE_SIGN_IDENTITY=`node -e "console.log(JSON.parse(require('fs').readFileSync(process.argv[1]), null, 4).CODE_SIGN_IDENTITY);" ios-config.json`
BUILD_HOST=`node -e "console.log(JSON.parse(require('fs').readFileSync(process.argv[1]), null, 4).BUILD_HOST);" ios-config.json`
export RCT_NO_LAUNCH_PACKAGER=true
# Reset cache
NEED_RESET_CACHE=`node -e "console.log(JSON.parse(require('fs').readFileSync(process.argv[1]), null, 4).NEED_RESET_CACHE);" ios-config.json`
if [ "$NEED_RESET_CACHE" == true ]
then
echo 'Reset cache'
rm -rf $TMPDIR/react-* && rm -rf $TMPDIR/metro-* && watchman watch-del-all && rm -rf ios/build && rm -rf node_modules/ && npm cache clean --force
fi
# Install dependencies
echo 'Install dependencies'
npm config set prefix ${WORKSPACE}
npm install
# Cleanup ios
echo 'Cleanup ios'
rm -rf "${WORKSPACE}/ios/build";
rm -rf "${WORKSPACE}"/ios/*.plist;
# React native build bundle
echo 'Build bundle'
echo ".env.${BUILD_ENVIRONMENT}" > /tmp/envfile;
npm run ios-version;
react-native bundle --platform ios --dev false --entry-file index.js --bundle-output ios/main.jsbundle
# iOS build script
echo 'Start build ipa'
cd ios
pod install
env DEVELOPER_DIR="/Applications/Xcode.app" /usr/bin/xcodebuild -workspace ${PROJECT_NAME}.xcworkspace/ -scheme ${PROJECT_NAME} -configuration Release CODE_SIGN_IDENTITY="${CODE_SIGN_IDENTITY}" archive -archivePath "${WORKSPACE}/ios/build/${ENV_NAME}/${ENV_NAME}.xcarchive" -verbose
cat <<EOT >> manifest-${ENV_NAME}.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>compileBitcode</key>
<true/>
<key>method</key>
<string>development</string>
<key>provisioningProfiles</key>
<dict>
<key>${BUNDLE_ID}</key>
<string>${PROVISIONING_PROFILE_NAME}</string>
</dict>
<key>signingCertificate</key>
<string>iPhone Developer</string>
<key>signingStyle</key>
<string>manual</string>
<key>stripSwiftSymbols</key>
<true/>
<key>teamID</key>
<string>${DEVELOPMENT_TEAM}</string>
<key>thinning</key>
<string>&lt;none&gt;</string>
<key>manifest</key>
<dict>
<key>appURL</key>
<string>${BUILD_HOST}/builds/${PROJECT_NAME}/ios/${BUILD_ENVIRONMENT}/${BUILD_NUMBER}/${ENV_NAME}.ipa</string>
<key>displayImageURL</key>
<string>${BUILD_HOST}/builds/images/icon57.png</string>
<key>fullSizeImageURL</key>
<string>${BUILD_HOST}/builds/images/icon512.png</string>
</dict>
</dict>
</plist>
EOT
# After export: .ipa + .plist files place at: ${WORKSPACE}/ios/build/${ENV_NAME}
echo 'Export'
${WORKSPACE}/ios-xcbuild.sh -exportArchive -archivePath ${WORKSPACE}/ios/build/${ENV_NAME}/${ENV_NAME}.xcarchive -exportPath ${WORKSPACE}/ios/build/${ENV_NAME} -exportOptionsPlist manifest-${ENV_NAME}.plist
#!/usr/bin/env bash -e
_PROJECT_NAME=$(cat package.json | grep name | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]')
_INFOPLIST_DIR="ios/${_PROJECT_NAME}/Info.plist"
_PACKAGE_VERSION=$(cat package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]')
# Set BUILD_NUMBER to the value 1 only if it is unset.
: ${BUILD_NUMBER=$(expr $(git log -1 --pretty=format:%ct) / 3600)}
# Update plist with new values
BUNDLE_VERSION=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${_INFOPLIST_DIR}")
if [ "$BUNDLE_VERSION" != "$BUILD_NUMBER" ]
then
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUILD_NUMBER" "${_INFOPLIST_DIR}"
fi
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString ${_PACKAGE_VERSION#*v}" "${_INFOPLIST_DIR}"
echo "****************************************"
echo "PROJECT_NAME: " $_PROJECT_NAME
echo "PACKAGE_VERSION: " $_PACKAGE_VERSION
echo "BUILD_NUMBER: " $BUILD_NUMBER
echo "****************************************"
#!/bin/bash --login
# Originally from, http://stackoverflow.com/questions/33041109
# Modified to work in RVM and non RVM environments
#
# Xcode 7 (incl. 7.0.1) seems to have a dependency on the system ruby.
# xcodebuild has issues by using rvm to map to another non-system
# ruby. This script is a fix that allows you call xcodebuild in a
# "safe" rvm environment, but will not (AFAIK) affect the "external"
# rvm setting.
#
# The script is a drop in replacement for your xcodebuild call.
#
# xcodebuild arg1 ... argn
#
# would become
#
# path/to/xcbuild-safe.sh arg1 ... argn
#
# More information available here: https://github.com/fastlane/fastlane/issues/6495
# -----
which rvm > /dev/null
if [[ $? -eq 0 ]]; then
echo "RVM detected, forcing to use system ruby"
# This allows you to use rvm in a script. Otherwise you get a BS
# error along the lines of "cannot use rvm as function". Jeez.
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
# Cause rvm to use system ruby. AFAIK, this is effective only for
# the scope of this script.
rvm use system
fi
if which rbenv > /dev/null; then
echo "rbenv detected, removing env variables"
# Cause rbenv to use system ruby. Lasts only for the scope of this
# session which will normally just be this script.
rbenv shell system
fi
# Since Xcode has a dependency to 2 external gems: sqlite and CFPropertyList
# More information https://github.com/fastlane/fastlane/issues/6495
# We have to unset those variables for rbenv, rvm and when the user uses bundler
unset RUBYLIB
unset RUBYOPT
unset BUNDLE_BIN_PATH
unset _ORIGINAL_GEM_PATH
unset BUNDLE_GEMFILE
# Even if we do not use rbenv in some environments such as CircleCI,
# We also need to unset GEM_HOME and GEM_PATH explicitly.
# More information https://github.com/fastlane/fastlane/issues/6277
unset GEM_HOME
unset GEM_PATH
set -x # echoes commands
xcodebuild "$@" # calls xcodebuild with all the arguments passed to this
"scripts": {
"ios-version": "./ios-version.sh",
"ios-release": "./ios-release.sh",
"android-release": "./android-release.sh"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment