packages feed

cabal-flatpak-1.0.0: src/application/Application/CLI/Option.hs

module Application.CLI.Option
  ( Command (..)
  , GenerateOptions (..)
  , Selection (..)
  , selection
  , info
  ) where

import Data.Set (Set)
import Data.Set qualified as Set
import Data.Text
import Options.Applicative
  ( help
  , long
  , metavar
  , option
  , value
  )
import Options.Applicative qualified as OP
import Shell.Utility.ParseArgument qualified as ParseArg

import Distribution.Flatpak qualified as Flatpak

newtype Command = Generate GenerateOptions
  deriving stock (Show)

data GenerateOptions = GenerateOptions
  { dependencies, manifest, force :: Bool
  , projectDir :: Maybe FilePath
  , cabalInstall :: Bool
  , local :: Bool
  , icon :: Maybe Text
  , archs :: Set Flatpak.Arch
  }
  deriving stock (Show)

data Selection = Selection
  { wantDependencies :: Bool
  , wantManifest :: Bool
  }
  deriving stock (Show, Eq)

selection :: GenerateOptions -> Selection
selection opts
  | not opts.dependencies && not opts.manifest = Selection True True
  | otherwise = Selection opts.dependencies opts.manifest

arch :: OP.ReadM Flatpak.Arch
arch =
  OP.eitherReader $ \str ->
    maybe (Left $ "unknown architecture: " ++ str) Right $
      ParseArg.enumMaybe Flatpak.archString str

parseGenerate :: OP.Parser GenerateOptions
parseGenerate =
  GenerateOptions
    <$> OP.switch
      ( long "dependencies"
          <> help "Generate haskell-sources.yaml only"
      )
    <*> OP.switch
      ( long "manifest"
          <> help "Generate the Flathub stub files only"
      )
    <*> OP.switch
      ( long "force"
          <> help "Overwrite existing files"
      )
    <*> option
      (Just <$> OP.str)
      ( metavar "DIR"
          <> value Nothing
          <> long "directory"
          <> help "Directory where the search for the project root starts (default: PWD)"
      )
    <*> OP.switch
      ( long "cabal-install"
          <> help "Build dependencies with cabal-install in one module (parallel)"
      )
    <*> OP.switch
      ( long "local"
          <> help "Build the application from the project directory and write *.local.* files"
      )
    <*> OP.optional
      ( OP.strOption
          ( metavar "FILE"
              <> long "icon"
              <> help "SVG or PNG icon to install, relative to the project directory"
          )
      )
    <*> fmap
      Set.fromList
      ( OP.many
          ( option
              arch
              ( metavar "ARCH"
                  <> long "arch"
                  <> help "Architecture to build for"
              )
          )
      )

parseCommand :: OP.Parser Command
parseCommand =
  OP.hsubparser $
    OP.command
      "generate"
      ( OP.info
          (Generate <$> parseGenerate)
          (OP.progDesc "Generate haskell-sources.yaml and stub Flathub files")
      )

desc :: OP.InfoMod a
desc =
  OP.fullDesc
    <> OP.progDesc "Generate FlatPak modules from a Cabal package description"

info :: OP.ParserInfo Command
info = OP.info (OP.helper <*> parseCommand) desc