Skip to content
Draft
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
5 changes: 5 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ formatting automatically.
- All system operations (filesystem, process, etc.) are performed through the
`Driver` effect. IO or separate packages are not used to interact with the
target system.
- The JSON representation of packages (and types nested in them) is defined
once via `JSONCodec` (`Mybox.JSONCodec`), which bundles the encoder, decoder and
documentation. Record codecs are built with `jsonCodecObject` from per-field
specs tagged with the Haskell field name; a mismatch with the record's
fields is a type error. `FromJSON`/`ToJSON` are derived via `ViaJSONCodec`.
- Use the `App` type alias to define the effect stack and make dependencies explicit.
- Organize code into highly modular components, each in its own module.
- Emphasize composability and testability via effects and typeclasses.
Expand Down
2 changes: 2 additions & 0 deletions mybox.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ library
Mybox.Installer.Flatpak.Internal
Mybox.Installer.MacOS
Mybox.Installer.MacOSCombined
Mybox.JSONCodec
Mybox.LockMap
Mybox.Main
Mybox.Package.AgentSettings
Expand Down Expand Up @@ -234,6 +235,7 @@ test-suite tests
Mybox.Installer.DNFSpec
Mybox.Installer.FlatpakSpec
Mybox.Installer.SpecBase
Mybox.JSONCodecSpec
Mybox.LockMapSpec
Mybox.Package.AgentSettingsSpec
Mybox.Package.ArchiveSpec
Expand Down
27 changes: 19 additions & 8 deletions src/Mybox/Aeson.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Mybox.Aeson (
module Data.Aeson,
Parser,
Pair,
listParser,
listValue,
jsonEncode,
jsonDecode,
yamlDecode,
Expand All @@ -13,7 +15,9 @@ module Mybox.Aeson (
withObjectTotal,
ObjectParser,
takeField,
takeFieldWith,
takeFieldMaybe,
takeFieldMaybeWith,
takeCollapsedList,
takeCollapsedListMaybe,
takeCollapsedNEList,
Expand Down Expand Up @@ -68,16 +72,23 @@ takeField_ k f = do
modify $ KM.delete k
lift $ f v <?> Key k

takeField :: FromJSON a => Key -> ObjectParser a
takeField k = takeField_ k $ \case
takeFieldWith :: Key -> (Value -> Parser a) -> ObjectParser a
takeFieldWith k p = takeField_ k $ \case
Nothing -> parseFail $ "key " <> show k <> " not found"
Just v -> parseJSON v
Just v -> p v

takeFieldMaybe :: FromJSON a => Key -> ObjectParser (Maybe a)
takeFieldMaybe k = takeField_ k $ \case
takeField :: FromJSON a => Key -> ObjectParser a
takeField k = takeFieldWith k parseJSON

-- | Take an optional field; @null@ counts as absent.
takeFieldMaybeWith :: Key -> (Value -> Parser a) -> ObjectParser (Maybe a)
takeFieldMaybeWith k p = takeField_ k $ \case
Nothing -> pure Nothing
Just Null -> pure Nothing
Just v -> Just <$> parseJSON v
Just v -> Just <$> p v

takeFieldMaybe :: FromJSON a => Key -> ObjectParser (Maybe a)
takeFieldMaybe k = takeFieldMaybeWith k parseJSON

parseObjectTotal :: ObjectParser a -> Object -> Parser a
parseObjectTotal p o = do
Expand All @@ -86,8 +97,8 @@ parseObjectTotal p o = do
then pure r
else parseFail $ "unexpected keys: " <> intercalate ", " (map show $ KM.keys o')

withObjectTotal :: String -> ObjectParser a -> Value -> Parser a
withObjectTotal desc = withObject desc . parseObjectTotal
withObjectTotal :: Text -> ObjectParser a -> Value -> Parser a
withObjectTotal desc = withObject (Text.unpack desc) . parseObjectTotal

newtype CollapsedEither a b = CollapsedEither {getCollapsedEither :: Either a b}
deriving (Eq, Ord, Show)
Expand Down
10 changes: 6 additions & 4 deletions src/Mybox/Config/CommandLine.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import Options.Applicative

import Mybox.Prelude

data CommandLine = CommandLine {selfUpdate :: Bool} | CLVersion
data CommandLine = CommandLine {selfUpdate :: Bool} | CLVersion | CLPackageHelp
deriving (Eq, Generic, Show)

withoutSelfUpdate :: CommandLine -> CommandLine
withoutSelfUpdate commandLine@CommandLine{} = commandLine{selfUpdate = False}
withoutSelfUpdate CLVersion = CLVersion
withoutSelfUpdate commandLine = commandLine

optionsParser :: Parser CommandLine
optionsParser =
flag' CLVersion (long "version")
flag' CLVersion (long "version" <> help "Show version")
<|> flag' CLPackageHelp (long "package-help" <> help "Describe the package definition format")
<|> (CommandLine <$> flag True False (long "no-self-update" <> help "Do not update Mybox before installing packages"))

commandLineArgs :: CommandLine -> [Text]
commandLineArgs CLVersion = ["--version"]
commandLineArgs commandLine = ["--no-self-update" | not commandLine.selfUpdate]
commandLineArgs CLPackageHelp = ["--package-help"]
commandLineArgs CommandLine{selfUpdate} = ["--no-self-update" | not selfUpdate]

optionsInfo :: ParserInfo CommandLine
optionsInfo = info (optionsParser <**> helper) fullDesc
Expand Down
6 changes: 6 additions & 0 deletions src/Mybox/Display.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Mybox.Display (
MDisplay,
displayLogText,
displayVersion,
displayDoc,
) where

import Data.Version
Expand All @@ -24,6 +25,7 @@ import Mybox.Display.Data
import Mybox.Display.Print (Print)
import Mybox.Display.Print qualified as Print
import Mybox.Display.Simple
import Mybox.JSONCodec
import Mybox.Prelude

type AppDisplay = Display MDisplay
Expand All @@ -39,3 +41,7 @@ displayLogText = displayLog . MLog

displayVersion :: AppDisplay :> es => Version -> Eff es ()
displayVersion = displayLog . MVersion

-- | Display documentation of a JSON value, e.g. the package definition format.
displayDoc :: AppDisplay :> es => ValueDoc -> Eff es ()
displayDoc = displayLog . MDoc
41 changes: 40 additions & 1 deletion src/Mybox/Display/Data.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Mybox.Display.Data where

import Data.List (intersperse)
import Data.Aeson.Key qualified as Key
import Data.List (intercalate, intersperse)
import Data.Map qualified as Map
import Data.Set qualified as Set
import Data.Text qualified as Text
Expand All @@ -9,13 +10,15 @@ import Prelude hiding (log)

import Mybox.Display.Class
import Mybox.Display.Ops
import Mybox.JSONCodec
import Mybox.Prelude

data MDisplay

data instance Log MDisplay
= MLog Text
| MVersion Version
| MDoc ValueDoc

instance TerminalShow (Log MDisplay) where
terminalShow _ (MLog log) = [[tiMk log]]
Expand All @@ -26,6 +29,42 @@ instance TerminalShow (Log MDisplay) where
, tiMk $ Text.pack $ showVersion version
]
]
terminalShow width (MDoc doc) = tiWrapLines width $ docLines doc

-- | Render documentation of a JSON value, one section per object shape.
docLines :: ValueDoc -> [TerminalLine]
docLines (DocOneOf docs) = intercalate [[]] $ map docLines docs
docLines (DocObject name fields) = [(tiMk name){foreground = Just Green}] : fieldLines 1 fields
docLines doc = [[tiMk $ docType doc]]

fieldLines :: Int -> [FieldDoc] -> [TerminalLine]
fieldLines indent fields = concatMap fieldLine fields
where
fieldLine f =
[ tiMk $ Text.replicate (indent * 2) " "
, tiMk $ if f.required then "* " else " "
, (tiMk $ pad keyWidth $ Key.toText f.key){foreground = Just Blue}
, tiMk " "
, tiMk $ pad typeWidth $ docType f.value
, tiMk " "
, tiMk f.description
]
: nested f.value
nested (DocObject _ inner) = fieldLines (succ indent) inner
nested _ = []
keyWidth = width $ Key.toText . (.key)
typeWidth = width $ docType . (.value)
width f = maximum $ 0 : map (Text.length . f) fields
pad n = Text.justifyLeft n ' '

-- | Short description of the type of a JSON value.
docType :: ValueDoc -> Text
docType DocText = "text"
docType DocBool = "boolean"
docType (DocEnum values) = Text.intercalate " | " values
docType (DocList doc) = "[" <> docType doc <> "]"
docType (DocOneOf docs) = Text.intercalate " | " $ map docType docs
docType (DocObject name _) = name

data instance Banner MDisplay = MBanner
{ all :: Set Text
Expand Down
39 changes: 15 additions & 24 deletions src/Mybox/Filters.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Data.Map.Strict qualified as Map
import Data.Text qualified as Text

import Mybox.Aeson
import Mybox.JSONCodec
import Mybox.Prelude

choose_ :: Show a => [a -> Bool] -> [a] -> Either String a
Expand Down Expand Up @@ -49,35 +50,25 @@ data FilterFields = FilterFields
instance RecValue FilterFields where
rvEmpty = mempty

takeFilter :: ObjectParser FilterFields
takeFilter = do
prefixes <- takeCollapsedList "prefix"
suffixes <- takeCollapsedList "suffix"
includes <- takeCollapsedList "include"
excludes <- takeCollapsedList "exclude"
pure FilterFields{..}

parseFilter :: Object -> Parser FilterFields
parseFilter = parseObjectTotal takeFilter

filterToJSON :: FilterFields -> [Pair]
filterToJSON a =
[ "prefix" .= a.prefixes
, "suffix" .= a.suffixes
, "include" .= a.includes
, "exclude" .= a.excludes
]
instance JSONObjectCodec FilterFields where
jsonObjectCodec =
recordFields $
fieldDefault @"prefixes" "prefix" "Keep candidates starting with one of these"
:* fieldDefault @"suffixes" "suffix" "Keep candidates ending with one of these"
:* fieldDefault @"includes" "include" "Keep candidates containing one of these (case-insensitive)"
:* fieldDefault @"excludes" "exclude" "Drop candidates containing any of these (case-insensitive)"
:* Nil

-- | 'FilterFields' as a standalone JSON object, rather than keys of an
-- enclosing one.
newtype StandaloneFilterFields = StandaloneFilterFields FilterFields
deriving (Eq, Ord, Show)
instance JSONCodec FilterFields where
jsonCodec = objectCodec "FilterFields" jsonObjectCodec

instance FromJSON StandaloneFilterFields where
parseJSON = withObject "FilterFields" $ fmap StandaloneFilterFields . parseFilter
takeFilter :: ObjectParser FilterFields
takeFilter = jsonObjectCodec.parse

getStandaloneFilterFields :: Maybe StandaloneFilterFields -> FilterFields
getStandaloneFilterFields = fromMaybe mempty . fmap (\(StandaloneFilterFields a) -> a)
parseFilter :: Object -> Parser FilterFields
parseFilter = parseObjectTotal takeFilter

toFilters :: FilterFields -> [Text -> Bool]
toFilters a =
Expand Down
14 changes: 5 additions & 9 deletions src/Mybox/Installer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,16 @@ import Mybox.Installer.Class
import Mybox.Installer.DNF
import Mybox.Installer.Flatpak
import Mybox.Installer.MacOSCombined
import Mybox.JSONCodec
import Mybox.Package.System.Class
import Mybox.Prelude

data InstallerKind = Flatpak | Brew
deriving (Eq, Generic, Show)
deriving (Bounded, Enum, Eq, Generic, Show)
deriving (FromJSON, ToJSON) via ViaJSONCodec InstallerKind

instance FromJSON InstallerKind where
parseJSON = withText "InstallerKind" $ \case
"flatpak" -> pure Flatpak
"brew" -> pure Brew
other -> fail $ "Unknown installer kind: " <> show other

instance ToJSON InstallerKind where
toJSON = \case
instance JSONCodec InstallerKind where
jsonCodec = enumCodec "InstallerKind" $ \case
Flatpak -> "flatpak"
Brew -> "brew"

Expand Down
12 changes: 6 additions & 6 deletions src/Mybox/Installer/Brew.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import Mybox.Aeson
import Mybox.Driver
import Mybox.Effects
import Mybox.Installer.Class
import Mybox.JSONCodec
import Mybox.Package.Class
import Mybox.Package.Queue
import Mybox.Package.System.Class
import Mybox.Prelude

data BrewBootstrap s = BrewBootstrap deriving (Eq, Show)
data BrewBootstrap s = BrewBootstrap
deriving (Eq, Generic, Show)
deriving (FromJSON, ToJSON) via ViaJSONCodec (BrewBootstrap s)

instance PackageName (BrewBootstrap s) where
splitName _ = ("homebrew", Nothing)

instance FromJSON (BrewBootstrap s) where
parseJSON = withObjectTotal "BrewBootstrap" $ pure BrewBootstrap

instance ToJSON (BrewBootstrap s) where
toJSON BrewBootstrap = object []
instance JSONCodec (BrewBootstrap s) where
jsonCodec = jsonCodecObject "BrewBootstrap" Nil

homebrewDirectory :: Driver :> es => Eff es (Path Abs)
homebrewDirectory = flip fmap drvOS $ \case
Expand Down
Loading
Loading