Skip to content

Commit

Permalink
major refactor; minimize public interface; eliminate unused / duplica…
Browse files Browse the repository at this point in the history
…te code
  • Loading branch information
philwalk committed Jan 1, 2024
1 parent 1a3f713 commit 07fc12d
Show file tree
Hide file tree
Showing 57 changed files with 525 additions and 4,365 deletions.
27 changes: 13 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Provides support for expressive idioms typical of scripting languages, for writi

* Supported Scala Versions
* `scala 3.x`
* `scala 2.13`

* Tested Target environments
* `Linux`
Expand All @@ -23,7 +22,7 @@ Provides support for expressive idioms typical of scripting languages, for writi

To use `pallet` in an `SBT` project, add this dependency to `build.sbt`
```sbt
"org.vastblue" % "pallet" % "0.10.0"
"org.vastblue" % "pallet_3" % "0.10.4"
```
For `scala` or `scala-cli` scripts, see examples below.

Expand All @@ -36,8 +35,8 @@ Simplicity and Universal Portability:
```scala
#!/usr/bin/env -S scala-cli shebang

//> using lib "org.vastblue::pallet::0.10.0"
import vastblue.pallet._
//> using lib "org.vastblue::pallet::0.10.4"
import vastblue.pallet.*

printf("uname / osType / osName:\n%s\n", s"platform info: ${unameLong} / ${osType} / ${osName}")
if (isLinux) {
Expand All @@ -61,7 +60,7 @@ Example: read process command lines from `/proc/$PID/cmdline` files
```scala
#!/usr/bin/env -S scala -deprecation -cp target/scala-3.3.1/classes

import vastblue.pallet._
import vastblue.pallet.*
import vastblue.file.ProcfsPaths.cmdlines

var verbose = false
Expand Down Expand Up @@ -227,7 +226,7 @@ Some differences to be aware of between `scala-cli` scripts and conventional `sc
For a per-user classpath `atFile`, define your classpath in a file named, e.g., `/Users/username/.scala3cp`.
To include the `scala3` version of this library, for example, the `@file` might contain:
```
-classpath /Users/username/.ivy2/local/org.vastblue/pallet_3/0.10.0/jars/pallet_3.jar
-classpath /Users/username/.ivy2/local/org.vastblue/pallet_3/0.10.4/jars/pallet_3.jar
```
With this configuration, your scala 3 `shebang` line will look like this:
```scala
Expand All @@ -247,8 +246,8 @@ This example might surprise developers working in a `Windows` posix shell, since
```scala
#!/ usr / bin / env -S scala

import vastblue.pallet._
import vastblue.Platform._
import vastblue.pallet.*
import vastblue.Platform.*

object Fstab {
def main(args: Array[String]): Unit = {
Expand All @@ -266,10 +265,10 @@ object Fstab {
#!/ usr / bin / env -S scala -cli shebang

//> using scala "3.3.1"
//> using lib "org.vastblue::pallet::0.10.0"
//> using lib "org.vastblue::pallet::0.10.4"

import vastblue.pallet._
import vastblue.Platform._
import vastblue.pallet.*
import vastblue.Platform.*

object FstabCli {
def main(args: Array[String]): Unit = {
Expand Down Expand Up @@ -298,9 +297,9 @@ Note that on Darwin, there is no `/etc/fstab` file, so the `Path#lines` extensio
#!/usr/bin/env -S scala-cli shebang

//> using scala "3.3.1"
//> using lib "org.vastblue::pallet::0.10.0"
//> using lib "org.vastblue::pallet::0.10.4"

import vastblue.pallet._
import vastblue.pallet.*

