Skip to content

Commit

Permalink
Merge pull request #18 from pipelinit/feat/include-version
Browse files Browse the repository at this point in the history
Include a header in generated  files
  • Loading branch information
oesgalha authored Aug 30, 2021
2 parents 831863d + ed48b63 commit f2f73ad
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 10 deletions.
48 changes: 48 additions & 0 deletions plugins/platforms/github/mod.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { context } from "../../../src/plugin/mod.ts";
import { assertEquals, deepMerge } from "../../../deps.ts";
import { RenderedTemplate } from "../../../src/platform/plugin.ts";

import { github } from "./mod.ts";

const FAKE_VERSION = "v10.10.10";

// FIXME
// This doesn't work:
// deepMerge(context, { version: FAKE_VERSION });
// Why?
const fakeContext = deepMerge(context, {});
fakeContext.version = FAKE_VERSION;

const fakeYaml = `
name: Runner check
on: pull_request
jobs:
stage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: ls
`;

const fakeYamlWithHeader = `# Generated with pipelinit ${FAKE_VERSION}
# https://pipelinit.com/
${fakeYaml}`;

Deno.test("Plugins > GitHub - prepends a header to generated files", async () => {
async function* templatesFixture(): AsyncIterableIterator<RenderedTemplate> {
yield {
name: "pipelinit.stage",
content: fakeYaml,
};
return;
}
const result = await github(fakeContext, templatesFixture());
assertEquals(result.length, 1);
assertEquals(
result,
[{
path: ".github/workflows/pipelinit.stage.yaml",
content: fakeYamlWithHeader,
}],
);
});
19 changes: 12 additions & 7 deletions plugins/platforms/github/mod.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { ensureFile, log, PlatformWriterFn } from "../deps.ts";
import { PlatformWriterFn } from "../deps.ts";

export const github: PlatformWriterFn = async (templates) => {
const logger = log.getLogger("main");
export const github: PlatformWriterFn = async (context, templates) => {
const header = `
# Generated with pipelinit ${context.version}
# https://pipelinit.com/
`.trimStart();
const configurationFiles = [];
for await (const template of templates) {
const { name, content } = template;
const filename = `.github/workflows/${name}.yaml`;
logger.info(`Writing ${filename}`);
await ensureFile(filename);
await Deno.writeTextFile(filename, content);
configurationFiles.push({
path: `.github/workflows/${name}.yaml`,
content: `${header}${content}`,
});
}
return configurationFiles;
};
11 changes: 10 additions & 1 deletion src/cli/commands/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@ import { renderTemplates } from "../../template/mod.ts";
import { prelude } from "../prelude/mod.ts";
import { GlobalOptions } from "../types.ts";
import { outputErrors } from "../../plugin/errors.ts";
import { context } from "../../plugin/mod.ts";
import { ensureFile } from "../../../deps.ts";

type DefaultOptions = GlobalOptions;

export default async function (opts: DefaultOptions): Promise<void> {
await prelude(opts);
const logger = context.getLogger("main");
const detected = await introspect();
const platform = "github";
await platformWriters[platform](
const files = await platformWriters[platform](
context,
renderTemplates(platform, detected),
);
for (const { path, content } of files) {
logger.info(`Writing ${path}`);
await ensureFile(path);
await Deno.writeTextFile(path, content);
}
outputErrors();
}
10 changes: 9 additions & 1 deletion src/platform/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { Context } from "../plugin/mod.ts";

export interface RenderedTemplate {
name: string;
content: string;
}

export interface CiConfigurationFile {
path: string;
content: string;
}

export type PlatformWriterFn = (
context: Context,
templates: AsyncIterableIterator<RenderedTemplate>,
) => Promise<void>;
) => Promise<Array<CiConfigurationFile>>;
4 changes: 3 additions & 1 deletion src/plugin/mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { log, semver } from "../../deps.ts";
import { each, includes, readJSON, readLines, readToml } from "./files.ts";
import { errors } from "./errors.ts";
import { log, semver } from "../../deps.ts";
import { VERSION } from "../version.ts";

export const context = {
getLogger: log.getLogger,
Expand All @@ -16,6 +17,7 @@ export const context = {
},
semver,
suggestDefault: true,
version: VERSION,
};

export type Context = typeof context;
Expand Down

0 comments on commit f2f73ad

Please sign in to comment.