diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2017-06-13 06:21:34 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2017-06-13 06:21:34 +0300 |
commit | c7094f015773285170c3ae15f2d4cb1f158a6c1a (patch) | |
tree | 2587b9e22f2a5c30ffb55b6ddbacae25e8f54f90 /app/Utils | |
parent | feature registry value type in prompts (diff) | |
download | windows-env-c7094f015773285170c3ae15f2d4cb1f158a6c1a.tar.gz windows-env-c7094f015773285170c3ae15f2d4cb1f158a6c1a.zip |
addpath: detect expandable paths
Diffstat (limited to 'app/Utils')
-rw-r--r-- | app/Utils/Path.hs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/app/Utils/Path.hs b/app/Utils/Path.hs new file mode 100644 index 0000000..01259e7 --- /dev/null +++ b/app/Utils/Path.hs @@ -0,0 +1,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 |