Skip to content
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 hackage-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ executable hackage-cli
-- aeson-1.2.4.0 for stack-8.2.2.yaml
, base-compat >= 0.13.0 && < 1
-- base-compat for applyWhen (added to base in 4.18)
, cabal-add >= 0.2 && < 0.3
, deepseq ^>= 1.4.0.0 || ^>= 1.5.0.0
, directory ^>= 1.2.0.1 || ^>= 1.3.0.0
, filepath >= 1.4.0.0 && < 2
Expand Down
33 changes: 12 additions & 21 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import qualified Distribution.Types.GenericPackageDescription.Lens as LC

-- import Cabal

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

Expand Down Expand Up @@ -589,7 +589,6 @@ optionsParserInfo
addboundParser = AddBound <$> (AddBoundOptions <$> OA.argument prsc (metavar "DEPENDENCY")
<*> OA.argument prsc (metavar "VERSIONRANGE")
<*> OA.switch (long "force" <> help "Add bound even if it is already subsumed by existing constraints.")
<*> 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'."))
<*> some (OA.argument str (metavar "CABALFILES..." <> action "file")))

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

old <- liftIO $ BS.readFile fp

-- idea is simple:
-- - .cabal is line oriented file
-- - find "library" section start
-- - bonus: look of an indentation used from the next field/section there
-- - insert data into a bytestring "manually"
fs <- either (\ err -> throwError $ unwords ["Parsing", fp, "failed:", show err]) return $
C.readFields old
(lin, indent) <- maybe
(throwError $ "Cannot find library section in " ++ fp)
return
(findLibrarySection fs)

let msgLines = map ("-- " ++) optABMessage
bdLine = "build-depends: " ++ C.prettyShow optABPackageName ++ " " ++ C.prettyShow optABVersionRange
midLines = [ BS8.pack $ replicate indent ' ' ++ l
| l <- msgLines ++ [bdLine]
] ++ [""] -- also add an empty line separator
(preLines, postLines) = splitAt lin $ BS8.lines old
new = BS8.unlines (preLines ++ midLines ++ postLines)
let addConfig = AddConfig
{ cnfOrigContents = old
, cnfFields = fs
, cnfComponent = Right (C.CLibName C.LMainLibName)
, cnfTargetField = BuildDepends
, cnfAdditions = pure $ BS8.pack $ C.prettyShow optABPackageName ++ " " ++ C.prettyShow optABVersionRange
}
new <- case executeAddConfig (\_ _ -> True) addConfig of
Just x -> pure x
Nothing -> throwError $ "Cannot find library section in " ++ fp

-- interpretation of version ranges
let oldGpd = parseGenericPackageDescription' old
Expand Down
Loading