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

Nushell support #6330

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ users)
## Clean

## Env
* [NEW] Add support for nushell in the opam env command
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* [NEW] Add support for nushell in the opam env command
* [NEW] Add support for nushell in the opam env command [#6330 @benjamin-voisin]


## Opamfile

Expand Down
1 change: 1 addition & 0 deletions src/client/opamArg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,7 @@ let shell_opt ?section cli validity =
None,"csh",SH_csh;
None,"zsh",SH_zsh;
None,"fish",SH_fish;
None,"nu",SH_nu;
Some cli2_2,"pwsh",SH_pwsh Powershell_pwsh;
Some cli2_2,"cmd",SH_cmd;
Some cli2_2,"powershell",SH_pwsh Powershell
Expand Down
2 changes: 1 addition & 1 deletion src/client/opamClient.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ let rec cygwin_menu ~bypass_checks ~interactive header =
options, internal_option, Some warning
| None ->
match OpamStd.Sys.guess_shell_compat () with
| SH_sh | SH_bash | SH_zsh | SH_csh | SH_fish ->
| SH_sh | SH_bash | SH_zsh | SH_csh | SH_fish | SH_nu ->
let default, warning =
if kind = `Msys2 && OpamSystem.resolve_command pacman = None then
internal_option, Printf.sprintf
Expand Down
8 changes: 4 additions & 4 deletions src/client/opamCommands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,7 @@ let config cli =
`Ok (OpamConfigCommand.env gt sw
~set_opamroot ~set_opamswitch
~csh:(shell=SH_csh) ~sexp ~fish:(shell=SH_fish)
~pwsh ~cmd:(shell=SH_cmd)
~pwsh ~cmd:(shell=SH_cmd) ~nu:(shell=SH_nu)
~inplace_path))
| Some `revert_env, [] ->
OpamGlobalState.with_ `Lock_none @@ fun gt ->
Expand All @@ -1429,7 +1429,7 @@ let config cli =
`Ok (OpamConfigCommand.ensure_env gt sw;
OpamConfigCommand.print_eval_env
~csh:(shell=SH_csh) ~sexp ~fish:(shell=SH_fish)
~pwsh ~cmd:(shell=SH_cmd)
~pwsh ~cmd:(shell=SH_cmd) ~nu:(shell=SH_nu)
(OpamEnv.add [] [])))
| Some `list, [] ->
OpamGlobalState.with_ `Lock_none @@ fun gt ->
Expand Down Expand Up @@ -1734,12 +1734,12 @@ let env cli =
OpamConfigCommand.env gt sw
~set_opamroot ~set_opamswitch
~csh:(shell=SH_csh) ~sexp ~fish:(shell=SH_fish)
~pwsh ~cmd:(shell=SH_cmd)
~pwsh ~cmd:(shell=SH_cmd) ~nu:(shell=SH_nu)
~inplace_path);
| true ->
OpamConfigCommand.print_eval_env
~csh:(shell=SH_csh) ~sexp ~fish:(shell=SH_fish)
~pwsh ~cmd:(shell=SH_cmd)
~pwsh ~cmd:(shell=SH_cmd) ~nu:(shell=SH_nu)
(OpamEnv.add [] [])
in
let open Common_config_flags in
Expand Down
19 changes: 16 additions & 3 deletions src/client/opamConfigCommand.ml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ let print_sexp_env output env =
aux env;
output ")\n"

let rec print_nu_env output env =
match env with
| [] -> ()
| (k,v,_)::r -> begin
Printf.ksprintf output "\t\"%s\" : \"%s\",\n" k v;
print_nu_env output r
end

let rec print_fish_env output env =
let set_arr_cmd ?(modf=fun x -> x) k v =
let v = modf @@ OpamStd.String.split v ':' in
Expand Down Expand Up @@ -183,7 +191,7 @@ let print_without_cr s =
output_string stdout s;
flush stdout

let print_eval_env ~csh ~sexp ~fish ~pwsh ~cmd env =
let print_eval_env ~csh ~sexp ~fish ~pwsh ~cmd ~nu env =
let env = (env : OpamTypes.env :> (string * string * string option) list) in
let output_normally = OpamConsole.msg "%s" in
let never_with_cr =
Expand All @@ -202,6 +210,11 @@ let print_eval_env ~csh ~sexp ~fish ~pwsh ~cmd env =
print_pwsh_env output_normally env
else if cmd then
print_cmd_env output_normally env
else if nu then begin
Printf.ksprintf never_with_cr "{\n";
print_nu_env never_with_cr env;
Printf.ksprintf never_with_cr "}"
end
else
print_env never_with_cr env

Expand Down Expand Up @@ -331,7 +344,7 @@ let ensure_env gt switch =
ignore (ensure_env_aux gt switch)

let env gt switch ?(set_opamroot=false) ?(set_opamswitch=false)
~csh ~sexp ~fish ~pwsh ~cmd ~inplace_path =
~csh ~sexp ~fish ~pwsh ~cmd ~nu ~inplace_path =
log "config-env";
let opamroot_not_current =
let current = gt.root in
Expand Down Expand Up @@ -371,7 +384,7 @@ let env gt switch ?(set_opamroot=false) ?(set_opamswitch=false)
let env =
ensure_env_aux ~set_opamroot ~set_opamswitch ~force_path gt switch
in
print_eval_env ~csh ~sexp ~fish ~pwsh ~cmd env
print_eval_env ~csh ~sexp ~fish ~pwsh ~cmd ~nu env
[@@ocaml.warning "-16"]

let subst gt fs =
Expand Down
4 changes: 2 additions & 2 deletions src/client/opamConfigCommand.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ open OpamStateTypes
val env:
'a global_state -> switch ->
?set_opamroot:bool -> ?set_opamswitch:bool ->
csh:bool -> sexp:bool -> fish:bool -> pwsh:bool -> cmd:bool ->
csh:bool -> sexp:bool -> fish:bool -> pwsh:bool -> cmd:bool -> nu:bool ->
inplace_path:bool -> unit

(** Ensures that the environment file exists in the given switch, regenerating
Expand All @@ -32,7 +32,7 @@ val ensure_env: 'a global_state -> switch -> unit

(** Like [env] but allows one to specify the precise env to print rather than
compute it from a switch state *)
val print_eval_env: csh:bool -> sexp:bool -> fish:bool -> pwsh:bool -> cmd:bool -> env -> unit
val print_eval_env: csh:bool -> sexp:bool -> fish:bool -> pwsh:bool -> cmd:bool -> nu:bool -> env -> unit

(** Display the content of all available packages variables *)
val list: 'a switch_state -> name list -> unit
Expand Down
6 changes: 5 additions & 1 deletion src/core/opamStd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1039,14 +1039,15 @@ module OpamSys = struct
fun () -> Lazy.force os

type powershell_host = Powershell_pwsh | Powershell
type shell = SH_sh | SH_bash | SH_zsh | SH_csh | SH_fish
type shell = SH_sh | SH_bash | SH_zsh | SH_csh | SH_fish | SH_nu
| SH_pwsh of powershell_host | SH_cmd

let all_shells =
[SH_sh; SH_bash;
SH_zsh;
SH_csh;
SH_fish;
SH_nu;
SH_pwsh Powershell_pwsh;
SH_pwsh Powershell;
SH_cmd]
Expand All @@ -1061,6 +1062,7 @@ module OpamSys = struct
| "zsh" -> Some SH_zsh
| "bash" -> Some SH_bash
| "fish" -> Some SH_fish
| "nu" -> Some SH_nu
| "pwsh" -> Some (SH_pwsh Powershell_pwsh)
| "dash"
| "sh" -> Some SH_sh
Expand Down Expand Up @@ -1159,6 +1161,8 @@ module OpamSys = struct
match shell with
| SH_fish ->
Some (List.fold_left Filename.concat (home ".config") ["fish"; "config.fish"])
| SH_nu ->
Some (List.fold_left Filename.concat (home ".config") ["nushell"; "env.nu"])
| SH_zsh ->
let zsh_home f =
try Filename.concat (Env.get "ZDOTDIR") f
Expand Down
2 changes: 1 addition & 1 deletion src/core/opamStd.mli
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ module Sys : sig

(** The different families of shells we know about *)
type powershell_host = Powershell_pwsh | Powershell
type shell = SH_sh | SH_bash | SH_zsh | SH_csh | SH_fish
type shell = SH_sh | SH_bash | SH_zsh | SH_csh | SH_fish | SH_nu
| SH_pwsh of powershell_host | SH_cmd

(** List of all supported shells *)
Expand Down
2 changes: 1 addition & 1 deletion src/format/opamTypes.mli
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ type pin_kind = [ `version | OpamUrl.backend ]
(** Shell compatibility modes *)
type powershell_host = OpamStd.Sys.powershell_host = Powershell_pwsh | Powershell
type shell = OpamStd.Sys.shell =
| SH_sh | SH_bash | SH_zsh | SH_csh | SH_fish | SH_pwsh of powershell_host
| SH_sh | SH_bash | SH_zsh | SH_csh | SH_fish | SH_nu | SH_pwsh of powershell_host
| SH_cmd

