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 :: 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)
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