on efb63ca help sign in
elk/builder/src/Stuff.hs
173 lines 3.1 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 , PackageCache
13 , getPackageCache
14 , registry
15 , package
16 , getReplCache
17 , getElmHome
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
122withRootLock :: FilePath -> IO a -> IO a
123withRootLock root work =
124 do let dir = stuff root
125 Dir.createDirectoryIfMissing True dir
126 Lock.withFileLock (dir </> "project.lock") Lock.Exclusive (\_ -> work)
130-- PACKAGE CACHES
133newtype PackageCache = PackageCache FilePath
136getPackageCache :: IO PackageCache
137getPackageCache =
138 PackageCache <$> getCacheDir "packages"
141registry :: PackageCache -> FilePath
142registry (PackageCache dir) =
143 dir </> "registry.dat"
146package :: PackageCache -> Pkg.Name -> V.Version -> FilePath
147package (PackageCache dir) name version =
148 dir </> Pkg.toFilePath name </> V.toChars version
152-- CACHE
155getReplCache :: IO FilePath
156getReplCache =
157 getCacheDir "repl"
160getCacheDir :: FilePath -> IO FilePath
161getCacheDir projectName =
162 do home <- getElmHome
163 let root = home </> compilerVersion </> projectName
164 Dir.createDirectoryIfMissing True root
165 return root
168getElmHome :: IO FilePath
169getElmHome =
170 do maybeCustomHome <- Env.lookupEnv "ELM_HOME"
171 case maybeCustomHome of
172 Just customHome -> return customHome
173 Nothing -> Dir.getAppUserDataDirectory "elm"