简体   繁体   English

为不同环境构建 Electron

[英]Build Electron for different environments

Ahoy,哎呀,

I have an electron app where I currently manually edit an URL in one of my files, depending on which environment (dev, prod, test) I wish to build for.我有一个电子应用程序,我目前在其中一个文件中手动编辑 URL,具体取决于我希望为其构建的环境(开发、生产、测试​​)。 However I would rather like to define that in my script so I don't have to rewrite the URL every time (eg electron-builder -w --x64 --ia32 --test or something like that).但是我更愿意在我的脚本中定义它,这样我就不必每次都重写 URL(例如 electron-builder -w --x64 --ia32 --test 或类似的东西)。

As far as I can tell it is possible to have different build files ( https://github.com/electron-userland/electron-builder/issues/1109 ), but I can't quite figure how the setup should be, and furthermore I can't figure how to get the attributes from the build files (eg build/publish/url).据我所知,可以有不同的构建文件( https://github.com/electron-userland/electron-builder/issues/1109 ),但我不太清楚应该如何设置,并且此外,我无法弄清楚如何从构建文件(例如构建/发布/url)中获取属性。 It seems possible to get icon and name ( https://github.com/electron-userland/electron-builder/issues/639 ), but not url?似乎可以获取图标和名称( https://github.com/electron-userland/electron-builder/issues/639 ),但不能获取 url?

package.json

    {... ,
      "build":{
        ... ,
        "publish":[{
          ... ,
          "url": "https://my.website.url" <--- Change on build and fetchable in code?
        }],
      }
    }

I'm sorry if I have missed anything - I consider writing an explicit tutorial somewhere once I've got a hang of this.如果我错过了什么,我很抱歉 - 一旦我掌握了这个,我会考虑在某处写一个明确的教程。

Thanks in advance提前致谢

These solutions are not very elegant, but can still help:这些解决方案不是很优雅,但仍然可以提供帮助:

Prepare different configuration files beforehand事先准备不同的配置文件

Have different configuration YAML prepared and copy them to electron-builder.yml before building, depending on your platform or flavour.根据您的平台或风格,在构建之前准备不同的配置 YAML 并将它们复制到electron-builder.yml

Create the build configuration dynamically动态创建构建配置

This works better for more complicated settings:这对于更复杂的设置更有效:

Prepare a YAML or something else that contains all configurations that are true for every platform and a mock-value (or leave it empty) for the one you want to change at build time.准备一个 YAML 或其他内容,其中包含适用于每个平台的所有配置以及您要在构建时更改的模拟值(或将其留空)。 I use a YAML file for this, because I can copy paste this into the electron-builder.yml in case I want something new, but you know best what you need.我为此使用了一个 YAML 文件,因为我可以将它复制粘贴到electron-builder.yml中,以防我想要一些新的东西,但你最清楚你需要什么。

Before building you run a node script that:在构建之前,您运行一个节点脚本:

  • optionally deletes the old electron-builder.yml from the previous build if present如果存在,可以选择从以前的构建中删除旧的electron-builder.yml
  • reads the prepared config file读取准备好的配置文件
  • replaces the mock-value(s) with the real information in the read data用读取数据中的真实信息替换模拟值
  • writes the electron-builder.yml with the modified data使用修改后的数据写入electron-builder.yml

Your npm script would then look like node prepareYml.js && electron-builder .你的 npm 脚本看起来像node prepareYml.js && electron-builder

I found a (albeit a bit hack'ish imo) solution which I will use for now.我找到了一个(虽然有点hack'ish imo)解决方案,我现在将使用它。

I simply added "--c.extraMetadata.productName=dev-" (and test instead of dev for the test build) to the build script, and then in my code i would check which environment that had been prepended to the build, and switch the URL accordingly.我只是在构建脚本中添加了“--c.extraMetadata.productName=dev-”(和 test 而不是 dev 用于测试构建),然后在我的代码中,我将检查哪个环境已经预先添加到构建中,并且相应地切换 URL。 The good thing about this approach is that now I can also see which environment the .exe file is pointing at!这种方法的好处是现在我还可以看到 .exe 文件指向哪个环境!

I'm still convinced that the "correct" way is having multiple YAML files, like Rhayene suggests, but the approach above just seems much easier and more directly controlled.我仍然相信“正确”的方法是拥有多个 YAML 文件,就像 Rhayene 建议的那样,但上面的方法似乎更容易,更直接地控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM