igor-petruk / scriptisto

A language-agnostic "shebang interpreter" that enables you to write scripts in compiled languages.
Apache License 2.0
943 stars 24 forks source link

Multiple shebangs are not processed with replace_shebang_with #34

Open W1M0R opened 2 years ago

W1M0R commented 2 years ago

First of all, thanks for this project!

The following is a working example that uses nix-shell to run scriptisto as interpreter. nix-shell will install all the dependencies required to run the script (e.g. scriptisto, pkg-config, clang and glibmm) and then execute scriptisto. You can read more about that here: https://nixos.org/manual/nix/stable/command-ref/nix-shell.html#use-as-a--interpreter

Two shebangs are used, one to specify nix-shell as the script runner, and one that configures nix-shell to use scriptisto as the interpreter. The second shebang is "wrapped" in C++ comments, to avoid compile errors. Ideally, scriptisto should have an option to indicate other shebangs that also need to be replaced with comments, e.g. other_shebangs_prefixes: #!nix-shell #!some-other-prefix.

#!/usr/bin/env nix-shell
/*
#!nix-shell -i scriptisto -p scriptisto pkg-config clang_9 glibmm_2_68
*/

#include <glibmm.h>
#include <iostream>

// scriptisto-begin
// script_src: main.cpp
// build_cmd: clang++ -std=c++17 -O2 main.cpp `pkg-config --libs --cflags glibmm-2.68` -o ./script
// replace_shebang_with: //
// scriptisto-end

int main(int argc, char *argv[]) {
  const auto user = Glib::getenv("USER");
  std::cout << "Hello, C++! Current user: " << user << std::endl;
  return 0;
}

The Nix derivation that installs scriptisto looks like this:

{ pkgs }:

pkgs.stdenv.mkDerivation rec {
    pname = "scriptisto";

    version = "0.6.14";

    owner = "igor-petruk";

    src = pkgs.fetchurl {
        url = "https://github.com/${owner}/${pname}/releases/download/v${version}/${pname}.bz2";
        sha256 = "YP00pnvADuRsuzKIe988L3zI/joHfYicaQTNT4x+Q00=";
    };

    sourceRoot = ".";

    unpackCmd = ''
        bzcat $src > ./scriptisto
    '';

    nativeBuildInputs = [
        pkgs.autoPatchelfHook
        pkgs.bzip2
    ];

    installPhase = ''
        runHook preInstall
        ls -al
        install -m755 -D scriptisto $out/bin/scriptisto
        runHook postInstall
    '';

    meta = with pkgs.lib; {
        license = licenses.asl20;
        homepage = "https://github.com/igor-petruk/scriptisto";
        description = "A language-agnostic shebang interpreter that enables you to write scripts in compiled languages.";
        maintainers = with maintainers; [ igor-petruk ];
    };
}
igor-petruk commented 1 year ago

Hi,

I did not have much time to look at the project, but now I will get back to it's development.

The initial "replace shebang" feature is more of a convenience rather than all powerful tool. For example, if I extend it as suggested, the next request could be to replace each shebang with different strings.

At that point I will be reimplementing sed -i.

Could using sed in build_cmd solve the problem as well? If yes, what I could do is to update the wiki with a recipe.