Skip to content

Commit fe77a83

Browse files
committed
Fix adding bounds to library with common stanza
1 parent e14f043 commit fe77a83

File tree

2 files changed

+13
-21
lines changed

2 files changed

+13
-21
lines changed

hackage-cli.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ executable hackage-cli
119119
-- aeson-1.2.4.0 for stack-8.2.2.yaml
120120
, base-compat >= 0.13.0 && < 1
121121
-- base-compat for applyWhen (added to base in 4.18)
122+
, cabal-add >= 0.2 && < 0.3
122123
, deepseq ^>= 1.4.0.0 || ^>= 1.5.0.0
123124
, directory ^>= 1.2.0.1 || ^>= 1.3.0.0
124125
, filepath >= 1.4.0.0 && < 2

src/Main.hs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import qualified Distribution.Types.GenericPackageDescription.Lens as LC
8282

8383
-- import Cabal
8484

85+
import Distribution.Client.Add
8586
import Distribution.Server.Util.CabalRevisions ( diffCabalRevisions, Change(Change) )
8687
import IndexShaSum ( IndexShaSumOptions(IndexShaSumOptions), run )
8788
import CabalEdit ( PkgRev, cabalEditXRev )
@@ -515,7 +516,6 @@ data AddBoundOptions' a = AddBoundOptions
515516
{ optABPackageName :: C.PackageName
516517
, optABVersionRange :: C.VersionRange
517518
, optForce :: Bool -- ^ Disable the check whether bound is subsumed by existing constraints.
518-
, optABMessage :: [String]
519519
, optABFiles :: a -- ^ One or several files.
520520
} deriving (Show, Functor)
521521

@@ -589,7 +589,6 @@ optionsParserInfo
589589
addboundParser = AddBound <$> (AddBoundOptions <$> OA.argument prsc (metavar "DEPENDENCY")
590590
<*> OA.argument prsc (metavar "VERSIONRANGE")
591591
<*> OA.switch (long "force" <> help "Add bound even if it is already subsumed by existing constraints.")
592-
<*> many (OA.option str (OA.short 'm' <> OA.long "message" <> metavar "MSG" <> help "Use given MSG as a comment. If multiple -m options are given, their values are concatenated with 'unlines'."))
593592
<*> some (OA.argument str (metavar "CABALFILES..." <> action "file")))
594593

595594
oParser
@@ -909,29 +908,21 @@ condTreeDataL f (C.CondNode x c cs) = f x <&> \y -> C.CondNode y c cs
909908
-- Non-fatal errors (like parse errors) are reported in the Except monad.
910909
--
911910
addBound :: AddBoundOptions' FilePath -> ExceptT String IO ()
912-
addBound AddBoundOptions{ optABPackageName, optABVersionRange, optForce, optABMessage, optABFiles = fp } = do
911+
addBound AddBoundOptions{ optABPackageName, optABVersionRange, optForce, optABFiles = fp } = do
913912

914913
old <- liftIO $ BS.readFile fp
915-
916-
-- idea is simple:
917-
-- - .cabal is line oriented file
918-
-- - find "library" section start
919-
-- - bonus: look of an indentation used from the next field/section there
920-
-- - insert data into a bytestring "manually"
921914
fs <- either (\ err -> throwError $ unwords ["Parsing", fp, "failed:", show err]) return $
922915
C.readFields old
923-
(lin, indent) <- maybe
924-
(throwError $ "Cannot find library section in " ++ fp)
925-
return
926-
(findLibrarySection fs)
927-
928-
let msgLines = map ("-- " ++) optABMessage
929-
bdLine = "build-depends: " ++ C.prettyShow optABPackageName ++ " " ++ C.prettyShow optABVersionRange
930-
midLines = [ BS8.pack $ replicate indent ' ' ++ l
931-
| l <- msgLines ++ [bdLine]
932-
] ++ [""] -- also add an empty line separator
933-
(preLines, postLines) = splitAt lin $ BS8.lines old
934-
new = BS8.unlines (preLines ++ midLines ++ postLines)
916+
let addConfig = AddConfig
917+
{ cnfOrigContents = old
918+
, cnfFields = fs
919+
, cnfComponent = Right (C.CLibName C.LMainLibName)
920+
, cnfTargetField = BuildDepends
921+
, cnfAdditions = pure $ BS8.pack $ C.prettyShow optABPackageName ++ " " ++ C.prettyShow optABVersionRange
922+
}
923+
new <- case executeAddConfig (\_ _ -> True) addConfig of
924+
Just x -> pure x
925+
Nothing -> throwError $ "Cannot find library section in " ++ fp
935926

936927
-- interpretation of version ranges
937928
let oldGpd = parseGenericPackageDescription' old

0 commit comments

Comments
 (0)