haskell-gha-1.0: src/HaskellGha/Command/Header.hs
-- | The header of a workflow file. It holds the command that generates the
-- file, so a run without @--generate@ can regenerate the file.
module HaskellGha.Command.Header
( -- * Writing
renderWorkflow
-- * Reading
, Header (..)
, readHeader
) where
import Data.Bifunctor
import Data.ByteString qualified as BS
import Data.Text qualified as T
import Data.Text.Encoding qualified as T
import Yamlet.Syntax
import HaskellGha.Command.Options
import HaskellGha.Workflow.Script
import HaskellGha.Yaml
-- | Render the workflow with its header comment.
renderWorkflow :: Options -> Node -> T.Text
renderWorkflow opts =
renderDocument
-- The header has no tool version, so that two versions with the same output
-- agree in --check.
[ headerMarker
, " " <> T.unwords (map (shellQuote . T.pack) $ commandLine opts)
, ""
, "To regenerate it, run from the root of the repository:"
, " haskell-gha"
, ""
, "For more information, see https://github.com/arybczak/haskell-gha"
]
separated
where
separated :: [T.Text] -> Bool
separated = \case
[] -> True
["jobs"] -> True
["jobs", _, "steps"] -> True
_ -> False
-- | The first line of the header. The next line has the command.
headerMarker :: T.Text
headerMarker = "Do not edit this file, it was generated by the following command:"
-- | The header of a workflow file.
data Header
= -- | The first line is not the marker, so the tool did not generate the
-- file.
NoHeader
| -- | The tool generated the file, but the header is broken.
BrokenHeader String
| -- | The options of the command in the header.
Header Options
-- | Read the header of a workflow file. The output of the command must be the
-- file itself.
readHeader
:: FilePath
-- ^ The name of the file in 'workflowDirectory'.
-> BS.ByteString
-> Header
readHeader name bytes = case T.lines (T.decodeUtf8Lenient bytes) of
marker : rest
| commentText marker == Just headerMarker -> either BrokenHeader Header $ case rest of
command : _
| Just text <- commentText command -> do
args <-
maybe (Left "the command in the header has a quote without its end") Right $
shellWords (T.unpack text)
opts <- first ("the command in the header is not valid: " ++) (parseCommandLine args)
if opts.output == name
then Right opts
else
Left $
"the command in the header writes the workflow to "
++ outputPath opts
++ ", not to this file"
_ -> Left "the header has no command on its second line"
_ -> NoHeader
where
commentText :: T.Text -> Maybe T.Text
commentText = fmap T.strip . T.stripPrefix "#"
-- The words of a command line from 'shellQuote'.
shellWords :: String -> Maybe [String]
shellWords s = case dropWhile (== ' ') s of
"" -> Just []
s' -> do
(w, rest) <- shellWord s'
(w :) <$> shellWords rest
shellWord :: String -> Maybe (String, String)
shellWord = \case
'\'' : rest -> case break (== '\'') rest of
(quoted, _ : rest') -> first (quoted ++) <$> shellWord rest'
(_, []) -> Nothing
'\\' : c : rest -> first (c :) <$> shellWord rest
c : rest | c /= ' ' -> first (c :) <$> shellWord rest
rest -> Just ("", rest)