Skip to content

Commit

Permalink
Support Windows Command Processor as a shell
Browse files Browse the repository at this point in the history
Support cmd as an additional option to --shell. Additionally, use
parent_putenv so that opam config env does not have to be run at the end
of opam init or after opam switch.

Windows Command Processor has no equivalent to .profile - at present,
the user must run opam env every time they launch a command
prompt.

Signed-off-by: David Allsopp <david.allsopp@metastack.com>
  • Loading branch information
dra27 committed Sep 2, 2017
1 parent ed44880 commit 2934dc4
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 86 deletions.
3 changes: 2 additions & 1 deletion src/client/opamArg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -738,12 +738,13 @@ let installed_roots_flag =
mk_flag ["installed-roots"] "Display only the installed roots."

let shell_opt =
let enum = [
let (enum : (string * OpamTypes.shell) list) = [
"bash",`bash;
"sh",`sh;
"csh",`csh;
"zsh",`zsh;
"fish",`fish;
"cmd",`cmd;
] in
mk_opt ["shell"] "SHELL"
(Printf.sprintf
Expand Down
5 changes: 4 additions & 1 deletion src/client/opamClient.ml
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,10 @@ let init
else false
in
let advised_deps =
[OpamStateConfig.(Lazy.force !r.makecmd); "m4"; "cc"]
if OpamStd.Sys.(os () = Win32) then
[]
else
[OpamStateConfig.(Lazy.force !r.makecmd); "m4"; "cc"]
in
(match List.filter (not @* check_external_dep) advised_deps with
| [] -> ()
Expand Down
10 changes: 5 additions & 5 deletions src/client/opamCommands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -859,10 +859,10 @@ let config =
if OpamStateConfig.(!r.current_switch) = None then `Ok () else
OpamSwitchState.with_ `Lock_none gt @@ fun st ->
`Ok (OpamConfigCommand.env st
~csh:(shell=`csh) ~sexp ~fish:(shell=`fish) ~inplace_path)
~cmd:(shell=`cmd) ~csh:(shell=`csh) ~sexp ~fish:(shell=`fish) ~inplace_path)
| Some `revert_env, [] ->
`Ok (OpamConfigCommand.print_eval_env
~csh:(shell=`csh) ~sexp ~fish:(shell=`fish)
~cmd:(shell=`cmd) ~csh:(shell=`csh) ~sexp ~fish:(shell=`fish)
(OpamEnv.add [] []))
| Some `setup, [] ->
let user = all || user in
Expand All @@ -885,7 +885,7 @@ let config =
Main options\n\
\ -l, --list %s\n\
\ -a, --all %s\n\
\ --shell=<bash|sh|csh|zsh|fish>\n\
\ --shell=<bash|sh|csh|zsh|fish|cmd>\n\
\ Configure assuming the given shell.\n\
\n\
User configuration\n\
Expand Down Expand Up @@ -1150,10 +1150,10 @@ let env =
if OpamStateConfig.(!r.current_switch) <> None then
OpamSwitchState.with_ `Lock_none gt @@ fun st ->
OpamConfigCommand.env st
~csh:(shell=`csh) ~sexp ~fish:(shell=`fish) ~inplace_path
~cmd:(shell=`cmd) ~csh:(shell=`csh) ~sexp ~fish:(shell=`fish) ~inplace_path
| true ->
OpamConfigCommand.print_eval_env
~csh:(shell=`csh) ~sexp ~fish:(shell=`fish)
~cmd:(shell=`cmd) ~csh:(shell=`csh) ~sexp ~fish:(shell=`fish)
(OpamEnv.add [] [])
in
Term.(const env $global_options $shell_opt $sexp $inplace_path $revert),
Expand Down
17 changes: 14 additions & 3 deletions src/client/opamConfigCommand.ml
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,31 @@ let rec print_fish_env env =
k (OpamStd.Env.escape_single_quotes ~using_backslashes:true v));
print_fish_env r

let print_eval_env ~csh ~sexp ~fish env =
let print_cmd_env env =
List.iter (fun (k, v, _) -> OpamConsole.msg "set %s=%s\n" k v) env

let print_eval_env ~cmd ~csh ~sexp ~fish env =
if sexp then
print_sexp_env env
else if csh then
print_csh_env env
else if fish then
print_fish_env env
else if cmd then
if OpamStd.Sys.tty_out then begin
log "parent-putenv";
OpamEnv.set_cmd_env env
end else begin
log "cmd-stdout";
print_cmd_env env
end
else
print_env env

let env st ~csh ~sexp ~fish ~inplace_path =
let env st ~cmd ~csh ~sexp ~fish ~inplace_path =
log "config-env";
let env = OpamEnv.get_opam ~force_path:(not inplace_path) st in
print_eval_env ~csh ~sexp ~fish env
print_eval_env ~cmd ~csh ~sexp ~fish env

let subst gt fs =
log "config-substitute";
Expand Down
11 changes: 6 additions & 5 deletions src/client/opamConfigCommand.mli
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@
open OpamTypes
open OpamStateTypes

(** Display the current environment. Booleans csh, sexp and fish set an alternative
output (unspecified if more than one is true, sh-style by default).
(** Display the current environment. Booleans cmd, csh, sexp and fish set an
alternative output (unspecified if more than one is true, sh-style by
default).
[inplace_path] changes how the PATH variable is updated when there is already
an opam entry: either at the same rank, or pushed in front. *)
val env:
'a switch_state -> csh:bool -> sexp:bool -> fish:bool -> inplace_path:bool ->
unit
'a switch_state -> cmd:bool -> csh:bool -> sexp:bool -> fish:bool ->
inplace_path:bool -> unit

(** Like [env] but allows 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 -> env -> unit
val print_eval_env: cmd:bool -> csh:bool -> sexp:bool -> fish:bool -> env -> unit

(** Display the content of all available variables; global summary if the list
is empty, package name "-" is understood as global configuration *)
Expand Down
12 changes: 10 additions & 2 deletions src/core/opamStd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,11 @@ module OpamSys = struct
| "zsh" -> `zsh
| "bash" -> `bash
| "fish" -> `fish
| _ -> `sh
| _ ->
if os () = Win32 then
`cmd
else
`sh

let executable_name =
if os () = Win32 then
Expand All @@ -902,7 +906,11 @@ module OpamSys = struct

let guess_shell_compat () =
try shell_of_string (Filename.basename (Env.get "SHELL"))
with Not_found -> `sh
with Not_found ->
if os () = Win32 then
`cmd
else
`sh

let guess_dot_profile shell =
let home f =
Expand Down
4 changes: 2 additions & 2 deletions src/core/opamStd.mli
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,10 @@ module Sys : sig
val executable_name : string -> string

(** Guess the shell compat-mode *)
val guess_shell_compat: unit -> [`csh|`zsh|`sh|`bash|`fish]
val guess_shell_compat: unit -> [`csh|`zsh|`sh|`bash|`fish|`cmd]

(** Guess the location of .profile *)
val guess_dot_profile: [`csh|`zsh|`sh|`bash|`fish] -> string
val guess_dot_profile: [`csh|`zsh|`sh|`bash|`fish|`cmd] -> string

(** The separator character used in the PATH variable (varies depending on
OS) *)
Expand Down
2 changes: 1 addition & 1 deletion src/format/opamTypes.mli
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ type universe = {
type pin_kind = [ `version | OpamUrl.backend ]

(** Shell compatibility modes *)
type shell = [`fish|`csh|`zsh|`sh|`bash]
type shell = [`fish|`csh|`zsh|`sh|`bash|`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 @@ -47,6 +47,7 @@ let string_of_shell = function
| `zsh -> "zsh"
| `sh -> "sh"
| `bash -> "bash"
| `cmd -> "Windows Command Processor"

let file_null = ""
let pos_file filename = OpamFilename.to_string filename, -1, -1
Expand Down
Loading

0 comments on commit 2934dc4

Please sign in to comment.