cordova-cli
cordova-cli copied to clipboard
Pass app id, name and version to build command.
Feature Request
Motivation Behind Feature
For a monorepo with single dependencies, a single config.xml I have to change the xml before build. It would be better if there is an option to pass these parameters to the build or run command. To set the app id, name and version. Without changing the config.xml. All other settings (incl. plugins) are shared for all apps.
Feature Description
cordova build android --release --app-id="com.foo.bar" --app-name="Foo Bar" --app-version="1.2.3"
cordova run android --app-id="com.test.me" --app-name="Test App" --app-version="0.0.0"
Parameter | Description |
---|---|
app-id | The app id like in config.xml widget.$.id. |
app-version | The app version like in config.xml widget.$.version. |
app-name | The app name like in config.xml widget.name. |
config.xml
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.foo.bar" version="1.2.3"
xmlns="http://www.w3.org/ns/widgets"
xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>Foo Bar</name>
<!-- etc -->
</widget>
Alternatives or Workarounds
I have to update the config.xml with a xml parser. It's a custom implementation. I also don't want this git changes.
This is unlikely to be implemented at the Cordova level, because things like changing the app ID in between commands will break the native projects.
If you're wanting to generate multiple native projects with different settings from a single repo, I have written a tool (seymour) that will create and build projects with settings coming in from environment variables, which might be similar to what you're looking for.
Thanks. I'll check out Seymour. If I understand it correctly, I just have to set some temp env variables for that metadata and call the regular build command. Sounds interesting. I'll test it.
@dpogue The build works but it seems that the env vars does not work. e.g. my apk has the wrong version. And I set release-build, but it's still debug. etc. Do you have some examples?
@infacto I wrote seymour specifically for us to manage builds on a Jenkins CI environment, where we wanted to have different names/IDs for stages like QA, Beta, and Production. We set the environment variables as part of our Jenkins build script.
However, if you're trying to make a build locally on the command-line, this should work:
SEY_APP_NAME=MyApp SEY_APP_VERSION=1.1.2 SEY_BUILD_TYPE=release npx seymour
If you want to set them once, and then run multiple builds, you should be able to define them in bash ahead of time:
export SEY_APP_NAME=MyApp
export SEY_APP_VERSION=1.1.2
export SEY_BUILD_TYPE=release
npx seymour
Note: Seymour's unit tests pass on Windows, but I've never actually run it there myself, so I'm not sure exactly how to configure the environment variables there. My examples should work on both Linux and macOS in bash-like shells.
It does not work for me (Windows, Linux not tested.) Anyway, I use my config.xml xml parser overwrite solution now. I wonder how to implement this issue "passing parameters for the build command". When is the config.xml used? At this point it should be possible to inject / overwrite selected parameters. Maybe I'll try seymour again. I don't want to change the config.xml. 🤔
This isn't a solution but in my projects I use a python script to read a template xml file, which writes/replaces the config.xml
used by cordova. config.xml
is git ignored.
One problem with this method is the ios platform uses a lot of the app names in the xcode project file structure, so the ios platform doesn't like it when the project name changes. I'm not certain exactly what this entails, but I would be surprised if it only includes file renaming. I'm sure there are files that contains path references that would have to be modified as well.
I use NodeJS with a xml to json parser and a json file per app with the required informations (id, name, version). Replaces the config.xml
. It's not ignored in git because the config.xml
is also required by Cordova for new plugins and other settings.
I could revert the file after build or on try-catch error. But it's not really safe. ...
Anyway ... In my opinion, passing parameters to the build command would be the best solution. Or a separate temporary file used by the Cordova build that overwrites the config.xml parameters if present. 🤔 Maybe an optional property in the build.json
?
Btw. to your iOS naming issue: The platforms
folder (incl. plugins
) should be not a part of the git repository. When you remove it before changing names, the build should work without issues. ...
Ok seymour works now for me. Because I call it directly in a NodeJS script, instead of using env vars and call via CLI.
build.js
const seymour = require('seymour');
seymour([], {
SEY_APP_NAME: 'MyApp',
SEY_APP_ID: 'com.my.app',
SEY_APP_VERSION: '1.2.3',
SEY_BUILD_TYPE: 'release'
});
Call: node build.js
.
It's optimal for me, because I use the app metadata from a custom json file. For me it's better than env vars.
But, this solution is like my solution. My solution is to parse and write xml via JavaScript.
Seymour also changes the config.xml
file. I wanted to avoid that. ... I hope for a solution to pass arguments to the cli or use a temporary file for this. Cordova could check if a file exists to overwrite the some data of config.xml. Or the CLI arguments solution. 🤔 Which Cordova repo is the right one for this?
Again the current workaround is to change the config.xml
. Either with seymour or an own solution. My solution is to use fast-xml-parser
, replace the json data and write the config.xml again. There is currently no solution to set parameters on the fly.
I'll see if I use seymour or my own solution (less dependencies). But I would prefer an official solution without changing the config.xml.
@dpogue I'm sorry to bother you. But I think you are more experienced in Cordova than I am. Just a short question: Which Cordova component is picking up the config.xml
to process the app id, name and version? I stick to my proposal, passing or injecting these metadata to the build without touching the config.xml
. I want to know it. 🙂 Just for interessting. My idea - as I already wrote - is to expand Cordova to use specific variable metadata. For example the build.json
for signing may include this. But idk. I want to see it first. Maybe there is a way to pass it via CLI. ... 🤔 What do you think today? I think there is no conflict when sending the app-id, app-name and app-version (only) via the CLI. Cordova just should know it, instead of using the data from config.xml
. ... I give up if you want. But I hope for this solution. ^^
All the config.xml parsing happens with cordova-common's ConfigParser: https://github.com/apache/cordova-common/blob/master/src/ConfigParser/ConfigParser.js
Note that this sometimes gets extended/patched in the various platforms to add platform-specific methods to it.