aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/WindowsEnv
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2017-06-11 05:37:27 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2017-06-11 05:37:27 +0300
commit742c541568f05d160784ace2024ee7256e3bd189 (patch)
tree1825605080aecab732ac59245dc762f4248945c7 /src/WindowsEnv
parentwindows-env.cabal: fix formatting (diff)
downloadwindows-env-742c541568f05d160784ace2024ee7256e3bd189.tar.gz
windows-env-742c541568f05d160784ace2024ee7256e3bd189.zip
refactoring
Diffstat (limited to 'src/WindowsEnv')
-rw-r--r--src/WindowsEnv/Environment.hs50
1 files changed, 34 insertions, 16 deletions
diff --git a/src/WindowsEnv/Environment.hs b/src/WindowsEnv/Environment.hs
index e56f5c9..9a42748 100644
--- a/src/WindowsEnv/Environment.hs
+++ b/src/WindowsEnv/Environment.hs
@@ -25,8 +25,12 @@ module WindowsEnv.Environment
, pathSplit
, expand
- , ExpandedPath(..)
, pathSplitAndExpand
+
+ , ExpandedPath(..)
+ , pathOriginal
+ , pathExpanded
+ , pathExists
) where
import Control.Monad.Trans.Class (lift)
@@ -35,6 +39,7 @@ import Data.List (intercalate)
import Data.List.Split (splitOn)
import Foreign.Marshal.Alloc (allocaBytes)
import Foreign.Storable (sizeOf)
+import System.Directory (doesDirectoryExist)
import System.IO.Error (catchIOError)
import qualified System.Win32.Types as WinAPI
@@ -119,19 +124,32 @@ expand value = ExceptT $ catchIOError (Right <$> doExpand) (return . Left)
else WinAPI.peekTString bufferPtr
doExpand = WinAPI.withTString value $ \valuePtr -> doExpandIn valuePtr WinAPI.nullPtr 0
-data ExpandedPath = ExpandedPath
- { pathOriginal :: String
- , pathExpanded :: String
- } deriving (Eq, Show)
-
-pathSplitAndExpand :: String -> ExceptT IOError IO [ExpandedPath]
-pathSplitAndExpand value = do
- expandedOnce <- expandOnce
- zipWith ExpandedPath originalPaths <$>
- if length expandedOnce == length originalPaths
- then return expandedOnce
- else expandEach
+data ExpandedPath = UnexpandedPath String
+ | ExpandedPath String String
+ deriving (Eq, Show)
+
+pathOriginal :: ExpandedPath -> String
+pathOriginal (UnexpandedPath path) = path
+pathOriginal (ExpandedPath original expanded) = original
+
+pathExpanded :: ExpandedPath -> String
+pathExpanded (UnexpandedPath path) = path
+pathExpanded (ExpandedPath original expanded) = expanded
+
+pathExists :: ExpandedPath -> IO Bool
+pathExists = doesDirectoryExist . pathExpanded
+
+pathSplitAndExpand :: VarValue -> ExceptT IOError IO [ExpandedPath]
+pathSplitAndExpand value
+ | varValueExpandable value = do
+ expanded <- expandOnce
+ zipWith ExpandedPath split <$>
+ if length expanded == length split
+ then return expanded
+ else expandEach
+ | otherwise = return $ map UnexpandedPath $ pathSplit joined
where
- originalPaths = pathSplit value
- expandOnce = pathSplit <$> expand value
- expandEach = mapM expand originalPaths
+ joined = varValueString value
+ split = pathSplit joined
+ expandOnce = pathSplit <$> expand joined
+ expandEach = mapM expand split