Install preloaded plugins

Change-Id: Ib5695451a77cd047ee05b85da71d2ea895fb59d7
This commit is contained in:
Viktar Donich
2018-07-19 14:27:30 -07:00
parent a24fb63189
commit 3e98dee3f9
4 changed files with 57 additions and 3 deletions

View File

@@ -37,9 +37,10 @@
const jsPlugins =
this._handleMigrations(plugins.js_resource_paths || [], htmlPlugins);
const defaultTheme = config.default_theme;
const pluginsPending =
[].concat(jsPlugins, htmlPlugins, defaultTheme || []).map(
p => this._urlFor(p));
const pluginsPending = []
.concat(jsPlugins, htmlPlugins, defaultTheme || [])
.filter(p => !Gerrit._isPluginPreloaded(p))
.map(p => this._urlFor(p));
Gerrit._setPluginsPending(pluginsPending);
if (defaultTheme) {
// Make theme first to be first to load.

View File

@@ -187,5 +187,21 @@ limitations under the License.
element.importHref.secondCall.args[2]();
assert.equal(Gerrit._pluginInstallError.callCount, 2);
});
test('skips preloaded plugins', () => {
sandbox.stub(Gerrit, '_isPluginPreloaded')
.withArgs('plugins/foo/bar').returns(true)
.withArgs('plugins/42').returns(true);
sandbox.stub(Gerrit, '_setPluginsCount');
sandbox.stub(Gerrit, '_setPluginsPending');
element.config = {
plugin: {
html_resource_paths: ['plugins/foo/bar', 'plugins/baz'],
js_resource_paths: ['plugins/42'],
},
};
assert.isTrue(
Gerrit._setPluginsPending.calledWith([url + '/plugins/baz']));
});
});
</script>

View File

@@ -412,6 +412,15 @@ limitations under the License.
element.EventType.ADMIN_MENU_LINKS);
});
test('preloaded plugins are installed', () => {
const installStub = sandbox.stub();
Gerrit._preloadedPlugins = {foo: installStub};
Gerrit._installPreloadedPlugins();
assert.isTrue(installStub.called);
const pluginApi = installStub.lastCall.args[0];
assert.strictEqual(pluginApi.getPluginName(), 'foo');
});
suite('test plugin with base url', () => {
setup(() => {
sandbox.stub(Gerrit.BaseUrlBehavior, 'getBaseUrl').returns('/r');

View File

@@ -31,6 +31,8 @@
let _pluginsPendingCount = -1;
const PRELOADED_PROTOCOL = 'preloaded:';
const UNKNOWN_PLUGIN = 'unknown';
const PANEL_ENDPOINTS_MAPPING = {
@@ -101,6 +103,15 @@
// http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html
window.$wnd = window;
function installPreloadedPlugins() {
if (!Gerrit._preloadedPlugins) { return; }
for (const name in Gerrit._preloadedPlugins) {
if (!Gerrit._preloadedPlugins.hasOwnProperty(name)) { continue; }
const callback = Gerrit._preloadedPlugins[name];
Gerrit.install(callback, API_VERSION, PRELOADED_PROTOCOL + name);
}
}
function getPluginNameFromUrl(url) {
if (!(url instanceof URL)) {
try {
@@ -110,6 +121,9 @@
return null;
}
}
if (url.protocol === PRELOADED_PROTOCOL) {
return url.pathname;
}
const base = Gerrit.BaseUrlBehavior.getBaseUrl();
const pathname = url.pathname.replace(base, '');
// Site theme is server from predefined path.
@@ -432,6 +446,7 @@
const app = document.querySelector('#app');
if (!app) {
// No gr-app found (running tests)
Gerrit._installPreloadedPlugins = installPreloadedPlugins;
Gerrit._resetPlugins = () => {
_allPluginsPromise = null;
_pluginsInstalled = [];
@@ -622,5 +637,18 @@
return `${pluginName}-screen-${screenName}`;
};
Gerrit._isPluginPreloaded = function(url) {
const name = getPluginNameFromUrl(url);
if (name && Gerrit._preloadedPlugins) {
return name in Gerrit._preloadedPlugins;
} else {
return false;
}
};
window.Gerrit = Gerrit;
// Preloaded plugins should be installed after Gerrit.install() is set,
// since plugin preloader substitutes Gerrit.install() temporarily.
installPreloadedPlugins();
})(window);