1module Command.Publish (command) where
2
3import Prelude
4
5import           Control.Monad.IO.Class (liftIO)
6import qualified Data.Aeson as A
7import qualified Data.ByteString.Lazy.Char8 as BL
8import           Data.Time.Clock (getCurrentTime)
9import           Data.Version (Version(..))
10import           Language.PureScript.Publish
11import           Language.PureScript.Publish.ErrorsWarnings
12import           Options.Applicative (Parser)
13import qualified Options.Applicative as Opts
14
15data PublishOptionsCLI = PublishOptionsCLI
16  { cliManifestPath :: FilePath
17  , cliResolutionsPath :: FilePath
18  , cliCompileOutputDir :: FilePath
19  , cliDryRun :: Bool
20  }
21
22manifestPath :: Parser FilePath
23manifestPath = Opts.strOption $
24     Opts.long "manifest"
25  <> Opts.metavar "FILE"
26  <> Opts.help "The package manifest file"
27
28resolutionsPath :: Parser FilePath
29resolutionsPath = Opts.strOption $
30     Opts.long "resolutions"
31  <> Opts.metavar "FILE"
32  <> Opts.help "The resolutions file"
33
34dryRun :: Parser Bool
35dryRun = Opts.switch $
36     Opts.long "dry-run"
37  <> Opts.help "Produce no output, and don't require a tagged version to be checked out."
38
39compileOutputDir :: Opts.Parser FilePath
40compileOutputDir = Opts.option Opts.auto $
41     Opts.value "output"
42  <> Opts.showDefault
43  <> Opts.long "compile-output"
44  <> Opts.metavar "DIR"
45  <> Opts.help "Compiler output directory"
46
47cliOptions :: Opts.Parser PublishOptionsCLI
48cliOptions =
49  PublishOptionsCLI <$> manifestPath <*> resolutionsPath <*> compileOutputDir <*> dryRun
50
51mkPublishOptions :: PublishOptionsCLI -> PublishOptions
52mkPublishOptions cliOpts =
53  let
54    opts =
55      defaultPublishOptions
56        { publishManifestFile = cliManifestPath cliOpts
57        , publishResolutionsFile = cliResolutionsPath cliOpts
58        , publishCompileOutputDir = cliCompileOutputDir cliOpts
59        }
60  in
61    if cliDryRun cliOpts
62      then
63        opts
64          { publishGetVersion = return ("0.0.0", Version [0,0,0] [])
65          , publishGetTagTime = const (liftIO getCurrentTime)
66          , publishWorkingTreeDirty = warn DirtyWorkingTreeWarn
67          }
68      else
69        opts
70
71command :: Opts.Parser (IO ())
72command = publish <$> (Opts.helper <*> cliOptions)
73
74publish :: PublishOptionsCLI -> IO ()
75publish cliOpts = do
76  let opts = mkPublishOptions cliOpts
77  pkg <- unsafePreparePackage opts
78  if cliDryRun cliOpts
79    then putStrLn "Dry run completed, no errors."
80    else BL.putStrLn (A.encode pkg)
81