module IHP.IDE.CodeGen.ScriptGenerator (buildPlan) where

import IHP.Prelude
import IHP.IDE.CodeGen.Types

buildPlan :: Text -> Either Text [GeneratorAction]
buildPlan :: Text -> Either Text [GeneratorAction]
buildPlan Text
scriptName =
    if Text -> Bool
forall mono. MonoFoldable mono => mono -> Bool
null Text
scriptName
        then Text -> Either Text [GeneratorAction]
forall a b. a -> Either a b
Left Text
"Script name cannot be empty"
        else do 
            let filePath :: Text
filePath = Text
"Application/Script/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
forall a b. ConvertibleStrings a b => a -> b
cs Text
scriptName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".hs"
            let fileContent :: Text
fileContent = Text -> Text
renderScript Text
scriptName
            [GeneratorAction] -> Either Text [GeneratorAction]
forall a b. b -> Either a b
Right [ CreateFile { Text
filePath :: Text
$sel:filePath:CreateFile :: Text
filePath, Text
fileContent :: Text
$sel:fileContent:CreateFile :: Text
fileContent }
                  , RunShellCommand { $sel:shellCommand:CreateFile :: Text
shellCommand = Text
"chmod +x " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
filePath }
                  ]

renderScript :: Text -> Text
renderScript :: Text -> Text
renderScript Text
scriptName' = String -> Text
forall a b. ConvertibleStrings a b => a -> b
cs [plain|#!/usr/bin/env run-script
module Application.Script.#{taskName} where

import Application.Script.Prelude

run :: Script
run = do
    putStrLn "Hello World!"
|]
    where String
taskName :: String = Text -> String
forall a b. ConvertibleStrings a b => a -> b
cs Text
scriptName'