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

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

问题描述

我正在使用cordova-plugin-facebook4插件为Facebook身份验证访问建立一个离子/ cordova项目。 config.xml 这看起来像:

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>

这样可以正常工作,但 APP_ID 正在使用是为开发应用程序,我们有一个单独的Facebook应用程序的其他环境,如QA。

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.

有任何方法来参数化这些变量在 config.xml ,并将它们替换为构建步骤的一部分?像:

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>

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

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

我在cordova文档中没有看到任何东西允许你这样做。

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

推荐答案

正如你在文章中提到的,在官方cordova文档中没有太多的文档。在这个分析上花费一些时间之后,这是我的结论:

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:

有一些最小的帮助现成的参数化插件变量 config.xml 。这可以通过官方cordova链接。但这个方法的问题是它的工作取决于插件是如何编码的。

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.

我尝试这种方法与facebook插件,但它没有工作: (我尝试如下:

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>

尝试了同样的方法google maps 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.

在facebook插件的情况下,如果你想参数化 APP_ID 变量,进行的方式。甚至一个简单的Windows批处理文件来替换字符串匹配应该很好,它可以在预构建操作来调用,以实现您所需要的。希望它有帮助。

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.

更新:

我同意Brandon的意见。

I do agree with Brandon's comments.

由于时间有限,我能够提出解决这个问题的cordova钩子。它可能是一个粗略的方式,它可以改进,但现在这种方法工作正常。在我的 github网页中发布了一个示例应用程序的挂钩,并且README文件包含完整的信息。希望它有帮助。保持我发布。

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天全站免登陆