(** {2 Generic command-line definitions with filters} *)
Expand Down
1 change: 1 addition & 0 deletions src/format/opamTypesBase.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ let all_std_paths =

let string_of_shell = function
| SH_fish -> "fish"
| SH_nu -> "nu"
| SH_csh -> "csh"
| SH_zsh -> "zsh"
| SH_sh -> "sh"
Expand Down
22 changes: 17 additions & 5 deletions src/state/opamEnv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,8 @@ let opam_env_invocation ?root ?switch ?(set_opamswitch=false) shell =
Printf.sprintf " \"--%s=%s\"" argname
| SH_sh | SH_bash | SH_zsh | SH_csh | SH_fish ->
Printf.sprintf " '--%s=%s'" argname
| SH_nu ->
Printf.sprintf " '--%s=%s'" argname
in
if filepath_needs_quote pathval then
quoted pathval
Expand Down Expand Up @@ -917,24 +919,25 @@ let eval_string gt ?(set_opamswitch=false) switch =

(** The shells for which we generate init scripts (bash and sh are the same
entry) *)
let shells_list = [ SH_sh; SH_zsh; SH_csh; SH_fish; SH_pwsh Powershell; SH_cmd ]
let shells_list = [ SH_sh; SH_zsh; SH_csh; SH_fish; SH_pwsh Powershell; SH_cmd ; SH_nu]

