Skip to content

Commit

Permalink
Port the Setup class
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Nov 3, 2024
1 parent 2402389 commit 4ea2f11
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 235 deletions.
1 change: 1 addition & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./release.js";
export * from "./setup.js";
32 changes: 32 additions & 0 deletions lib/setup.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {Release} from "./release.js";

/**
* Manages the download and installation of Apache Ant.
*/
export declare class Setup {

/**
* The release to download and install.
*/
release: Release;

/**
* Creates a new setup.
* @param release The release to download and install.
*/
constructor(release: Release);

/**
* Downloads and extracts the ZIP archive of Apache Ant.
* @param options Value indicating whether to fetch the Ant optional tasks.
* @returns The path to the extracted directory.
*/
download(options?: {optionalTasks?: boolean}): Promise<string>;

/**
* Installs Apache Ant, after downloading it if required.
* @param options Value indicating whether to fetch the Ant optional tasks.
* @returns The path to the installation directory.
*/
install(options?: {optionalTasks?: boolean}): Promise<string>;
}
27 changes: 0 additions & 27 deletions src/ant/Program.hx

This file was deleted.

41 changes: 0 additions & 41 deletions src/ant/Release.hx

This file was deleted.

70 changes: 0 additions & 70 deletions src/ant/Setup.hx

This file was deleted.

19 changes: 19 additions & 0 deletions src/cli.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {getBooleanInput, getInput, info, setFailed} from "@actions/core"
import process from "node:process"
import {Release} from "./release.js"
import {Setup} from "./setup.js"

try
process.title = "Setup Ant"

version = getInput "version"
release = Release.find if not version or version is "latest" then "*" else version
throw Error "No release matching the version constraint." if not release

optionalTasks = getBooleanInput "optional-tasks"
installed = if optionalTasks then "installed with optional tasks" else "installed"
path = await new Setup(release).install {optionalTasks}
info "Apache Ant #{release.version} successfully #{installed} in \"#{path}\"."

catch error
setFailed if error instanceof Error then error else String error
1 change: 1 addition & 0 deletions src/index.coffee
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./release.js"
export * from "./setup.js"
46 changes: 46 additions & 0 deletions src/setup.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {addPath, exportVariable} from "@actions/core"
import {cacheDir, downloadTool, extractZip, find} from "@actions/tool-cache"
import {execFile} from "node:child_process"
import {readdir} from "node:fs/promises"
import {join} from "node:path"
import {promisify} from "node:util"

# Manages the download and installation of Apache Ant.
export class Setup

# Creates a new setup.
constructor: (release) ->

# The release to download and install.
@release = release

# Downloads and extracts the ZIP archive of Apache Ant.
# Returns the path to the extracted directory.
download: (options = {}) ->
directory = await extractZip await downloadTool @release.url.href
antHome = join directory, await @_findSubfolder directory
await @_fetchOptionalTasks antHome if options.optionalTasks
antHome

# Installs Apache Ant, after downloading it if required.
# Returns the path to the install directory.
install: (options = {}) ->
antHome = find("ant", @release.version) ? await cacheDir (await @download options), "ant", @release.version
addPath join antHome, "bin"
exportVariable "ANT_HOME", antHome
antHome

# Fetches the external libraries required by Ant optional tasks.
_fetchOptionalTasks: (antHome) ->
run = promisify execFile
run "java", ["-jar", "lib/ant-launcher.jar", "-buildfile", "fetch.xml", "-noinput", "-silent", "-Ddest=system"],
cwd: antHome
env: {ANT_HOME: antHome}

# Determines the name of the single subfolder in the specified directory.
_findSubfolder: (directory) ->
folders = (await readdir directory, withFileTypes: true).filter (entity) -> entity.isDirectory()
switch folders.length
when 0 then throw Error "No subfolder found in: #{directory}."
when 1 then return folders[0].name
else throw Error "Multiple subfolders found in: #{directory}."
51 changes: 0 additions & 51 deletions test/ant/ReleaseTest.hx

This file was deleted.

43 changes: 0 additions & 43 deletions test/ant/SetupTest.hx

This file was deleted.

6 changes: 3 additions & 3 deletions test/release_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {Release} from "@cedx/setup-ant"
# Tests the features of the `Release` class.
describe "Release", ->
existingRelease = new Release "1.10.15"
nonExistentRelease = new Release "666.6.6"
nonExistingRelease = new Release "666.6.6"

describe "exists", ->
it "should return `false` if the release does not exist", -> ok not nonExistentRelease.exists
it "should return `false` if the release does not exist", -> ok not nonExistingRelease.exists
it "should return `true` if the release exists", -> ok existingRelease.exists

describe "latest", ->
Expand All @@ -17,7 +17,7 @@ describe "Release", ->
describe "url", ->
it "should return the URL of the Ant archive", ->
equal existingRelease.url.href, "https://dlcdn.apache.org/ant/binaries/apache-ant-1.10.15-bin.zip"
equal nonExistentRelease.url.href, "https://dlcdn.apache.org/ant/binaries/apache-ant-666.6.6-bin.zip"
equal nonExistingRelease.url.href, "https://dlcdn.apache.org/ant/binaries/apache-ant-666.6.6-bin.zip"

describe "find()", ->
it "should return `null` if no release matches the version constraint", ->
Expand Down
25 changes: 25 additions & 0 deletions test/setup_test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {doesNotReject, equal, ok} from "node:assert/strict"
import {access, readdir} from "node:fs/promises"
import {extname, join, resolve} from "node:path"
import {env, platform} from "node:process"
import {describe, it} from "node:test"
import {Release, Setup} from "@cedx/setup-ant"

# Tests the features of the `Setup` class.
describe "Setup", ->
env.RUNNER_TEMP ?= resolve "var/tmp"
env.RUNNER_TOOL_CACHE ?= resolve "var/cache"

describe "download()", ->
it "should properly download and extract Apache Ant", ->
path = await new Setup(Release.latest).download optionalTasks: yes
await doesNotReject access join path, "bin", if platform is "win32" then "ant.cmd" else "ant"

jars = (await readdir join path, "lib").filter (file) -> extname(file) is ".jar"
equal jars.filter((file) -> file.startsWith "ivy-").length, 1

describe "install()", ->
it "should add the Ant directory to the PATH environment variable", ->
path = await new Setup(Release.latest).install optionalTasks: no
equal env.ANT_HOME, path
ok env.PATH.includes path

0 comments on commit 4ea2f11

Please sign in to comment.