使用环境变量/参数化 config.xml [英] Using environment variables / parameterizing config.xml

查看:39
本文介绍了使用环境变量/参数化 config.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用用于 Facebook 身份验证访问的 cordova-plugin-facebook4 插件构建一个 ionic/cordova 项目.在 config.xml 中,这看起来像:

<变量名=APP_ID"值=1234567890123456"/><变量名=APP_NAME"值=My_Appy_App"/></插件>

这可以正常工作,但我们使用的 APP_ID 用于开发应用,我们有一个单独的 Facebook 应用用于其他环境,例如 QA.

有什么方法可以在 config.xml 中参数化这些变量并将它们替换为构建步骤的一部分?类似的东西:

<variable name="bar" value="${env.APP_ID}"/></插件>

...然后运行 ​​APP_ID=baz ionic build android 或类似的东西.

我在cordova 文档中没有看到任何允许您执行此操作的内容.

解决方案

正如你在帖子中提到的,cordova 官方文档中没有太多关于此的文档.在花了一些时间进行分析后,我得出以下结论:

有一些现成的最小帮助可用于参数化config.xml 中可用的插件变量.这可以通过官方cordova链接<中提到的偏好变量来实现/a>.但是这种方法的问题在于它的工作取决于插件的编码方式.

我用 facebook 插件尝试了这种方法,但没有用 :( 我尝试如下:

<插件名称="cordova-plugin-facebook4" spec="~1.7.1"><变量名="APP_ID">$MY_CUSTOM_STRING</variable><变量名=APP_NAME"值=My_Appy_App"/></插件>

为谷歌地图插件尝试了相同的方法,它奏效了:) 我尝试如下:

<plugin name="cordova-plugin-googlemaps" spec="~1.3.9"><变量名称="API_KEY_FOR_ANDROID">$MY_CUSTOM_STRING</variable></插件>

所以我只能得出结论,参数化方法依赖于核心插件代码.

在 facebook 插件的情况下,如果你想参数化 APP_ID 变量,那么我猜钩子是继续的方式.即使是一个简单的 Windows 批处理文件来替换字符串匹配也应该没问题,并且可以在预构建操作中调用它来实现您的要求.希望有帮助.

更新:

我同意布兰登的评论.

在我有限的时间里,我想出了解决这个问题的cordova hook.这可能是一种粗略的方法,也可以改进,但目前这种方法效果很好.已在我的 github 页面 中将钩子作为示例应用发布,README 文件中包含有关它的完整信息.希望能帮助到你.随时关注我.

I am building an ionic/cordova project using the cordova-plugin-facebook4 plugin for Facebook authentication access. In config.xml this looks like:

<plugin name="cordova-plugin-facebook4" spec="~1.7.1">
  <variable name="APP_ID" value="1234567890123456"/>
  <variable name="APP_NAME" value="My_Appy_App"/>
</plugin>

This works okay, but the APP_ID we are using is for the dev app and we have a separate facebook app for other environments such as QA.

Is there any way to parameterize these variables in config.xml and have them be replaced as part of a build step? Something like:

<plugin name="foo" spec="~0.0.0">
  <variable name="bar" value="${env.APP_ID}"/>
</plugin>

... and then run APP_ID=baz ionic build android or something like that.

I don't see anything in the cordova documentation that allows you to do this.

解决方案

As you mentioned in the post, there is not much documentation about this in the official cordova documentation. After spending some time on this analysis, this is what I concluded:

There is some minimal help available ready-made to parameterize plugin variables available in config.xml. This can be achieved through preference variables as mentioned in the official cordova link. But the problem with this approach is that its working depends on how on the plugin is coded.

I tried this approach with the facebook plugin, but it didn't work :( I tried as below:

<preference name="MY_CUSTOM_STRING" default="12345678901234567" />
    <plugin name="cordova-plugin-facebook4" spec="~1.7.1">
        <variable name="APP_ID">$MY_CUSTOM_STRING</variable>
        <variable name="APP_NAME" value="My_Appy_App"/>
    </plugin>

Tried out the same approach for google maps plugin and it worked :) I tried as below:

<preference name="MY_CUSTOM_STRING" default="12345678901234567" />
<plugin name="cordova-plugin-googlemaps" spec="~1.3.9">
    <variable name="API_KEY_FOR_ANDROID">$MY_CUSTOM_STRING</variable>
</plugin>

So all I could conclude is that the parameterizing approach is dependent on the core plugin code.

In the case of the facebook plugin, if you want to parameterize the APP_ID variable, then I guess hooks are the way to proceed. Even a simple windows batch file to replace a string match should be fine and it can be invoked on pre build action to achieve what you require. Hope it helps.

UPDATE:

I do agree with Brandon's comments.

With the limited time i had, I was able to come up with the cordova hook that resolves this issue. It may be a crude way and it can be refined too but for now this approach works fine. Have posted the hook as a sample app in my github page and the README file has complete info about it. Hope it helps. Keep me posted.

这篇关于使用环境变量/参数化 config.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