slackbuilder/app/Main.hs

58 lines
2.2 KiB
Haskell
Raw Normal View History

module Main
( main
) where
import qualified Data.Text.IO as Text.IO
import Data.Maybe (fromMaybe)
import Options.Applicative (execParser)
import SlackBuilder.CommandLine
2023-08-09 20:59:42 +02:00
import SlackBuilder.Config
2023-08-15 10:33:19 +02:00
import SlackBuilder.Trans
import SlackBuilder.Updater
2023-08-09 20:59:42 +02:00
import qualified Toml
import qualified Data.Text as Text
2023-08-15 10:33:19 +02:00
import Control.Monad.Trans.Reader (ReaderT(..))
import SlackBuilder.Download
2023-08-25 10:30:24 +02:00
import Text.URI (mkURI)
main :: IO ()
main = do
programCommand <- execParser slackBuilderParser
2023-08-09 20:59:42 +02:00
settings <- Toml.decodeFile settingsCodec "config/config.toml"
2023-08-15 10:33:19 +02:00
latestVersion <- flip runReaderT settings
$ runSlackBuilderT
$ executeCommand programCommand
Text.IO.putStrLn $ fromMaybe "" latestVersion
2023-08-09 20:59:42 +02:00
where
2023-08-15 10:33:19 +02:00
executeCommand = \case
PackagistCommand packagistArguments ->
latestPackagist packagistArguments
TextCommand textArguments -> latestText textArguments
GhCommand ghArguments@GhArguments{ transform }
-> latestGitHub ghArguments $ chooseTransformFunction transform
SlackBuildCommand packagePath version ->
updateSlackBuildVersion packagePath version >> pure Nothing
CommitCommand packagePath version ->
commit packagePath version >> pure Nothing
ExistsCommand urlPath -> pure . Text.pack . show
<$> remoteFileExists urlPath
ArchiveCommand repo nameVersion tarball tagPrefix ->
cloneAndArchive repo nameVersion tarball tagPrefix >> pure Nothing
2023-08-25 10:30:24 +02:00
DownloadCommand url target
| Just uri <- mkURI url -> fmap (Text.pack . show)
<$> download uri target
| otherwise -> pure Nothing
CloneCommand repo tarball tagPrefix -> fmap (Text.pack . show)
<$> clone repo tarball tagPrefix
2023-08-10 12:47:43 +02:00
chooseTransformFunction (Just "php") = phpTransform
chooseTransformFunction (Just "rdiff-backup") = Text.stripPrefix "v"
chooseTransformFunction _ = stripPrefix "v"
2023-08-09 20:59:42 +02:00
stripPrefix prefix string = Just
$ fromMaybe string
$ Text.stripPrefix prefix string
2023-08-10 12:47:43 +02:00
phpTransform version
2023-08-21 13:38:20 +02:00
| (majorPrefix, _patchVersion) <- Text.breakOnEnd "." version
2023-08-10 12:47:43 +02:00
, majorPrefix == "php-8.2." = Just $ Text.drop (Text.length "php-") version
| otherwise = Nothing