Skip to content

Commit b5ec5e3

Browse files
feat(blazebuild): buildSrc plugins
1 parent a969fde commit b5ec5e3

File tree

4 files changed

+89
-24
lines changed

4 files changed

+89
-24
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type Blaze from "../core/Blaze";
2+
import type AbstractTask from "../tasks/AbstractTask";
3+
import type { Awaitable } from "../types/utils";
4+
5+
abstract class BlazePlugin {
6+
public constructor(protected readonly blaze: Blaze) {}
7+
public abstract boot(): Awaitable<void>;
8+
public tasks?(): Awaitable<Iterable<new (blaze: Blaze) => AbstractTask>>;
9+
}
10+
11+
export default BlazePlugin;

blazebuild/src/main/typescript/script/BuildScriptManager.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import { lstat } from "fs/promises";
1+
import { existsSync } from "fs";
2+
import { lstat, readdir } from "fs/promises";
23
import path from "path";
4+
import type Blaze from "../core/Blaze";
35
import Manager from "../core/Manager";
46
import MissingBuildScriptError from "../errors/MissingBuildScriptError";
5-
import { println } from "../io/IO";
7+
import IO, { println } from "../io/IO";
8+
import type BlazePlugin from "../plugins/BlazePlugin";
69

710
export const BUILD_SCRIPT_PATH = "build.blaze.ts";
811

@@ -31,6 +34,7 @@ class BuildScriptManager extends Manager {
3134
}
3235

3336
await this.setupEnvironment(global as Record<string, unknown>);
37+
await this.loadBuildSrc();
3438
await import(buildScriptPath);
3539
}
3640

@@ -40,6 +44,43 @@ class BuildScriptManager extends Manager {
4044
global.blaze = this.blaze;
4145
global.println = println;
4246
}
47+
48+
private async loadBuildSrc() {
49+
const buildSrcPath = path.resolve(process.cwd(), "build_src/src/main/typescript");
50+
51+
if (!existsSync(buildSrcPath)) {
52+
return;
53+
}
54+
55+
const files = await readdir(buildSrcPath);
56+
let pluginPath: string | undefined;
57+
58+
for (const file of files) {
59+
const resolved = path.resolve(buildSrcPath, file);
60+
61+
if (file.endsWith("Plugin.ts") && (await lstat(resolved)).isFile()) {
62+
pluginPath = resolved;
63+
break;
64+
}
65+
}
66+
67+
if (!pluginPath) {
68+
IO.fatal("No plugin found in build_src!");
69+
}
70+
71+
// HACK: This is a dummy implementation of plugin loading.
72+
const { default: Plugin }: { default: new (blaze: Blaze) => BlazePlugin } = await import(
73+
pluginPath
74+
);
75+
const plugin = new Plugin(this.blaze);
76+
await plugin.boot();
77+
78+
if (plugin.tasks) {
79+
for (const task of await plugin.tasks()) {
80+
this.blaze.taskManager.register(task);
81+
}
82+
}
83+
}
4384
}
4485

4586
export default BuildScriptManager;

build.blaze.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,8 @@
1-
import CleanTask from "@buildSrc/tasks/CleanTask";
2-
import CompileTask from "@buildSrc/tasks/CompileTask";
3-
import CompileTypeScriptTask from "@buildSrc/tasks/CompileTypeScriptTask";
4-
import DependenciesTask from "@buildSrc/tasks/DependenciesTask";
5-
import LintTask from "@buildSrc/tasks/LintTask";
6-
import ProcessCoverageReportsTask from "@buildSrc/tasks/ProcessCoverageReportsTask";
7-
import RunTask from "@buildSrc/tasks/RunTask";
8-
import TestTask from "@buildSrc/tasks/TestTask";
1+
import "blazebuild";
92

103
project.name = "sudobot";
114
project.version = "1.0.0";
125
project.description = "Sudobot. A Discord moderation bot.";
136
project.structure.sourcesRootDirectory = "src";
147
project.structure.sourceModules = ["main", "framework"];
158
project.structure.testsDirectory = "test";
16-
17-
tasks.register(DependenciesTask);
18-
tasks.register(CleanTask);
19-
tasks.register(CompileTypeScriptTask);
20-
tasks.register(CompileTask);
21-
tasks.register(TestTask);
22-
tasks.register(LintTask);
23-
tasks.register(ProcessCoverageReportsTask);
24-
tasks.register(RunTask);
25-
26-
tasks.named("build", {
27-
dependsOn: ["compile", "lint", "test"],
28-
outputs: [project.structure.buildOutputDirectory],
29-
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Awaitable } from "blazebuild";
2+
import BlazePlugin from "blazebuild/src/main/typescript/plugins/BlazePlugin";
3+
import CleanTask from "./tasks/CleanTask";
4+
import CompileTask from "./tasks/CompileTask";
5+
import CompileTypeScriptTask from "./tasks/CompileTypeScriptTask";
6+
import DependenciesTask from "./tasks/DependenciesTask";
7+
import LintTask from "./tasks/LintTask";
8+
import ProcessCoverageReportsTask from "./tasks/ProcessCoverageReportsTask";
9+
import RunTask from "./tasks/RunTask";
10+
import TestTask from "./tasks/TestTask";
11+
12+
class BuildPlugin extends BlazePlugin {
13+
public override boot(): Awaitable<void> {
14+
this.blaze.taskManager.named("build", {
15+
dependsOn: ["compile", "lint", "test"],
16+
outputs: [this.blaze.projectManager.properties.structure?.buildOutputDirectory ?? ""]
17+
});
18+
}
19+
20+
public override tasks() {
21+
return [
22+
CleanTask,
23+
CompileTask,
24+
CompileTypeScriptTask,
25+
DependenciesTask,
26+
LintTask,
27+
ProcessCoverageReportsTask,
28+
RunTask,
29+
TestTask
30+
];
31+
}
32+
}
33+
34+
export default BuildPlugin;

0 commit comments

Comments
 (0)