Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new build-time version injection method with git describe #427

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ jobs:
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@d70bba72b1f3fd22344832f00baa16ece964efeb # v3.3.0
with:
Expand All @@ -57,10 +54,11 @@ jobs:
- name: Build and push Docker image
uses: docker/build-push-action@2cdde995de11925a030ce8070c3d77a52ffcf1c0 # v5.3.0
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
platforms: linux/amd64,linux/arm64
tags: ${{ steps.metadata.outputs.tags }}
labels: ${{ steps.metadata.outputs.labels }}
build-args: |
BUILDKIT_CONTEXT_KEEP_GIT_DIR=1
cache-from: type=gha
cache-to: type=gha,mode=max
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ ARG BUILDPLATFORM
RUN --mount=type=cache,id=npm-$BUILDPLATFORM,target=/.npm \
npm ci
COPY --link ./src/ ./src/
RUN npm run build
COPY --link ./esbuild-plugin-metadata-injector/ ./esbuild-plugin-metadata-injector/
COPY --link ./tsconfig.json ./
RUN --mount=type=bind,source=./.git/,target=./.git/ npm run build

FROM --platform=$BUILDPLATFORM node:20.14.0-bookworm@sha256:ab71b9da5ba19445dc5bb76bf99c218941db2c4d70ff4de4e0d9ec90920bfe3f AS dictionary
WORKDIR /app
Expand Down
6 changes: 4 additions & 2 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// @ts-check

import { build } from "esbuild";
import { esbuildPluginVersionInjector } from "esbuild-plugin-version-injector";
import esbuildPluginMetadataInjector from "./esbuild-plugin-metadata-injector/plugin.js";
import packageJson from "./package.json" with { type: "json" };

await build({
Expand All @@ -9,6 +11,6 @@ await build({
platform: "node",
format: "esm",
external: Object.keys(packageJson.dependencies),
plugins: [esbuildPluginVersionInjector()],
plugins: [esbuildPluginMetadataInjector()],
minify: true,
});
2 changes: 2 additions & 0 deletions esbuild-plugin-metadata-injector/placeholder.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const PACKAGE_VERSION: string;
export const GIT_DESCRIBE: string;
7 changes: 7 additions & 0 deletions esbuild-plugin-metadata-injector/placeholder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @ts-check

import gitDescribe from "git-describe";
import packageJSON from "../package.json" with { type: "json" };

export const PACKAGE_VERSION = packageJSON.version;
export const GIT_DESCRIBE = gitDescribe.gitDescribeSync().raw;
Mogyuchi marked this conversation as resolved.
Show resolved Hide resolved
29 changes: 29 additions & 0 deletions esbuild-plugin-metadata-injector/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @ts-check

import { fileURLToPath } from "node:url";
import * as placeholder from "./placeholder.js";

/**
* @param {string} path
* @returns {RegExp}
*/
function exact(path) {
return new RegExp(`^${path.replace(/[/.]/g, "\\$&")}$`);
}

const target = fileURLToPath(import.meta.resolve("./placeholder.js"));

/**
* @returns {import("esbuild").Plugin}
*/
export default function esbuildPluginMetadataInjector() {
return {
name: "esbuild-plugin-metadata-injector",
setup(build) {
build.onLoad({ filter: exact(target) }, () => ({
loader: "json",
contents: JSON.stringify(placeholder),
}));
},
};
}
59 changes: 33 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"@types/node": "20.14.2",
"dotenv": "16.4.5",
"esbuild": "0.21.5",
"esbuild-plugin-version-injector": "1.2.1",
"eslint": "8.57.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-import": "2.29.1",
"git-describe": "4.1.1",
"globals": "15.4.0",
"husky": "9.0.11",
"lint-staged": "15.2.5",
Expand Down
7 changes: 5 additions & 2 deletions src/commands/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import type {
ChatInputCommandInteraction,
RESTPostAPIChatInputApplicationCommandsJSONBody,
} from "discord.js";
import { OM_VERSION } from "../version";
import {
PACKAGE_VERSION,
GIT_DESCRIBE,
} from "esbuild-plugin-metadata-injector/placeholder";

export const definition = {
name: "version",
Expand All @@ -19,7 +22,7 @@ export async function handler(
) {
await interaction.reply({
content: `\`\`\`
discordjs-japan/om: ${OM_VERSION}
discordjs-japan/om: ${PACKAGE_VERSION} (${GIT_DESCRIBE})
discordjs-japan/om-syrinx: ${OM_SYRINX_VERSION}
jpreprocess/jpreprocess: ${JPREPROCESS_VERSION}
jpreprocess/jbonsai: ${JBONSAI_VERSION}
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ActivityType, Client, Events, GatewayIntentBits } from "discord.js";
import { PACKAGE_VERSION } from "esbuild-plugin-metadata-injector/placeholder";
import * as join from "./commands/join";
import * as leave from "./commands/leave";
import * as skip from "./commands/skip";
import * as version from "./commands/version";
import { ReplyableError } from "./error";
import Pipeline from "./pipeline";
import { OM_VERSION } from "./version";

process.title = "discordjs-japan/om";

Expand Down Expand Up @@ -64,7 +64,7 @@ client.once(Events.ClientReady, async (client) => {
]);
client.user.setActivity({
type: ActivityType.Custom,
name: `v${OM_VERSION}`,
name: `v${PACKAGE_VERSION}`,
});
});

Expand Down
1 change: 0 additions & 1 deletion src/version.ts

This file was deleted.

7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"verbatimModuleSyntax": true
"verbatimModuleSyntax": true,
"paths": {
"esbuild-plugin-metadata-injector/placeholder": [
"./esbuild-plugin-metadata-injector/placeholder"
]
}
},
"include": ["src/**/*"]
}
Loading