Skip to content

Instantly share code, notes, and snippets.

@stargt
Forked from wynch/Xcode_version_from_package.sh
Created December 7, 2022 05:41
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 stargt/47af2915ff6c5219cf20cde5c15feb3c to your computer and use it in GitHub Desktop.
Save stargt/47af2915ff6c5219cf20cde5c15feb3c to your computer and use it in GitHub Desktop.
React Native - Set Gradle & XCode build version from package.json
// ======================== inject version as project ext from android/build.gradle ====================== //
/**
* Read version from package.json
*/
subprojects { subproject ->
ext {
def npmVersion = getNpmVersionArray()
versionMajor = npmVersion[0]
versionMinor = npmVersion[1]
versionPatch = npmVersion[2]
}
}
import groovy.json.JsonSlurper
def getNpmVersionArray() {
def inputFile = new File("$rootDir/../package.json")
def packageJson = new JsonSlurper().parseText(inputFile.text)
def version = packageJson["version"]
def (major, minor, patch) = version.tokenize('.')
return [Integer.parseInt(major), Integer.parseInt(minor), Integer.parseInt(patch)] as int[]
}
// ======================== Read injected version from android/app/build.gradle ====================== //
android {
// ...
defaultConfig {
// Here, Compute version Name & versionCode the way you see fit
versionCode versionMajor * 10000 + versionMinor * 100 + versionPatch
versionName "${versionMajor}.${versionMinor}.${versionPatch}"
}
}
#!/usr/bin/env bash -e
##
## Automatic version from package.json file
##
## Call this script from your XCode Scheme:
## - Copy / paste this script in a .sh file
## - Open your app scheme in XCode (shortcut: Cmd + <)
## - go to Build > Pre-actions
## - Add a run Script ('+' button in scheme window > "New Run Script Action"
## - in this XCode script, simply call the sh file containing following code.
##
#
# OS-X compatible Bash function to get absolute path
# Source : https://stackoverflow.com/questions/284662/how-do-you-normalize-a-file-path-in-bash/29256624#29256624
#
function normpath() {
# Remove all /./ sequences.
local path=${1//\/.\//\/}
# Remove dir/.. sequences.
while [[ $path =~ ([^/][^/]*/\.\./) ]]; do
path=${path/${BASH_REMATCH[0]}/}
done
echo $path
}
# retrieve XCode environement Variables (these are automatically set when calling 'run script' phase in your target)
echo "PROJECT_DIR $PROJECT_DIR"
echo "INFOPLIST_FILE $INFOPLIST_FILE"
echo "BUILT_PRODUCTS_DIR ${BUILT_PRODUCTS_DIR}"
echo "TARGET_BUILD_DIR ${TARGET_BUILD_DIR}"
echo "INFOPLIST_PATH ${INFOPLIST_PATH}"
# from these, resolve current Info.plist & package.json paths
#
# Note: We need to update info.plist in build directory as well
# as project original Info.plist is already copied when this script is run
#
SOURCE_PLIST_PATH="${PROJECT_DIR}/${INFOPLIST_FILE}"
BUILD_PLIST_PATH="${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
PACKAGE_JSON_PATH="$(normpath ${PROJECT_DIR}/../package.json)"
echo "Project Plist file path $INFOPLIST_PATH"
echo "Product Plist file path $BUILD_PLIST_PATH"
echo "package.json path $PACKAGE_JSON_PATH"
# extract version information from package.json ()
PACKAGE_VERSION=$(cat ${PACKAGE_JSON_PATH} | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]')
# set Internal Field Separator to .
IFS='.'
# split version string and read it into VERSIONS array
read -ra VERSIONS <<< "$PACKAGE_VERSION"
# We choose a range of 100 for each version step
MAJOR=$((${VERSIONS[0]} * 10000))
MINOR=$((${VERSIONS[1]} * 100))
PATCH=$((${VERSIONS[2]} * 1))
BUILD_NUMBER="$(($MAJOR + $MINOR + $PATCH))"
echo "Package version $PACKAGE_VERSION"
echo "MAJOR : $MAJOR"
echo "MINOR : $MINOR"
echo "PATCH : $PATCH"
echo "BUILD_NUMBER : $BUILD_NUMBER"
# Update Source & Built plist files with new values
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString ${PACKAGE_VERSION}" "${SOURCE_PLIST_PATH}"
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString ${PACKAGE_VERSION}" "${BUILD_PLIST_PATH}"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUILD_NUMBER" "${SOURCE_PLIST_PATH}"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUILD_NUMBER" "${BUILD_PLIST_PATH}"
# git add source Info.plist to save changes
git add "${SOURCE_PLIST_PATH}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment