Skip to content

NS7 update #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 46 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,66 @@
Nativescript hook plugin to maintain native app version
=======================================================
# Nativescript hook plugin to maintain native app version

This plugin just takes the `nativescript.version` property from `package.json` and puts on the specific platform resources: `AndroidManifest.xml` file for the Android sources, and `Info.plist` for iOS sources.
This plugin takes the `version` and `versionNumber` properties from `package.json` and puts on the specific platform resources: `AndroidManifest.xml` file for the Android sources, and `Info.plist` for iOS sources.

This plugin is mainly a fork of [jacargentina/nativescript-dev-version](https://github.com/jacargentina/nativescript-dev-version), with the great ideas from [speigg/nativescript-dev-version](https://github.com/speigg/nativescript-dev-version/tree/patch-1) and [simplec-dev/nativescript-dev-version](https://github.com/simplec-dev/nativescript-dev-version).

Compatible with NS 6.

## How to use

How to use
----------
```
$ tns plugin add nativescript-dev-version
```

The above command installs this module and installs the necessary NativeScript hooks.

Then, specify and maintain the desired release version on the `package.json` file under the `nativescript.version` property, for example:
Then, specify and maintain the desired release version on the `./package.json` file under the `nativescript.version` property, for example:

```json
{
"nativescript": {
"id": "org.nativescript.MySampleApp",
"version": "1.2.3"
"version": "1.2.3",
"versionNumber": "1"
...
},
...
}
```

When running `tns prepare ...` the hooks will take care of the native resources, and your app will get the `1.2.3` version number!
or:

```json
{
"version": "1.2.3",
"versionNumber": "1"
...
}
```

When running `tns prepare ...` the hooks will take care of the native resources.


> **Warning**
> Remove/comment the lines in your `App_Resources/Android/app.gradle`:
> ```
> // Version Information
> // versionCode 1
> // versionName "1.0.0"
> ```

On iOS, your `Info.plist` will get:

```
<key>CFBundleShortVersionString</key>
<string>1.2.3</string>
<key>CFBundleVersion</key>
<string>1</string>
```

On Android, `AndroidManifest.xml` will have:

```
<manifest
(...) android:versionCode="10203001" android:versionName="1.2.3"
```
95 changes: 68 additions & 27 deletions lib/after-prepare.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,72 @@
var fs = require('fs');
var path = require('path');
var glob = require('glob');
var Promise = require('bluebird');
var AndroidManifest = require('androidmanifest');
var iOSPList = require('plist');

module.exports = function ($logger, $projectData, $usbLiveSyncService) {
var appPackage = require($projectData.projectFilePath);
if (!appPackage.nativescript || !appPackage.nativescript.version) {
$logger.warn('Nativescript version is not defined. Skipping set native package version.');
return;
}
var platformService = $injector.resolve('platformService');
var platformsData = $injector.resolve('platformsData');
return Promise.each(platformService.getPreparedPlatforms($projectData), function (platform) {
var platformData = platformsData.getPlatformData(platform);
if (platform == 'android') {
var manifest = new AndroidManifest().readFile(platformData.configurationFilePath)
manifest.$('manifest').attr('android:versionCode', appPackage.nativescript.version.replace(/\./g, ''))
manifest.$('manifest').attr('android:versionName', appPackage.nativescript.version);
manifest.writeFile(platformData.configurationFilePath);
}
else if (platform == 'ios') {
var plist = iOSPList.parse(fs.readFileSync(platformData.configurationFilePath, 'utf8'));
plist.CFBundleShortVersionString = appPackage.nativescript.version;
plist.CFBundleVersion = appPackage.nativescript.version;
fs.writeFileSync(platformData.configurationFilePath, iOSPList.build(plist));
}
});
}
module.exports = function($logger, $projectData, hookArgs) {
var appPackage = require($projectData.projectFilePath);
var appVersion =
(appPackage.nativescript && appPackage.nativescript.version) ||
appPackage.version;
let appVersionNumber =
(appPackage.nativescript && appPackage.nativescript.versionNumber) ||
appPackage.versionNumber;
if (!appVersion) {
$logger.warn(
'Nativescript version is not defined. Skipping set native package version.'
);
return;
}

var platformsData = getPlatformsData($injector);
var platform = (
hookArgs.platform ||
(hookArgs.prepareData && hookArgs.prepareData.platform)
).toLowerCase();
$logger.info(`Platform: ${platform}`);

var platformData = platformsData.getPlatformData(platform);
$logger.debug(
`platformData.configurationFilePath: ${platformData.configurationFilePath}`
);
if (platform == 'android') {
var manifest = new AndroidManifest().readFile(
platformData.configurationFilePath
);

// transforms e.g. "1.2.3" into 102003.
let versionCode = appVersion
.split('.')
.reduce(
(acc, v, i, a) => acc + v * Math.pow(10, (a.length - i - 1) * 2),
0
);

if (appVersionNumber) {
versionCode =
versionCode * 100 +
(appVersionNumber < 10 ? '0' : '') + // left pad appVersionNumber
appVersionNumber;
}

manifest.$('manifest').attr('android:versionCode', versionCode);
manifest.$('manifest').attr('android:versionName', appVersion);
manifest.writeFile(platformData.configurationFilePath);
$logger.info(`Updated android:versionCode: ${versionCode}, android:versionName: ${appVersion}`);
} else if (platform == 'ios') {
var plist = iOSPList.parse(
fs.readFileSync(platformData.configurationFilePath, 'utf8')
);
plist.CFBundleShortVersionString = appVersion;
plist.CFBundleVersion = appVersionNumber;
fs.writeFileSync(platformData.configurationFilePath, iOSPList.build(plist));
$logger.info(`Updated CFBundleShortVersionString: ${appVersion}, CFBundleVersion: ${appVersionNumber}`);
}
};

function getPlatformsData($injector) {
try {
return $injector.resolve('platformsData');
} catch (err) {
return $injector.resolve('platformsDataService');
}
}
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-dev-version",
"version": "0.1.1",
"version": "0.2.1",
"description": "Installs nativescript hooks to maintain native app version (AndroidManifest.xml and Info.plist)'",
"main": "index.js",
"scripts": {
Expand All @@ -20,9 +20,8 @@
]
},
"dependencies": {
"@nativescript/hook": "~2.0.0",
"androidmanifest": "^2.0.0",
"bluebird": "^3.4.7",
"nativescript-hook": "^0.2.1",
"plist": "^2.0.1"
}
}
2 changes: 1 addition & 1 deletion postinstall.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require('nativescript-hook')(__dirname).postinstall();
require('@nativescript/hook')(__dirname).postinstall();
2 changes: 1 addition & 1 deletion preuninstall.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require('nativescript-hook')(__dirname).preuninstall();
require('@nativescript/hook')(__dirname).preuninstall();