on master help sign in
elk/builder/src/Stuff.hs
179 lines 3.3 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module Stuff
3 ( details
4 , interfaces
5 , objects
6 , prepublishDir
7 , elmi
8 , elmo
9 , temp
10 , findRoot
11 , withRootLock
12 , withRegistryLock
13 , PackageCache
14 , getPackageCache
15 , registry
16 , package
17 , getReplCache
18 , getElmHome
19 )
20 where
23import qualified System.Directory as Dir
24import qualified System.Environment as Env
25import qualified System.FileLock as Lock
26import qualified System.FilePath as FP
27import System.FilePath ((</>), (<.>))
29import qualified Elm.ModuleName as ModuleName
30import qualified Elm.Package as Pkg
31import qualified Elm.Version as V
35-- PATHS
38stuff :: FilePath -> FilePath
39stuff root =
40 root </> "elm-stuff" </> compilerVersion
43details :: FilePath -> FilePath
44details root =
45 stuff root </> "d.dat"
48interfaces :: FilePath -> FilePath
49interfaces root =
50 stuff root </> "i.dat"
53objects :: FilePath -> FilePath
54objects root =
55 stuff root </> "o.dat"
58prepublishDir :: FilePath -> FilePath
59prepublishDir root =
60 stuff root </> "prepublish"
63compilerVersion :: FilePath
64compilerVersion =
65 V.toChars V.compiler
69-- ELMI and ELMO
72elmi :: FilePath -> ModuleName.Raw -> FilePath
73elmi root name =
74 toArtifactPath root name "elmi"
77elmo :: FilePath -> ModuleName.Raw -> FilePath
78elmo root name =
79 toArtifactPath root name "elmo"
82toArtifactPath :: FilePath -> ModuleName.Raw -> String -> FilePath
83toArtifactPath root name ext =
84 stuff root </> ModuleName.toHyphenPath name <.> ext
88-- TEMP
91temp :: FilePath -> String -> FilePath
92temp root ext =
93 stuff root </> "temp" <.> ext
97-- ROOT
100findRoot :: IO (Maybe FilePath)
101findRoot =
102 do dir <- Dir.getCurrentDirectory
103 findRootHelp (FP.splitDirectories dir)
106findRootHelp :: [String] -> IO (Maybe FilePath)
107findRootHelp dirs =
108 case dirs of
109 [] ->
110 return Nothing
112 _:_ ->
113 do exists <- Dir.doesFileExist (FP.joinPath dirs </> "elm.json")
114 if exists
115 then return (Just (FP.joinPath dirs))
116 else findRootHelp (init dirs)
120-- LOCKS
123withRootLock :: FilePath -> IO a -> IO a
124withRootLock root work =
125 do let dir = stuff root
126 Dir.createDirectoryIfMissing True dir
127 Lock.withFileLock (dir </> "lock") Lock.Exclusive (\_ -> work)
130withRegistryLock :: PackageCache -> IO a -> IO a
131withRegistryLock (PackageCache dir) work =
132 Lock.withFileLock (dir </> "lock") Lock.Exclusive (\_ -> work)
136-- PACKAGE CACHES
139newtype PackageCache = PackageCache FilePath
142getPackageCache :: IO PackageCache
143getPackageCache =
144 PackageCache <$> getCacheDir "packages"
147registry :: PackageCache -> FilePath
148registry (PackageCache dir) =
149 dir </> "registry.dat"
152package :: PackageCache -> Pkg.Name -> V.Version -> FilePath
153package (PackageCache dir) name version =
154 dir </> Pkg.toFilePath name </> V.toChars version
158-- CACHE
161getReplCache :: IO FilePath
162getReplCache =
163 getCacheDir "repl"
166getCacheDir :: FilePath -> IO FilePath
167getCacheDir projectName =
168 do home <- getElmHome
169 let root = home </> compilerVersion </> projectName
170 Dir.createDirectoryIfMissing True root
171 return root
174getElmHome :: IO FilePath
175getElmHome =
176 do maybeCustomHome <- Env.lookupEnv "ELM_HOME"
177 case maybeCustomHome of
178 Just customHome -> return customHome
179 Nothing -> Dir.getAppUserDataDirectory "elm"