zwirn-0.2.2.0: app/zwirnzi/CI/Setup.hs
module CI.Setup (setup) where
{-
Setup.hs - setup of the various components of the backend
Copyright (C) 2023, Martin Gius
This library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this library. If not, see <http://www.gnu.org/licenses/>.
-}
import CI.Config as C
import Control.Concurrent (forkIO)
import Control.Monad (void, when)
import qualified Data.Map as Map
import qualified Data.Text as T
import System.Directory.OsPath
import System.IO (hPutStrLn, stderr)
import System.OsPath
import Zwirn.Language.Builtin.Prelude
import Zwirn.Language.Compiler as Compiler
import Zwirn.Language.Macro (defaultMacroMap)
import Zwirn.Stream.Handshake (sendHandshake)
import Zwirn.Stream.Listen
import Zwirn.Stream.Target (Target (..))
import Zwirn.Stream.Types
import Zwirn.Stream.UI
setup :: FullConfig -> IO Environment
setup config = do
str <- setupStream config
when (ciConfigListener $ fullConfigCi config) (setupListener str)
let initE = getInitialEnv (toCiConfig $ fullConfigCi config) str
checkBoot (fullConfigCi config) initE
setupStream :: FullConfig -> IO Stream
setupStream config = startStream (toStream $ fullConfigStream config)
setupListener :: Stream -> IO ()
setupListener str = do
case Map.lookup "superdirt" (sTargetMap str) of
Nothing -> return ()
Just (Target _ _ addr _) -> sendHandshake (sLocal str) addr
void (forkIO $ listen str)
getInitialEnv :: Compiler.CiConfig -> Stream -> Environment
getInitialEnv config str = Environment str (builtinEnvironmentWithStream str) (Just $ ConfigEnv configPath resetConfig) config defaultMacroMap
checkBoot :: C.CiConfig -> Environment -> IO Environment
checkBoot (C.CiConfig "" _ _ _ _) env = hPutStrLn stderr "Starting without Bootfile." >> return env
checkBoot (C.CiConfig path _ _ _ _) env = do
ospath <- encodeUtf path
isfile <- doesFileExist ospath
ps <-
if isfile
then return $ decodeUtf ospath
else do
isfolder <- doesDirectoryExist ospath
if isfolder
then do
pss <- listDirectory ospath
fs <- mapM decodeUtf pss
return $ map (\f -> path ++ "/" ++ f) fs
else return []
res <- runCI env (compilerInterpreterBoot $ map T.pack ps)
case res of
Left (CIError err newEnv) -> hPutStrLn stderr ("Error in Bootfile: " ++ show err) >> return newEnv
Right newEnv ->
if ps /= []
then hPutStrLn stderr ("Successfully loaded Bootfiles from " ++ path) >> return newEnv
else hPutStrLn stderr ("No Bootfiles found at " ++ path) >> return newEnv