on 1238607 help sign in
elk/builder/src/Stuff.hs
180 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 , 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
123withProjectLock :: FilePath -> IO a -> IO a
124withProjectLock root work =
125 do let dir = stuff root
126 Dir.createDirectoryIfMissing True dir
127 Lock.withFileLock (dir </> "project.lock") Lock.Exclusive (\_ -> work)
130withCacheLock :: PackageCache -> IO a -> IO a
131withCacheLock (PackageCache dir) work =
132 Lock.withFileLock (dir </> "cache.lock") Lock.Exclusive (\_ -> work)
137-- PACKAGE CACHES
140newtype PackageCache = PackageCache FilePath
143getPackageCache :: IO PackageCache
144getPackageCache =
145 PackageCache <$> getCacheDir "packages"
148registry :: PackageCache -> FilePath
149registry (PackageCache dir) =
150 dir </> "registry.dat"
153package :: PackageCache -> Pkg.Name -> V.Version -> FilePath
154package (PackageCache dir) name version =
155 dir </> Pkg.toFilePath name </> V.toChars version
159-- CACHE
162getReplCache :: IO FilePath
163getReplCache =
164 getCacheDir "repl"
167getCacheDir :: FilePath -> IO FilePath
168getCacheDir projectName =
169 do home <- getElmHome
170 let root = home </> compilerVersion </> projectName
171 Dir.createDirectoryIfMissing True root
172 return root
175getElmHome :: IO FilePath
176getElmHome =
177 do maybeCustomHome <- Env.lookupEnv "ELM_HOME"
178 case maybeCustomHome of
179 Just customHome -> return customHome
180 Nothing -> Dir.getAppUserDataDirectory "elm"