Skip to content

Commit 7d665e1

Browse files
committed
feat: plugins - custom build
1 parent a66d26a commit 7d665e1

File tree

4 files changed

+115
-6
lines changed

4 files changed

+115
-6
lines changed

package-lock.json

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
"@types/tmp": "^0.2.6",
4040
"@types/which": "^3.0.4",
4141
"tmp": "^0.2.5",
42-
"which": "^5.0.0"
42+
"which": "^5.0.0",
43+
"yaml": "^2.8.1"
4344
},
4445
"devDependencies": {
4546
"@typescript-eslint/eslint-plugin": "^8.46.2",

src/plugins.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import * as core from "@actions/core"
2+
import { exec, ExecOptionsWithStringEncoding } from "child_process"
3+
import * as fs from "fs"
4+
import * as path from "path"
5+
import { promisify } from "util"
6+
import YAML from "yaml"
7+
8+
const execShellCommand = promisify(exec)
9+
10+
type ExecRes = {
11+
stdout: string
12+
stderr: string
13+
}
14+
15+
const printOutput = (res: ExecRes): void => {
16+
if (res.stdout) {
17+
core.info(res.stdout)
18+
}
19+
if (res.stderr) {
20+
core.info(res.stderr)
21+
}
22+
}
23+
24+
export async function install(binPath: string): Promise<string> {
25+
let rootDir = core.getInput(`working-directory`)
26+
27+
if (rootDir) {
28+
if (!fs.existsSync(rootDir) || !fs.lstatSync(rootDir).isDirectory()) {
29+
throw new Error(`working-directory (${rootDir}) was not a path`)
30+
}
31+
32+
rootDir = path.resolve(rootDir)
33+
} else {
34+
rootDir = process.cwd()
35+
}
36+
37+
const configFile = [".custom-gcl.yml", ".custom-gcl.yaml", ".custom-gcl.json"]
38+
.map((v) => path.join(rootDir, v))
39+
.find((filePath) => fs.existsSync(filePath))
40+
41+
if (!configFile || configFile === "") {
42+
return ""
43+
}
44+
45+
core.info(`Found configuration for the plugin module system : ${configFile}`)
46+
47+
core.info(`Building and installing custom golangci-lint binary...`)
48+
49+
const startedAt = Date.now()
50+
51+
const config = YAML.parse(fs.readFileSync(configFile, "utf-8"))
52+
53+
const v: string = core.getInput(`version`)
54+
if (v !== "" && config.version !== v) {
55+
core.warning(
56+
`The golangci-lint version (${config.version}) defined inside in ${configFile} does not match the version defined in the action (${v})`
57+
)
58+
}
59+
60+
if (!config.destination) {
61+
config.destination = "."
62+
}
63+
if (!config.name) {
64+
config.name = "custom-gcl"
65+
}
66+
67+
if (!fs.existsSync(config.destination)) {
68+
core.info(`Creating destination directory: ${config.destination}`)
69+
fs.mkdirSync(config.destination, { recursive: true })
70+
}
71+
72+
const cmd = `${binPath} custom`
73+
74+
core.info(`Running [${cmd}] in [${rootDir}] ...`)
75+
76+
try {
77+
const options: ExecOptionsWithStringEncoding = {
78+
cwd: rootDir,
79+
}
80+
81+
const res = await execShellCommand(cmd, options)
82+
printOutput(res)
83+
84+
core.info(`Built custom golangci-lint binary in ${Date.now() - startedAt}ms`)
85+
86+
return path.join(rootDir, config.destination, config.name)
87+
} catch (exc) {
88+
throw new Error(`Failed to build custom golangci-lint binary: ${exc.message}`)
89+
}
90+
}

src/run.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { promisify } from "util"
88
import { restoreCache, saveCache } from "./cache"
99
import { install } from "./install"
1010
import { fetchPatch, isOnlyNewIssues } from "./patch"
11+
import * as plugins from "./plugins"
1112

1213
const execShellCommand = promisify(exec)
1314

@@ -22,7 +23,13 @@ async function prepareEnv(installOnly: boolean): Promise<Env> {
2223
// Prepare cache, lint and go in parallel.
2324
await restoreCache()
2425

25-
const binPath = await install()
26+
let binPath = await install()
27+
28+
// Build custom golangci-lint if needed.
29+
const customBinPath = await plugins.install(binPath)
30+
if (customBinPath !== "") {
31+
binPath = customBinPath
32+
}
2633

2734
if (installOnly) {
2835
return { binPath, patchPath: `` }
@@ -203,9 +210,7 @@ export async function run(): Promise<void> {
203210
try {
204211
const installOnly = core.getBooleanInput(`install-only`, { required: true })
205212

206-
const { binPath, patchPath } = await core.group(`prepare environment`, () => {
207-
return prepareEnv(installOnly)
208-
})
213+
const { binPath, patchPath } = await core.group(`prepare environment`, () => prepareEnv(installOnly))
209214

210215
core.addPath(path.dirname(binPath))
211216

0 commit comments

Comments
 (0)