Skip to content

Commit

Permalink
Add build number to iOS settings page
Browse files Browse the repository at this point in the history
To provide users with the visibility of the build number on
the settings page, which aids in troubleshooting and version
tracking.

Implemented a Cordova hook to read the build number from the
environment variable and update the `Root.plist` file.
The `plist` library is used to parse and write the plist file.
The dependency was added using `npm install --save plist`.

Requires the plist library as a new dependency.
  • Loading branch information
karolszafranski committed Jul 31, 2024
1 parent dbeb7c4 commit a40a4ed
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cordova/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<plugin name="cordova-plugin-ios-non-exempt-encryption" spec="1.0.0" />

<platform name="ios">
<resource-file src="res/Settings.bundle" target="Settings.bundle" />
<resource-file src="res/Settings.bundle/Root.plist" target="Settings.bundle/Root.plist" />

<preference name="WKWebViewOnly" value="true" />

<feature name="CDVWKWebViewEngine">
Expand Down
21 changes: 21 additions & 0 deletions cordova/res/Settings.bundle/Root.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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>StringsTable</key>
<string>Root</string>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Type</key>
<string>PSTitleValueSpecifier</string>
<key>Title</key>
<string>$BUILD_NUMBER</string>
<key>Key</key>
<string>app_version_setting_id</string>
<key>DefaultValue</key>
<string></string>
</dict>
</array>
</dict>
</plist>
36 changes: 36 additions & 0 deletions cordova/scripts/replace-config-env-vars.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = function(context) {

const fs = require('fs');
const plist = require('plist');
const cordova_util = context.requireCordovaModule('cordova-lib/src/cordova/util');

let projectRoot = cordova_util.isCordova();
Expand All @@ -10,4 +11,39 @@ module.exports = function(context) {
data = data.replace('$' + envVar, process.env[envVar]);
}
fs.writeFileSync(configXML, data, 'utf8');

const plistPath = 'res/Settings.bundle/Root.plist';
console.log(`Reading plist file from ${plistPath}`);
if (fs.existsSync(plistPath)) {
let plistData = fs.readFileSync(plistPath, 'utf8');
console.log('Plist file read successfully.');

let plistObj;
try {
plistObj = plist.parse(plistData);
console.log('Plist file parsed successfully.');
} catch (err) {
console.error('Error parsing plist data:', err);
return;
}

let buildNumberReplaced = false;
plistObj.PreferenceSpecifiers.forEach(specifier => {
console.log(`Specifier: ${JSON.stringify(specifier)}`);
if (specifier.Title === '$BUILD_NUMBER') {
specifier.Title = `Build number: ${process.env.BUILD_NUMBER}`;
console.log(`Replaced $BUILD_NUMBER with ${specifier.Title}`);
buildNumberReplaced = true;
}
});

if (buildNumberReplaced) {
fs.writeFileSync(plistPath, plist.build(plistObj), 'utf8');
console.log('Plist file written successfully.');
} else {
console.warn('$BUILD_NUMBER was not found in plist file.');
}
} else {
console.warn(`Plist file does not exist at ${plistPath}`);
}
};
74 changes: 74 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"start": "tabris serve -a -w"
},
"dependencies": {
"plist": "^3.1.0",
"tabris": "latest"
},
"devDependencies": {
Expand Down

0 comments on commit a40a4ed

Please sign in to comment.