From 336d3ab5c60cbc4cc28d83e39493227b26d7036b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wro=C5=84ski?= Date: Sun, 22 May 2022 17:50:38 +0200 Subject: [PATCH] Build ls app using scala-cli --- .github/scripts/package.sc | 25 +++++++++++++++++++++ .github/workflows/ci.yml | 46 ++++++++++++++++++++++++++++++++++++++ .scalafmt.conf | 2 ++ Ls.scala | 10 +++++++++ TestLs.test.scala | 27 ++++++++++++++++++++++ 5 files changed, 110 insertions(+) create mode 100644 .github/scripts/package.sc create mode 100644 .github/workflows/ci.yml create mode 100644 .scalafmt.conf create mode 100644 Ls.scala create mode 100644 TestLs.test.scala diff --git a/.github/scripts/package.sc b/.github/scripts/package.sc new file mode 100644 index 0000000..9d8b508 --- /dev/null +++ b/.github/scripts/package.sc @@ -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 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..93a0729 --- /dev/null +++ b/.github/workflows/ci.yml @@ -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 + ) diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 0000000..c38b9e4 --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1,2 @@ +version = 3.5.2 +runner.dialect = scala3 \ No newline at end of file diff --git a/Ls.scala b/Ls.scala new file mode 100644 index 0000000..293e523 --- /dev/null +++ b/Ls.scala @@ -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") diff --git a/TestLs.test.scala b/TestLs.test.scala new file mode 100644 index 0000000..fdf2056 --- /dev/null +++ b/TestLs.test.scala @@ -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)) + } + } +}