def main(args: Array[String]): Unit = {
// list child directories of "."
Expand Down Expand Up @@ -336,7 +335,7 @@ This script demonstrates a consistent, portable way to get command line argument
#!/usr/bin/env -S scala
package vastblue

import vastblue.pallet._
import vastblue.pallet.*

def main(args: Array[String]): Unit = {
// display default args
Expand Down
26 changes: 18 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
lazy val scala213 = "2.13.12"
//lazy val scala213 = "2.13.12"
lazy val scala331 = "3.3.1"
lazy val scalaVer = scala331

lazy val supportedScalaVersions = List(scala331, scala213)
lazy val supportedScalaVersions = List(scala331)
// lazy val supportedScalaVersions = List(scalaVer)

javacOptions ++= Seq("-source", "11", "-target", "11")

//ThisBuild / envFileName := "dev.env" // sbt-dotenv plugin gets build environment here
ThisBuild / scalaVersion := scalaVer
ThisBuild / version := "0.10.1"
ThisBuild / version := "0.10.4"
ThisBuild / versionScheme := Some("semver-spec")

ThisBuild / organization := "org.vastblue"
Expand Down Expand Up @@ -55,17 +57,23 @@ resolvers += Resolver.mavenLocal

publishTo := sonatypePublishToBundle.value

lazy val root = (project in file(".")).settings(
crossScalaVersions := supportedScalaVersions,
name := "pallet"
)
lazy val root = (project in file(".")).
enablePlugins(BuildInfoPlugin).
settings(
crossScalaVersions := supportedScalaVersions,
name := "pallet",
// mainClass := Some("vast.apps.ShowSysProps"),
buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion),
buildInfoPackage := "pallet", // available as "import pallet.BuildInfo"
)

libraryDependencies ++= Seq(
"org.simpleflatmapper" % "sfm-csv-jre6" % "8.2.3",
"com.github.sbt" % "junit-interface" % "0.13.3" % Test,
"org.scalatest" %% "scalatest" % "3.2.17" % Test,
"org.scalacheck" %% "scalacheck" % "1.17.0" % Test,
"io.github.chronoscala" %% "chronoscala" % "2.0.10",
"org.vastblue" % "unifile_3" % "0.2.4",
)

/*
Expand Down Expand Up @@ -100,13 +108,15 @@ scalacOptions := {
scalacOptions ++= (CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, n)) if n >= 13 =>
Seq(
"-Ytasty-reader",
"-Xsource:3",
"-Xmaxerrs",
"10",
"-Yscala3-implicit-resolution",
"-language:implicitConversions",
)
case _ => Nil
case _ =>
Nil
})

// key identifier, otherwise this field is ignored; passwords supplied by pinentry
Expand Down
2 changes: 1 addition & 1 deletion jsrc/bashPath.sc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env -S scala

//> using scala "3.3.1"
//> using lib "org.vastblue::pallet::0.9.2"
//> using lib "org.vastblue::pallet::0.10.4"

import vastblue.pallet.*

Expand Down
2 changes: 1 addition & 1 deletion jsrc/bashPathCli.sc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env -S scala-cli shebang

//> using scala "3.3.1"
//> using lib "org.vastblue::pallet::0.9.2"
//> using lib "org.vastblue::pallet::0.10.4"

import vastblue.pallet.*

Expand Down
23 changes: 8 additions & 15 deletions jsrc/classpath
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,14 @@ lazy val (psep, cp) = {
(ps, clp)
}
lazy val homedir = sys.props("user.home").replace('\\', '/')
lazy val scriptName = sys.props("script.path").norm.replaceAll(".*/", "")
lazy val scriptName = sys.props("script.path").posx.replaceAll(".*/", "")

def usage(msg: String=""): Nothing = {
if ( msg.nonEmpty ) {
printf("%s\n", msg)
}
printf("%s [<options>]\n", scriptName)
val info = Seq(
_usage(msg, Seq("[<options>]",
"-d ; list classpath directories",
"-j ; list classpath jars",
"-v ; verbose",
)
for (s <- info) {
printf("%s\n", s)
}
sys.exit(0)
))
}

// analyze classpath, selectively showing dirs, jars and bad entries
Expand All @@ -43,6 +35,7 @@ def main(args: Array[String]): Unit =
if (!dirs && !jars && !verbose) {
usage()
}
printf("scriptName[%s]\n", scriptName)
if (verbose) {
printf("psep[%s]\n", psep)
}
Expand All @@ -53,17 +46,17 @@ def main(args: Array[String]): Unit =
e
}
if (verbose) {
printf("%s\n", fname.norm)
printf("%s\n", fname.posx)
}
val p = java.nio.file.Paths.get(fname)
val isdir = p.toFile.isDirectory
val isfil = p.toFile.isFile
(isfil, isdir) match {
case (true, false) =>
if (jars) printf("jar: %s\n", p.norm)
if (jars) printf("jar: %s\n", p.posx)
case (false, true) =>
if (dirs) printf("dir: %s\n", p.norm)
if (dirs) printf("dir: %s\n", p.posx)
case _ =>
printf("%5s: %5s: %s\n", isdir, isfil, p.norm)
printf("%5s: %5s: %s\n", isdir, isfil, p.posx)
}
}
4 changes: 2 additions & 2 deletions jsrc/csvWriteRead.sc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env -S scala -cp target/scala-3.3.1/classes
//package vastblue
package vastblue.demo

import vastblue.pallet.*

Expand All @@ -19,7 +19,7 @@ object CsvWriteRead {
}

