Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(NA): introduce new yarn kbn reset command to support bazel workflow #89597

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
b89c402
chore(NA): introduce new yarn kbn destroy command to support bazel wo…
mistic Jan 28, 2021
229e602
Merge remote-tracking branch 'upstream/master' into add-kbn-destroy-c…
mistic Jan 28, 2021
4149c35
chore(NA): update wording for new kbn pm changes
mistic Jan 28, 2021
5bc9861
Merge branch 'master' into add-kbn-destroy-cmd-for-bazel-integration
kibanamachine Jan 28, 2021
2a6aebf
Merge branch 'master' into add-kbn-destroy-cmd-for-bazel-integration
kibanamachine Jan 28, 2021
60ad545
Merge branch 'master' into add-kbn-destroy-cmd-for-bazel-integration
kibanamachine Jan 28, 2021
77ed40a
Merge remote-tracking branch 'upstream/master' into add-kbn-destroy-c…
mistic Jan 28, 2021
bf74bf8
Merge branch 'master' into add-kbn-destroy-cmd-for-bazel-integration
kibanamachine Jan 29, 2021
c62a943
Merge branch 'master' into add-kbn-destroy-cmd-for-bazel-integration
kibanamachine Jan 31, 2021
0ebe848
Merge branch 'master' into add-kbn-destroy-cmd-for-bazel-integration
kibanamachine Feb 2, 2021
c830f51
chore(NA): update .bazelrc.common
mistic Feb 2, 2021
dafb69f
chore(NA): update .bazelrc
mistic Feb 2, 2021
198ab76
chore(NA): update packages/kbn-pm/src/commands/clean.ts
mistic Feb 2, 2021
8b4b975
chore(NA): update packages/kbn-pm/src/commands/destroy.ts
mistic Feb 2, 2021
6174436
chore(NA): update packages/kbn-pm/src/commands/clean.ts
mistic Feb 2, 2021
19f1413
chore(NA): update packages/kbn-pm/src/commands/destroy.ts
mistic Feb 2, 2021
f5ef355
chore(NA): rename destroy command into reset
mistic Feb 2, 2021
12b83f7
Merge remote-tracking branch 'upstream/master' into add-kbn-destroy-c…
mistic Feb 2, 2021
94def68
chore(NA): update packages/kbn-pm/src/commands/clean.ts
mistic Feb 2, 2021
37ba587
chore(NA): restore old behaviour on kbn clean
mistic Feb 2, 2021
5c52143
chore(NA): update reset command to delete bazel caches on disk
mistic Feb 2, 2021
5a46fa8
chore(NA): update packages/kbn-pm/src/commands/clean.ts
mistic Feb 2, 2021
976eb34
chore(NA): update prefix to match bazel defined one for cache settings
mistic Feb 2, 2021
4740d1b
Merge branch 'add-kbn-destroy-cmd-for-bazel-integration' of github.co…
mistic Feb 2, 2021
aee3b7f
chore(NA): update kbn pm dist file
mistic Feb 2, 2021
a8abd93
chore(NA): merge and solve conflicts with master
mistic Feb 3, 2021
9f844e9
chore(NA): update kbn pm dist
mistic Feb 3, 2021
ac98e19
chore(NA): update gitignore
mistic Feb 3, 2021
1354eeb
Merge remote-tracking branch 'upstream/master' into add-kbn-destroy-c…
mistic Feb 3, 2021
f8a9ef5
chore(NA): add new ignore files after changed bazel aggregated folder
mistic Feb 3, 2021
3176915
Merge remote-tracking branch 'upstream/master' into add-kbn-destroy-c…
mistic Feb 3, 2021
4f35ee9
Merge branch 'master' into add-kbn-destroy-cmd-for-bazel-integration
kibanamachine Feb 3, 2021
7e0afa2
chore(NA): merging and solve conflicts with master
mistic Feb 3, 2021
e27c9bb
Merge branch 'add-kbn-destroy-cmd-for-bazel-integration' of github.co…
mistic Feb 3, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Inspired on from https://raw.githubusercontent.com/bazelbuild/rules_nodejs/master/.bazelrc
# Inspired on https://raw.githubusercontent.com/bazelbuild/rules_nodejs/master/.bazelrc
mistic marked this conversation as resolved.
Show resolved Hide resolved
# Import shared settings first so we can override below
import %workspace%/.bazelrc.common