let complete_file = function
| SH_sh | SH_bash -> Some "complete.sh"
| SH_zsh -> Some "complete.zsh"
| SH_csh | SH_fish | SH_pwsh _ | SH_cmd -> None
| SH_csh | SH_fish | SH_pwsh _ | SH_cmd | SH_nu-> None

let env_hook_file = function
| SH_sh | SH_bash -> Some "env_hook.sh"
| SH_zsh -> Some "env_hook.zsh"
| SH_csh -> Some "env_hook.csh"
| SH_fish -> Some "env_hook.fish"
| SH_pwsh _ | SH_cmd -> None
| SH_pwsh _ | SH_cmd | SH_nu -> None

let variables_file = function
| SH_sh | SH_bash | SH_zsh -> "variables.sh"
| SH_csh -> "variables.csh"
| SH_fish -> "variables.fish"
| SH_nu -> "variables.nu"
| SH_pwsh _ -> "variables.ps1"
| SH_cmd -> "variables.cmd"

Expand All @@ -943,21 +946,22 @@ let init_file = function
| SH_zsh -> "init.zsh"
| SH_csh -> "init.csh"
| SH_fish -> "init.fish"
| SH_nu -> "init.nu"
| SH_pwsh _ -> "init.ps1"
| SH_cmd -> "init.cmd"

let complete_script = function
| SH_sh | SH_bash -> Some OpamScript.complete
| SH_zsh -> Some OpamScript.complete_zsh
| SH_csh | SH_fish -> None
| SH_csh | SH_fish | SH_nu -> None
| SH_pwsh _ | SH_cmd -> None

let env_hook_script_base = function
| SH_sh | SH_bash -> Some OpamScript.env_hook
| SH_zsh -> Some OpamScript.env_hook_zsh
| SH_csh -> Some OpamScript.env_hook_csh
| SH_fish -> Some OpamScript.env_hook_fish
| SH_pwsh _ | SH_cmd -> None
| SH_pwsh _ | SH_cmd | SH_nu -> None

let export_in_shell shell =
let make_comment comment_opt =
Expand Down Expand Up @@ -993,6 +997,9 @@ let export_in_shell shell =
Printf.sprintf "%sset -gx %s %s;\n"
(make_comment comment) k v
in
let nu (k,v,comment) =
Printf.sprintf "load-env (opam env %s %s %s)\n" (make_comment comment) k v
in
let pwsh (k,v,comment) =
Printf.sprintf "%s$env:%s=%s\n"
(make_comment comment) k v in
Expand All @@ -1005,6 +1012,7 @@ let export_in_shell shell =
match shell with
| SH_zsh | SH_bash | SH_sh -> sh
| SH_fish -> fish
| SH_nu -> nu
| SH_csh -> csh
| SH_pwsh _ -> pwsh
| SH_cmd -> cmd
Expand All @@ -1030,6 +1038,8 @@ let source root shell f =
| SH_fish ->
let fname = unix_transform ~using_backslashes:true () in
Printf.sprintf "test -r '%s' && source '%s' > /dev/null 2> /dev/null; or true\n" fname fname
| SH_nu ->
Printf.sprintf "%s:TODO" fname
| SH_sh | SH_bash ->
let fname = unix_transform () in
Printf.sprintf "test -r '%s' && . '%s' > /dev/null 2> /dev/null || true\n"
Expand Down Expand Up @@ -1065,6 +1075,8 @@ let if_interactive_script shell t e =
Printf.sprintf "if ( $?prompt ) then\n %s%sendif\n" t @@ ielse e
| SH_fish ->
Printf.sprintf "if status is-interactive\n %s%send\n" t @@ ielse e
| SH_nu ->
Printf.sprintf "%sTODO%s" t @@ ielse e
| SH_cmd ->
Printf.sprintf "echo %%cmdcmdline%% | find /i \"%%~0\" >nul\nif errorlevel 1 (\n%s%s)\n" t @@ ielse_cmd e
| SH_pwsh _ ->
Expand Down