2

我的目标是创建一个在 ember build 和 ember serve 上都更新的自动递增内部版本号。最后,如果我只能在构建时使用它,那完全可以。

我最初问了这个问题: In-repo addon writing public files on build会导致无限的构建循环在服务 中我试图通过写出JSON文件来解决这个问题。问题基本解决了,但没有使用 ember serve。

我现在没有这样做,而是尝试更新本地环境。但这与 ember serve 有类似的问题。我的内部版本号递增得很好。我可以使用 config() 方法在环境中设置自定义/动态变量。我遇到的问题是,即使我可以在调用 config() 时在终端中记录更改,并且当文件更改时我可以看到它在服务上运行,但当我输出 Ember 时我看不到浏览器中的更改ENV 使用 ember 服务。到目前为止,这是我的插件的方法。

注意:appNumberSetup() 函数只是读取项目根目录中的本地 json 文件并更新内部版本号。这工作正常。关于 pubSettingsFile 的任何内容都可以忽略,我不会继续使用它。

init(parent, project) {
    this._super.init && this._super.init.apply(this, arguments);
    // we need to setup env in init() so config() and prebuild()
    // will see update immediately
    this.settingsFile = path.resolve(this.appDir,  this.settingsFileName);
    this.addonPubDataPath = path.resolve(this.appDir, 'lib', this.name, 'inc', 'public', 'build-data-output');
    this.pubSettingsFile = path.resolve(this.addonPubDataPath,  this.pubSettingsFileName);
    // this only checks for .env variables and sets defaults
    this.dotEnvSetup();
    // must set this so prebuild skips processing a build number on build
    // else we get build number incremented twice on first run
    // then appNumberSetup() disables so subsequent serve preBuild() will run.
    this.skipPreBuild = true;
    this.appNumberSetup();

},
// this sends our created settings data to ENV.localBuildSettings in app
config(environment, appConfig){
    // this 'buildme' is just an experiment
    let x = `buildme${this.buildNumber}`;
    let r = {
        localBuildSettings: this.settings
    };
    r[`buildme${this.buildNumber}`] = this.buildNumber;
    this.dlog("Config ran...");
    this.dlog(JSON.stringify(r, null, 4));
    return r;
},
preBuild: function(result){
    // init() disables preBuild() here, but subsequent builds with serve still
    // run appNumberSetup() to update this.settings for env and JSON
    if(this.skipPreBuild === true){
        this.skipPreBuild = false;
    }
    else {
        // only run here after init runs
        this.appNumberSetup();
    }
    // don't do this... write file makes endless loop on serve
    // this.saveSettingsFile(this.pubSettingsFile, this.settings);

},

this.settings 是插件中的局部变量,它在构建/服务时更新,JSON 如下所示:

{
"appVersion": 911,
"appBuildNumber": 7117
}

有没有办法用动态数据更新 Ember 的 ENV?(就像一个新的内部版本号)

插件 config() 似乎会在 ember serve 中的每次更改时运行,并在终端输出中显示内部版本号。但它看起来像在 postBuild() 之后运行。也许这就是我看不到变化的原因。有没有办法在 preBuild() 期间更新该环境?

4

1 回答 1

0

我不确定具体细节,但ember-cli-new-version可以做到这一点。在构建阶段,他们创建了一个 VERSION.txt 文件,甚至可以执行您已经需要的操作,而无需自己编写。

于 2019-05-26T04:57:35.173 回答