question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Suggestion: Self-bootstrapping scripts

See original GitHub issue

I was thinking if there was a way to make the scripts I write more standalone in a sense that I could share it with someone without either having to explain how to install kotlin&kscript or producing a binary they can’t read/edit. Much like you can do today with shell/python scripts.

I believe I have found a fairly practical way to achieve this: by wrapping an existing script with a specific header and footer it can be made into a valid shell script and kotlin script at the same time. No unpacking step needed! See example below.

That means original script can be rewritten once, replacing it, and further iteration can happen with the bootsrapping footer in place. IntelliJ support is available for both kotlin and bash parts of the script with kscript --idea script.kts thanks to the intellij’s language injection feature. Having the modifying happen only once also has the benefit that the shell code despite being auto-generated is ok to modify, as it won’t be overwritten. See example below where I choose to propagate the script file name to kotlin.

So I was thinking, that it would be great to have this built into kscript on an “opt-in” basis, e.g. kscript --bootstrapify script.kts ^ prepends/appends things to script.kts making it shareable

P.S.: Happy to work on a PR if this seems ok to add

Example of self-bootstrapping script.kts: (try opening with kscript --idea to see how IDE experience is uncompromised; installing “BashSupport” pluging may be required for bash support)

#!/bin/bash
//usr/bin/env cat << '__EOF_KOTLIN' >/dev/null

import org.intellij.lang.annotations.Language


println("Hello from kotlin!")
if (System.console() == null) {
    print(generateSequence(::readLine).joinToString("\n") + " | ")
}
println("${System.getenv("KSCRIPT_FILE")} ${args.toList().joinToString(" ")}")





//DEPS org.jetbrains:annotations-java5:16.0.2
@Language("sh")
private val __SHELL_BOOTSTRAP = """
__EOF_KOTLIN
    function in_path() { command -v "$1" >/dev/null 2>&1; }
    in_path kscript || {
        function echo_and_eval() { echo "$ $@" 1>&2; eval "$@" 1>&2; }
        in_path sdk || {
            curl "https://get.sdkman.io" | bash 1>&2 && \
                echo_and_eval source "$"SDKMAN_DIR/bin/sdkman-init.sh
        }
        in_path kotlin || echo_and_eval sdk install kotlin
        in_path gradle || echo_and_eval sdk install gradle
        echo_and_eval sdk install kscript
    }
    export KSCRIPT_FILE="$0";
    kscript $0 "$@";
    exit $?
"""

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
EugeneSuslacommented, Nov 30, 2018

It’s nice to see it that short! Will use the latest one as the template when implementing then

DO NOT TOUCH!

I was thinking it should maybe say the opposite (or just don’t say either way), as we’ll never overwrite custom changes there. There are some things that can be only done in bash, capturing script name being one example.

Should it write the modified script to stdout or do an inplace file replacement?

I think in-place replacement is the only right way - we should encourage people to not duplicate their script to a new file as much as we can. Without clear guidance I can imagine thinking this is basically just another flavor of --package and redirecting output to a new file is expected of me.

–add-boostrap-header

Seems pretty clear, if a bit long. I’ll go with it then

no need to save stdin to a file first

yeah, meant to say the script itself, not stdin 😃 we’re on the same page here

0reactions
EugeneSuslacommented, Dec 6, 2018

Closing as the implementation is now merged

Read more comments on GitHub >

github_iconTop Results From Across the Web

Register Clients with a Bootstrap Script - SUSE Documentation
Registering clients with a bootstrap script gives you control over parameters, and can help if you have to register a large number of...
Read more >
Use scripts to automate routine execution of tasks - Eric J. Ma
If you find yourself executing the exact same commands over and over and over, you should probably put them together into a bash,...
Read more >
Bootstrap scripts to setup workstations - GitHub
Bootstrap. This repository hosts a BASH bootstrap script that can be used to setup workstations and personal computers running on either Linux or...
Read more >
Self-hosting the Appcues SDK Bootstrap Script
This article covers self-hosting the Appcues SDK's Bootstrap script and configuring it to proxy to the Appcues API endpoints.
Read more >
Bootstrapping Business Rules - ServiceNow Community
Create a Script Include that corresponds to each specific stage/state of the executing Object; say, Incident, Change Request, or Custom Object ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found