-- | Scratch files for the actions that must produce a branch that compiles (@update@, @delete@, @merge@, @pull@ and
-- @upgrade@). When one of them can't, it leaves the user on a temporary branch, with a scratch file of the definitions
-- that need fixing. That file gets its own name, like @merge-bob-into-alice-a0458b71.u@, rather than reusing
-- @scratch.u@, so that it is unmistakable which file goes with which action and branch.
module Unison.Cli.ScratchFile
  ( scratchFileName,
    writeActionScratchFile,
  )
where

import Control.Lens ((?=))
import Control.Monad.Reader (ask)
import Crypto.Hash qualified as Crypto
import Data.ByteArray.Encoding qualified as BE
import Data.Char (isAlphaNum, isAscii)
import Data.Text qualified as Text
import Data.Text.Encoding qualified as Text
import System.FilePath (replaceFileName)
import Unison.Cli.Monad (Cli, Env (..))
import Unison.Cli.MonadUtils qualified as Cli
import Unison.Prelude

-- | @scratchFileName action words parts@ is @\<action\>-\<words\>-\<shorthash\>.u@. The @words@ say what the action is
-- about, like the branches involved (they are made safe for a file name, and shortened). The short hash (8 characters)
-- is of the action, the @words@ and the @parts@: the arguments, and the hashes of the branches involved. The same
-- action on the same branches therefore always gets the same file name, and a different one gets a different file name.
scratchFileName :: Text -> [Text] -> [Text] -> Text
scratchFileName :: Text -> [Text] -> [Text] -> Text
scratchFileName Text
action [Text]
words_ [Text]
parts =
  Text -> [Text] -> Text
Text.intercalate Text
"-" (Text
action Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
safe [Text]
words_ [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Int -> Text -> Text
Text.take Int
8 Text
shortHash]) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".u"
  where
    safe :: Text -> Text
    safe :: Text -> Text
safe =
      Int -> Text -> Text
Text.take Int
30 (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Char) -> Text -> Text
Text.map \Char
c -> if Char -> Bool
isAscii Char
c Bool -> Bool -> Bool
&& (Char -> Bool
isAlphaNum Char
c Bool -> Bool -> Bool
|| Char
c Char -> FilePath -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (FilePath
"._-" :: String)) then Char
c else Char
'-'

    shortHash :: Text
    shortHash :: Text
shortHash =
      Text -> [Text] -> Text
Text.intercalate Text
"\0" (Text
action Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: [Text]
words_ [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text]
parts)
        Text -> (Text -> ByteString) -> ByteString
forall a b. a -> (a -> b) -> b
& Text -> ByteString
Text.encodeUtf8
        ByteString -> (ByteString -> Digest SHA256) -> Digest SHA256
forall a b. a -> (a -> b) -> b
& SHA256 -> ByteString -> Digest SHA256
forall ba alg.
(ByteArrayAccess ba, HashAlgorithm alg) =>
alg -> ba -> Digest alg
Crypto.hashWith SHA256
Crypto.SHA256
        Digest SHA256 -> (Digest SHA256 -> ByteString) -> ByteString
forall a b. a -> (a -> b) -> b
& Base -> Digest SHA256 -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
Base -> bin -> bout
BE.convertToBase Base
BE.Base16
        ByteString -> (ByteString -> Text) -> Text
forall a b. a -> (a -> b) -> b
& (ByteString -> Text
Text.decodeUtf8 :: ByteString -> Text)

-- | Write @contents@ to the scratch file for @action@ (see 'scratchFileName'), in the directory of the latest file (or
-- the current directory), and make it the latest file. Returns the file's path.
writeActionScratchFile :: Text -> [Text] -> [Text] -> Text -> Cli FilePath
writeActionScratchFile :: Text -> [Text] -> [Text] -> Text -> Cli FilePath
writeActionScratchFile Text
action [Text]
words_ [Text]
parts Text
contents = do
  env <- Cli Env
forall r (m :: * -> *). MonadReader r m => m r
ask
  path <-
    Cli.getLatestFile <&> \case
      Maybe (FilePath, Bool)
Nothing -> Text -> FilePath
Text.unpack Text
name
      Just (FilePath
latestFile, Bool
_) -> FilePath -> FilePath -> FilePath
replaceFileName FilePath
latestFile (Text -> FilePath
Text.unpack Text
name)
  #latestFile ?= (path, True)
  liftIO
    if env.unattended
      then writeUtf8 path contents
      else env.writeSource (Text.pack path) contents True
  pure path
  where
    name :: Text
name = Text -> [Text] -> [Text] -> Text
scratchFileName Text
action [Text]
words_ [Text]
parts