Skip to content

Commit

Permalink
feat: add simple container_layer generation (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattem authored Apr 28, 2020
1 parent 183baf5 commit 9076497
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 7 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/buildozer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const DEFAULT_LOAD_SITES = new Map<string, string>([
['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'],
]);

/**
Expand Down Expand Up @@ -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<string | Label>, 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);
}

Expand Down
14 changes: 13 additions & 1 deletion src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> {
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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',
Expand Down
32 changes: 32 additions & 0 deletions src/generators/containers/container-layer.generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { GeneratorType } from '../../flags';
import { BuildFileGenerator } from '../generator';

export class ContainerLayerGenerator extends BuildFileGenerator {

async generate(): Promise<void> {
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;
}
}
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:');
Expand All @@ -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}`);
}
Expand Down
12 changes: 10 additions & 2 deletions src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 9076497

Please sign in to comment.