Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mint-executable-directory option to mint installation directory #28

Merged
merged 4 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ jobs:
mint-directory: example
- shell: bash -xe {0}
run: |
ls -alh "$HOME/bin" | grep mint
"$HOME/bin/mint" --version
"$HOME/bin/mint" list
ls -alh /usr/local/bin | grep mint
mint --version
mint list
ls -alh ~/.mint
ls -alh ~/.mint/bin
ls -alh ~/.mint/packages/*/build/*
test-linux:
runs-on: ubuntu-latest
Expand All @@ -29,8 +30,9 @@ jobs:
mint-directory: example
- shell: bash -xe {0}
run: |
ls -alh "$HOME/bin" | grep mint
"$HOME/bin/mint" --version
"$HOME/bin/mint" list
ls -alh /usr/local/bin | grep mint
mint --version
mint list
ls -alh ~/.mint
ls -alh ~/.mint/bin
ls -alh ~/.mint/packages/*/build/*
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ setup-mint step will do:

* Retrieve mint version from Mintfile
* setup-mint will use mint@master if mint version is not specified in Mintfile
* Install mint to $HOME/mint
* Install mint to /usr/local/bin/mint
* Cache mint binary for next run
* Run `mint bootstrap` to install swift commands
* Cleanup unused swift commands, that is not listed in Mintfile.
Expand All @@ -46,6 +46,8 @@ This action can be used in a macOS runner and a Linux runner.
with:
# a directory contains Mintfile, default: GITHUB_WORKSPACE
mint-directory: .
# a directory where mint executable itself will be installed, default: /usr/local/bin
mint-executable-directory: /usr/local/bin
# run mint bootstrap, default: true
bootstrap: true
# cache swift commands (~/.mint), default: true
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ inputs:
description: 'a directory of Mintfile'
required: false
default: '.'
mint-executable-directory:
description: 'a directory where mint executable itself will be installed'
required: false
default: '/usr/local/bin'
bootstrap:
description: 'execute mint bootstrap'
required: false
Expand Down
45 changes: 27 additions & 18 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ function pathContains(parent: string, child: string): boolean {
return !path.relative(parent, child).startsWith("../")
}

/**
* expand `~` to os.homedir()
*/
function expandHome(path: string): string {
return path.replace(/^~\//, `${os.homedir()}/`)
}

// Caching may throw errors for legitimate reasons that should not fail the action.
// Example: Race condition between multiple github runners where both try to save cache with the same key at the same time.
// You only need 1 of the runners to save the cache. The other runners can gracefully ignore the error and continue running.
Expand All @@ -47,12 +54,14 @@ async function saveCache(paths: string[], key: string): Promise<void> {

async function main() {
try {
const mintDirectory = core.getInput('mint-directory')
const mintDirectory = expandHome(core.getInput('mint-directory'))
const mintExecutableDirectory = expandHome(core.getInput('mint-executable-directory'))
const bootstrap = (core.getInput('bootstrap') == 'true')
const useCache = (core.getInput('use-cache') == 'true')
const cachePrefix = core.getInput('cache-prefix')
const clean = (core.getInput('clean') == 'true')
core.info(`mintDirectory: ${mintDirectory}`)
core.info(`mintExecutableDirectory: ${mintExecutableDirectory}`)
core.info(`bootstrap: ${bootstrap}`)
core.info(`useCache: ${useCache}`)
core.info(`cachePrefix: ${cachePrefix}`)
Expand All @@ -68,15 +77,13 @@ async function main() {
core.info(`mintVersion from Mintfile: ${mintVersion}`)
}
}
const binaryDirectoryPrefix = `${process.env['HOME']}`
const binaryDirectory = `${binaryDirectoryPrefix}/bin`
const mintPath = `${binaryDirectory}/mint`
const mint = `${mintExecutableDirectory}/mint`
const mintCacheKey = `${cachePrefix}-${process.env['RUNNER_OS']}-${process.env['RUNNER_ARCH']}-irgaly/setup-mint-${mintVersion}`
const mintPaths = [mintPath]
const mintPaths = [mint]
core.info(`mint cache key: ${mintCacheKey}`)
const mintRestored = ((await cache.restoreCache(mintPaths, mintCacheKey)) != undefined)
if (mintRestored) {
core.info(`${mintPath} restored from cache`)
core.info(`${mint} restored from cache`)
} else {
const temp = path.join(process.env['RUNNER_TEMP'] || '.', uuidv4())
fs.mkdirSync(temp, { recursive: true })
Expand All @@ -88,21 +95,23 @@ async function main() {
'-b', mintVersion,
'https://github.com/yonaskolb/Mint.git'])
if (os.platform() == 'darwin') {
await execute('make', ['-C', `${temp}/Mint`, `PREFIX=${binaryDirectoryPrefix}`])
await execute('make', ['build', '-C', `${temp}/Mint`])
fs.mkdirSync(mintExecutableDirectory, { recursive: true })
fs.copyFileSync(`${temp}/Mint/.build/apple/Products/Release/mint`, mint)
} else {
await execute('swift', ['build', '-c', 'release'], `${temp}/Mint`)
fs.mkdirSync(binaryDirectory, { recursive: true })
fs.copyFileSync(`${temp}/Mint/.build/release/mint`, mintPath)
fs.mkdirSync(mintExecutableDirectory, { recursive: true })
fs.copyFileSync(`${temp}/Mint/.build/release/mint`, mint)
}

await saveCache(mintPaths, mintCacheKey)
}
if (hasMintfile && bootstrap) {
const mintDirectory = (process.env['MINT_PATH'] || '~/.mint').replace(/^~\//, `${os.homedir()}/`)
const mintBinaryDirectory = (process.env['MINT_LINK_PATH'] || '~/.mint/bin').replace(/^~\//, `${os.homedir()}/`)
const mintBinaryNeedsCache = !pathContains(mintDirectory, mintBinaryDirectory)
const mintPackagesDirectory = `${mintDirectory}/packages`
const mintDependencyPaths = [mintDirectory]
const mintPathDirectory = expandHome(process.env['MINT_PATH'] || '~/.mint')
const mintBinaryDirectory = expandHome(process.env['MINT_LINK_PATH'] || '~/.mint/bin')
const mintBinaryNeedsCache = !pathContains(mintPathDirectory, mintBinaryDirectory)
const mintPackagesDirectory = `${mintPathDirectory}/packages`
const mintDependencyPaths = [mintPathDirectory]
const mintDependencyCacheKey = `${cachePrefix}-${process.env['RUNNER_OS']}-${process.env['RUNNER_ARCH']}-irgaly/setup-mint-deps-${await hashFiles(mintFile)}`
const mintDependencyRestoreKeys = [`${cachePrefix}-${process.env['RUNNER_OS']}-${process.env['RUNNER_ARCH']}-irgaly/setup-mint-deps-`]
const mintBinaryPaths = [mintBinaryDirectory]
Expand All @@ -121,9 +130,9 @@ async function main() {
mintDependencyRestored = (mintDependencyRestoredKey == mintDependencyCacheKey)
}
if (mintDependencyRestored) {
core.info(`${mintDirectory} / ${mintBinaryDirectory} restored from cache`)
core.info(`${mintPathDirectory} / ${mintBinaryDirectory} restored from cache`)
} else {
await execute(mintPath, ['bootstrap', '-v', '-m', `${mintFile}`])
await execute(mint, ['bootstrap', '-v', '-m', `${mintFile}`])
if (useCache) {
if (clean) {
const mintFileString = fs.readFileSync(mintFile).toString()
Expand Down Expand Up @@ -152,8 +161,8 @@ async function main() {
for (const installed of installedPackages) {
core.info(`installed: ${installed.name}`)
if (!defined.includes(installed.name) && !defined.includes(installed.short)) {
core.info(`=> unisntall: ${installed.name}`)
await execute(mintPath, ['uninstall', `${installed.name}`])
core.info(`=> uninstall: ${installed.name}`)
await execute(mint, ['uninstall', `${installed.name}`])
const builds = path.dirname(installed.build)
if (fs.readdirSync(builds).length == 0) {
fs.rmdirSync(path.dirname(builds), { recursive: true })
Expand Down