Skip to content

Commit

Permalink
feat: Refactor, Change build info, Command Completion, pwd, whoami, c…
Browse files Browse the repository at this point in the history
…hdir
  • Loading branch information
chun-awa committed Aug 21, 2024
1 parent 23dc4d6 commit a5890a7
Show file tree
Hide file tree
Showing 16 changed files with 432 additions and 28 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# LMFSBox
> Better replacement of HOJVBOX
> A simple toolbox
### How to build?
```bash
./gradlew shadowJar
./gradlew build
```
27 changes: 19 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import com.palantir.gradle.gitversion.VersionDetails
import java.util.Date

plugins {
kotlin("jvm") version "2.0.20-RC2"
id("com.github.johnrengelman.shadow") version "8.1.1"
id("com.palantir.git-version") version "3.1.0"
application
}

Expand All @@ -15,8 +17,10 @@ repositories {

dependencies {
testImplementation(kotlin("test"))
implementation(kotlin("reflect"))
implementation("org.fusesource.jansi:jansi:2.4.1")
implementation("com.github.ajalt.clikt:clikt:4.4.0")
implementation("org.jline:jline:3.26.3")
}

tasks.test {
Expand All @@ -27,15 +31,22 @@ kotlin {
jvmToolchain(21)
}

val versionDetails: groovy.lang.Closure<VersionDetails> by extra

tasks.withType<ProcessResources> {
val resourceTargets = listOf("META-INF/lmfsbox.properties", "META-INF/lmfsbox.license.txt", "META-INF/lmfsbox.copyright.txt")
val replaceProperties = mapOf(
Pair("gradle", gradle),
Pair("project", project),
Pair("date", Date()),
Pair("building", "Gradle v${gradle.gradleVersion} (JavaToolchain ${java.toolchain.languageVersion.get()}) on ${System.getProperty("os.arch")}"),
Pair("licenseText", rootProject.file("LICENSE").readText()),
Pair("copyrightText", rootProject.file("COPYRIGHT").readText())
val resourceTargets = listOf(
"META-INF/lmfsbox.properties",
"META-INF/lmfsbox.license.txt",
"META-INF/lmfsbox.copyright.txt"
)
val replaceProperties = mapOf<String, Any?>(
"gradle" to gradle,
"project" to project,
"date" to Date(),
"building" to "Gradle v${gradle.gradleVersion} (JavaToolchain ${java.toolchain.languageVersion.get()}) ${System.getProperty("os.arch").uppercase()}",
"licenseText" to rootProject.file("LICENSE").readText(),
"copyrightText" to rootProject.file("COPYRIGHT").readText(),
"gitVersion" to versionDetails()
)
filesMatching(resourceTargets) {
expand(replaceProperties)
Expand Down
99 changes: 81 additions & 18 deletions src/main/kotlin/io/github/lmfs/box/LMFSBox.kt
Original file line number Diff line number Diff line change
@@ -1,56 +1,119 @@
package io.github.lmfs.box

import com.github.ajalt.clikt.testing.test
import io.github.lmfs.box.commands.Commands
import com.github.ajalt.mordant.rendering.AnsiLevel
import io.github.lmfs.box.command.Commands
import io.github.lmfs.box.util.makeJLineTreeCompleter
import io.github.lmfs.box.util.space
import org.fusesource.jansi.Ansi
import org.fusesource.jansi.Ansi.ansi
import org.jline.reader.EndOfFileException
import org.jline.reader.LineReaderBuilder
import org.jline.reader.UserInterruptException
import org.jline.terminal.TerminalBuilder
import java.util.*


object LMFSBox {

val license = this@LMFSBox::class.java.classLoader.getResourceAsStream("META-INF/lmfsbox.license.txt")?.readAllBytes()?.toString(Charsets.UTF_8) ?: ""
val copyright = this@LMFSBox::class.java.classLoader.getResourceAsStream("META-INF/lmfsbox.copyright.txt")?.readAllBytes()?.toString(Charsets.UTF_8) ?: ""
@JvmStatic
val LICENSE = this.javaClass.classLoader
.getResourceAsStream("META-INF/lmfsbox.license.txt")
?.readAllBytes()
?.toString(Charsets.UTF_8)
?: ""

@JvmStatic
val COPYRIGHT = this.javaClass.classLoader
.getResourceAsStream("META-INF/lmfsbox.copyright.txt")
?.readAllBytes()
?.toString(Charsets.UTF_8)
?: ""

val properties = Properties().apply {
load(this@LMFSBox::class.java.classLoader.getResourceAsStream("META-INF/lmfsbox.properties"))
@JvmStatic
val PROPERTIES = Properties().also {
it.load(
this.javaClass.classLoader.getResourceAsStream("META-INF/lmfsbox.properties")
)
}

val version = properties["lmfsbox.version"]
val build = properties["lmfsbox.build"]
val buildDate = properties["lmfsbox.build.date"]
@JvmStatic
val VERSION = PROPERTIES["lmfsbox.version"]

@JvmStatic
val BUILD = PROPERTIES["lmfsbox.build"]

@JvmStatic
val BUILD_DATE = PROPERTIES["lmfsbox.build.date"]

@JvmStatic
val GIT_HASH = PROPERTIES["lmfsbox.git.hash"]

@JvmStatic
val GIT_BRANCH = PROPERTIES["lmfsbox.git.branch"]

fun main(args: Array<String>) {
if (args.isNotEmpty()) return runCommand(args.toList())
if (args.isNotEmpty())
return runCommand(args.toList())

println(
ansi()
.fgBrightBlue().bold().a("LMFSBox").reset().space(1)
.fgBrightMagenta().a("$version").reset().space(1)
.fgYellow().a("($buildDate)").reset().space(1)
.fgBrightYellow().a("[$build]").reset()
.fgBrightMagenta().a("$VERSION").reset().space(1)
.fgYellow().a("(commits/$GIT_BRANCH:$GIT_HASH, $BUILD_DATE)").reset().space(1)
.fgBrightYellow().a("[$BUILD]").reset()
)

println(
ansi().a(Ansi.Attribute.ITALIC)
.a("Type \"help\", \"copyright\" or \"license\" for more information.")
)
println(ansi().a(Ansi.Attribute.ITALIC).a("Type \"help\", \"copyright\", \"credits\" or \"license\" for more information."))
val builder = LineReaderBuilder.builder()
.terminal(
TerminalBuilder.builder()
.system(true)
.jansi(true)
.dumb(true)
.build()
)
.completer(Commands.makeJLineTreeCompleter())
.build()
while (true) {
print(ansi().fgGreen().a(">>>").space(1).reset())
val input = readln()
try {
val input = builder.readLine(ansi().fgGreen().a(">>>").space(1).reset().toString())
runCommand(input)
} catch (exception: EndOfFileException) {
break
} catch (exception: UserInterruptException) {
break
} catch (throwable: Throwable) {
throwable.printStackTrace(System.err)
}
}
}

fun runCommand(input: String) {
val result = Commands.test(input, includeSystemEnvvars = true)
val result = Commands.test(
input,
includeSystemEnvvars = true,
ansiLevel = AnsiLevel.TRUECOLOR,
height = Int.MAX_VALUE,
width = Int.MAX_VALUE
)
print(result.output)
}

fun runCommand(input: List<String>) {
val result = Commands.test(input, includeSystemEnvvars = true)
val result = Commands.test(
input,
includeSystemEnvvars = true,
ansiLevel = AnsiLevel.TRUECOLOR,
height = Int.MAX_VALUE,
width = Int.MAX_VALUE
)
print(result.output)
}

}

fun main(args: Array<String>) = LMFSBox.main(args)
fun main(args: Array<String>) = LMFSBox.main(args)
22 changes: 22 additions & 0 deletions src/main/kotlin/io/github/lmfs/box/command/CdCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.lmfs.box.command

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.ProgramResult
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.types.path
import kotlin.io.path.Path


object CdCommand : CliktCommand("Change the working directory", name = "cd") {

private val to by argument("to").path(mustExist = false, canBeSymlink = true, canBeDir = true, canBeFile = false)

override fun run() {
val current = Path(System.getProperty("user.dir"))
val changed = current.resolve(to).toFile()
if (!changed.isDirectory) throw ProgramResult(1)
System.setProperty("user.dir", changed.toString())
throw ProgramResult(0)
}

}
40 changes: 40 additions & 0 deletions src/main/kotlin/io/github/lmfs/box/command/Commands.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.github.lmfs.box.command

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.Context
import com.github.ajalt.clikt.core.subcommands
import com.github.ajalt.clikt.output.HelpFormatter

object Commands : CliktCommand("LMFSBox", name = ">>>", hidden = true, printHelpOnEmptyArgs = false) {

init {
Commands.subcommands(
HelpCommand, CopyrightCommand, LicenseCommand, LsCommand, ExitCommand,
TimeCommand, WhoAmICommand, PwdCommand, CdCommand, EchoCommand
)
}

@JvmStatic
val COMMAND_ALIASES = mutableMapOf<String, List<String>>()

@JvmStatic
fun createRootContext(parent: Context? = null): Context =
CliktCommand::class.java.getDeclaredMethod(
"createContext",
List::class.java,
Context::class.java,
List::class.java
).apply {
isAccessible = true
}.invoke(this, emptyList<Any?>(), parent, emptyList<Any?>()) as Context


override fun allHelpParams(): List<HelpFormatter.ParameterHelp> {
return super.allHelpParams().filterNot { it is HelpFormatter.ParameterHelp.Option && "--help" in it.names }
}

override fun aliases() = COMMAND_ALIASES

override fun run() = Unit

}
12 changes: 12 additions & 0 deletions src/main/kotlin/io/github/lmfs/box/command/CopyrightCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.lmfs.box.command

import com.github.ajalt.clikt.core.CliktCommand
import io.github.lmfs.box.LMFSBox

object CopyrightCommand : CliktCommand("Show copyright information", name = "copyright") {

override fun run() {
echo(LMFSBox.COPYRIGHT)
}

}
15 changes: 15 additions & 0 deletions src/main/kotlin/io/github/lmfs/box/command/EchoCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.lmfs.box.command

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.option

object EchoCommand : CliktCommand("Write content to the standard output.", name = "echo") {

private val text by option("--text", "-T").default("")

override fun run() {
echo(text)
}

}
17 changes: 17 additions & 0 deletions src/main/kotlin/io/github/lmfs/box/command/ExitCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.lmfs.box.command

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.default
import com.github.ajalt.clikt.parameters.types.int
import kotlin.system.exitProcess

object ExitCommand : CliktCommand("Exit the program", name = "exit") {

private val code by argument("code", help = "Status code").int().default(0)

override fun run() {
exitProcess(code)
}

}
22 changes: 22 additions & 0 deletions src/main/kotlin/io/github/lmfs/box/command/HelpCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.lmfs.box.command

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.optional

object HelpCommand : CliktCommand("Show this help message", name = "help") {

private val argument by argument("command").optional()

override fun run() {
if (argument != null) {
val subCommand = Commands.registeredSubcommands()
.firstOrNull { it.commandName == argument }
?: HelpCommand
subCommand.echoFormattedHelp()
} else {
Commands.echoFormattedHelp()
}
}

}
12 changes: 12 additions & 0 deletions src/main/kotlin/io/github/lmfs/box/command/LicenseCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.lmfs.box.command

import com.github.ajalt.clikt.core.CliktCommand
import io.github.lmfs.box.LMFSBox

object LicenseCommand : CliktCommand("Show license information", name = "license") {

override fun run() {
echo(LMFSBox.LICENSE)
}

}
Loading

0 comments on commit a5890a7

Please sign in to comment.