{- -----------------------------------------------------------------------------
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 <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO FilePath
localConfigPath
  forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Handle -> FilePath -> IO ()
hPutStrLn Handle
stderr forall a b. (a -> b) -> a -> b
$ FilePath
"Writing local config to " forall a. [a] -> [a] -> [a]
++ FilePath
configFile forall a. [a] -> [a] -> [a]
++ FilePath
"."
  FilePath
serialized <- forall a (m :: * -> *).
(ConfigFormat a, CollectErrorsM m) =>
a -> m FilePath
autoWriteConfig (Resolver -> Backend -> LocalConfig
LocalConfig Resolver
resolver Backend
backend)
  forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO 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 <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO FilePath
localConfigPath
  Bool
isFile <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ FilePath -> IO Bool
doesFileExist FilePath
configFile
  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not Bool
isFile) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. ErrorContextM m => FilePath -> m a
compilerErrorM FilePath
"Zeolite has not been configured. Please run zeolite-setup."
  FilePath
configString <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ FilePath -> IO FilePath
readFile FilePath
configFile
  LocalConfig
lc <- forall a (m :: * -> *).
(ConfigFormat a, ErrorContextM m) =>
FilePath -> FilePath -> m a
autoReadConfig FilePath
configFile FilePath
configString forall (m :: * -> *) a. ErrorContextM m => m a -> FilePath -> m a
<!! FilePath
"Zeolite configuration is corrupt. Please rerun zeolite-setup."
  FilePath
pathsFile   <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ IO FilePath
globalPathsPath
  Bool
pathsExists <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ FilePath -> IO Bool
doesFileExist FilePath
pathsFile
  [FilePath]
paths <- if Bool
pathsExists
              then forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ FilePath -> IO FilePath
readFile FilePath
pathsFile forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> [FilePath]
lines
              else forall (m :: * -> *) a. Monad m => a -> m a
return []
  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 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 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 forall a. [a] -> [a] -> [a]
++ [FilePath]
ps2)