|
| 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 | +} |
0 commit comments