From 0c1be0710bcdb4de7f4c109914ac64b30e9cca95 Mon Sep 17 00:00:00 2001 From: Joel Rudsberg Date: Mon, 6 Jan 2025 10:31:43 +0200 Subject: [PATCH] Re-use 'setNativeImageOption' --- src/features/reports.ts | 50 ++++------------------------------------- src/features/sbom.ts | 9 ++------ src/utils.ts | 46 +++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 53 deletions(-) diff --git a/src/features/reports.ts b/src/features/reports.ts index eca3898..3793b31 100644 --- a/src/features/reports.ts +++ b/src/features/reports.ts @@ -3,17 +3,17 @@ import * as core from '@actions/core' import * as fs from 'fs' import * as github from '@actions/github' import * as semver from 'semver' -import {join} from 'path' -import {tmpdir} from 'os' import { createPRComment, findExistingPRCommentId, isPREvent, toSemVer, - updatePRComment + updatePRComment, + tmpfile, + setNativeImageOption } from '../utils' -const BUILD_OUTPUT_JSON_PATH = join(tmpdir(), 'native-image-build-output.json') +const BUILD_OUTPUT_JSON_PATH = tmpfile('native-image-build-output.json') const BYTES_TO_KiB = 1024 const BYTES_TO_MiB = 1024 * 1024 const BYTES_TO_GiB = 1024 * 1024 * 1024 @@ -22,11 +22,6 @@ const DOCS_BASE = const INPUT_NI_JOB_REPORTS = 'native-image-job-reports' const INPUT_NI_PR_REPORTS = 'native-image-pr-reports' const INPUT_NI_PR_REPORTS_UPDATE = 'native-image-pr-reports-update-existing' -const NATIVE_IMAGE_CONFIG_FILE = join( - tmpdir(), - 'native-image-options.properties' -) -const NATIVE_IMAGE_CONFIG_FILE_ENV = 'NATIVE_IMAGE_CONFIG_FILE' const PR_COMMENT_TITLE = '## GraalVM Native Image Build Report' interface AnalysisResult { @@ -168,43 +163,6 @@ function arePRReportsUpdateEnabled(): boolean { return isPREvent() && core.getInput(INPUT_NI_PR_REPORTS_UPDATE) === 'true' } -function setNativeImageOption( - javaVersionOrDev: string, - optionValue: string -): void { - const coercedJavaVersionOrDev = semver.coerce(javaVersionOrDev) - if ( - (coercedJavaVersionOrDev && - semver.gte(coercedJavaVersionOrDev, '22.0.0')) || - javaVersionOrDev === c.VERSION_DEV || - javaVersionOrDev.endsWith('-ea') - ) { - /* NATIVE_IMAGE_OPTIONS was introduced in GraalVM for JDK 22 (so were EA builds). */ - let newOptionValue = optionValue - const existingOptions = process.env[c.NATIVE_IMAGE_OPTIONS_ENV] - if (existingOptions) { - newOptionValue = `${existingOptions} ${newOptionValue}` - } - core.exportVariable(c.NATIVE_IMAGE_OPTIONS_ENV, newOptionValue) - } else { - const optionsFile = getNativeImageOptionsFile() - if (fs.existsSync(optionsFile)) { - fs.appendFileSync(optionsFile, ` ${optionValue}`) - } else { - fs.writeFileSync(optionsFile, `NativeImageArgs = ${optionValue}`) - } - } -} - -function getNativeImageOptionsFile(): string { - let optionsFile = process.env[NATIVE_IMAGE_CONFIG_FILE_ENV] - if (optionsFile === undefined) { - optionsFile = NATIVE_IMAGE_CONFIG_FILE - core.exportVariable(NATIVE_IMAGE_CONFIG_FILE_ENV, optionsFile) - } - return optionsFile -} - function createReport(data: BuildOutput): string { const context = github.context const info = data.general_info diff --git a/src/features/sbom.ts b/src/features/sbom.ts index 959cf37..dc6a566 100644 --- a/src/features/sbom.ts +++ b/src/features/sbom.ts @@ -5,6 +5,7 @@ import * as github from '@actions/github' import * as glob from '@actions/glob' import {basename} from 'path' import * as semver from 'semver' +import {setNativeImageOption} from '../utils' const INPUT_NI_SBOM = 'native-image-enable-sbom' const SBOM_FILE_SUFFIX = '.sbom.json' @@ -73,13 +74,7 @@ export function setUpSBOMSupport( } validateJavaVersionAndDistribution(javaVersionOrDev, distribution) - - let options = process.env[c.NATIVE_IMAGE_OPTIONS_ENV] || '' - if (options.length > 0) { - options += ' ' - } - options += '--enable-sbom=export' - core.exportVariable(c.NATIVE_IMAGE_OPTIONS_ENV, options) + setNativeImageOption(javaVersionOrDev, '--enable-sbom=export') core.info('Enabled SBOM generation for Native Image build') } diff --git a/src/utils.ts b/src/utils.ts index 655d1d9..cc652dc 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -4,11 +4,13 @@ import * as github from '@actions/github' import * as httpClient from '@actions/http-client' import * as semver from 'semver' import * as tc from '@actions/tool-cache' +import * as fs from 'fs' import {ExecOptions, exec as e} from '@actions/exec' import {readFileSync, readdirSync} from 'fs' import {Octokit} from '@octokit/core' import {createHash} from 'crypto' import {join} from 'path' +import {tmpdir} from 'os' // Set up Octokit for github.com only and in the same way as @actions/github (see https://git.io/Jy9YP) const baseUrl = 'https://api.github.com' @@ -247,3 +249,47 @@ export async function createPRComment(content: string): Promise { ) } } + +export function tmpfile(fileName: string) { + return join(tmpdir(), fileName) +} + +export function setNativeImageOption( + javaVersionOrDev: string, + optionValue: string +): void { + const coercedJavaVersionOrDev = semver.coerce(javaVersionOrDev) + if ( + (coercedJavaVersionOrDev && + semver.gte(coercedJavaVersionOrDev, '22.0.0')) || + javaVersionOrDev === c.VERSION_DEV || + javaVersionOrDev.endsWith('-ea') + ) { + /* NATIVE_IMAGE_OPTIONS was introduced in GraalVM for JDK 22 (so were EA builds). */ + let newOptionValue = optionValue + const existingOptions = process.env[c.NATIVE_IMAGE_OPTIONS_ENV] + if (existingOptions) { + newOptionValue = `${existingOptions} ${newOptionValue}` + } + core.exportVariable(c.NATIVE_IMAGE_OPTIONS_ENV, newOptionValue) + } else { + const optionsFile = getNativeImageOptionsFile() + if (fs.existsSync(optionsFile)) { + fs.appendFileSync(optionsFile, ` ${optionValue}`) + } else { + fs.writeFileSync(optionsFile, `NativeImageArgs = ${optionValue}`) + } + } +} + +const NATIVE_IMAGE_CONFIG_FILE = tmpfile('native-image-options.properties') +const NATIVE_IMAGE_CONFIG_FILE_ENV = 'NATIVE_IMAGE_CONFIG_FILE' + +function getNativeImageOptionsFile(): string { + let optionsFile = process.env[NATIVE_IMAGE_CONFIG_FILE_ENV] + if (optionsFile === undefined) { + optionsFile = NATIVE_IMAGE_CONFIG_FILE + core.exportVariable(NATIVE_IMAGE_CONFIG_FILE_ENV, optionsFile) + } + return optionsFile +} \ No newline at end of file