Skip to content

Commit

Permalink
add github-build-matrix command
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Sep 7, 2024
1 parent c93ac11 commit 5558e79
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .blueprint/cli/commands.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const defaultCommands = {
desc: 'Generate a test sample',
blueprint: '@jhipster/jhipster-dev',
},
'github-build-matrix': {
desc: 'Generate a matrix for GitHub Actions',
blueprint: '@jhipster/jhipster-dev',
},
'update-vscode': {
desc: 'Update generator-jhipster vscode files',
blueprint: '@jhipster/jhipster-dev',
Expand Down
14 changes: 14 additions & 0 deletions .blueprint/github-build-matrix/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { JHipsterCommandDefinition } from '../../generators/index.js';

export default {
configs: {
workflow: {
description: 'Workflow',
argument: {
type: String,
},
scope: 'generator',
choices: ['testcontainers'],
},
},
} as const satisfies JHipsterCommandDefinition;
22 changes: 22 additions & 0 deletions .blueprint/github-build-matrix/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import BaseGenerator from '../../generators/base/index.js';
import { setGithubTaskOutput } from '../../testing/index.js';
import { convertToGitHubMatrix } from './support/github-ci-matrix.js';
import { testcontainersMatrix } from './support/testcontainers.js';

export default class extends BaseGenerator {
workflow;

constructor(args, opts, features) {
super(args, opts, { ...features, jhipsterBootstrap: true });
}

get [BaseGenerator.WRITING]() {
return this.asWritingTaskGroup({
async buildMatrix() {
if (this.workflow === 'testcontainers') {
setGithubTaskOutput('matrix', JSON.stringify(convertToGitHubMatrix(testcontainersMatrix), null, 2));
}
},
});
}
}
2 changes: 2 additions & 0 deletions .blueprint/github-build-matrix/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default } from './generator.js';
export { default as command } from './command.js';
13 changes: 13 additions & 0 deletions .blueprint/github-build-matrix/support/cli-args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { kebabCase } from 'lodash-es';

export const convertToCliArgs = (opts: Record<string, any>): string => {
return Object.entries(opts)
.map(([key, value]) => {
key = kebabCase(key);
if (typeof value === 'boolean') {
return `--${value ? '' : 'no-'}${key}`;
}
return `--${key} ${value}`;
})
.join(' ');
};
38 changes: 38 additions & 0 deletions .blueprint/github-build-matrix/support/github-ci-matrix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { RECOMMENDED_JAVA_VERSION, RECOMMENDED_NODE_VERSION } from '../../../generators/index.js';

type GitHubMatrix = {
os: string;
'node-version': string;
'java-version': string;
'default-environment': string;
'job-name': string;
args: string;
};

type GitHubMatrixOutput = {
includes: GitHubMatrix[];
};

export const defaultEnvironment = {
os: 'ubuntu-latest',
'node-version': RECOMMENDED_NODE_VERSION,
'java-version': RECOMMENDED_JAVA_VERSION,
'default-environment': 'prod',
};

export const defaultEnvironmentMatrix = {
os: ['ubuntu-latest'],
'node-version': [RECOMMENDED_NODE_VERSION],
'java-version': [RECOMMENDED_JAVA_VERSION],
'default-environment': ['prod'],
};

export const convertToGitHubMatrix = (matrix: Record<string, any>): GitHubMatrixOutput => {
return {
includes: Object.entries(matrix).map(([key, value]) => ({
'job-name': key,
...defaultEnvironment,
...value,
})),
};
};
21 changes: 21 additions & 0 deletions .blueprint/github-build-matrix/support/testcontainers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { extendMatrix, fromMatrix } from '../../../testing/index.js';
import { convertToCliArgs } from './cli-args.js';

export const testcontainersMatrix = Object.fromEntries(
Object.entries(
extendMatrix(
{
...fromMatrix({ reactive: [false, true], databaseType: ['cassandra', 'mongodb', 'neo4j'] }),
...extendMatrix(fromMatrix({ reactive: [false, true], prodDatabaseType: ['postgresql', 'mysql', 'mariadb'] }), {
cacheProvider: ['no', 'redis', 'memcached'],
}),
},
{
searchEngine: ['no', 'elasticsearch'],
auth: ['jwt', 'oauth2'],
serviceDiscovery: ['no', 'eureka', 'consul'],
messageBroker: ['no', 'kafka', 'pulsar'],
},
),
).map(([key, value]) => [key, { args: convertToCliArgs(value) }]),
);

0 comments on commit 5558e79

Please sign in to comment.