Skip to content

Commit

Permalink
Allow to run a script (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
fwilhe2 authored Sep 6, 2020
1 parent 247aae2 commit 71adbe0
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: ./
test-with-arg:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-20.04, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v2
- uses: ./
with:
script: |
#!/usr/bin/env kotlin
@file:Repository("https://dl.bintray.com/jakubriegel/kotlin-shell")
@file:DependsOn("eu.jrie.jetbrains:kotlin-shell-core:0.2.1")
@file:DependsOn("org.slf4j:slf4j-simple:1.7.28")
@file:CompilerOptions("-Xopt-in=kotlin.RequiresOptIn")
@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
import eu.jrie.jetbrains.kotlinshell.shell.*
shell {
"uname"()
}
docker:
runs-on: ubuntu-latest
steps:
Expand Down
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Setup the [Kotlin](https://kotlinlang.org/) cli compiler in GitHub Actions
# Setup the [Kotlin](https://kotlinlang.org/) cli compiler in GitHub Actions

This action downloads the Kotlin compiler and installs it to the path.
It won't touch the installed JREs.
Expand All @@ -24,6 +24,23 @@ jobs:
- run: kotlin myScript.main.kts
```
## Running a script inline
If you provide a string-argument `script`, the action will execute it via [`kotlin-main-kts` script definition jar](https://github.com/Kotlin/kotlin-script-examples/blob/master/jvm/main-kts/MainKts.md), see this example:

```yml
- uses: fwilhe2/setup-kotlin@main
with:
script: |
#!/usr/bin/env kotlin
//more kotlin script code here
```

## Disclaimer

This software is not affiliated with or endorsed by the owner of the [Kotlin trademark](https://kotlinlang.org/foundation/guidelines.html).
The trademark is used to describe what this software does.

## License

This software is released under the MIT License (MIT), see [LICENSE](./LICENSE) for details.
4 changes: 3 additions & 1 deletion __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import * as path from 'path'

// shows how the runner will run a javascript action with env / stdout protocol
test('test runs', () => {
process.env['INPUT_VERSION'] = '1.0'
process.env['INPUT_SCRIPT'] = 'println(234234)'
const ip = path.join(__dirname, '..', 'lib', 'main.js')
const options: cp.ExecSyncOptions = {
env: process.env
}
console.log(cp.execSync(`node ${ip}`, options).toString())
const output = cp.execSync(`node ${ip}`, options).toString().split('\n')
expect(output[output.length - 2]).toEqual('234234')
})
5 changes: 2 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ branding:
icon: 'code'
color: 'orange'
inputs:
milliseconds: # change this
script:
required: false
description: 'input description here'
default: 'default value if applicable'
description: 'Script to execute'
runs:
using: 'node12'
main: 'dist/index.js'
8 changes: 7 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as core from '@actions/core'
import * as tc from '@actions/tool-cache'
import * as exec from '@actions/exec'
import * as fs from 'fs'

async function run(): Promise<void> {
try {
Expand All @@ -10,6 +11,12 @@ async function run(): Promise<void> {
const ktPathExtractedFolder = await tc.extractZip(ktPath)
core.addPath(`${ktPathExtractedFolder}/kotlinc/bin`)
exec.exec('kotlinc', ['-version'])

const script = core.getInput('script')
if (script) {
fs.writeFileSync('script.main.kts', script)
exec.exec('kotlin', ['script.main.kts'])
}
} catch (error) {
core.setFailed(error.message)
}
Expand Down

0 comments on commit 71adbe0

Please sign in to comment.