packages feed

phoityne-vscode-0.0.8.0: app/Phoityne/VSCode/IO/Main.hs

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE MultiWayIf          #-}

module Phoityne.VSCode.IO.Main (run) where

-- モジュール
import qualified Phoityne.VSCode.Argument as A
import qualified Phoityne.VSCode.IO.Control as CTRL

-- システム
import Data.Either.Utils
import qualified Control.Exception as E
import qualified Data.ConfigFile as C
import qualified System.Console.CmdArgs as CMD
import qualified System.Log.Logger as L


-- |
--  アプリケーションメイン
-- 
run :: IO Int
run = flip E.catches handlers $ do

  -- コマンドライン引数設定
  args <- getArgs

  -- INI設定ファイルのRead
  iniSet <- loadIniFile args

  -- ロジック実行
  flip E.finally finalProc $ do
    CTRL.run args iniSet

  where
    handlers = [ E.Handler helpExcept
               , E.Handler ioExcept
               , E.Handler someExcept
               ]
    finalProc = L.removeAllHandlers 
    helpExcept (_ :: A.HelpExitException) = return 0
    ioExcept   (e :: E.IOException)       = print e >> return 1
    someExcept (e :: E.SomeException)     = print e >> return 1


-- |
--  引数データの取得
-- 
getArgs :: IO A.ArgData
getArgs = E.catches (CMD.cmdArgs A.config) handlers
  where
    handlers = [E.Handler someExcept]
    someExcept (e :: E.SomeException) = if
      | "ExitSuccess" == show e -> E.throw A.HelpExitException
      | otherwise -> E.throwIO e


-- |
--  INI設定ファイルデータの取得
-- 
loadIniFile :: A.ArgData          -- コマンドライン引数
            -> IO C.ConfigParser  -- INI設定
loadIniFile _ = do
  let cp = forceEither $ C.readstring C.emptyCP defaultIniSetting
  return cp{ C.accessfunc = C.interpolatingAccess 5 }


-- |
-- デフォルトINI設定
-- 
defaultIniSetting :: String
defaultIniSetting = unlines [
    "[DEFAULT]"
  , "work_dir = ./"
  , ""
  , "[LOG]"
  , "file  = %(work_dir)sphoityne.log"
  , "level = WARNING"
  , ""
  , "[PHOITYNE]"
  ]