Expand Down
2 changes: 1 addition & 1 deletion .bazelrc.common
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Inspired on from https://raw.githubusercontent.com/bazelbuild/rules_nodejs/master/common.bazelrc
# Inspired on https://raw.githubusercontent.com/bazelbuild/rules_nodejs/master/common.bazelrc
mistic marked this conversation as resolved.
Show resolved Hide resolved
# Common Bazel settings for JavaScript/NodeJS workspaces
# This rc file is automatically discovered when Bazel is run in this workspace,
# see https://docs.bazel.build/versions/master/guide.html#bazelrc
Expand Down
3 changes: 3 additions & 0 deletions packages/kbn-dev-utils/stdio/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": "../target/stdio"
}
20,211 changes: 10,249 additions & 9,962 deletions packages/kbn-pm/dist/index.js

Large diffs are not rendered by default.

23 changes: 15 additions & 8 deletions packages/kbn-pm/src/commands/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,31 @@
* Public License, v 1.
*/

import dedent from 'dedent';
import del from 'del';
import ora from 'ora';
import { join, relative } from 'path';

import { runBazel } from '../utils/bazel';
import { isDirectory } from '../utils/fs';
import { log } from '../utils/log';
import { ICommand } from './';

export const CleanCommand: ICommand = {
description: 'Remove the node_modules and target directories from all projects.',
description:
'Remove target directories from all projects, remove extra patterns and runs a Bazel state soft clean.',
mistic marked this conversation as resolved.
Show resolved Hide resolved
name: 'clean',

async run(projects) {
log.warning(dedent`

'yarn kbn clean' does not remove node_modules now. It runs a more soft clean and should be used as the way to go between branches using Bazel.
When switching between a non Bazel branch and a Bazel branch or for situations where you need a deeper clean, run 'yarn kbn destroy' instead.

mistic marked this conversation as resolved.
Show resolved Hide resolved
`);

const toDelete = [];
for (const project of projects.values()) {
if (await isDirectory(project.nodeModulesLocation)) {
toDelete.push({
cwd: project.path,
pattern: relative(project.path, project.nodeModulesLocation),
});
}

if (await isDirectory(project.targetLocation)) {
toDelete.push({
cwd: project.path,
Expand All @@ -44,6 +47,10 @@ export const CleanCommand: ICommand = {
}
}

// Runs Bazel soft clean
await runBazel(['clean']);
log.success('Soft cleaned bazel');

if (toDelete.length === 0) {
log.success('Nothing to delete');
} else {
Expand Down
91 changes: 91 additions & 0 deletions packages/kbn-pm/src/commands/destroy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* and the Server Side Public License, v 1; you may not use this file except in
* compliance with, at your election, the Elastic License or the Server Side
* Public License, v 1.
*/

import dedent from 'dedent';
import del from 'del';
import ora from 'ora';
import { join, relative } from 'path';

import { runBazel } from '../utils/bazel';
import { isDirectory } from '../utils/fs';
import { log } from '../utils/log';
import { ICommand } from './';

export const DestroyCommand: ICommand = {
description:
'Remove the node_modules, remove target directories from all projects, remove extra patterns and runs a Bazel state hard clean.',
mistic marked this conversation as resolved.
Show resolved Hide resolved
name: 'destroy',
mistic marked this conversation as resolved.
Show resolved Hide resolved

async run(projects) {
log.warning(dedent`

'yarn kbn destroy' is the most deeper and destroying clean command. Use it only when switching between a non Bazel branch into a
Bazel branch or when you are having problems with node_modules.
Otherwise please just run 'yarn kbn clean'.

mistic marked this conversation as resolved.
Show resolved Hide resolved
`);

const toDelete = [];
for (const project of projects.values()) {
if (await isDirectory(project.nodeModulesLocation)) {
toDelete.push({
cwd: project.path,
pattern: relative(project.path, project.nodeModulesLocation),
});
}

if (await isDirectory(project.targetLocation)) {
toDelete.push({
cwd: project.path,
pattern: relative(project.path, project.targetLocation),
});
}

const { extraPatterns } = project.getCleanConfig();
if (extraPatterns) {
toDelete.push({
cwd: project.path,
pattern: extraPatterns,
});
}
}

// Runs Bazel hard clean
await runBazel(['clean', '--expunge']);
log.success('Hard cleaned bazel');

if (toDelete.length === 0) {
return;
}

/**
* In order to avoid patterns like `/build` in packages from accidentally
* impacting files outside the package we use `process.chdir()` to change
* the cwd to the package and execute `del()` without the `force` option
* so it will check that each file being deleted is within the package.
*
* `del()` does support a `cwd` option, but it's only for resolving the
* patterns and does not impact the cwd check.
*/
const originalCwd = process.cwd();
try {
for (const { pattern, cwd } of toDelete) {
process.chdir(cwd);
const promise = del(pattern);

if (log.wouldLogLevel('info')) {
ora.promise(promise, relative(originalCwd, join(cwd, String(pattern))));
}

await promise;
}
} finally {
process.chdir(originalCwd);
}
},
};
2 changes: 2 additions & 0 deletions packages/kbn-pm/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ export interface ICommand {

import { BootstrapCommand } from './bootstrap';
import { CleanCommand } from './clean';
import { DestroyCommand } from './destroy';
import { RunCommand } from './run';
import { WatchCommand } from './watch';
import { Kibana } from '../utils/kibana';

export const commands: { [key: string]: ICommand } = {
bootstrap: BootstrapCommand,
clean: CleanCommand,
destroy: DestroyCommand,
run: RunCommand,
watch: WatchCommand,
};
1 change: 1 addition & 0 deletions packages/kbn-pm/src/utils/bazel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
*/

export * from './install_tools';
export * from './run';
43 changes: 43 additions & 0 deletions packages/kbn-pm/src/utils/bazel/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* and the Server Side Public License, v 1; you may not use this file except in
* compliance with, at your election, the Elastic License or the Server Side
* Public License, v 1.
*/

import chalk from 'chalk';
import execa from 'execa';
import * as Rx from 'rxjs';
import { tap } from 'rxjs/operators';
import { observeLines } from '@kbn/dev-utils/stdio';
import { spawn } from '../child_process';
import { log } from '../log';

export async function runBazel(bazelArgs: string[], runOpts: execa.Options = {}) {
// Force logs to pipe in order to control the output of them
const bazelOpts: execa.Options = {
...runOpts,
stdio: 'pipe',
};

const bazelProc = spawn('bazel', bazelArgs, bazelOpts);

const bazelLogs$ = new Rx.Subject<string>();

// Bazel outputs machine readable output into stdout and human readable output goes to stderr.
// Therefore we need to get both. In order to get errors we need to parse the actual text line
const bazelLogSubscription = Rx.merge(
observeLines(bazelProc.stdout!).pipe(
tap((line) => log.info(`${chalk.cyan('[bazel]')} ${line}`))
),
observeLines(bazelProc.stderr!).pipe(
tap((line) => log.info(`${chalk.cyan('[bazel]')} ${line}`))
)
).subscribe(bazelLogs$);

// Wait for process and logs to finish, unsubscribing in the end
await bazelProc;
await bazelLogs$.toPromise();
await bazelLogSubscription.unsubscribe();
}