Skip to content

Commit

Permalink
improve shell autocompletions
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkyAI committed Mar 14, 2021
1 parent 9ac7e69 commit 903fab4
Show file tree
Hide file tree
Showing 18 changed files with 95 additions and 78 deletions.
5 changes: 1 addition & 4 deletions .run/CLI autocompletes bash.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
<configuration default="false" name="CLI autocompletes bash" type="JetRunConfigurationType">
<module name="voodoo-parent.voodoo.main" />
<option name="VM_PARAMETERS" value="" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="PROGRAM_PARAMETERS" value="--generate-completion=bash" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="MAIN_CLASS_NAME" value="Main" />
<option name="WORKING_DIRECTORY" value="file://$PROJECT_DIR$/samples" />
<envs>
<env name="_VOODOO_COMPLETE" value="bash" />
</envs>
<method v="2">
<option name="Make" enabled="true" />
</method>
Expand Down
18 changes: 0 additions & 18 deletions .run/CLI autocompletes fish.run.xml

This file was deleted.

5 changes: 1 addition & 4 deletions .run/CLI autocompletes zsh.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
<configuration default="false" name="CLI autocompletes zsh" type="JetRunConfigurationType">
<module name="voodoo-parent.voodoo.main" />
<option name="VM_PARAMETERS" value="" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="PROGRAM_PARAMETERS" value="--generate-completion=zsh" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="MAIN_CLASS_NAME" value="Main" />
<option name="WORKING_DIRECTORY" value="file://$PROJECT_DIR$/samples" />
<envs>
<env name="_VOODOO_COMPLETE" value="zsh" />
</envs>
<method v="2">
<option name="Make" enabled="true" />
</method>
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,20 @@ bash, zsh and fish Autocomplete
the autocompletions should be regenerated manually on voodoo update

```
# generate wrapper and shell scripts
java -jar voodoo.jar generateWrapper
# generate wrapper and shell scripts
java -jar voodoo.jar init project
# generating autocompletion for bash
_VOODOO_COMPLETE=bash ./voodoo > ~/voodoo-completion.sh
source ~/voodoo-completion.sh
source <(./voodoo --generate-completion=bash)
# generating autocompletion for zsh
_VOODOO_COMPLETE=zsh ./voodoo > ~/voodoo-completion.sh
source <(./voodoo --generate-completion=zsh)
# generating autocompletion for fish
_VOODOO_COMPLETE=fish ./voodoo > ~/.config/fish/voodoo-completion.fish
./voodoo --generate-completion=fish | source
# generating for a custom shell script name
VOODOO_COMMAND=voodoo-dev ./voodoo-dev --generate-completion=fish | source
```

Expand Down
17 changes: 8 additions & 9 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,20 @@ bash and zsh autocomplete
the autocompletions should be regenerated manually on voodoo update

```
# creating a alias
alias voodoo='java -jar voodoo.jar'
# generate wrapper and shell scripts
java -jar voodoo.jar init project
# generating autocompletion for bash
_VOODOO_COMPLETE=bash voodoo > ~/voodoo-completion.sh
source ~/voodoo-completion.sh
source <(./voodoo --generate-completion=bash)
# generating autocompletion for zsh
_VOODOO_COMPLETE=zsh voodoo > ~/voodoo-completion.sh
source <(./voodoo --generate-completion=zsh)
# bash example
alias voodoo='java -jar voodoo.jar'
_VOODOO_COMPLETE=bash voodoo > voodoo-autocomplete.sh
source voodoo-autocomplete.sh
# generating autocompletion for fish
./voodoo --generate-completion=fish | source
# generating for a custom shell script name
VOODOO_COMMAND=voodoo-dev ./voodoo-dev --generate-completion=fish | source
```

