on 83faa2e 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 , withProjectLock
12 , withCacheLock
13 , PackageCache
14 , getPackageCache
15 , registry
16 , package
17 , getReplCache
18 )
19 where
22import qualified System.Directory as Dir
23import qualified System.Environment as Env
24import qualified System.FileLock as Lock
25import qualified System.FilePath as FP
26import System.FilePath ((</>), (<.>))
28import qualified Elm.ModuleName as ModuleName
29import qualified Elm.Package as Pkg
30import qualified Elm.Version as V
34-- PATHS
37stuff :: FilePath -> FilePath
38stuff root =
39 root </> "elm-stuff" </> compilerVersion
42details :: FilePath -> FilePath
43details root =
44 stuff root </> "d.dat"
47interfaces :: FilePath -> FilePath
48interfaces root =
49 stuff root </> "i.dat"
52objects :: FilePath -> FilePath
53objects root =
54 stuff root </> "o.dat"
57prepublishDir :: FilePath -> FilePath
58prepublishDir root =
59 stuff root </> "prepublish"
62compilerVersion :: FilePath
63compilerVersion =
64 V.toChars V.compiler
68-- ELMI and ELMO
71elmi :: FilePath -> ModuleName.Raw -> FilePath
72elmi root name =
73 toArtifactPath root name "elmi"
76elmo :: FilePath -> ModuleName.Raw -> FilePath
77elmo root name =
78 toArtifactPath root name "elmo"
81toArtifactPath :: FilePath -> ModuleName.Raw -> String -> FilePath
82toArtifactPath root name ext =
83 stuff root </> ModuleName.toHyphenPath name <.> ext
87-- TEMP
90temp :: FilePath -> String -> FilePath
91temp root ext =
92 stuff root </> "temp" <.> ext
96-- ROOT
99findRoot :: IO (Maybe FilePath)
100findRoot =
101 do dir <- Dir.getCurrentDirectory
102 findRootHelp (FP.splitDirectories dir)
105findRootHelp :: [String] -> IO (Maybe FilePath)
106findRootHelp dirs =
107 case dirs of
108 [] ->
109 return Nothing
111 _:_ ->
112 do exists <- Dir.doesFileExist (FP.joinPath dirs </> "elm.json")
113 if exists
114 then return (Just (FP.joinPath dirs))
115 else findRootHelp (init dirs)
119-- LOCKS
122withProjectLock :: FilePath -> IO a -> IO a
123withProjectLock root work =
124 do let dir = stuff root
125 Dir.createDirectoryIfMissing True dir
126 Lock.withFileLock (dir </> "project.lock") Lock.Exclusive (\_ -> work)
129withCacheLock :: PackageCache -> IO a -> IO a
130withCacheLock (PackageCache dir) work =
131 Lock.withFileLock (dir </> "cache.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"