Skip to content

Commit

Permalink
refactor: Extract each step to seperate file
Browse files Browse the repository at this point in the history
Also update documentation
  • Loading branch information
pr1metine committed Jul 2, 2023
1 parent 4354632 commit 4c2ac04
Show file tree
Hide file tree
Showing 11 changed files with 359 additions and 233 deletions.
19 changes: 16 additions & 3 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,30 @@ on:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
processing:
- tag: "processing-1292-4.2"
semver: "1292.0.0"
- tag: "processing-1272-4.0a3"
semver: "1272.0.0"
- tag: "processing-0270-3.5.4"
semver: "270.0.0"
- tag: "processing-0232-3.0a5"
semver: "232.0.0"
- tag: "processing-0227-2.2.1"
semver: "227.0.0"
steps:
- uses: actions/checkout@v2
- uses: ./
with:
tag: "processing-1292-4.2"
tag: ${{matrix.processing.tag}}
- run: pwd
- run: echo $PATH
- run: ls -lah /opt/hostedtoolcache/
- run: ls -lah /opt/hostedtoolcache/processing
- run: ls -lah /opt/hostedtoolcache/processing/1292.0.0
- run: ls -lah /opt/hostedtoolcache/processing/1292.0.0/x64
- run: ls -lah /opt/hostedtoolcache/processing/${{matrix.processing.semver}}
- run: ls -lah /opt/hostedtoolcache/processing/${{matrix.processing.semver}}/x64
- run: which processing
- run: which processing-java
- run: processing-java --help
Expand Down
24 changes: 19 additions & 5 deletions .github/workflows/macosx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,26 @@ on:
jobs:
test:
runs-on: macos-latest
strategy:
matrix:
processing:
- tag: "processing-1292-4.2"
semver: "1292.0.0"
- tag: "processing-1272-4.0a3"
semver: "1272.0.0"
- tag: "processing-0270-3.5.4"
semver: "270.0.0"
- tag: "processing-0232-3.0a5"
semver: "232.0.0"
- tag: "processing-0227-2.2.1"
semver: "227.0.0"
steps:
- uses: actions/checkout@v2
- uses: ./
with:
tag: "processing-1292-4.2"
- run: which -a processing
- run: which -a processing-java
- run: processing --help
- run: processing-java --help
tag: ${{matrix.processing.tag}}
# TODO: Fix
# - run: which -a processing
# - run: which -a processing-java
# - run: processing --help
# - run: processing-java --help
25 changes: 14 additions & 11 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ on:
branches: [master]

jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: ./
with:
version: "3.5.4"
platform-filetype: "windows64.zip"

test:
needs: build
runs-on: windows-latest
strategy:
matrix:
processing:
- tag: "processing-1292-4.2"
semver: "1292.0.0"
- tag: "processing-1272-4.0a3"
semver: "1272.0.0"
- tag: "processing-0270-3.5.4"
semver: "270.0.0"
- tag: "processing-0232-3.0a5"
semver: "232.0.0"
- tag: "processing-0227-2.2.1"
semver: "227.0.0"
steps:
- uses: actions/checkout@v2
- uses: ./
with:
tag: "processing-1292-4.2"
tag: ${{matrix.processing.tag}}
- run: where.exe processing
- run: where.exe processing-java
- run: processing-java --help
Expand Down
31 changes: 21 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
# setup-processing

