Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple shebangs are not processed with replace_shebang_with #34

Open
W1M0R opened this issue Feb 9, 2022 · 1 comment
Open

Multiple shebangs are not processed with replace_shebang_with #34

W1M0R opened this issue Feb 9, 2022 · 1 comment

Comments

@W1M0R
Copy link

W1M0R commented Feb 9, 2022

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
Copy link
Owner

igor-petruk commented Dec 8, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants