From 907649780419d5876edb72cc3eb1e7618e606b9a Mon Sep 17 00:00:00 2001 From: Matt Mackay Date: Tue, 28 Apr 2020 19:03:18 -0400 Subject: [PATCH] feat: add simple container_layer generation (#38) --- CHANGELOG.md | 7 ++-- README.md | 1 + src/buildozer.ts | 5 +-- src/flags.ts | 14 +++++++- .../containers/container-layer.generator.ts | 32 +++++++++++++++++++ src/main.ts | 3 ++ src/workspace.ts | 12 +++++-- 7 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 src/generators/containers/container-layer.generator.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 345feb7..8deaa83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,5 +3,8 @@ #### 0.0.0-PLACEHOLDER * **feat**: Support generating `nodejs_binary` target. The `data` attr will be set to `entry_points` generating rule [#36](https://github.com/Evertz/bzlgen/pull/36) -* **feat**: Use the buildozer API via the NodeJS bindings rather than invoking directly in a shell on a text file [#35](https://github.com/Evertz/bzlgen/pull/35) -* **fix**: Handle NodeJS builtins, adding `@types/node` as a dep +* **feat**: Use the buildozer API via the NodeJS bindings rather than invoking directly in a shell on a text file [#34](https://github.com/Evertz/bzlgen/pull/34) +* **feat**: Support generating `ts_library` targets for a single file [#35](https://github.com/Evertz/bzlgen/pull/35) +* **feat**: Support simple generation of `container_layer` targets, adding files at `path` to the `files` attribute [#38](https://github.com/Evertz/bzlgen/pull/38) +* **feat**: Add `--pattern` flag. When `path` represents a directory, `pattern` is used as a glob for filtering files [#38](https://github.com/Evertz/bzlgen/pull/38) +* **fix**: Handle NodeJS builtins, adding `@types/node` as a dep [#37](https://github.com/Evertz/bzlgen/pull/37) diff --git a/README.md b/README.md index 2e7e6c3..f0813bd 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ The generator can create rules for the following and can be extended to provide * ng_module * ts_library * nodejs_binary +* container_layer The generator is _somewhat_ flexible in the source structure, but does make a number of assumptions in certain cases. It will try and 'best guess' labels from other packages. It's currently not expected to generate a 100% correct and working build file, diff --git a/src/buildozer.ts b/src/buildozer.ts index 4e9f622..11acc28 100644 --- a/src/buildozer.ts +++ b/src/buildozer.ts @@ -7,6 +7,7 @@ const DEFAULT_LOAD_SITES = new Map([ ['ts_library', '@npm_bazel_typescript//:index.bzl'], ['ng_module', '@npm_angular_bazel//:index.bzl'], ['nodejs_binary', '@build_bazel_rules_nodejs//:index.bzl'], + ['container_layer', '@io_bazel_rules_docker//container:container.bzl'], ]); /** @@ -203,11 +204,11 @@ export class Buildozer { this.addCommand(`new_load ${from} ${symbols}`, label.withTarget(Buildozer.PKG)); } - addAttr(attr: string, value: string[], label: Label) { + addAttr(attr: string, value: Array, label: Label) { this.addCommand(`add ${attr} ${value.join(' ')}`, label); } - setAttr(attr: string, value: string, label: Label) { + setAttr(attr: string, value: string | Label, label: Label) { this.addCommand(`set ${attr} "${value}"`, label); } diff --git a/src/flags.ts b/src/flags.ts index 3c68b19..a39d3f6 100644 --- a/src/flags.ts +++ b/src/flags.ts @@ -12,7 +12,8 @@ export enum GeneratorType { NG_BUNDLE = 'ng_bundle', SASS = 'sass', TS = 'ts', - JS_BINARY = 'js_binary' + JS_BINARY = 'js_binary', + CONTAINER_LAYER = 'container_layer' } function coerceMappingFlag(loads: string[]): Map { @@ -40,6 +41,11 @@ interface CommonFlags { */ path: string; + /** + * A glob pattern that is applied to the files at path when path represents a directory + */ + pattern: string; + /** * Remove the existing build file before creating the new one */ @@ -262,6 +268,12 @@ export const setupAndParseArgs = (argv: string[], ignorerc = false, strip = 2): default: false, group: 'Configuration' }) + .option('pattern', { + type: 'string', + description: 'A glob pattern that is applied to the files at path when path represents a directory', + default: false, + group: 'Configuration' + }) .option('base_dir', { type: 'string', description: 'Base dir that is prefixed to \'path\' to form an absolute path', diff --git a/src/generators/containers/container-layer.generator.ts b/src/generators/containers/container-layer.generator.ts new file mode 100644 index 0000000..d2bcc95 --- /dev/null +++ b/src/generators/containers/container-layer.generator.ts @@ -0,0 +1,32 @@ +import { GeneratorType } from '../../flags'; +import { BuildFileGenerator } from '../generator'; + +export class ContainerLayerGenerator extends BuildFileGenerator { + + async generate(): Promise { + const label = this.workspace.getLabelForPath().withTarget('layer'); + + this.buildozer.loadRule('container_layer', label); + this.buildozer.newRule('container_layer', label); + + const files = []; + if (this.workspace.isDirectory()) { + let labels = this.workspace.readDirectory() + .map(file => this.workspace.getFileLabel(file)); + + files.push(...labels); + } else { + files.push(this.workspace.getFileLabel(this.flags.path)); + } + + this.buildozer.addAttr('files', files, label); + } + + getGeneratorType(): GeneratorType { + return GeneratorType.CONTAINER_LAYER; + } + + supportsDirectories(): boolean { + return true; + } +} diff --git a/src/main.ts b/src/main.ts index f533961..01bc96e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,6 +9,7 @@ import { TsGenerator } from './generators/ts/ts.generator'; import { debug, fatal, lb, log, warn } from './logger'; import { snapshot, wrap, TRACER_PATH } from './tracing'; import { Workspace } from './workspace'; +import { ContainerLayerGenerator } from './generators/containers/container-layer.generator'; function printFlags(flags: Flags) { log('Canonicalized Flags:'); @@ -35,6 +36,8 @@ function getGenerator(type: GeneratorType, workspace: Workspace): BuildFileGener return new NgGenerator(workspace); case GeneratorType.JS_BINARY: return new NodejsBinaryGenerator(workspace); + case GeneratorType.CONTAINER_LAYER: + return new ContainerLayerGenerator(workspace); default: fatal(`No generator found for type ${type}`); } diff --git a/src/workspace.ts b/src/workspace.ts index b53c48d..3e36596 100644 --- a/src/workspace.ts +++ b/src/workspace.ts @@ -10,6 +10,7 @@ import { Buildozer } from './buildozer'; import { Flags } from './flags'; import { Label } from './label'; import { debug, fatal, isDebugEnabled, lb, log, warn } from './logger'; +import { match } from 'minimatch'; export class Workspace { private static readonly QUERY_FLAGS = `--output label --order_output=no`; @@ -68,8 +69,15 @@ export class Workspace { readDirectory(): string[] { if (!this.isDirectory()) { return []; } - return readdirSync(this.getAbsolutePath()) - .map(file => this.resolveRelativeToWorkspace(file)); + const files = readdirSync(this.getAbsolutePath()) + .map(file => this.resolveRelativeToWorkspace(file)) + .filter(file => !file.endsWith(this.flags.build_file_name)); + + if (this.flags.pattern) { + return match(files, join(this.flags.path, this.flags.pattern)); + } + + return files; } /**