Last active
August 29, 2015 14:02
-
-
Save phadej/b4e63bc0a5b9b0593134 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# if we have haskell installed | |
command -v runhaskell > /dev/null 2>&1 | |
if [ $? ]; then | |
# Embedded haskell | |
TMP=`mktemp /tmp/orderpath.XXXXXX` | |
cat > $TMP <<'EOF' | |
module Main where | |
import System.Environment (getEnvironment) | |
import Data.Function (on) | |
import Data.List.Split (splitOn) | |
import Data.List (intercalate, sortBy, isPrefixOf) | |
sortPath :: String -> String | |
sortPath = intercalate ":" . map snd . sortBy (compare `on` metric) . zip [0, 1 ..] . splitOn ":" | |
where metric (i, p) = i + if "/usr/local" `isPrefixOf` p then 0 else 1000 | |
defaultPath :: String | |
defaultPath = "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" | |
main :: IO () | |
main = do | |
env <- getEnvironment | |
putStrLn $ maybe defaultPath sortPath $ lookup "PATH" env | |
EOF | |
# Run, and if succeed assign new PATH | |
SORTEDPATH=`runhaskell $TMP` | |
if [ $? ]; then | |
export PATH=$SORTEDPATH | |
fi | |
# cleanup | |
rm $TMP | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import System.Environment (getEnvironment) | |
import Data.Function (on) | |
import Data.List.Split (splitOn) | |
import Data.List (intercalate, sortBy, isPrefixOf) | |
sortPath :: String -> String | |
sortPath = intercalate ":" . map snd . sortBy (compare `on` metric) . zip [0, 1 ..] . splitOn ":" | |
where metric (i, p) = i + if "/usr/local" `isPrefixOf` p then 0 else 1000 | |
defaultPath :: String | |
defaultPath = "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" | |
main :: IO () | |
main = do | |
env <- getEnvironment | |
putStrLn $ maybe defaultPath sortPath $ lookup "PATH" env |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment