Skip to content

Commit

Permalink
[bazel] validate packages/BUILD.bazel in CI (#127241)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spencer authored Mar 15, 2022
1 parent 6b4e01f commit 9871111
Show file tree
Hide file tree
Showing 16 changed files with 236 additions and 159 deletions.
1 change: 1 addition & 0 deletions .buildkite/scripts/steps/checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export DISABLE_BOOTSTRAP_VALIDATION=false
.buildkite/scripts/bootstrap.sh

.buildkite/scripts/steps/checks/commit/commit.sh
.buildkite/scripts/steps/checks/bazel_packages.sh
.buildkite/scripts/steps/checks/telemetry.sh
.buildkite/scripts/steps/checks/ts_projects.sh
.buildkite/scripts/steps/checks/jest_configs.sh
Expand Down
8 changes: 8 additions & 0 deletions .buildkite/scripts/steps/checks/bazel_packages.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

set -euo pipefail

source .buildkite/scripts/common/util.sh

echo --- Check Bazel Packages Manifest
node scripts/generate packages_build_manifest --validate
3 changes: 2 additions & 1 deletion packages/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
################
################
## This file is automatically generated, to create a new package use `node scripts/generate package --help`
## This file is automatically generated, to create a new package use `node scripts/generate package --help` or run
## `node scripts/generate packages_build_manifest` to regenerate it from the current state of the repo
################
################

Expand Down
3 changes: 3 additions & 0 deletions packages/kbn-bazel-packages/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ RUNTIME_DEPS = [
"//packages/kbn-utils",
"//packages/kbn-std",
"@npm//globby",
"@npm//normalize-path",
]

# In this array place dependencies necessary to build the types, which will include the
Expand All @@ -55,7 +56,9 @@ RUNTIME_DEPS = [
TYPES_DEPS = [
"//packages/kbn-utils:npm_module_types",
"//packages/kbn-std:npm_module_types",
"@npm//@types/normalize-path",
"@npm//globby",
"@npm//normalize-path",
]

jsts_transpiler(
Expand Down
18 changes: 14 additions & 4 deletions packages/kbn-bazel-packages/src/bazel_package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,34 @@ const OWN_BAZEL_BUILD_FILE = Fs.readFileSync(Path.resolve(__dirname, '../BUILD.b

describe('hasBuildRule()', () => {
it('returns true if there is a rule with the name "build"', () => {
const pkg = new BazelPackage('foo', {}, OWN_BAZEL_BUILD_FILE);
const pkg = new BazelPackage('foo', { name: 'foo' }, OWN_BAZEL_BUILD_FILE);
expect(pkg.hasBuildRule()).toBe(true);
});

it('returns false if there is no rule with name "build"', () => {
const pkg = new BazelPackage('foo', {}, ``);
const pkg = new BazelPackage('foo', { name: 'foo' }, ``);
expect(pkg.hasBuildRule()).toBe(false);
});

it('returns false if there is no BUILD.bazel file', () => {
const pkg = new BazelPackage('foo', { name: 'foo' });
expect(pkg.hasBuildRule()).toBe(false);
});
});

describe('hasBuildTypesRule()', () => {
it('returns true if there is a rule with the name "build_types"', () => {
const pkg = new BazelPackage('foo', {}, OWN_BAZEL_BUILD_FILE);
const pkg = new BazelPackage('foo', { name: 'foo' }, OWN_BAZEL_BUILD_FILE);
expect(pkg.hasBuildTypesRule()).toBe(true);
});

it('returns false if there is no rule with name "build_types"', () => {
const pkg = new BazelPackage('foo', {}, ``);
const pkg = new BazelPackage('foo', { name: 'foo' }, ``);
expect(pkg.hasBuildTypesRule()).toBe(false);
});

it('returns false if there is no BUILD.bazel file', () => {
const pkg = new BazelPackage('foo', { name: 'foo' });
expect(pkg.hasBuildTypesRule()).toBe(false);
});
});
68 changes: 64 additions & 4 deletions packages/kbn-bazel-packages/src/bazel_package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,49 @@
* Side Public License, v 1.
*/

import { inspect } from 'util';
import Path from 'path';
import Fsp from 'fs/promises';

import normalizePath from 'normalize-path';
import { REPO_ROOT } from '@kbn/utils';

const BUILD_RULE_NAME = /(^|\s)name\s*=\s*"build"/;
const BUILD_TYPES_RULE_NAME = /(^|\s)name\s*=\s*"build_types"/;

/**
* Simple parsed representation of a package.json file, validated
* by `assertParsedPackageJson()` and extensible as needed in the future
*/
export interface ParsedPackageJson {
/**
* The name of the package, usually `@kbn/`+something
*/
name: string;
/**
* All other fields in the package.json are typed as unknown as all we need at this time is "name"
*/
[key: string]: unknown;
}

function isObj(v: unknown): v is Record<string, unknown> {
return !!(typeof v === 'object' && v);
}

function assertParsedPackageJson(v: unknown): asserts v is ParsedPackageJson {
if (!isObj(v) || typeof v.name !== 'string') {
throw new Error('Expected parsed package.json to be an object with at least a "name" property');
}
}

/**
* Representation of a Bazel Package in the Kibana repository
*/
export class BazelPackage {
/**
* Create a BazelPackage object from a package directory. Reads some files from the package and returns
* a Promise for a BazelPackage instance
*/
static async fromDir(dir: string) {
let pkg;
try {
Expand All @@ -22,6 +57,8 @@ export class BazelPackage {
throw new Error(`unable to parse package.json in [${dir}]: ${error.message}`);
}

assertParsedPackageJson(pkg);

let buildBazelContent;
if (pkg.name !== '@kbn/pm') {
try {
Expand All @@ -31,20 +68,43 @@ export class BazelPackage {
}
}

return new BazelPackage(Path.relative(REPO_ROOT, dir), pkg, buildBazelContent);
return new BazelPackage(normalizePath(Path.relative(REPO_ROOT, dir)), pkg, buildBazelContent);
}

constructor(
public readonly repoRelativeDir: string,
public readonly pkg: any,
public readonly buildBazelContent?: string
/**
* Relative path from the root of the repository to the package directory
*/
public readonly normalizedRepoRelativeDir: string,
/**
* Parsed package.json file from the package
*/
public readonly pkg: ParsedPackageJson,
/**
* Content of the BUILD.bazel file
*/
private readonly buildBazelContent?: string
) {}

/**
* Returns true if the package includes a `:build` bazel rule
*/
hasBuildRule() {
return !!(this.buildBazelContent && BUILD_RULE_NAME.test(this.buildBazelContent));
}

/**
* Returns true if the package includes a `:build_types` bazel rule
*/
hasBuildTypesRule() {
return !!(this.buildBazelContent && BUILD_TYPES_RULE_NAME.test(this.buildBazelContent));
}

/**
* Custom inspect handler so that logging variables in scripts/generate doesn't
* print all the BUILD.bazel files
*/
[inspect.custom]() {
return `BazelPackage<${this.normalizedRepoRelativeDir}>`;
}
}

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion packages/kbn-bazel-packages/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@

export * from './discover_packages';
export type { BazelPackage } from './bazel_package';
export * from './generate_packages_build_bazel_file';
3 changes: 2 additions & 1 deletion packages/kbn-generate/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Render } from './lib/render';
import { ContextExtensions } from './generate_command';

import { PackageCommand } from './commands/package_command';
import { PackagesBuildManifestCommand } from './commands/packages_build_manifest_command';

/**
* Runs the generate CLI. Called by `node scripts/generate` and not intended for use outside of that script
Expand All @@ -26,6 +27,6 @@ export function runGenerateCli() {
};
},
},
[PackageCommand]
[PackageCommand, PackagesBuildManifestCommand]
).execute();
}
Loading

0 comments on commit 9871111

Please sign in to comment.