Skip to content

Commit

Permalink
Merge pull request #1 from remarkablegames/feat/butler
Browse files Browse the repository at this point in the history
feat: set up butler
  • Loading branch information
remarkablemark authored Feb 24, 2024
2 parents a998fbe + a7f0dba commit 95c678c
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 168 deletions.
15 changes: 8 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
version: [1.14.0, 2.27.0]
version: [15.21.0, 14.14.0]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout repository
Expand All @@ -73,20 +73,21 @@ jobs:
- name: Run action
uses: ./
with:
cli-version: ${{ matrix.version }}
cli-name: gh
butler-version: ${{ matrix.version }}
cli-name: butler

- name: Locate binary
run: command -v gh
run: command -v butler

- name: Check version
if: matrix.os != 'windows-latest'
run: |
if [[ $(gh --version) != *'${{ matrix.version }}'* ]]; then
butler --version &> BUTLER_VERSION
if [[ $(cat BUTLER_VERSION) != *'${{ matrix.version }}'* ]]; then
echo 'Version does not match. See version below:'
gh --version
butler --version
exit 1
fi
- name: Show help
run: gh --help
run: butler --help
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![version](https://badgen.net/github/release/remarkablegames/setup-butler)](https://github.com/remarkablegames/setup-butler/releases)
[![build](https://github.com/remarkablegames/setup-butler/actions/workflows/build.yml/badge.svg)](https://github.com/remarkablegames/setup-butler/actions/workflows/build.yml)
[![codecov](https://codecov.io/gh/remarkablegames/setup-butler/graph/badge.svg?token=AAbBz3SIPn)](https://codecov.io/gh/remarkablegames/setup-butler)

🎩 Set up your GitHub Actions workflow with [itch.io](https://itch.io/) [butler](https://itch.io/docs/butler/).

Expand Down Expand Up @@ -32,22 +33,22 @@ See [action.yml](action.yml)
### `cli-version`

**Optional**: The CLI [version](https://github.com/cli/cli/releases). Defaults to [`2.33.0`](https://github.com/cli/cli/releases/tag/v2.33.0):
**Optional**: The CLI [version](https://broth.itch.ovh/butler). Defaults to `LATEST`:

```yaml
- uses: remarkablegames/setup-butler@v1
with:
cli-version: 2.33.0
butler-version: LATEST
```

### `cli-name`

**Optional**: The htmlq CLI name. Defaults to `gh`:
**Optional**: The butler CLI name. Defaults to `butler`:

```yaml
- uses: remarkablegames/setup-butler@v1
with:
cli-name: gh
cli-name: butler
```

## Contributions
Expand Down
12 changes: 6 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ name: setup-butler
description: Set up GitHub Actions workflow with itch.io butler
author: remarkablemark
inputs:
cli-version:
description: CLI version
butler-version:
description: butler version
required: false
default: 2.33.0
default: LATEST
cli-name:
description: CLI name
required: false
default: gh
default: butler
runs:
using: node20
main: dist/index.js
branding:
icon: activity
color: gray-dark
icon: archive
color: orange
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

75 changes: 18 additions & 57 deletions src/__snapshots__/utils.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,70 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getBinaryPath when OS is "darwin" returns CLI filepath 1`] = `"directory/name"`;
exports[`getBinaryPath when platform is "darwin" returns CLI path 1`] = `"directory/name"`;

exports[`getBinaryPath when OS is "linux" returns CLI filepath 1`] = `"directory/name"`;
exports[`getBinaryPath when platform is "linux" returns CLI path 1`] = `"directory/name"`;

exports[`getBinaryPath when OS is "win32" returns CLI filepath 1`] = `"directory/name.exe"`;
exports[`getBinaryPath when platform is "win32" returns CLI path 1`] = `"directory/name.exe"`;

exports[`getDownloadObject when OS is "darwin" and arch is "arm" gets download object 1`] = `
{
"binaryDirectory": "gh_2.27.0_macOS_arm/bin",
"url": "https://github.com/cli/cli/releases/download/v2.27.0/gh_2.27.0_macOS_arm.tar.gz",
}
`;
exports[`getDownloadUrl when platform is "darwin" and arch is "arm" returns download object 1`] = `"https://broth.itch.ovh/butler/darwin-386/1.2.3/archive/default"`;

exports[`getDownloadObject when OS is "darwin" and arch is "x32" gets download object 1`] = `
{
"binaryDirectory": "gh_2.27.0_macOS_386/bin",
"url": "https://github.com/cli/cli/releases/download/v2.27.0/gh_2.27.0_macOS_386.tar.gz",
}
`;
exports[`getDownloadUrl when platform is "darwin" and arch is "arm64" returns download object 1`] = `"https://broth.itch.ovh/butler/darwin-amd64/1.2.3/archive/default"`;

exports[`getDownloadObject when OS is "darwin" and arch is "x64" gets download object 1`] = `
{
"binaryDirectory": "gh_2.27.0_macOS_amd64/bin",
"url": "https://github.com/cli/cli/releases/download/v2.27.0/gh_2.27.0_macOS_amd64.tar.gz",
}
`;
exports[`getDownloadUrl when platform is "darwin" and arch is "x32" returns download object 1`] = `"https://broth.itch.ovh/butler/darwin-386/1.2.3/archive/default"`;

exports[`getDownloadObject when OS is "linux" and arch is "arm" gets download object 1`] = `
{
"binaryDirectory": "gh_2.27.0_linux_arm/bin",
"url": "https://github.com/cli/cli/releases/download/v2.27.0/gh_2.27.0_linux_arm.tar.gz",
}
`;
exports[`getDownloadUrl when platform is "darwin" and arch is "x64" returns download object 1`] = `"https://broth.itch.ovh/butler/darwin-amd64/1.2.3/archive/default"`;

exports[`getDownloadObject when OS is "linux" and arch is "x32" gets download object 1`] = `
{
"binaryDirectory": "gh_2.27.0_linux_386/bin",
"url": "https://github.com/cli/cli/releases/download/v2.27.0/gh_2.27.0_linux_386.tar.gz",
}
`;
exports[`getDownloadUrl when platform is "linux" and arch is "arm" returns download object 1`] = `"https://broth.itch.ovh/butler/linux-386/1.2.3/archive/default"`;

exports[`getDownloadObject when OS is "linux" and arch is "x64" gets download object 1`] = `
{
"binaryDirectory": "gh_2.27.0_linux_amd64/bin",
"url": "https://github.com/cli/cli/releases/download/v2.27.0/gh_2.27.0_linux_amd64.tar.gz",
}
`;
exports[`getDownloadUrl when platform is "linux" and arch is "arm64" returns download object 1`] = `"https://broth.itch.ovh/butler/linux-amd64/1.2.3/archive/default"`;

exports[`getDownloadObject when OS is "win32" and arch is "arm" gets download object 1`] = `
{
"binaryDirectory": "bin",
"url": "https://github.com/cli/cli/releases/download/v2.27.0/gh_2.27.0_windows_arm.zip",
}
`;
exports[`getDownloadUrl when platform is "linux" and arch is "x32" returns download object 1`] = `"https://broth.itch.ovh/butler/linux-386/1.2.3/archive/default"`;

exports[`getDownloadObject when OS is "win32" and arch is "x32" gets download object 1`] = `
{
"binaryDirectory": "bin",
"url": "https://github.com/cli/cli/releases/download/v2.27.0/gh_2.27.0_windows_386.zip",
}
`;
exports[`getDownloadUrl when platform is "linux" and arch is "x64" returns download object 1`] = `"https://broth.itch.ovh/butler/linux-amd64/1.2.3/archive/default"`;

exports[`getDownloadObject when OS is "win32" and arch is "x64" gets download object 1`] = `
{
"binaryDirectory": "bin",
"url": "https://github.com/cli/cli/releases/download/v2.27.0/gh_2.27.0_windows_amd64.zip",
}
`;
exports[`getDownloadUrl when platform is "win32" and arch is "arm" returns download object 1`] = `"https://broth.itch.ovh/butler/windows-386/1.2.3/archive/default"`;

exports[`getDownloadUrl when platform is "win32" and arch is "arm64" returns download object 1`] = `"https://broth.itch.ovh/butler/windows-amd64/1.2.3/archive/default"`;

exports[`getDownloadUrl when platform is "win32" and arch is "x32" returns download object 1`] = `"https://broth.itch.ovh/butler/windows-386/1.2.3/archive/default"`;

exports[`getDownloadUrl when platform is "win32" and arch is "x64" returns download object 1`] = `"https://broth.itch.ovh/butler/windows-amd64/1.2.3/archive/default"`;
4 changes: 2 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const CLI_NAME = 'gh';
export const VERSION = '2.33.0';
export const CLI_NAME = 'butler';
export const VERSION = 'LATEST';
49 changes: 27 additions & 22 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,30 @@ const mockedExec = jest.mocked(exec);
const mockedTc = jest.mocked(tc);
const mockedOs = jest.mocked(os);

beforeEach(() => {
jest.resetAllMocks();
});

const name = 'cli-name';
const version = '1.2.3';

describe.each(['darwin', 'win32', 'linux'])('when OS is %p', (os) => {
const path = {
cli: 'path/to/cli',
zip: 'path/to/archive.zip',
} as const;

const platforms = ['darwin', 'linux', 'win32'] as const;

afterEach(() => {
jest.resetAllMocks();
});

describe.each(platforms)('platform is %p', (platform) => {
beforeEach(() => {
mockedOs.platform.mockReturnValueOnce(os as NodeJS.Platform);
mockedOs.arch.mockReturnValueOnce('arm64');
mockedOs.platform.mockReturnValue(platform);
mockedOs.arch.mockReturnValue('x64');
});

it('downloads, extracts, and adds CLI in PATH', async () => {
const pathToTarball = 'path/to/tarball';
const pathToCLI = 'path/to/cli';

it('downloads, extracts, and adds CLI to PATH', async () => {
mockedCore.getInput.mockImplementation((input) => {
switch (input) {
case 'cli-version':
case 'butler-version':
return version;
case 'cli-name':
return name;
Expand All @@ -43,34 +47,35 @@ describe.each(['darwin', 'win32', 'linux'])('when OS is %p', (os) => {
}
});

mockedTc.downloadTool.mockResolvedValueOnce(pathToTarball);
const extract = os === 'win32' ? mockedTc.extractZip : mockedTc.extractTar;
extract.mockResolvedValueOnce(pathToCLI);
mockedTc.downloadTool.mockResolvedValueOnce(path.zip);
mockedTc.extractZip.mockResolvedValueOnce(path.cli);

await run();

expect(mockedTc.downloadTool).toHaveBeenCalledWith(
expect.stringContaining(
`https://github.com/cli/cli/releases/download/v${version}/gh_${version}_`,
expect.stringMatching(
new RegExp(
`https://broth.itch.ovh/butler/[a-zA-Z0-9-]+/${version}/archive/default`,
),
),
);

expect(extract).toHaveBeenCalledWith(pathToTarball);
expect(mockedTc.extractZip).toHaveBeenCalledWith(path.zip);

expect(mockedExec.exec).toHaveBeenCalledWith('mv', [
expect.stringContaining('/bin/gh'),
expect.stringContaining(`/bin/${name}`),
expect.stringContaining(`${path.cli}/butler`),
expect.stringContaining(`${path.cli}/${name}`),
]);

expect(mockedTc.cacheFile).toHaveBeenCalledWith(
expect.stringContaining(`/bin/${name}`),
expect.stringContaining(`${path.cli}/${name}`),
name,
name,
version,
);

expect(mockedCore.addPath).toHaveBeenCalledWith(
expect.stringContaining(pathToCLI),
expect.stringContaining(path.cli),
);
});
});
Expand Down
29 changes: 9 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,34 @@
import { addPath, getInput, setFailed } from '@actions/core';
import { exec } from '@actions/exec';
import {
cacheFile,
downloadTool,
extractTar,
extractZip,
} from '@actions/tool-cache';
import path from 'path';
import { cacheFile, downloadTool, extractZip } from '@actions/tool-cache';

import { CLI_NAME, VERSION } from './constants';
import { getBinaryPath, getDownloadObject } from './utils';
import { getBinaryPath, getDownloadUrl } from './utils';

export async function run() {
try {
// Get the version of the tool to be installed
const version = getInput('cli-version') || VERSION;
const version = getInput('butler-version') || VERSION;
const name = getInput('cli-name') || CLI_NAME;

// Download the specific version of the tool (e.g., tarball/zipball)
const download = getDownloadObject(version);
const pathToTarball = await downloadTool(download.url);
const downloadUrl = getDownloadUrl(version);
const pathToTarball = await downloadTool(downloadUrl);

// Extract the tarball/zipball onto the host runner
const extract = download.url.endsWith('.zip') ? extractZip : extractTar;
const extractDirectory = await extract(pathToTarball);
const extractDirectory = await extractZip(pathToTarball);

// Rename the binary
const binaryDirectory = path.join(
extractDirectory,
download.binaryDirectory,
);
const binaryPath = getBinaryPath(binaryDirectory, name);
const binaryPath = getBinaryPath(extractDirectory, name);
if (name !== CLI_NAME) {
await exec('mv', [getBinaryPath(binaryDirectory, CLI_NAME), binaryPath]);
await exec('mv', [getBinaryPath(extractDirectory, CLI_NAME), binaryPath]);
}

// Cache the tool
await cacheFile(binaryPath, name, name, version);

// Expose the tool by adding it to the PATH
addPath(binaryDirectory);
addPath(extractDirectory);
} catch (error) {
if (error instanceof Error) {
setFailed(error.message);
Expand Down
Loading

0 comments on commit 95c678c

Please sign in to comment.