assert(testFile.isFile)
printf("\n# filename: %s\n", testFile.norm)
printf("\n# filename: %s\n", testFile.posx)
// display file text lines
for ((line: String, i: Int) <- testFile.lines.zipWithIndex) {
printf("%d: %s\n", i, line)
Expand Down
8 changes: 4 additions & 4 deletions jsrc/demo.sc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env -S scala @./atFile
//package vastblue.demo
package vastblue.demo

import vastblue.pallet._
import vastblue.pallet.*
import vastblue.MainArgs

object Demo {
Expand All @@ -16,10 +16,10 @@ object Demo {
// list child directories of the current working directory
val cwd: Path = ".".path
for ((p: Path) <- cwd.paths.filter { _.isDirectory }) {
printf("%s\n", p.norm)
printf("%s\n", p.posx)
}

val argv = MainArgs.prepArgs(args.toSeq)
val argv = MainArgs.prepArgv(args.toSeq)
for ((arg, i) <- argv.zipWithIndex) {
printf("%2d: [%s]\n", i, arg)
}
Expand Down
2 changes: 1 addition & 1 deletion jsrc/dirlist.sc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import vastblue.pallet.*

def main(args: Array[String]): Unit = {
".".path.paths.filter { _.isDirectory }.foreach { (p: Path) => printf("%s\n", p.norm) }
".".path.paths.filter { _.isDirectory }.foreach { (p: Path) => printf("%s\n", p.posx) }
}
2 changes: 1 addition & 1 deletion jsrc/dummyCmdline.sc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env -S scala

import vastblue.script.legalMainClass
import vastblue.ScriptInfo.legalMainClass

// test code to skip java portion of "sun.command.line" property
// especially legalMainClass(s)
Expand Down
14 changes: 7 additions & 7 deletions jsrc/find.sc
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env -S scala @${HOME}/.scala3cp
//package vastblue.demo
package vastblue.demo

// shebang line error on OSX/Darwin due to non-gnu /usr/bin/env
// hash bang line error on OSX/Darwin due to non-gnu /usr/bin/env
// portable way to set classpath:
// export SCALA_OPTS="@/Users/username/.scala3cp -save"
// .scala3cp contains '-cp <path-to-pallet.jar>'
// .scala3cp contains '-cp <path-to-thislib.jar>'

import vastblue.pallet._
import vastblue.pallet.*

// partial implementation of the gnu find utility
object Find {
Expand All @@ -18,7 +18,7 @@ object Find {
for (f <- walkTree(dir.toFile, maxdepth = parms.maxdepth)) {
val p = f.toPath
if (parms.matches(p)) {
printf("%s\n", p.relpath.posixpath)
printf("%s\n", p.relpath.posx)
}
}
}
Expand Down Expand Up @@ -51,15 +51,15 @@ object Find {
}

/**
* prepArgs returns `argv`, equivalent to C language main arguments vector.
* prepArgv returns `argv`, equivalent to C language main arguments vector.
* jvm main#args and `argv.tail` identical if no `glob` args are passed.
* `argv` always delivers unexpanded glob arguments, unlike main#args.
*/

def parseArgs(_args: Seq[String]): CmdParams = {
val cmdParms = new CmdParams()

val argv = prepArgs(_args) // derive C-style argv
val argv = prepArgv(_args) // derive C-style argv
val thisScript = argv.head
var args = argv.tail.toList

Expand Down
2 changes: 1 addition & 1 deletion jsrc/fstab.sc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ object Fstab {
val p = Paths.get("/etc/fstab")
// format: off
printf("env: %-10s| posixroot: %-12s| %-22s| %d lines\n",
uname("-o"), posixroot, p.norm, p.lines.size)
uname("-o"), posixroot, p.posx, p.lines.size)
}
}
4 changes: 2 additions & 2 deletions jsrc/fstabCli.sc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env -S scala-cli shebang

//> using scala "3.3.1"
//> using lib "org.vastblue::pallet::0.9.2"
//> using lib "org.vastblue::pallet::0.10.4"

import vastblue.pallet._

Expand All @@ -12,7 +12,7 @@ object FstabCli {
val p = Paths.get("/etc/fstab")
// format: off
printf("env: %-10s| posixroot: %-12s| %-22s| %d lines\n",
uname("-o"), posixroot, p.norm, p.lines.size)
uname("-o"), posixroot, p.posx, p.lines.size)
}
}
FstabCli.main(args)
4 changes: 2 additions & 2 deletions jsrc/globArg.sc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env -S scala -cp target/scala-3.3.1/classes
//package vastblue.demo
package vastblue.demo

import vastblue.pallet.*

Expand All @@ -9,7 +9,7 @@ object GlobArg {
for ((a, i) <- args.zipWithIndex) {
printf(" %2d: [%s]\n", i, a)
}
val argv = prepArgs(args.toSeq)
val argv = prepArgv(args.toSeq)
for ((a, i) <- argv.zipWithIndex) {
printf(" %2d: [%s]\n", i, a)
}
Expand Down
16 changes: 16 additions & 0 deletions jsrc/isWinJunctionPoint.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env -S scala
package vastblue.demo

import vastblue.pallet.*

object IsWinJunctionPoint {
def main(args: Array[String]): Unit =
for (arg <- args){
val (flag, target) = vastblue.Stuff.isWindowsJunction(arg)
if (flag) {
printf("points to [%s]\n", target)
} else {
printf("not a junction point\n")
}
}
}
Loading

0 comments on commit 07fc12d

Please sign in to comment.