if you use a different alias then use the correct environment variable to generate completions
Expand Down
2 changes: 1 addition & 1 deletion samples/voodoo-dev
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
java -jar $DIR/../voodoo/build/libs/voodoo-0.6.0-local-all.jar $@
VOODOO_COMMAND='voodoo-dev' java -jar $DIR/../voodoo/build/libs/voodoo-0.6.0-local-all.jar $@
6 changes: 5 additions & 1 deletion samples/voodoo-dev.bat
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
java -jar ..\voodoo\build\libs\voodoo-0.6.0-local-all.jar %*
@echo off
setlocal
set VOODOO_COMMAND=voodoo-dev
java -jar ..\voodoo\build\libs\voodoo-0.6.0-local-all.jar %*
endlocal
2 changes: 1 addition & 1 deletion samples/voodoo-release
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
java -jar $DIR/wrapper/wrapper.jar $@
VOODOO_COMMAND='voodoo-release' java -jar $DIR/wrapper/wrapper.jar $@
6 changes: 5 additions & 1 deletion samples/voodoo-release.bat
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
java -jar wrapper\wrapper.jar %*
@echo off
setlocal
set VOODOO_COMMAND=voodoo-release
java -jar wrapper\wrapper.jar %*
endlocal
2 changes: 1 addition & 1 deletion samples/wrapper-dev
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cp -f $DIR/../wrapper/build/libs/wrapper-0.6.0-local-all.jar $DIR/wrapper/wrapper-dev.jar
java -jar $DIR/wrapper/wrapper-dev.jar $@
VOODOO_COMMAND='wrapper-dev' java -jar $DIR/wrapper/wrapper-dev.jar $@
Binary file added samples/wrapper/wrapper-dev.jar
Binary file not shown.
5 changes: 4 additions & 1 deletion voodoo/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ object Main {
System.setProperty(DEBUG_PROPERTY_NAME, DEBUG_PROPERTY_VALUE_ON)
// DebugProbes.install()

VoodooCommand().main(args)
// System.getenv().forEach { (k, v) -> System.err.println("$k: $v") }

val voodooCommandName = System.getenv("VOODOO_COMMAND") ?: "voodoo"
VoodooCommand(voodooCommandName).main(args)

// DebugProbes.dumpCoroutines(System.out)
// DebugProbes.uninstall()
Expand Down
5 changes: 4 additions & 1 deletion voodoo/src/main/kotlin/voodoo/cli/CompileCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package voodoo.cli
import com.eyeem.watchadoin.Stopwatch
import com.eyeem.watchadoin.saveAsHtml
import com.eyeem.watchadoin.saveAsSvg
import com.github.ajalt.clikt.completion.CompletionCandidates
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.requireObject
import com.github.ajalt.clikt.parameters.arguments.argument
Expand All @@ -22,6 +23,7 @@ import voodoo.pack.MetaPack
import voodoo.pack.VersionPack
import voodoo.util.VersionComparator
import voodoo.util.json
import voodoo.util.toRelativeUnixPath

class CompileCommand() : CliktCommand(
name = "compile",
Expand All @@ -33,7 +35,8 @@ class CompileCommand() : CliktCommand(

val packFiles by argument(
"PACK_FILE",
"path to .${VersionPack.extension} file"
"path to .${VersionPack.extension} file",
completionCandidates = CompletionCandidates.Custom.fromStdout("find **/*.${VersionPack.extension}")
).file(mustExist = true, canBeFile = true, canBeDir = false)
.multiple()
.validate { files ->
Expand Down
5 changes: 4 additions & 1 deletion voodoo/src/main/kotlin/voodoo/cli/PackageCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package voodoo.cli
import com.eyeem.watchadoin.Stopwatch
import com.eyeem.watchadoin.saveAsHtml
import com.eyeem.watchadoin.saveAsSvg
import com.github.ajalt.clikt.completion.CompletionCandidates
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.requireObject
import com.github.ajalt.clikt.parameters.arguments.argument
Expand All @@ -18,6 +19,7 @@ import mu.withLoggingContext
import voodoo.Pack
import voodoo.data.lock.LockPack
import voodoo.pack.MetaPack
import voodoo.pack.VersionPack
import voodoo.util.SharedFolders
import voodoo.util.VersionComparator
import java.io.File
Expand All @@ -31,7 +33,8 @@ class PackageCommand(): CliktCommand(

val metaPackFile by argument(
"META_FILE",
"path to ${MetaPack.FILENAME} file"
"path to ${MetaPack.FILENAME} file",
completionCandidates = CompletionCandidates.Custom.fromStdout("find **/${MetaPack.FILENAME}")
).file(mustExist = true, canBeFile = true, canBeDir = false)
.validate { file ->
require(file.name == MetaPack.FILENAME) {
Expand Down
7 changes: 5 additions & 2 deletions voodoo/src/main/kotlin/voodoo/cli/VoodooCommand.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package voodoo.cli

import ch.qos.logback.classic.Level
import com.github.ajalt.clikt.completion.CompletionCommand
import com.github.ajalt.clikt.completion.completionOption
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.subcommands
import com.github.ajalt.clikt.parameters.options.default
Expand All @@ -18,14 +20,15 @@ import voodoo.util.SharedFolders
import voodoo.voodoo.GeneratedConstants
import java.io.File

class VoodooCommand(invocation: String = "voodoo") : CliktCommand(
class VoodooCommand(invocation: String) : CliktCommand(
name = invocation,
help = "modpack building magic",
// allowMultipleSubcommands = true
// invokeWithoutSubcommand = true
) {
private val logger = KotlinLogging.logger {}
init {
completionOption()
versionOption(GeneratedConstants.FULL_VERSION)
subcommands(
InitCommand(),
Expand All @@ -35,7 +38,7 @@ class VoodooCommand(invocation: String = "voodoo") : CliktCommand(
LaunchCommand(),
GenerateSchemaCommand(),
UpdateCommand(),
ImportCurseCommand()
ImportCurseCommand(),
)
}

Expand Down
33 changes: 30 additions & 3 deletions voodoo/src/main/kotlin/voodoo/cli/init/InitProjectCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class InitProjectCommand : CliktCommand(

rootDir.resolve("voodoo.bat").let { batFile ->
if(batFile.exists()) {
logger.info { "$batFile already exists, not overwriting" }
logger.error { "$batFile already exists, not overwriting" }
return@let
}

Expand All @@ -72,7 +72,7 @@ class InitProjectCommand : CliktCommand(

rootDir.resolve("voodoo").let { bashFile ->
if(bashFile.exists()) {
logger.info { "$bashFile already exists, not overwriting" }
logger.error { "$bashFile already exists, not overwriting" }
return@let
}

Expand All @@ -86,10 +86,37 @@ class InitProjectCommand : CliktCommand(
logger.info { "generated $bashFile" }
}

rootDir.resolve("shell_completion").let { bashFile ->
if(bashFile.exists()) {
logger.error { "$bashFile already exists, not overwriting" }
return@let
}

bashFile.writeText("""
#!/usr/bin/env bash
DIR="${'$'}( cd "${'$'}( dirname "${'$'}{BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
case ${'$'}(basename ${'$'}(readlink -f "/proc/${'$'}PPID/exe")) in
bash)
source <(${'$'}DIR/voodoo --generate-completion=bash)
;;
fish)
${'$'}DIR/voodoo --generate-completion=fish | source
;;
zsh)
source <(${'$'}DIR/voodoo --generate-completion=zsh)
;;
esac
""".trimIndent())
bashFile.setExecutable(true)
logger.info { "generated $bashFile" }
}

//TODO: move to setup command
rootDir.resolve(".gitignore").let { gitignoreFile ->
if(gitignoreFile.exists()) {
logger.info { "$gitignoreFile already exists, not overwriting" }
logger.error { "$gitignoreFile already exists, not overwriting" }
return@let
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package voodoo.cli.launch
import com.eyeem.watchadoin.Stopwatch
import com.eyeem.watchadoin.saveAsHtml
import com.eyeem.watchadoin.saveAsSvg
import com.github.ajalt.clikt.completion.CompletionCandidates
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.requireObject
import com.github.ajalt.clikt.parameters.arguments.argument
Expand Down Expand Up @@ -32,7 +33,8 @@ class LaunchMultiMCCommand(): CliktCommand(

val packFile by argument(
"PACK_FILE",
"path to .${VersionPack.extension} file"
"path to .${VersionPack.extension} file",
completionCandidates = CompletionCandidates.Custom.fromStdout("find **/*${VersionPack.extension}")
).file(mustExist = true, canBeFile = true, canBeDir = false)
.validate { file ->
require(file.name.endsWith("." + VersionPack.extension)) {
Expand Down
Loading

0 comments on commit 903fab4

Please sign in to comment.