Skip to content

Commit

Permalink
Build ls app using scala-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
lwronski committed May 24, 2022
1 parent 603f4d8 commit 336d3ab
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/scripts/package.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//> using scala "3.1.2"
//> using lib "com.lihaoyi::os-lib:0.8.0"
import scala.util.Properties

val platformSuffix: String = {
val os =
if (Properties.isWin) "pc-win32"
else if (Properties.isLinux) "pc-linux"
else if (Properties.isMac) "apple-darwin"
else sys.error(s"Unrecognized OS: ${sys.props("os.name")}")
os
}
val artifactsPath = os.Path("artifacts", os.pwd)
val destPath =
if (Properties.isWin) artifactsPath / s"ls-$platformSuffix.exe"
else artifactsPath / s"ls-$platformSuffix"
val scalaCLILauncher =
if (Properties.isWin) "scala-cli.bat" else "scala-cli"

os.makeDir(artifactsPath)
os.proc(scalaCLILauncher, "package", ".", "-o", destPath, "--native-image")
.call(cwd = os.pwd)
.out
.text()
.trim
46 changes: 46 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: CI
on:
push:
branches:
- main
tags:
- "v*"
pull_request:

jobs:
build:
runs-on: ${{ matrix.OS }}
strategy:
matrix:
OS: ["ubuntu-latest", "macos-latest", "windows-latest"]
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: coursier/cache-action@v6.3
- uses: VirtusLab/scala-cli-setup@v0.1
- run: scala-cli test .
- name: Package app
run: scala-cli .github/scripts/package.sc
- uses: actions/upload-artifact@v3
with:
name: launchers
path: artifacts
if-no-files-found: error
retention-days: 2

format:
runs-on: "ubuntu-latest"
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: coursier/cache-action@v6.3
- uses: VirtusLab/scala-cli-setup@v0.1
- name: Scalafmt check
run: |
scala-cli fmt --check . || (
echo "To format code run"
echo " scala-cli fmt ."
exit 1
)
2 changes: 2 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version = 3.5.2
runner.dialect = scala3
10 changes: 10 additions & 0 deletions Ls.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//> using scala "3.1.2"
//> using lib "com.lihaoyi::os-lib:0.8.0"

@main def hello(args: String*) =
val path = args.headOption match
case Some(p) => os.Path(p, os.pwd)
case _ => os.pwd

if (os.isDir(path)) println(os.list(path).mkString(","))
else System.err.println("Expected directory path as an input")
27 changes: 27 additions & 0 deletions TestLs.test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//> using lib "org.scalameta::munit::0.7.27"

import scala.util.Properties

class TestsLs extends munit.FunSuite {
test("ls") {
// prepare test directory
val tempDir = os.temp.dir()
// create files
val expectedFiles = Seq("Ls", "Hello").map(tempDir / _)
expectedFiles.foreach(os.write(_, "Hello"))

// check
val scalaCLILauncher =
if (Properties.isWin) "scala-cli.bat" else "scala-cli"
val foundFiles =
os.proc(scalaCLILauncher, "Ls.scala", "--", tempDir)
.call()
.out
.text()
.trim

expectedFiles.map(_.toString).foreach { file =>
assert(foundFiles.contains(file))
}
}
}

0 comments on commit 336d3ab

Please sign in to comment.