Skip to content

Commit 958f054

Browse files
committed
@actions/core: abstract @actions/core usage
Introduce a simple abstraction of the @actions/core module, such that we can provide a different implementation in the future (for Azure Pipelines, for example). Rename 'run' to 'setup' (which is the sibling to 'cleanup'), and add a new 'run' function that takes only an ICore implementation, using @actions/core. Signed-off-by: Matthew John Cheetham <[email protected]>
1 parent aaca187 commit 958f054

File tree

8 files changed

+308
-74
lines changed

8 files changed

+308
-74
lines changed

dist/index.js

Lines changed: 117 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

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

main.ts

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import * as core from '@actions/core'
1+
import {ICore} from './src/core'
2+
import {ActionsCore} from './src/actions_core'
23
import {mkdirp} from './src/downloader'
34
import {restoreCache, saveCache} from '@actions/cache'
45
import process from 'process'
@@ -11,9 +12,6 @@ import {
1112
import {getViaCIArtifacts} from './src/ci_artifacts'
1213
import * as fs from 'fs'
1314

14-
const flavor = core.getInput('flavor')
15-
const architecture = core.getInput('architecture')
16-
1715
/**
1816
* Some Azure VM types have a temporary disk which is local to the VM and therefore provides
1917
* _much_ faster disk IO than the OS Disk (or any other attached disk).
@@ -23,7 +21,7 @@ const architecture = core.getInput('architecture')
2321
*
2422
* https://learn.microsoft.com/en-us/azure/virtual-machines/managed-disks-overview#temporary-disk
2523
*/
26-
function getDriveLetterPrefix(): string {
24+
function getDriveLetterPrefix(core: ICore): string {
2725
if (fs.existsSync('D:/')) {
2826
core.info('Found a fast, temporary disk on this VM (D:/). Will use that.')
2927
return 'D:/'
@@ -32,7 +30,11 @@ function getDriveLetterPrefix(): string {
3230
return 'C:/'
3331
}
3432

35-
async function run(): Promise<void> {
33+
async function setup(
34+
core: ICore,
35+
flavor: string,
36+
architecture: string
37+
): Promise<void> {
3638
try {
3739
if (process.platform !== 'win32') {
3840
core.warning(
@@ -47,10 +49,10 @@ async function run(): Promise<void> {
4749

4850
const {artifactName, download, id} =
4951
flavor === 'minimal'
50-
? await getViaCIArtifacts(architecture, githubToken)
51-
: await getViaGit(flavor, architecture, githubToken)
52+
? await getViaCIArtifacts(core, architecture, githubToken)
53+
: await getViaGit(core, flavor, architecture, githubToken)
5254
const outputDirectory =
53-
core.getInput('path') || `${getDriveLetterPrefix()}${artifactName}`
55+
core.getInput('path') || `${getDriveLetterPrefix(core)}${artifactName}`
5456
core.setOutput('result', outputDirectory)
5557

5658
let useCache: boolean
@@ -158,7 +160,7 @@ async function run(): Promise<void> {
158160
}
159161
}
160162

161-
function cleanup(): void {
163+
function cleanup(core: ICore, flavor: string, architecture: string): void {
162164
if (core.getInput('cleanup') !== 'true') {
163165
core.info(
164166
`Won't clean up SDK files as the 'cleanup' input was not provided or doesn't equal 'true'.`
@@ -168,7 +170,7 @@ function cleanup(): void {
168170

169171
const outputDirectory =
170172
core.getInput('path') ||
171-
`${getDriveLetterPrefix()}${
173+
`${getDriveLetterPrefix(core)}${
172174
getArtifactMetadata(flavor, architecture).artifactName
173175
}`
174176

@@ -203,20 +205,22 @@ function cleanup(): void {
203205
core.info(`Finished cleaning up ${outputDirectory}.`)
204206
}
205207

206-
/**
207-
* Indicates whether the POST action is running
208-
*/
209-
export const isPost = !!core.getState('isPost')
210-
211-
if (!isPost) {
212-
run()
213-
/*
214-
* Publish a variable so that when the POST action runs, it can determine it should run the cleanup logic.
215-
* This is necessary since we don't have a separate entry point.
216-
* Inspired by https://github.com/actions/checkout/blob/v3.1.0/src/state-helper.ts#L56-L60
217-
*/
218-
core.saveState('isPost', 'true')
219-
} else {
220-
// If the POST action is running, we cleanup our artifacts
221-
cleanup()
208+
async function run(core: ICore): Promise<void> {
209+
const flavor = core.getInput('flavor')
210+
const architecture = core.getInput('architecture')
211+
const isPost = !!core.getState('isPost')
212+
if (!isPost) {
213+
setup(core, flavor, architecture)
214+
/*
215+
* Publish a variable so that when the POST action runs, it can determine it should run the cleanup logic.
216+
* This is necessary since we don't have a separate entry point.
217+
* Inspired by https://github.com/actions/checkout/blob/v3.1.0/src/state-helper.ts#L56-L60
218+
*/
219+
core.saveState('isPost', 'true')
220+
} else {
221+
// If the POST action is running, we cleanup our artifacts
222+
cleanup(core, flavor, architecture)
223+
}
222224
}
225+
226+
run(new ActionsCore())

0 commit comments

Comments
 (0)