{- -----------------------------------------------------------------------------
Copyright 2020-2021 Kevin P. Barry

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
----------------------------------------------------------------------------- -}

-- Author: Kevin P. Barry [ta0kira@gmail.com]

module Config.LoadConfig (
  localConfigPath,
  loadConfig,
  saveConfig,
) where

import Control.Monad (when)
import Control.Monad.IO.Class
import System.Directory
import System.IO

import Base.CompilerError
import Config.LocalConfig
import Config.ParseConfig ()
import Module.ParseMetadata

import Paths_zeolite_lang (getDataFileName)


saveConfig :: (MonadIO m, CollectErrorsM m) => (Resolver,Backend) -> m ()
saveConfig :: forall (m :: * -> *).
(MonadIO m, CollectErrorsM m) =>
(Resolver, Backend) -> m ()
saveConfig (Resolver
resolver,Backend
backend) = do
  FilePath
configFile <- IO FilePath -> m FilePath
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO FilePath
localConfigPath
  IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Handle -> FilePath -> IO ()
hPutStrLn Handle
stderr (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath
"Writing local config to " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
configFile FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"."
  FilePath
serialized <- LocalConfig -> m FilePath
forall a (m :: * -> *).
(ConfigFormat a, CollectErrorsM m) =>
a -> m FilePath
autoWriteConfig (Resolver -> Backend -> LocalConfig
LocalConfig Resolver
resolver Backend
backend)
  IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> IO ()
writeFile FilePath
configFile FilePath
serialized

loadConfig :: (MonadIO m, CollectErrorsM m) => m (Resolver,Backend)
loadConfig :: forall (m :: * -> *).
(MonadIO m, CollectErrorsM m) =>
m (Resolver, Backend)
loadConfig = do
  FilePath
configFile <- IO FilePath -> m FilePath
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO FilePath
localConfigPath
  Bool
isFile <- IO Bool -> m Bool
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> m Bool) -> IO Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ FilePath -> IO Bool
doesFileExist FilePath
configFile
  Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not Bool
isFile) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ FilePath -> m ()
forall a. FilePath -> m a
forall (m :: * -> *) a. ErrorContextM m => FilePath -> m a
compilerErrorM FilePath
"Zeolite has not been configured. Please run zeolite-setup."
  FilePath
configString <- IO FilePath -> m FilePath
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO FilePath -> m FilePath) -> IO FilePath -> m FilePath
forall a b. (a -> b) -> a -> b
$ FilePath -> IO FilePath
readFile FilePath
configFile
  LocalConfig
lc <- FilePath -> FilePath -> m LocalConfig
forall a (m :: * -> *).
(ConfigFormat a, ErrorContextM m) =>
FilePath -> FilePath -> m a
autoReadConfig FilePath
configFile FilePath
configString m LocalConfig -> FilePath -> m LocalConfig
forall (m :: * -> *) a. ErrorContextM m => m a -> FilePath -> m a
<!! FilePath
"Zeolite configuration is corrupt. Please rerun zeolite-setup."
  FilePath
pathsFile   <- IO FilePath -> m FilePath
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO FilePath -> m FilePath) -> IO FilePath -> m FilePath
forall a b. (a -> b) -> a -> b
$ IO FilePath
globalPathsPath
  Bool
pathsExists <- IO Bool -> m Bool
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> m Bool) -> IO Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ FilePath -> IO Bool
doesFileExist FilePath
pathsFile
  [FilePath]
paths <- if Bool
pathsExists
              then IO [FilePath] -> m [FilePath]
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [FilePath] -> m [FilePath]) -> IO [FilePath] -> m [FilePath]
forall a b. (a -> b) -> a -> b
$ FilePath -> IO FilePath
readFile FilePath
pathsFile IO FilePath -> (FilePath -> IO [FilePath]) -> IO [FilePath]
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [FilePath] -> IO [FilePath]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([FilePath] -> IO [FilePath])
-> (FilePath -> [FilePath]) -> FilePath -> IO [FilePath]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> [FilePath]
lines
              else [FilePath] -> m [FilePath]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return []
  (Resolver, Backend) -> m (Resolver, Backend)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Resolver -> [FilePath] -> Resolver
addPaths (LocalConfig -> Resolver
lcResolver LocalConfig
lc) [FilePath]
paths,LocalConfig -> Backend
lcBackend LocalConfig
lc)

localConfigPath :: IO FilePath
localConfigPath :: IO FilePath
localConfigPath = FilePath -> IO FilePath
getDataFileName FilePath
localConfigFilename IO FilePath -> (FilePath -> IO FilePath) -> IO FilePath
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= FilePath -> IO FilePath
canonicalizePath

localConfigFilename :: FilePath
localConfigFilename :: FilePath
localConfigFilename = FilePath
".local-config"

globalPathsFilename :: FilePath
globalPathsFilename :: FilePath
globalPathsFilename = FilePath
"global-paths"

globalPathsPath :: IO FilePath
globalPathsPath :: IO FilePath
globalPathsPath = FilePath -> IO FilePath
getDataFileName FilePath
globalPathsFilename IO FilePath -> (FilePath -> IO FilePath) -> IO FilePath
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= FilePath -> IO FilePath
canonicalizePath

addPaths :: Resolver -> [FilePath] -> Resolver
addPaths :: Resolver -> [FilePath] -> Resolver
addPaths (SimpleResolver [FilePath]
ls [FilePath]
ps) [FilePath]
ps2 = [FilePath] -> [FilePath] -> Resolver
SimpleResolver [FilePath]
ls ([FilePath]
ps [FilePath] -> [FilePath] -> [FilePath]
forall a. [a] -> [a] -> [a]
++ [FilePath]
ps2)