nativescript-multiple-environments-building v0.8.2
nativescript-multiple-environments-building
This Hook aims to provide a better support for building a NativeScript application with multiple environments, such as:
- Easily use different
App Bundle IDin different environments. - Quickly apply the
Version #frompackage.jsonto the actual destinations (Info.plistoniOSandAndroidManifest.xmlonAndroid).- With the new
autoBuildNumberconfig, you could easily auto generate aVersion Codeand use it when in the RELEASE mode.
- With the new
- Safely configure the File Copying strategies to any type of file under different environments, like:
- Using diff.
Info.plistorstrings.xmlto set diff. App Name or other configs. - Having dif.
GoogleService-Info.plistfor Google services.
- Using diff.
- Simply generate dedicated App Icon image for each environment.
Credits to federicorp, the original creator of this amazing idea (via @nativescript-dev/multiple-environments), as well as jitendraP-ashutec who helped add the support to separate env. rule files for
iOSandAndroid(via @nativescript-dev/multiple-environments).
Installation
npm i nativescript-multiple-environments-building --save-devHow to use?
Add two Env Rules files into the project's root folder, and they will contain the configurations for iOS and Android separately.
environment-rules.ios.json- foriOS.environment-rules.android.json- forAndroid.
An example Env Rules file looks like this:
{
"version": "1.6.6",
"buildNumber": "66",
"versionCode": "1060666",
"autoVersionCode": true,
"default": "staging",
"extraPaths": [
"environments"
],
"directCopyRules": {
"Info.plist": "App_Resources/iOS/Info.plist",
},
"appIconPath": "environments/app-icon/icon.png",
"envFilesMatchRules": "staging|release",
"environments": [
{
"name": "staging",
"appBundleId": "org.nativescript.appId.staging",
"matchRules": "(.*\\.staging\\..*)"
},
{
"name": "release",
"appBundleId": "org.nativescript.appId.release",
"matchRules": "(.*\\.release\\..*)"
}
]
}With it, using --env.use.ENV_NAME to specify the actual environment to process while {NS} doing the prepare process. For example:
# Debug iOS app with `staging` env. configs.
ns debug ios --env.use.staging
# Build a final release
ns run ios --bundle --env.aot --env.uglify --env.use.releaseEnvironment rules
The Env Rules file currently support the following configurations:
default- The defaultEnv. Nameto use if thecmddoes not include--env.use.ENV_NAME.extraPaths- The additional paths to check theenv.files and do the file copy if needed. For example, addingenvironmentswill help choose rightenvironment.tsforAngularto use.- By default, it checks the
App_Resourcesfolder. And theextraPathswill help cover other places. - The paths in
extraPathsare relative to theproject root folder.
- By default, it checks the
environments- It defines for each environment, including:name- TheEnv. Name, used by--env.use.ENV_NAME.appBundleId- The target App Bundle ID.matchRules- The matching rule to help find thesource env. fileand do the file copy.- For example,
(.*\\.staging\\..*)will help locate the fileInfo.staging.plist.
- For example,
directCopyRules- The direct copying rules conducted AFTER the normal file copy. Typically, it is used to help prepare the files oniOS.- Each
pairincludes two parts: thesource filenameand thedestination path(relative to theproject root folder). - For example:
{ "Info.plist":"App_Resources/iOS/Info.plist" }means to copy theInfo.plistto theApp_Resourcesfolder once it gets copied from itsenv.version (likeInfo.dev.plist). - NOTE: The direct file copying ONLY happens after done a normal file copy. In other words, if the
source filenameis not for a output file of a normal file copy, it will be ignored.
- Each
appIconPath- Thefile pathused forns resources generate iconsCMD. This is very useful when the app has diff. App Icon under diff. environments onIOS.- Still, the
pathis relative to theproject root folder. - NOTE: On
Android, the diff. App Icon under diff. environments could be still inside theApp_Resourcesfolder which could be safely deleted AFTER theprepareprocess.
- Still, the
envFilesMatchRules- The specificmatching rulesto help identify all theenv.files that could be deleted AFTER aprepare.- For example, if the
matchRulesuse the words (devandrelease) to help identify theenvfiles, the value ofenvFilesMatchRulesshould bedev|release. - NOTE: This should be an
AndroidONLY config. OniOS, it more relies ondirectCopyRulesconfig to help achieve the goals. - If not set, it will use the default value -
development|dev|edge|test|uat|beta|staging|sta|release|prod|production.
- For example, if the
version- The version #, same as the one in thepackage.json.buildNumber- The build # that changes each time when building the app.versionCode- The version code that is used to fillCFBundleVersion(oniOS) orVersionCode(onAndroid).autoVersionCode- The flag to indicate if using the built-in logic to auto generateVersion Codebased onVersion #andBuild #.
Discussions
Usage of directCopyRules
Given the case that the ns prepare process will add all files inside the App_Resources/iOS folder into the Xcode project which will include all env. files for other environments, the rules inside directCopyRules will help conduct the file copy OUTSIDE the App_Resources/iOS folder.
And for Android, since it still can be safe to delete those env. files AFTER the ns prepare process, the deletion process is just simply added to the after-prepare Hook.
In other words, directCopyRules would be probably used on iOS side unless you are still having some other needs for Android.
App Icon Generation
The generation of App Icon is conducted by the built-in {NS} CMD - ns resources generate icons.
By default, if the icon.png file is inside the App_Resources folder for both iOS and Android, the process can help prepare the App Icon files for both iOS and Android. However, the NEW Android OS versions are actually NOT using icon.png to be the App Icon. Instead, it uses a mipmap style (by an xml file to define both foreground and background). Luckily, the new method could be still under the realm of env. based file copying.
Example of ic_launcher.xml file:
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>NOTE: In order to make the
ns resources generate iconsCMD working, please be sure to NOT delete the defaulticon.pngfiles fromApp_Resourcesfolder. Otherwise, the generation process may fail!!!
App Versioning
With the new added related Version Info of the Env Rules file, now you could manage the App Version in the following ways:
- The
Manualway - Enter a free value to those fields into the Env Rules file every time BEFORE the app building.- In this way, set the flag
autoVersionCodetofalseand totally ignorebuildNumber.
- In this way, set the flag
- The
Autoway - By settingautoVersionCodetotrue, before the app building, it will automatically generate a newVersion Codebased on the given values ofVersion #andBuild #.- For example, a version # (
1.16.6) and build # (22) will generate a newVersion Codeas1160622.
- For example, a version # (
For buildNumber,
- It is a number between
0and99. - When the app gets building, if the
Version #does not change, it will be added by1automatically.- If changed, it will be reset to
1from the start - meaning "the FIRST build of the new version".
- If changed, it will be reset to
NOTES:
- In order to make the
Version Codegeneration process works, theminer,patchparts ofVersion #andBuild #should not exceed99. This limitation should cover most of the cases - typically you may have already bumped the upper part of the version number.- The whole updating logic of
Build #andVersion Codeis ONLY used in the RELEASE mode!! With it, it will not bring too much hassle while in the Dev mode.- With the "NEW" Android building structure, the "version info" could be added to the
gradlefile. In other words, we could simply update them BEFORE theprepareprocess.
// Example of adding "version info" to `gradle` file
android {
defaultConfig {
// Version Information
versionCode 1060606
versionName "1.6.6"
}
}Also, the iOS and Android manage their Version Info separately. In the normal case, if you build the app against both platforms at the same time, the generated Version Code (as well as the buildNumber) should be identical for each release. However, if it does not (due to some unexpected reasons), it should be very easy to fix it by Manually change it to a correct value inside the Env Rules file.