From b245c11b1d77236c75c29fb094bbb9cfd70eed48 Mon Sep 17 00:00:00 2001
From: Julian Ospald <hasufell@posteo.de>
Date: Fri, 28 Jan 2022 23:08:35 +0100
Subject: [PATCH] Allow to disable self-upgrade functionality wrt #305

---
 app/ghcup/GHCup/OptParse.hs |  8 ++++++++
 app/ghcup/Main.hs           | 15 ++++++++++++++-
 ghcup.cabal                 | 14 +++++++++++++-
 3 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/app/ghcup/GHCup/OptParse.hs b/app/ghcup/GHCup/OptParse.hs
index 1dc74e54..120c713a 100644
--- a/app/ghcup/GHCup/OptParse.hs
+++ b/app/ghcup/GHCup/OptParse.hs
@@ -15,7 +15,9 @@ module GHCup.OptParse (
   , module GHCup.OptParse.Config
   , module GHCup.OptParse.Whereis
   , module GHCup.OptParse.List
+#ifndef DISABLE_UPGRADE
   , module GHCup.OptParse.Upgrade
+#endif
   , module GHCup.OptParse.ChangeLog
   , module GHCup.OptParse.Prefetch
   , module GHCup.OptParse.GC
@@ -35,7 +37,9 @@ import           GHCup.OptParse.Compile
 import           GHCup.OptParse.Config
 import           GHCup.OptParse.Whereis
 import           GHCup.OptParse.List
+#ifndef DISABLE_UPGRADE
 import           GHCup.OptParse.Upgrade
+#endif
 import           GHCup.OptParse.ChangeLog
 import           GHCup.OptParse.Prefetch
 import           GHCup.OptParse.GC
@@ -89,7 +93,9 @@ data Command
   | Compile CompileCommand
   | Config ConfigCommand
   | Whereis WhereisOptions WhereisCommand
+#ifndef DISABLE_UPGRADE
   | Upgrade UpgradeOpts Bool
+#endif
   | ToolRequirements
   | ChangeLog ChangeLogOptions
   | Nuke
@@ -208,6 +214,7 @@ com =
            (info (List <$> listOpts <**> helper)
                  (progDesc "Show available GHCs and other tools")
            )
+#ifndef DISABLE_UPGRADE
       <> command
            "upgrade"
            (info
@@ -218,6 +225,7 @@ com =
              )
              (progDesc "Upgrade ghcup")
            )
+#endif
       <> command
            "compile"
            (   Compile
diff --git a/app/ghcup/Main.hs b/app/ghcup/Main.hs
index 5cfb30c3..f937aaa4 100644
--- a/app/ghcup/Main.hs
+++ b/app/ghcup/Main.hs
@@ -140,7 +140,12 @@ main = do
         <> hidden
         )
   let listCommands = infoOption
-        "install set rm install-cabal list upgrade compile debug-info tool-requirements changelog"
+        ("install set rm install-cabal list"
+#ifndef DISABLE_UPGRADE
+          <> " upgrade"
+#endif
+          <> " compile debug-info tool-requirements changelog"
+        )
         (  long "list-commands"
         <> help "List available commands for shell completion"
         <> internal
@@ -238,10 +243,14 @@ Report bugs at <https://gitlab.haskell.org/haskell/ghcup-hs/issues>|]
                              alreadyInstalling' <- alreadyInstalling optCommand newTool
                              when (not alreadyInstalling') $
                                case t of
+#ifdef DISABLE_UPGRADE
+                                 GHCup -> pure ()
+#else
                                  GHCup -> runLogger $
                                             logWarn ("New GHCup version available: "
                                               <> prettyVer l
                                               <> ". To upgrade, run 'ghcup upgrade'")
+#endif
                                  _ -> runLogger $
                                         logWarn ("New "
                                           <> T.pack (prettyShow t)
@@ -296,7 +305,9 @@ Report bugs at <https://gitlab.haskell.org/haskell/ghcup-hs/issues>|]
             Config configCommand     -> config configCommand settings keybindings runLogger
             Whereis whereisOptions
                     whereisCommand   -> whereis whereisCommand whereisOptions runAppState leanAppstate runLogger
+#ifndef DISABLE_UPGRADE
             Upgrade uOpts force'     -> upgrade uOpts force' dirs runAppState runLogger
+#endif
             ToolRequirements         -> toolRequirements runAppState runLogger
             ChangeLog changelogOpts  -> changelog changelogOpts runAppState runLogger
             Nuke                     -> nuke appState runLogger
@@ -339,7 +350,9 @@ Report bugs at <https://gitlab.haskell.org/haskell/ghcup-hs/issues>|]
     (HLS, ver)   = cmp' HLS (Just $ ToolVersion (mkTVer over)) ver
   alreadyInstalling (Compile (CompileHLS HLSCompileOptions{ targetHLS = Left tver }))
     (HLS, ver)   = cmp' HLS (Just $ ToolVersion (mkTVer tver)) ver
+#ifndef DISABLE_UPGRADE
   alreadyInstalling (Upgrade _ _) (GHCup, _) = pure True
+#endif
   alreadyInstalling _ _ = pure False
 
   cmp' :: ( HasLog env
diff --git a/ghcup.cabal b/ghcup.cabal
index c5d61761..611b112a 100644
--- a/ghcup.cabal
+++ b/ghcup.cabal
@@ -48,6 +48,13 @@ flag no-exe
   default:     False
   manual:      True
 
+flag disable-upgrade
+  description:
+    Build the brick powered tui (ghcup tui). This is disabled on windows.
+
+  default:     False
+  manual:      True
+
 library
   exposed-modules:
     GHCup
@@ -195,7 +202,6 @@ executable ghcup
     GHCup.OptParse.Set
     GHCup.OptParse.ToolRequirements
     GHCup.OptParse.UnSet
-    GHCup.OptParse.Upgrade
     GHCup.OptParse.Whereis
 
   hs-source-dirs:     app/ghcup
@@ -262,6 +268,12 @@ executable ghcup
   if flag(no-exe)
     buildable: False
 
+  if (flag(disable-upgrade))
+    cpp-options:   -DDISABLE_UPGRADE
+  else
+    other-modules:
+        GHCup.OptParse.Upgrade
+
 test-suite ghcup-test
   type:               exitcode-stdio-1.0
   main-is:            Main.hs
-- 
GitLab