Skip to content

Commit

Permalink
[7.10] [plugin-helpers/build] improve bwc by writing kibana.version t…
Browse files Browse the repository at this point in the history
…o package.json (elastic#82484)

* [plugin-helpers/build] improve bwc by writing kibana.version to package.json

* remove unused import

* update jest integration tests to use 7.9 and update snapshots

* update comment

Co-authored-by: spalger <[email protected]>
  • Loading branch information
Spencer and spalger authored Nov 4, 2020
1 parent 6dc36c3 commit 7408326
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
14 changes: 14 additions & 0 deletions packages/kbn-plugin-helpers/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import Path from 'path';

import semver from 'semver';
import { RunWithCommands, createFlagError, createFailError } from '@kbn/dev-utils';

import { findKibanaJson } from './find_kibana_json';
Expand Down Expand Up @@ -76,6 +77,19 @@ export function runCli() {
const plugin = loadKibanaPlatformPlugin(pluginDir);
const config = await loadConfig(log, plugin);
const kibanaVersion = await resolveKibanaVersion(versionFlag, plugin);

if (semver.satisfies(kibanaVersion, '<7.9')) {
log.error(
'These tools are not designed to work with version before 7.9, please checkout an earlier version of Kibana to build your plugin'
);
process.exit(1);
}
if (semver.satisfies(kibanaVersion, '~7.9.0')) {
log.warning(
'These tools might work with 7.9 versions, but there are known workarounds required. See https://github.com/elastic/kibana/issues/82466 for more info'
);
}

const sourceDir = plugin.directory;
const buildDir = Path.resolve(plugin.directory, 'build/kibana', plugin.manifest.id);

Expand Down
12 changes: 7 additions & 5 deletions packages/kbn-plugin-helpers/src/integration_tests/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ import del from 'del';
import globby from 'globby';
import loadJsonFile from 'load-json-file';

const KIBANA_VERSION = '7.9.0';
const PLUGIN_DIR = Path.resolve(REPO_ROOT, 'plugins/foo_test_plugin');
const PLUGIN_BUILD_DIR = Path.resolve(PLUGIN_DIR, 'build');
const PLUGIN_ARCHIVE = Path.resolve(PLUGIN_BUILD_DIR, `fooTestPlugin-7.5.0.zip`);
const PLUGIN_ARCHIVE = Path.resolve(PLUGIN_BUILD_DIR, `fooTestPlugin-${KIBANA_VERSION}.zip`);
const TMP_DIR = Path.resolve(__dirname, '__tmp__');

expect.addSnapshotSerializer(createReplaceSerializer(/[\d\.]+ sec/g, '<time>'));
Expand Down Expand Up @@ -64,22 +65,23 @@ it('builds a generated plugin into a viable archive', async () => {

const buildProc = await execa(
process.execPath,
['../../scripts/plugin_helpers', 'build', '--kibana-version', '7.5.0'],
['../../scripts/plugin_helpers', 'build', '--kibana-version', KIBANA_VERSION],
{
cwd: PLUGIN_DIR,
all: true,
}
);

expect(buildProc.all).toMatchInlineSnapshot(`
" info deleting the build and target directories
" warn These tools might work with 7.9 versions, but there are known workarounds required. See https://github.com/elastic/kibana/issues/82466 for more info
info deleting the build and target directories
info running @kbn/optimizer
│ info initialized, 0 bundles cached
│ info starting worker [1 bundle]
│ succ 1 bundles compiled successfully after <time>
info copying source into the build and converting with babel
info running yarn to install dependencies
info compressing plugin into [fooTestPlugin-7.5.0.zip]"
info compressing plugin into [fooTestPlugin-7.9.0.zip]"
`);

await extract(PLUGIN_ARCHIVE, { dir: TMP_DIR });
Expand Down Expand Up @@ -111,7 +113,7 @@ it('builds a generated plugin into a viable archive', async () => {
.toMatchInlineSnapshot(`
Object {
"id": "fooTestPlugin",
"kibanaVersion": "7.5.0",
"kibanaVersion": "7.9.0",
"optionalPlugins": Array [],
"requiredPlugins": Array [
"navigation",
Expand Down
35 changes: 26 additions & 9 deletions packages/kbn-plugin-helpers/src/tasks/write_server_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import { pipeline } from 'stream';
import { promisify } from 'util';

import semver from 'semver';
import vfs from 'vinyl-fs';
import { transformFileWithBabel, transformFileStream } from '@kbn/dev-utils';

Expand All @@ -37,16 +38,20 @@ export async function writeServerFiles({
}: BuildContext) {
log.info('copying source into the build and converting with babel');

const KIBANA_VERSION_79 = semver.satisfies(kibanaVersion, '~7.9.0');

// copy source files and apply some babel transformations in the process
await asyncPipeline(
vfs.src(
[
'kibana.json',
...(plugin.manifest.server
// always copy over the package.json file if we're building for 7.9
...(KIBANA_VERSION_79 ? ['package.json'] : []),
// always copy over server files if we're building for 7.9, otherwise rely on `server: true` in kibana.json manifest
...(KIBANA_VERSION_79 || plugin.manifest.server
? config.serverSourcePatterns || [
'yarn.lock',
'tsconfig.json',
'package.json',
'index.{js,ts}',
'{lib,server,common,translations}/**/*',
]
Expand All @@ -66,20 +71,32 @@ export async function writeServerFiles({
}
),

// add kibanaVersion to kibana.json files
// add kibanaVersion to kibana.json files and kibana.version to package.json files
// we don't check for `kibana.version` in 7.10+ but the plugin helpers can still be used
// to build plugins for older Kibana versions so we do our best to support those needs by
// setting the property if the package.json file is encountered
transformFileStream((file) => {
if (file.relative !== 'kibana.json') {
if (file.relative !== 'kibana.json' && file.relative !== 'package.json') {
return;
}

const json = file.contents.toString('utf8');
const manifest = JSON.parse(json);
const parsed = JSON.parse(json);

file.contents = Buffer.from(
JSON.stringify(
{
...manifest,
kibanaVersion,
},
file.relative === 'kibana.json'
? {
...parsed,
kibanaVersion,
}
: {
...parsed,
kibana: {
...parsed.kibana,
version: kibanaVersion,
},
},
null,
2
)
Expand Down

0 comments on commit 7408326

Please sign in to comment.