You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by GitBox <gi...@apache.org> on 2019/12/02 23:43:07 UTC
[GitHub] [cordova-ios] raphinesse commented on issue #613: Arrays in plist
files can not be adjusted with edit-config
raphinesse commented on issue #613: Arrays in plist files can not be adjusted with edit-config
URL: https://github.com/apache/cordova-ios/issues/613#issuecomment-560923150
Thanks for this detailed issue report. I can reproduce the behavior described by you and after some code reading, I unfortunately have to confirm that what you want to do is indeed not possible with `edit-config` or `config-file` tags at the moment. Below I will provide more details on my findings and a possible workaround.
First off, `edit-config` is for editing _attributes_ only. To quote [the docs](https://cordova.apache.org/docs/en/9.x/plugin_ref/spec.html#edit-config) (emphasis mine):
> Instead of appending new children to an XML document tree, `edit-config` makes modifications to _attributes_ of XML elements.
So what you are trying to do above _is not supposed to work_. Deletion of _tags_ is not supported using Cordova's built-in config editing mechanism. Neither for plist nor for any other XML files.
The reason I am making this distinction here, is that plist files are [handled separately from other XML files](https://github.com/apache/cordova-common/blob/2021a39a9eaf473424eafe20e0e126b587933359/src/ConfigChanges/ConfigFile.js#L103). If you look at the linked code, you will realize that the `mode` of the config change is completely disregarded for plist files. In fact, all changes to a plist file are handled by [this code](https://github.com/apache/cordova-common/blob/2021a39a9eaf473424eafe20e0e126b587933359/src/util/plist-helpers.js#L30-L38) which only does _merges_. Either of objects or of arrays. That explains the results you are seeing.
This special treatment of plist files seems to make sense given the fact that, due to their specific structure, we could do very little of use with them if we would apply the default rules for other XML files. However, I could not find this special treatment to be documented anywhere (did I miss it?). Furthermore I clearly see the limitations of the current editing capabilities – for plist files as well as for other XML files.
As a workaround, the following `after_prepare` hook should be able to do what you want:
```js
const fs = require('fs');
const path = require('path');
const plist = require('plist');
module.exports = ({ opts: { projectRoot, platforms } }) => {
if (!platforms.includes('ios')) return;
const projectName = 'HelloCordova';
const platformPath = path.join(projectRoot, 'platforms/ios');
const infoPlistPath = path.join(platformPath, projectName, `${projectName}-Info.plist`);
const infoPlist = plist.parse(fs.readFileSync(infoPlistPath, 'utf8'));
infoPlist.UISupportedInterfaceOrientations = [
'UIInterfaceOrientationPortrait',
'UIInterfaceOrientationPortraitUpsideDown',
];
infoPlist['UISupportedInterfaceOrientations~ipad'] = [
'UIInterfaceOrientationPortrait',
'UIInterfaceOrientationLandscapeLeft',
'UIInterfaceOrientationPortraitUpsideDown',
'UIInterfaceOrientationLandscapeRight',
];
fs.writeFileSync(infoPlistPath, plist.build(infoPlist, { indent: '\t' }));
}
```
So what do we take away from this issue? My suggestions would be:
- The behavior for plist files needs to be documented (if it is indeed not documented yet)
- It would be nice to at least have a warning if using `edit-config`/`mode` with plist files
- We need better config editing capabilities
Any additions?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org