on efe80bc help sign in
elk/builder/src/Stuff.hs
159 lines 2.7 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module Stuff
3 ( details
4 , interfaces
5 , objects
6 , prepublishDir
7 , elmi
8 , elmo
9 , temp
10 , getRoot
11 , PackageCache
12 , getPackageCache
13 , registry
14 , package
15 , getReplCache
16 )
17 where
20import qualified System.Directory as Dir
21import qualified System.Environment as Env
22import qualified System.FilePath as FP
23import System.FilePath ((</>), (<.>))
25import qualified Elm.ModuleName as ModuleName
26import qualified Elm.Package as Pkg
27import qualified Elm.Version as V
31-- PATHS
34stuff :: FilePath
35stuff =
36 "elm-stuff" </> compilerVersion
39details :: FilePath
40details =
41 stuff </> "d.dat"
44interfaces :: FilePath
45interfaces =
46 stuff </> "i.dat"
49objects :: FilePath
50objects =
51 stuff </> "o.dat"
54prepublishDir :: FilePath
55prepublishDir =
56 stuff </> "prepublish"
59compilerVersion :: FilePath
60compilerVersion =
61 V.toChars V.compiler
65-- ELMI and ELMO
68elmi :: FilePath -> ModuleName.Raw -> FilePath
69elmi root name =
70 toArtifactPath root name "elmi"
73elmo :: FilePath -> ModuleName.Raw -> FilePath
74elmo root name =
75 toArtifactPath root name "elmo"
78toArtifactPath :: FilePath -> ModuleName.Raw -> String -> FilePath
79toArtifactPath root name ext =
80 root </> stuff </> ModuleName.toHyphenPath name <.> ext
84-- TEMP
87temp :: String -> FilePath
88temp ext =
89 stuff </> "temp" <.> ext
93-- GET ROOT
96getRoot :: IO (Maybe FilePath)
97getRoot =
98 do dir <- Dir.getCurrentDirectory
99 getRootHelp (FP.splitDirectories dir)
102getRootHelp :: [String] -> IO (Maybe FilePath)
103getRootHelp dirs =
104 case dirs of
105 [] ->
106 return Nothing
108 _:_ ->
109 do exists_ <- Dir.doesFileExist (FP.joinPath dirs </> "elm.json")
110 if exists_
111 then return (Just (FP.joinPath dirs))
112 else getRootHelp (init dirs)
116-- PACKAGE CACHES
119newtype PackageCache = PackageCache FilePath
122getPackageCache :: IO PackageCache
123getPackageCache =
124 PackageCache <$> getCacheDir "packages"
127registry :: PackageCache -> FilePath
128registry (PackageCache dir) =
129 dir </> "registry.dat"
132package :: PackageCache -> Pkg.Name -> V.Version -> FilePath
133package (PackageCache dir) name version =
134 dir </> Pkg.toFilePath name </> V.toChars version
138-- CACHE
141getReplCache :: IO FilePath
142getReplCache =
143 getCacheDir "repl"
146getCacheDir :: FilePath -> IO FilePath
147getCacheDir projectName =
148 do home <- getElmHome
149 let root = home </> compilerVersion </> projectName
150 Dir.createDirectoryIfMissing True root
151 return root
154getElmHome :: IO FilePath
155getElmHome =
156 do maybeCustomHome <- Env.lookupEnv "ELM_HOME"
157 case maybeCustomHome of
158 Just customHome -> return customHome
159 Nothing -> Dir.getAppUserDataDirectory "elm"