packages feed

cabal-flatpak-1.0.0: src/domain/Distribution/Flatpak.hs

module Distribution.Flatpak
  ( SourceType (..)
  , archive
  , Source (..)
  , Module (..)
  , Buildsystem (..)
  , buildsystemString
  , Arch (..)
  , archString
  , encodeYaml
  ) where

import Data.ByteString (ByteString)
import Data.Monoid.HT qualified as Mn
import Data.Text (Text)
import Data.Text qualified as Text
import Yamlet qualified as Y
import Yamlet.Syntax qualified as S

import Cabal.Plan qualified as Plan

-- | Source types accepted by flatpak-builder.
data SourceType
  = -- | Archive with its `strip-components` value.
    Archive Int
  | Git
  | Bzr
  | Svn
  | File
  | Dir
  | -- | Script with its commands.
    Script [Text]
  | -- | Inline source with its contents.
    Inline Text
  | Shell
  | Patch
  | ExtraData
  deriving stock (Show)

-- | Archive with flatpak-builder's default `strip-components` of 1.
-- See https://docs.flatpak.org/en/latest/module-sources.html.
archive :: SourceType
archive = Archive 1

sourceTypeName :: SourceType -> Text
sourceTypeName = \case
  Archive _ -> "archive"
  Git -> "git"
  Bzr -> "bzr"
  Svn -> "svn"
  File -> "file"
  Dir -> "dir"
  Script _ -> "script"
  Inline _ -> "inline"
  Shell -> "shell"
  Patch -> "patch"
  ExtraData -> "extra-data"

data Source
  = Source
  { typ :: SourceType
  , url :: Maybe Text
  , path :: Maybe Text
  , destFilename :: Maybe Text
  , dest :: Maybe Text
  , sha256 :: Maybe Plan.Sha256
  }
  deriving stock (Show)

instance Y.ToYaml Source where
  toYaml source =
    Y.mapping $
      ["type" Y..= sourceTypeName source.typ]
        <> optional "url" source.url
        <> optional "path" source.path
        <> optional "sha256" (fmap Plan.dispSha256 source.sha256)
        <> stripComponents
        <> optional "dest" source.dest
        <> optional "dest-filename" source.destFilename
        <> body
    where
      stripComponents = case source.typ of
        Archive strip -> Mn.when (strip /= 1) ["strip-components" Y..= strip]
        (Git; Bzr; Svn; File; Dir; Script _; Inline _; Shell; Patch; ExtraData) -> []
      body = case source.typ of
        Script cmds -> ["commands" Y..= cmds]
        Inline contents -> ["contents" Y..= contents]
        (Archive _; Git; Bzr; Svn; File; Dir; Shell; Patch; ExtraData) -> []

-- | Architectures that Flathub builds for.
data Arch = X86_64 | AArch64
  deriving stock (Show, Eq, Ord, Enum, Bounded)

-- | Architecture name, as used by Flatpak, GHC and cabal-install.
archString :: Arch -> String
archString arch =
  case arch of
    X86_64 -> "x86_64"
    AArch64 -> "aarch64"

instance Y.ToYaml Arch where
  toYaml = Y.toYaml . Text.pack . archString

-- | Build systems accepted by flatpak-builder.
data Buildsystem
  = Autotools
  | CMake
  | CMakeNinja
  | Meson
  | QMake
  | Simple
  deriving stock (Show, Eq, Enum, Bounded)

buildsystemString :: Buildsystem -> String
buildsystemString buildsystem =
  case buildsystem of
    Autotools -> "autotools"
    CMake -> "cmake"
    CMakeNinja -> "cmake-ninja"
    Meson -> "meson"
    QMake -> "qmake"
    Simple -> "simple"

instance Y.ToYaml Buildsystem where
  toYaml = Y.toYaml . Text.pack . buildsystemString

data Module
  = Module
  { name :: Text
  , onlyArches :: [Arch]
  , buildsystem :: Buildsystem
  , builddir :: Bool
  , configOpts :: [Text]
  , buildCommands :: [Text]
  , postInstall :: [Text]
  , cleanupModule :: [Text]
  , sources :: [Source]
  , modules :: [Module]
  }
  deriving stock (Show)

instance Y.ToYaml Module where
  toYaml m =
    Y.mapping $
      ["name" Y..= m.name]
        <> nonEmpty "only-arches" m.onlyArches
        <> ["buildsystem" Y..= m.buildsystem, "builddir" Y..= m.builddir]
        <> nonEmpty "config-opts" m.configOpts
        <> nonEmpty "build-commands" m.buildCommands
        <> nonEmpty "post-install" m.postInstall
        <> nonEmpty "cleanup" m.cleanupModule
        <> nonEmpty "sources" m.sources
        <> nonEmpty "modules" m.modules

optional :: (Y.ToYaml a) => Text -> Maybe a -> [(S.Node, S.Node)]
optional key = foldMap (\value -> [key Y..= value])

nonEmpty :: (Y.ToYaml a) => Text -> [a] -> [(S.Node, S.Node)]
nonEmpty key values = Mn.when (not (null values)) [key Y..= values]

encodeYaml :: Module -> ByteString
encodeYaml = Y.encode