Str*_*bbl 15 versioning version cordova
我怎么知道我的Cordova应用程序是哪个版本的?我需要该信息才能在"关于"屏幕中显示.
是否可以读取config.xml并获取我已在此文件中维护的版本字符串?
<widget ... version="0.0.1" ...
Run Code Online (Sandbox Code Playgroud)
我想有一个解决方案,我不必在代码中的几个地方维护应用程序的版本号.此外,我不想为此目的插件,因为大多数插件不支持Android,iOS和浏览器作为平台.
或者我是否忽略了核心插件属性?
或者有一个解决方案,我维护一个文件,这不是config.xml,并且config.xml从该文件获取版本信息?
您是如何在应用程序的屏幕上实现"显示版本信息"的?任何提示都表示赞赏.
我的Cordova版本是:
>cordova -v
4.2.0
Run Code Online (Sandbox Code Playgroud)
Gan*_*dhi 19
如果您对使用任何第三方插件不感兴趣,那么我觉得Cordova挂钩是要走的路.您可以提供一个before_build钩子来从config.xml文件中读取版本,并在应用程序中使用相同的版本.
幸运的是,这个钩子在以下链接中随时可用,它们从config.xml读取版本并在需要时将其作为依赖项注入.
建议您也查看cordova挂钩的官方文档,以便更好地理解.
实际上,您还可以尝试从config.xml中读取版本信息,并将其直接放在您可以在其中创建的占位符中index.html以显示版本信息.这可以在使用钩子进行cordova构建之前完成,而钩子又使用执行文件操作的Windows批处理文件.您可以在我的github页面中查看使用批处理文件的cordova钩子的工作示例.
根据其他答案,我创建了以下before_prepare最适合我的挂钩cordova 9 / ionic 5。
它不需要任何额外的库来解析配置 xml,因为它通过正则表达式获取版本。它适用于browser、iOS和android平台环境。它也不起作用,ionic serve因为这里没有执行科尔多瓦挂钩。
#!/usr/bin/env node
// Add %%VERSION%% at any place to index.html file to be replaced by this script.
var fs = require('fs');
var path = require('path');
function fileStringReplace(filename, search, replace) {
var content = fs.readFileSync(filename, 'utf8');
content = content.replace(new RegExp(search, "g"), replace);
fs.writeFileSync(filename, content, 'utf8');
}
module.exports = function(context) {
var rawConfig = fs.readFileSync("config.xml", 'ascii');
var match = /^<widget.+version="([\d\.]+)".+?>$/gm.exec(rawConfig);
if(!match || match.length != 2)
throw new Error("version parse failed");
var version = match[1];
fileStringReplace("www/index.html", "%%VERSION%%", version);
console.log("replaced version to " + version);
}
Run Code Online (Sandbox Code Playgroud)
将以下钩子注册添加到config.xml:
<hook src="hooks/patch-indexhtml-version.js" type="before_prepare" />
Run Code Online (Sandbox Code Playgroud)
index.html行动中:
<html lang="de" data-appversion="%%VERSION%%">
Run Code Online (Sandbox Code Playgroud)
现在您可以执行以下操作来获取应用程序中的版本:
document.documentElement.getAttribute("data-appversion");
Run Code Online (Sandbox Code Playgroud)
根据@Gandhi 在他的回答中指出的脚本,我更新了脚本以使其适用于当前的cordova@8:我将它用作“ after_prepare ”钩子,而不是“before_build”,在后一种情况下在执行钩子后不久,cordova 将内容复制到平台目录时,更改的文件再次被覆盖......
该脚本使用 xml2js,因此请务必执行 execnpm i xml2js --save-dev以使 xml2js 可用。
#!/usr/bin/env node
// taken from https://www.bram.us/2015/01/04/cordova-build-hook-script-for-displaying-build-version-in-your-app/
// see /sf/answers/2985558971/
// This plugin replaces text in a file with the app version from config.xml.
// be sure to exec `npm i xml2js --save-dev` to make xml2js available
var wwwFileToReplace = "js/config.js";
var fs = require('fs');
var path = require('path');
var xml2js = require('xml2js');
function loadConfigXMLDoc(filePath) {
var json = "";
try {
var fileData = fs.readFileSync(filePath, 'ascii');
var parser = new xml2js.Parser();
parser.parseString(fileData.substring(0, fileData.length), function (err, result) {
//console.log("config.xml as JSON", JSON.stringify(result, null, 2));
json = result;
});
console.log("File '" + filePath + "' was successfully read.");
return json;
} catch (ex) {
console.log(ex)
}
}
function replace_string_in_file(filename, to_replace, replace_with) {
var data = fs.readFileSync(filename, 'utf8');
var result = data.replace(new RegExp(to_replace, "g"), replace_with);
fs.writeFileSync(filename, result, 'utf8');
//console.log("replaced in ", filename, "(", to_replace, " -> ", replace_with);
var data2 = fs.readFileSync(filename, 'utf8');
console.log(data2);
}
module.exports = function(context) {
// var rootdir = process.argv[2]; // old cordova version
var rootdir = context.opts.projectRoot;
console.log("projectRoot=", rootdir);
var configXMLPath = "config.xml";
var rawJSON = loadConfigXMLDoc(configXMLPath);
var version = rawJSON.widget.$.version;
console.log("Version:", version);
// var currentBuildPlatforms = process.env.CORDOVA_PLATFORMS.split(","); // old cordova version
var currentBuildPlatforms = context.opts.cordova.platforms;
//console.log(JSON.stringify(context));
console.log("Current build platforms: ", currentBuildPlatforms);
if (rootdir) {
currentBuildPlatforms.forEach(function(val, index, array) {
var wwwPath = "";
switch(val) {
case "ios":
wwwPath = "platforms/ios/www/";
break;
case "android":
wwwPath = "platforms/android/assets/www/";
break;
default:
console.log("Unknown build platform: " + val);
}
var fullfilename = path.join(rootdir, wwwPath + wwwFileToReplace);
if (fs.existsSync(fullfilename)) {
replace_string_in_file(fullfilename, "%%VERSION%%", version);
console.log("Replaced version in file: " + fullfilename);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
在您的config.xml:
<hook src="scripts/after_prepare_read_app_version.js" type="after_prepare" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14191 次 |
| 最近记录: |