[![CI with Linux](https://github.com/ifP1/setup-processing/workflows/CI%20with%20Linux/badge.svg)](https://github.com/ifP1/setup-processing/actions?query=workflow%3A%22CI+with+Linux%22)
[![CI with Windows](https://github.com/ifP1/setup-processing/workflows/CI%20with%20Windows/badge.svg)](https://github.com/ifP1/setup-processing/actions?query=workflow%3A%22CI+with+Windows%22)
[![CI with MacOS](https://github.com/ifP1/setup-processing/workflows/CI%20with%20MacOS/badge.svg)](https://github.com/ifP1/setup-processing/actions?query=workflow%3A%22CI+with+MacOS%22)
[![GitHub issues](https://img.shields.io/github/issues/ifP1/setup-processing)](https://github.com/ifP1/setup-processing/issues)
[![CI with Linux](https://github.com/ifP1/setup-processing/workflows/CI%20with%20Linux/badge.svg)](https://github.com/pr1metine/setup-processing/actions?query=workflow%3A%22CI+with+Linux%22)
[![CI with Windows](https://github.com/ifP1/setup-processing/workflows/CI%20with%20Windows/badge.svg)](https://github.com/pr1metine/setup-processing/actions?query=workflow%3A%22CI+with+Windows%22)
[![CI with MacOS](https://github.com/ifP1/setup-processing/workflows/CI%20with%20MacOS/badge.svg)](https://github.com/pr1metine/setup-processing/actions?query=workflow%3A%22CI+with+MacOS%22)
[![GitHub issues](https://img.shields.io/github/issues/ifP1/setup-processing)](https://github.com/pr1metine/setup-processing/issues)

Sets up the Processing SDK. Will try to fetch https://processing.org/download/processing-${version}-${platform-filetype}
Sets up the Processing SDK. Given a tag name and a release asset name, `setup-processing` will try to download a release asset from [Processing 4](https://github.com/processing/processing4/releases) or [Processing](https://github.com/processing/processing/releases).

## Note

- If no tag name `tag` is provided, the latest release will be used.
- If no asset name `asset-name` is provided, this action will try to choose the right asset based on the Action Runner's OS and CPU architecture.

## Code

```yaml
- name: Setup Processing
uses: pr1metine/setup-processing@v1.1.1
uses: pr1metine/setup-processing@v2.0.0
with:
# Version of Processing, e.g. 3.5.4
version: # default is 3.5.4
# e.g. 'windows64.zip', 'windows32.zip', 'linux64.tgz'
platform-filetype: # optional, default is linux64.tgz
# Tag of Processing GitHub Release, e.g. processing-1292-4.2
# See https://github.com/processing/processing4/releases and
# https://github.com/processing/processing4/releases
tag: # optional, will use latest release by default
# Name of a Processing GitHub Release Asset, e.g. processing-4.2-linux-arm64.tgz
asset-name: # optional, will infer based on Runner by default
```
## Development
[This file](src/index.ts) serves as this Action's entry point. Use [act](https://github.com/nektos/act) to test this GitHub Action locally before pushing.
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 'Setup Processing'
description: 'Sets up the Processing SDK. Will try to fetch https://processing.org/download/processing-${version}-${platform-filetype}'
description: 'Sets up the Processing SDK. Downloads and installs a GitHub Release Asset from Processing'
inputs:
tag:
description: 'Tag of Processing GitHub Release, e.g. processing-1292-4.2'
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

105 changes: 105 additions & 0 deletions src/downloadAsset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import * as core from "@actions/core";
import * as tc from "@actions/tool-cache";
import { ArchiveType, ReleaseAsset } from "./types";

/**
* Download a specified asset from a GitHub Release.
*
* If no assetName is given, `setup-processing` tries to infer the
* assetName. It does so based on the OS and CPU architecture of
* the GitHub runner.
*
* @param assets Available assets for a Processing release
* @param assetName Name of the specified asset to be downloaded
* @returns Path to extracted asset
*/
async function downloadAsset(
assets: ReleaseAsset[],
assetName?: string
): Promise<string> {
let asset = getAssetByName(assets, assetName);
if (asset === undefined) {
throw new Error(
`The given archive name does not exist: ${
assetName ?? "(Infered, please open an issue for this)"
}
Available choices were: ${JSON.stringify(
assets.map((asset) => asset.name)
)}
`
);
}
core.info(`Downloading release asset "${asset.name}"`);

let path = await extractTool(
await tc.downloadTool(asset.url),
asset.content_type
);

return path;
}

function getAssetByName(
assets: ReleaseAsset[],
assetName?: string
): ReleaseAsset | undefined {
if (assetName === undefined) {
core.info(
`Infering correct asset file name (os=${process.platform}, arch=${process.arch})...`
);
let matchers = getMatchers(process.platform, process.arch);
return assets.find((asset) =>
matchers.every((matcher) => asset.name.match(matcher) !== null)
);
}

return assets.find((asset) => asset.name === assetName);
}
function getMatchers(os: NodeJS.Platform, arch: NodeJS.Architecture): RegExp[] {
let out = new Array<RegExp>();
switch (os) {
case "win32":
out.push(/.*windows.*/);
break;
case "linux":
out.push(/.*linux.*/);
break;
case "darwin":
out.push(/.*macos.*/);
break;
default:
throw new Error(`Unsupported platform ${os}. Please open an issue`);
}

switch (arch) {
case "arm":
out.push(/.*(arm32|armv6hf).*/);
break;
case "arm64":
out.push(/.*(arm64|aarch64).*/);
break;
case "x64":
out.push(/.*(x64|linux64|windows64|macosx).*/);
break;
// Apparently no x86 32 bit
default:
throw new Error(`Unsupported architecture ${arch}. Please open an issue`);
}

return out;
}
function extractTool(path: string, type: ArchiveType): Promise<string> {
switch (type) {
case "application/zip":
return tc.extractZip(path, "extracted-processing");
case "application/octet-stream":
core.warning(
`Asset type of ${path} is "application/octet-stream". Assumed to be gzip`
);
case "application/gzip":
return tc.extractTar(path, "extracted-processing");
}
}

export default downloadAsset;
93 changes: 93 additions & 0 deletions src/getReleaseByTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import * as core from "@actions/core";
import { Octokit, RestEndpointMethodTypes } from "@octokit/rest";

import { ArchiveType, Release, Repository } from "./types";

let OCTOKIT = new Octokit();

/**
* Retrieves a release with the specified tag.
*
* If no tag is specified, the latest will be returned. This function will search for
* the given tag inside the given repositories `repos`. If the given tag occurs in
* multiple repositories, the release from the left most repo of `repos` will be
* returned.
*
* @param repos Repositories to look for a Processing release
* @param tag The tag of the desired release
* @returns The desired release
*/
async function getReleaseByTag(
repos: Repository[],
tag?: string
): Promise<Release> {
if (repos.length < 1) {
throw new Error(
`No Processing repos specified in which to look for the specified tag`
);
}

let releases = await Promise.allSettled(
repos.map((repo) =>
tag !== undefined
? OCTOKIT.rest.repos.getReleaseByTag({
...repo,
tag: tag,
})
: OCTOKIT.rest.repos.getLatestRelease(repo)
)
).then((results) =>
results.filter(isFulfilled).map((result) => result.value)
);

let successfullyRetrievedReleases = releases.filter(
(release) => release.status >= 200 && release.status < 300
);

if (successfullyRetrievedReleases.length < 1) {
throw new Error(
`Could not find a Processing release for the following tag: ${JSON.stringify(
tag
)}`
);
}

let release = successfullyRetrievedReleases[0].data;

core.debug(`Pruning asset descriptions: ${JSON.stringify(release.assets)}`);
let out = {
tag: release.tag_name,
assets: release.assets.map((asset) => {
return {
name: asset.name,
url: asset.browser_download_url,
content_type: getArchiveType(asset.content_type),
};
}),
};

return out;
}

const isFulfilled = <T>(
input: PromiseSettledResult<T>
): input is PromiseFulfilledResult<T> => input.status === "fulfilled";

function getArchiveType(content_type: string): ArchiveType {
if (
content_type !== "application/zip" &&
content_type !== "application/gzip" &&
content_type !== "application/octet-stream"
) {
throw new Error(
`Unsupported archive type: ${content_type}. Please file an issue`
);
}

if (content_type === "application/octet-stream") {
}

return <ArchiveType>content_type;
}

export default getReleaseByTag;
Loading

0 comments on commit 4c2ac04

Please sign in to comment.