cabal-flatpak-1.0.0: src/domain/Distribution/Flatpak/Stub.hs
module Distribution.Flatpak.Stub
( BuildMode (..)
, Origin (..)
, StubContext (..)
, IconInfo (..)
, haskellSourcesFile
, manifest
, metainfo
, desktop
, stubFiles
) where
import Data.Char
import Data.Maybe
import Data.Monoid.HT qualified as Mn
import Data.Text (Text)
import Data.Text qualified as T
import Yamlet qualified as Y
import Yamlet.Syntax qualified as S
import Distribution.Flatpak.App
import Distribution.Flatpak.AppId
import Distribution.Flatpak.Generate
data BuildMode = SetupMode | CabalMode
data Origin = Forge | LocalDir
data StubContext = StubContext
{ appId :: AppId
, app :: App
, packageDir :: Maybe FilePath
, buildMode :: BuildMode
, origin :: Origin
, icon :: Maybe IconInfo
}
data IconInfo = IconInfo
{ sourceFile :: Text
, extension :: Text
, destDir :: FilePath
}
localInfix :: Origin -> Text
localInfix = \case
Forge -> ""
LocalDir -> ".local"
haskellSourcesFile :: Origin -> FilePath
haskellSourcesFile origin = T.unpack ("haskell-sources" <> localInfix origin <> ".yaml")
stubFiles :: StubContext -> [(FilePath, Text)]
stubFiles ctx =
[ (T.unpack (ident <> ".yaml"), manifest ctx)
, (T.unpack (ident <> ".metainfo.xml"), metainfo ctx)
, (T.unpack (ident <> ".desktop"), desktop ctx)
]
where
ident = ctx.appId.value <> localInfix ctx.origin
manifest :: StubContext -> Text
manifest ctx =
S.renderSyntax S.defaultRenderOptions [S.document root]
where
app = ctx.app
ident = ctx.appId.value
fileIdent = ident <> localInfix ctx.origin
executable = fromMaybe app.name (command app)
root =
Y.mapping
[ "id" Y..= fixmes (Mn.when ctx.appId.placeholder ["choose an application ID you own."]) (str ident)
, "runtime" Y..= str "org.freedesktop.Platform"
, "runtime-version" Y..= str "26.08"
, "sdk" Y..= str "org.freedesktop.Sdk"
, "command" Y..= fixmes (Mn.when (isNothing (command app)) ["set the executable to run."]) (str executable)
, "finish-args"
Y..= fixmes
["keep only the permissions that the application needs."]
(map str ["--socket=wayland", "--socket=fallback-x11", "--share=ipc", "--device=dri"])
, "cleanup" Y..= map str cleanup
, "modules" Y..= [str (T.pack (haskellSourcesFile ctx.origin)), appModule]
]
appModule =
Y.mapping
[ "name" Y..= str app.name
, "buildsystem" Y..= fixmes subdirFixme (str "simple")
, "build-commands"
Y..= ( map str buildCommands
<> [ str ("install -Dm644 " <> fileIdent <> ".metainfo.xml /app/share/metainfo/" <> ident <> ".metainfo.xml")
, str ("install -Dm644 " <> fileIdent <> ".desktop /app/share/applications/" <> ident <> ".desktop")
, iconCommand
]
)
, "sources" Y..= (appSource : inlineProject)
]
installIcon icon =
"install -Dm644 "
<> icon.sourceFile
<> " "
<> T.pack icon.destDir
<> ident
<> icon.extension
iconCommand = case ctx.icon of
Nothing ->
fixmeNode
["supply the icon at this path, or change the path."]
(str (installIcon $ IconInfo (ident <> ".svg") ".svg" "/app/share/icons/hicolor/scalable/apps/"))
Just icon -> str (installIcon icon)
subdirFixme =
foldMap
( \dir ->
[ "the package is in "
<> T.pack dir
<> "/; add `subdir: "
<> T.pack dir
<> "` to this module and adjust the install paths below."
]
)
setupSubdir
appSource = case ctx.origin of
Forge ->
Y.mapping
[ "type" Y..= str "git"
, "url"
Y..= fixmes
repoFixme
(str (maybe "https://example.org/FIXME.git" (.location) app.sourceRepo))
, "tag" Y..= fixmes ["set the tag and the commit of the release."] (str ("v" <> app.version))
, "commit" Y..= str "0000000000000000000000000000000000000000"
]
LocalDir ->
Y.mapping
[ "type" Y..= str "dir"
, "path" Y..= str "."
, "skip" Y..= map str ["dist-newstyle", ".flatpak-builder"]
]
repoFixme = case app.sourceRepo of
Nothing -> ["set the Git URL."]
Just repo
| repo.git -> []
| otherwise -> ["the head source-repository is not Git; set a Git URL, or change the source type."]
setupSubdir = case ctx.buildMode of
SetupMode -> ctx.packageDir
CabalMode -> Nothing
cleanup = case ctx.buildMode of
SetupMode ->
[ "/lib/*-linux-ghc-*"
, "/share/doc/*-linux-ghc-*"
]
CabalMode ->
[ "/lib/haskell-repo"
, "/lib/cabal-store/*/package.db"
, "/lib/cabal-store/*/*/lib"
, "/lib/cabal-store/*/*/bin"
]
buildCommands = case ctx.buildMode of
SetupMode ->
setupCommands []
CabalMode ->
[ "cabal --config-file=/app/lib/haskell-repo/cabal.config --project-file=flatpak.project install exe:"
<> executable
<> " --installdir=/app/bin --install-method=copy -j$FLATPAK_BUILDER_N_JOBS"
]
inlineProject = case ctx.buildMode of
SetupMode -> []
CabalMode ->
[ Y.mapping
[ "type" Y..= str "inline"
, "dest-filename" Y..= str "flatpak.project"
, "contents"
Y..= S.scalarNode
S.Literal
( T.unlines
[ "packages: " <> maybe "./" (\dir -> T.pack dir <> "/") ctx.packageDir
, "import: /app/lib/haskell-repo/freeze.project"
]
)
]
]
str :: Text -> S.Node
str = Y.toYaml
fixmeComments :: [Text] -> S.Comments
fixmeComments notes = S.noComments {S.before = map (\note -> S.Comment ("FIXME: " <> note)) notes}
fixmes :: [Text] -> a -> Y.Commented a
fixmes notes value = Y.Commented value (fixmeComments notes)
fixmeNode :: [Text] -> S.Node -> S.Node
fixmeNode notes = S.withComments (fixmeComments notes)
metainfo :: StubContext -> Text
metainfo ctx =
T.unlines $
mconcat
[
[ """
<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop-application">
"""
]
, Mn.when ctx.appId.placeholder [" <!-- FIXME: choose an application ID you own. -->"]
,
[ " <id>" <> esc ctx.appId.value <> "</id>"
, indent
2
"""
<!-- FIXME: check the metadata license. -->
<metadata_license>CC0-1.0</metadata_license>
"""
, " <project_license>" <> esc app.license <> "</project_license>"
, " <name>" <> esc app.name <> "</name>"
]
, Mn.when (T.null summary) [" <!-- FIXME: write a one-line summary. -->"]
,
[ " <summary>" <> esc (orFixme summary) <> "</summary>"
, " <!-- FIXME: set the developer ID (reverse DNS). -->"
]
, Mn.when (T.null developer) [" <!-- FIXME: set the developer name. -->"]
,
[ " <developer id=\"com.example\"><name>" <> esc (orFixme developer) <> "</name></developer>"
, indent
2
"""
<description>
<!-- FIXME: rewrite the description; Haddock markup is not converted. -->
"""
]
, map (\p -> " <p>" <> esc p <> "</p>") (orFixmeList (paragraphs app.description))
, [" </description>"]
, foldMap (\url -> [" <url type=\"homepage\">" <> esc url <> "</url>"]) app.homepage
,
[ " <launchable type=\"desktop-id\">" <> esc ctx.appId.value <> ".desktop</launchable>"
, indent
2
"""
<!-- FIXME: add screenshots. -->
<!-- FIXME: fill in the content rating with https://hughsie.github.io/oars/ -->
<content_rating type="oars-1.1"/>
<releases>
<!-- FIXME: set the release date. -->
"""
, " <release version=\"" <> esc app.version <> "\" date=\"1970-01-01\"/>"
, " </releases>"
, "</component>"
]
]
where
app = ctx.app
summary = oneLine app.synopsis
developer =
let name = stripEmail app.maintainer
in if T.null name then stripEmail app.author else name
desktop :: StubContext -> Text
desktop ctx =
T.unlines $
mconcat
[
[ """
[Desktop Entry]
Type=Application
"""
, "Name=" <> app.name
]
, Mn.when (T.null comment) ["# FIXME: write a one-line comment."]
,
[ "Comment=" <> orFixme comment
]
, Mn.when (isNothing (command app)) ["# FIXME: set the executable to run."]
,
[ "Exec=" <> fromMaybe app.name (command app)
, "Icon=" <> ctx.appId.value
, """
# FIXME: set freedesktop categories, see https://specifications.freedesktop.org/menu-spec/latest/category-registry.html
Categories=Utility;
"""
]
]
where
app = ctx.app
comment = oneLine app.synopsis
indent :: Int -> Text -> Text
indent n = T.intercalate "\n" . map (T.replicate n " " <>) . T.lines
orFixme :: Text -> Text
orFixme t = if T.null t then "FIXME" else t
orFixmeList :: [Text] -> [Text]
orFixmeList [] = ["FIXME"]
orFixmeList ts = ts
oneLine :: Text -> Text
oneLine = T.unwords . T.words
paragraphs :: Text -> [Text]
paragraphs = filter (not . T.null) . map (oneLine . T.unlines) . go [] . T.lines
where
go acc [] = [reverse acc]
go acc (l : ls)
| T.all isSpace l || T.stripStart l == "." = reverse acc : go [] ls
| otherwise = go (l : acc) ls
stripEmail :: Text -> Text
stripEmail = T.strip . T.takeWhile (/= '<')
esc :: Text -> Text
esc = T.concatMap escChar
where
escChar = \case
'&' -> "&"
'<' -> "<"
'>' -> ">"
'"' -> """
'\'' -> "'"
c -> T.singleton c