aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/app/Utils/Path.hs
blob: 01259e7867eb83e8f4bab6549ce43808eb2d942f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
-- |
-- Copyright   : (c) 2017 Egor Tensin <Egor.Tensin@gmail.com>
-- License     : MIT
-- Maintainer  : Egor.Tensin@gmail.com
-- Stability   : experimental
-- Portability : Windows-only

module Utils.Path
    ( ExpandedPath(..)
    , pathExpandValue

    , pathExpandAll
    , pathAnyExpanded
    ) where

import Control.Monad.Trans.Except (ExceptT)

import qualified WindowsEnv

data ExpandedPath = ExpandedPath
    { pathOriginal :: String
    , pathExpanded :: String
    } deriving (Eq, Show)

pathExpandValue :: WindowsEnv.Value -> ExceptT IOError IO [ExpandedPath]
pathExpandValue value
    | WindowsEnv.valueExpandable value = do
        expanded <- expandOnce
        zipWith ExpandedPath split <$>
            if length expanded == length split
                then return expanded
                else expandEach
    | otherwise = return $ zipWith ExpandedPath split split
  where
    joined = WindowsEnv.valueString value
    split = WindowsEnv.pathSplit joined
    expandOnce = WindowsEnv.pathSplit <$> WindowsEnv.expand joined
    expandEach = WindowsEnv.expandAll split

pathExpandAll :: [String] -> ExceptT IOError IO [ExpandedPath]
pathExpandAll paths = zipWith ExpandedPath paths <$> WindowsEnv.expandAll paths

pathIsExpanded :: ExpandedPath -> Bool
pathIsExpanded path = pathOriginal path /= pathExpanded path

pathAnyExpanded :: [ExpandedPath] -> Bool
pathAnyExpanded = any pathIsExpanded