Skip to content

Commit 80ccb00

Browse files
authored
Merge pull request #6034 from dra27/opam-init-ya
Suppress Windows-specific menus with `opam init -ya`
2 parents 8018f2f + db22a53 commit 80ccb00

File tree

2 files changed

+66
-38
lines changed

2 files changed

+66
-38
lines changed

master_changes.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ users)
2020
## Plugins
2121

2222
## Init
23+
* Suppress all the Windows menus when running with `opam init -ya` [#6034 @dra27]
2324

2425
## Config report
2526

src/client/opamClient.ml

+65-38
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ let string_of_kind = function
691691
| `Msys2 -> "MSYS2"
692692
| `Cygwin -> "Cygwin"
693693

694-
let git_for_windows kind mechanism cygwin_is_tweakable =
694+
let git_for_windows kind mechanism ~interactive ~cygwin_is_tweakable =
695695
let resolve_git_in p =
696696
OpamSystem.resolve_command ~env:[||] (Filename.concat p "git.exe")
697697
in
@@ -890,7 +890,7 @@ let git_for_windows kind mechanism cygwin_is_tweakable =
890890
| None -> false
891891
| Some git -> is_git_for_windows git
892892
in
893-
if not git_found && OpamStd.Sys.tty_out then
893+
if not git_found && interactive && OpamStd.Sys.tty_out then
894894
(OpamConsole.header_msg "Git";
895895
OpamConsole.msg
896896
"Cygwin Git is functional but can have credentials issues for private \
@@ -900,8 +900,12 @@ let git_for_windows kind mechanism cygwin_is_tweakable =
900900
"Git for Windows can be downloaded and installed from \
901901
https://gitforwindows.org" ]);
902902
menu ())
903-
else
904-
None, not git_found && cygwin_is_tweakable
903+
else begin
904+
let install_via_depext = not git_found && cygwin_is_tweakable in
905+
if not interactive && install_via_depext then
906+
OpamConsole.note "opam will add Git to %s" (string_of_kind kind);
907+
None, install_via_depext
908+
end
905909
in
906910
OpamStd.Option.iter (fun _ ->
907911
OpamConsole.msg
@@ -1004,10 +1008,10 @@ let cygwin_searches ?first () =
10041008
in
10051009
seq cygwin_searches
10061010

1007-
let rec cygwin_menu ~bypass_checks header =
1011+
let rec cygwin_menu ~bypass_checks ~interactive header =
10081012
let start = Unix.gettimeofday () in
10091013
let test_mechanism (roots, count, mechanisms) search =
1010-
match test_mechanism ~bypass_checks header search with
1014+
match test_mechanism ~bypass_checks ~interactive header search with
10111015
| Some ((kind, `Root root) as mechanism) ->
10121016
if OpamFilename.Dir.Set.mem root roots then
10131017
roots, count, mechanisms
@@ -1148,20 +1152,31 @@ let rec cygwin_menu ~bypass_checks header =
11481152
end
11491153
end
11501154
in
1151-
Lazy.force header;
1152-
OpamConsole.msg
1153-
"\n\
1154-
opam and the OCaml ecosystem in general require various Unix tools in \
1155-
order to operate correctly. At present, this requires the installation \
1156-
of Cygwin to provide these tools.\n\n";
1157-
match OpamConsole.menu "How should opam obtain Unix tools?"
1158-
~default ~yes:default ~no:default ~options with
1155+
let result =
1156+
Lazy.force header;
1157+
if interactive then begin
1158+
OpamConsole.msg
1159+
"\n\
1160+
opam and the OCaml ecosystem in general require various Unix tools in \
1161+
order to operate correctly. At present, this requires the installation \
1162+
of Cygwin to provide these tools.\n\n";
1163+
OpamConsole.menu "How should opam obtain Unix tools?"
1164+
~default ~yes:default ~no:default ~options
1165+
end else
1166+
default
1167+
in
1168+
match result with
11591169
| `Chosen (kind, `Internal) ->
11601170
assert (kind = `Cygwin);
1171+
if not interactive then
1172+
OpamConsole.note "opam will maintain an internal Cygwin installation";
11611173
Some (kind, `Internal OpamInitDefaults.required_packages_for_cygwin)
11621174
| `Chosen (kind, ((`Root _) as mechanism)) ->
11631175
Some (kind, mechanism)
1164-
| `Chosen ((_, `Path _) as mechanism) ->
1176+
| `Chosen ((kind, `Path _) as mechanism) ->
1177+
if not interactive then
1178+
OpamConsole.note "opam will use the %s installation found in your Path"
1179+
(string_of_kind kind);
11651180
OpamStd.Option.iter (OpamConsole.warning "%s") warn_path;
11661181
Some mechanism
11671182
| `Specify ->
@@ -1176,11 +1191,11 @@ let rec cygwin_menu ~bypass_checks header =
11761191
Some (kind, `Root root)
11771192
| Error msg ->
11781193
OpamConsole.error "%s" msg;
1179-
cygwin_menu ~bypass_checks header
1194+
cygwin_menu ~bypass_checks ~interactive header
11801195
end
11811196
| `Abort -> OpamStd.Sys.exit_because `Aborted
11821197

1183-
and test_mechanism ~bypass_checks header = function
1198+
and test_mechanism ~bypass_checks ~interactive header = function
11841199
| (`Internal _) as mechanism -> Some (`Cygwin, mechanism)
11851200
| `Path ->
11861201
let cygcheck =
@@ -1204,7 +1219,7 @@ and test_mechanism ~bypass_checks header = function
12041219
| Error msg ->
12051220
OpamConsole.error_and_exit `Not_found "%s" msg
12061221
end
1207-
| `Menu -> cygwin_menu ~bypass_checks header
1222+
| `Menu -> cygwin_menu ~bypass_checks ~interactive header
12081223

12091224
let string_of_cygwin_setup = function
12101225
| `internal pkgs ->
@@ -1268,7 +1283,7 @@ let initialise_msys2 root =
12681283
OpamStd.Sys.exit_because `Aborted
12691284

12701285
let determine_windows_configuration ?cygwin_setup ?git_location
1271-
~bypass_checks config =
1286+
~bypass_checks ~interactive config =
12721287
OpamStd.Option.iter
12731288
(log "Cygwin (from CLI): %a" (slog string_of_cygwin_setup)) cygwin_setup;
12741289
(* Check whether symlinks can be created. Developer Mode is not the only way
@@ -1379,7 +1394,7 @@ let determine_windows_configuration ?cygwin_setup ?git_location
13791394
else
13801395
cygwin_setup
13811396
in
1382-
let mechanisms, cygwin_tweakable =
1397+
let mechanisms, cygwin_is_tweakable =
13831398
match cygwin_setup with
13841399
| Some (`internal packages) ->
13851400
(* git, if needed, will be added later *)
@@ -1407,7 +1422,7 @@ let determine_windows_configuration ?cygwin_setup ?git_location
14071422
(* Reduce mechanisms to a single mechanism (which may therefore display a
14081423
menu). *)
14091424
let kind, mechanism =
1410-
let test_mechanism = test_mechanism ~bypass_checks header in
1425+
let test_mechanism = test_mechanism ~bypass_checks ~interactive header in
14111426
match OpamCompat.Seq.find_map test_mechanism mechanisms with
14121427
| Some result -> result
14131428
| None ->
@@ -1493,7 +1508,7 @@ let determine_windows_configuration ?cygwin_setup ?git_location
14931508
`Internal, pkgs
14941509
| (`Root _ | `Path _) as mechanism ->
14951510
let cygwin_packages =
1496-
if cygwin_tweakable && not OpamStateConfig.(!r.no_depexts) then
1511+
if cygwin_is_tweakable && not OpamStateConfig.(!r.no_depexts) then
14971512
OpamInitDefaults.required_packages_for_cygwin
14981513
else
14991514
[]
@@ -1503,14 +1518,14 @@ let determine_windows_configuration ?cygwin_setup ?git_location
15031518
if git_location = None && not git_determined
15041519
&& not have_git_for_windows_in_path then
15051520
let git_location, from_cygwin =
1506-
git_for_windows kind mechanism cygwin_tweakable
1521+
git_for_windows kind mechanism ~interactive ~cygwin_is_tweakable
15071522
in
15081523
let config =
15091524
OpamStd.Option.map_default (apply_git_location config)
15101525
config git_location
15111526
in
15121527
let cygwin_packages =
1513-
if cygwin_tweakable && from_cygwin then
1528+
if cygwin_is_tweakable && from_cygwin then
15141529
OpamSysPkg.of_string "git" :: cygwin_packages
15151530
else
15161531
cygwin_packages
@@ -1601,7 +1616,7 @@ let reinit ?(init_config=OpamInitDefaults.init_config()) ~interactive
16011616
let config, mechanism, system_packages, msys2_check_root =
16021617
if Sys.win32 then
16031618
determine_windows_configuration ?cygwin_setup ?git_location
1604-
~bypass_checks config
1619+
~bypass_checks ~interactive config
16051620
else
16061621
config, None, [], None
16071622
in
@@ -1670,7 +1685,7 @@ let setup_redirection target =
16701685
OpamStateConfig.update ~root_dir ();
16711686
root_dir
16721687

1673-
let get_redirected_root () =
1688+
let get_redirected_root ~interactive =
16741689
let {contents = {OpamStateConfig.original_root_dir = root; root_from; _}} =
16751690
OpamStateConfig.r
16761691
in
@@ -1728,9 +1743,19 @@ let get_redirected_root () =
17281743
OpamStd.Option.replace check (OpamConsole.read "Root directory for opam: ")
17291744
in
17301745
let rec menu () =
1731-
match OpamConsole.menu "Where should opam store files?" ~options
1732-
~default ~yes:default ~no:default with
1746+
let result =
1747+
if interactive then
1748+
OpamConsole.menu "Where should opam store files?" ~options
1749+
~default ~yes:default ~no:default
1750+
else begin
1751+
default
1752+
end
1753+
in
1754+
match result with
17331755
| `Redirect ->
1756+
if not interactive then
1757+
OpamConsole.note "opam is storing files in %s"
1758+
(OpamFilename.Dir.to_string default_redirect_root);
17341759
Some None
17351760
| `Endure ->
17361761
None
@@ -1743,14 +1768,16 @@ let get_redirected_root () =
17431768
| `Quit ->
17441769
OpamStd.Sys.exit_because `Aborted
17451770
in
1746-
OpamConsole.header_msg "opam root file store";
1747-
OpamConsole.msg
1748-
"\n\
1749-
%s\n\
1750-
\n\
1751-
Many parts of the OCaml ecosystem do not presently work correctly\n\
1752-
when installed to directories containing spaces. You have been warned!%s\n\
1753-
\n" explanation collision;
1771+
if interactive then begin
1772+
OpamConsole.header_msg "opam root file store";
1773+
OpamConsole.msg
1774+
"\n\
1775+
%s\n\
1776+
\n\
1777+
Many parts of the OCaml ecosystem do not presently work correctly when\n\
1778+
installed to directories containing spaces. You have been warned!%s\n\
1779+
\n" explanation collision;
1780+
end;
17541781
Option.map setup_redirection (menu ())
17551782

17561783
let init
@@ -1776,7 +1803,7 @@ let init
17761803
if root_empty &&
17771804
Sys.win32 &&
17781805
has_space (OpamFilename.Dir.to_string root) then
1779-
get_redirected_root ()
1806+
get_redirected_root ~interactive
17801807
else
17811808
None
17821809
in
@@ -1821,7 +1848,7 @@ let init
18211848
let config, mechanism, system_packages, msys2_check_root =
18221849
if Sys.win32 then
18231850
determine_windows_configuration ?cygwin_setup ?git_location
1824-
~bypass_checks config
1851+
~bypass_checks ~interactive config
18251852
else
18261853
config, None, [], None
18271854
in

0 commit comments

Comments
 (0)