- Version: 0.8.2
- GitHub: https://github.com/cowfox/nativescript-multiple-environments-building
- NPM: https://www.npmjs.com/package/nativescript-multiple-environments-building
- Downloads:
- Last Day: 0
- Last Week: 0
- Last Month: 0
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 ID
in different environments. - Quickly apply the
Version #
frompackage.json
to the actual destinations (Info.plist
oniOS
andAndroidManifest.xml
onAndroid
).- With the new
autoBuildNumber
config, you could easily auto generate aVersion Code
and 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.plist
orstrings.xml
to set diff. App Name or other configs. - Having dif.
GoogleService-Info.plist
for 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
iOS
andAndroid
(via @nativescript-dev/multiple-environments).
Installation
npm i nativescript-multiple-environments-building --save-dev
How 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.release
Environment rules
The Env Rules file currently support the following configurations:
default
- The defaultEnv. Name
to use if thecmd
does not include--env.use.ENV_NAME
.extraPaths
- The additional paths to check theenv.
files and do the file copy if needed. For example, addingenvironments
will help choose rightenvironment.ts
forAngular
to use.- By default, it checks the
App_Resources
folder. And theextraPaths
will help cover other places. - The paths in
extraPaths
are 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. file
and 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
pair
includes two parts: thesource filename
and thedestination path
(relative to theproject root folder
). - For example:
{ "Info.plist":"App_Resources/iOS/Info.plist" }
means to copy theInfo.plist
to theApp_Resources
folder 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 filename
is not for a output file of a normal file copy, it will be ignored.
- Each
appIconPath
- Thefile path
used forns resources generate icons
CMD. This is very useful when the app has diff. App Icon under diff. environments onIOS
.- Still, the
path
is relative to theproject root folder
. - NOTE: On
Android
, the diff. App Icon under diff. environments could be still inside theApp_Resources
folder which could be safely deleted AFTER theprepare
process.
- Still, the
envFilesMatchRules
- The specificmatching rules
to help identify all theenv.
files that could be deleted AFTER aprepare
.- For example, if the
matchRules
use the words (dev
andrelease
) to help identify theenv
files, the value ofenvFilesMatchRules
should bedev|release
. - NOTE: This should be an
Android
ONLY config. OniOS
, it more relies ondirectCopyRules
config 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 Code
based 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 icons
CMD working, please be sure to NOT delete the defaulticon.png
files fromApp_Resources
folder. 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
Manual
way - Enter a free value to those fields into the Env Rules file every time BEFORE the app building.- In this way, set the flag
autoVersionCode
tofalse
and totally ignorebuildNumber
.
- In this way, set the flag
- The
Auto
way - By settingautoVersionCode
totrue
, before the app building, it will automatically generate a newVersion Code
based on the given values ofVersion #
andBuild #
.- For example, a version # (
1.16.6
) and build # (22
) will generate a newVersion Code
as1160622
.
- For example, a version # (
For buildNumber
,
- It is a number between
0
and99
. - When the app gets building, if the
Version #
does not change, it will be added by1
automatically.- If changed, it will be reset to
1
from the start - meaning "the FIRST build of the new version".
- If changed, it will be reset to
NOTES:
- In order to make the
Version Code
generation process works, theminer
,patch
parts 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 Code
is 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
gradle
file. In other words, we could simply update them BEFORE theprepare
process.
// 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.