Skip to content

Commit

Permalink
Extract platform-specific details into platform.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyvaartjes committed Aug 27, 2024
1 parent 5239d5b commit 1b1cf9d
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .github/linters/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ rules:
'i18n-text/no-en': 'off',
'import/no-namespace': 'off',
'no-console': 'off',
'no-shadow': 'off',
'no-unused-vars': 'off',
'prettier/prettier': 'error',
'semi': 'off',
Expand All @@ -64,6 +65,7 @@ rules:
'@typescript-eslint/no-namespace': 'error',
'@typescript-eslint/no-non-null-assertion': 'warn',
'@typescript-eslint/no-require-imports': 'error',
'@typescript-eslint/no-shadow': 'error',
'@typescript-eslint/no-unnecessary-qualifier': 'error',
'@typescript-eslint/no-unnecessary-type-assertion': 'error',
'@typescript-eslint/no-unused-vars': 'error',
Expand Down
36 changes: 9 additions & 27 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from '@actions/core'
import * as tc from '@actions/tool-cache'
import os from 'node:os'
import { chmod } from 'fs/promises'
import { determinePlatformInfo } from './platform'

/**
* The main function for the action.
Expand All @@ -17,8 +17,8 @@ export async function run(): Promise<void> {
if (cachedPath) {
core.debug(`Found cached Pkl version at ${cachedPath}`)
} else {
const assetName = findAssetName()
const downloadUrl = `https://github.com/apple/pkl/releases/download/${pklVersion}/${assetName}`
const platformInfo = determinePlatformInfo()
const downloadUrl = `https://github.com/apple/pkl/releases/download/${pklVersion}/${platformInfo.githubSourceAssetName}`

core.debug(`Download URL: ${downloadUrl}`)

Expand All @@ -32,7 +32,12 @@ export async function run(): Promise<void> {
core.debug(`Set executable permissions on: ${pklBinaryPath}`)

// Cache the downloaded file
cachedPath = await tc.cacheFile(pklBinaryPath, 'pkl', 'pkl', pklVersion)
cachedPath = await tc.cacheFile(
pklBinaryPath,
platformInfo.targetFileName,
'pkl',
pklVersion
)
core.debug(`Cached PKL binary to: ${cachedPath}`)
}

Expand All @@ -43,26 +48,3 @@ export async function run(): Promise<void> {
if (error instanceof Error) core.setFailed(error.message)
}
}

function findAssetName(): string {
const op = os.platform()
const arch = os.arch()

core.info(`Try to find asset name for: ${op}-${arch}`)
switch (op) {
case 'linux':
return 'pkl-linux-amd64'
case 'darwin':
switch (arch) {
case 'x64':
return 'pkl-macos-amd64'
case 'arm64':
return 'pkl-macos-aarch64'
}
break
case 'win32':
return 'pkl-windows-amd64.exe'
}

throw new Error(`Couldn't find asset name for ${op}-${arch}`)
}
98 changes: 98 additions & 0 deletions src/platform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import os from 'node:os'

enum Platform {
Linux,
MacOS,
Windows
}

enum Architecture {
arm64,
x64
}

type PlatformInfo = {
plat: Platform
arch: Architecture
githubSourceAssetName: string
targetFileName: string
}

export function determinePlatformInfo(): PlatformInfo {
const plat = determineOS()
const arch = determineArch()

return {
plat,
arch,
githubSourceAssetName: determineGithubAsset(plat, arch),
targetFileName: determineTargetFileName(plat)
}
}

function determineOS(): Platform {
switch (os.platform()) {
case 'linux':
return Platform.Linux
case 'darwin':
return Platform.MacOS
case 'win32':
return Platform.Windows
default:
throw new Error('Unsupported platform')
}
}

function determineArch(): Architecture {
switch (os.arch()) {
case 'arm64':
return Architecture.arm64
case 'x64':
return Architecture.x64
default:
throw new Error('Unsupported architecture')
}
}

function determineGithubAsset(plat: Platform, arch: Architecture): string {
switch (plat) {
case Platform.Linux:
switch (arch) {
case Architecture.arm64:
return 'pkl-linux-aarch64'
case Architecture.x64:
return 'pkl-linux-amd64'
default:
throw new Error('Unsupported architecture')
}
case Platform.MacOS:
switch (arch) {
case Architecture.arm64:
return 'pkl-macos-aarch64'
case Architecture.x64:
return 'pkl-macos-amd64'
default:
throw new Error('Unsupported architecture')
}
case Platform.Windows:
switch (arch) {
case Architecture.arm64:
throw new Error('Windows arm not yet supported')
case Architecture.x64:
return 'pkl-windows-amd64.exe'
default:
throw new Error('Unsupported architecture')
}
default:
throw new Error('Unsupported platform')
}
}

function determineTargetFileName(plat: Platform): string {
switch (plat) {
case Platform.Windows:
return 'pkl.exe'
default:
return 'pkl'
}
}

0 comments on commit 1b1cf9d

Please sign in to comment.