@@ -0,0 +1,147 @@
1+{-# OPTIONS_GHC -Wall #-}
2+module Stuff
3+ ( details
4+ , prepublishDir
5+ , elmi
6+ , elmo
7+ , temp
8+ , getRoot
9+ , PackageCache
10+ , getPackageCache
11+ , registry
12+ , metadata
13+ , getReplCache
14+ )
15+ where
16+
17+
18+import qualified System.Directory as Dir
19+import qualified System.Environment as Env
20+import qualified System.FilePath as FP
21+import System.FilePath ((</>), (<.>))
22+
23+import qualified Elm.ModuleName as ModuleName
24+import qualified Elm.Package as Pkg
25+import qualified Elm.Version as V
26+
27+
28+
29+-- PATHS
30+
31+
32+stuff :: FilePath
33+stuff =
34+ "elm-stuff" </> compilerVersion
35+
36+
37+details :: FilePath
38+details =
39+ stuff </> "details.dat"
40+
41+
42+prepublishDir :: FilePath
43+prepublishDir =
44+ stuff </> "prepublish"
45+
46+
47+compilerVersion :: FilePath
48+compilerVersion =
49+ V.toChars V.compiler
50+
51+
52+
53+-- ELMI and ELMO
54+
55+
56+elmi :: FilePath -> ModuleName.Raw -> FilePath
57+elmi root name =
58+ toArtifactPath root name "elmi"
59+
60+
61+elmo :: FilePath -> ModuleName.Raw -> FilePath
62+elmo root name =
63+ toArtifactPath root name "elmo"
64+
65+
66+toArtifactPath :: FilePath -> ModuleName.Raw -> String -> FilePath
67+toArtifactPath root name ext =
68+ root </> stuff </> ModuleName.toHyphenPath name <.> ext
69+
70+
71+
72+-- TEMP
73+
74+
75+temp :: String -> FilePath
76+temp ext =
77+ stuff </> "temp" <.> ext
78+
79+
80+
81+-- GET ROOT
82+
83+
84+getRoot :: IO (Maybe FilePath)
85+getRoot =
86+ do dir <- Dir.getCurrentDirectory
87+ getRootHelp (FP.splitDirectories dir)
88+
89+
90+getRootHelp :: [String] -> IO (Maybe FilePath)
91+getRootHelp dirs =
92+ case dirs of
93+ [] ->
94+ return Nothing
95+
96+ _:_ ->
97+ do exists_ <- Dir.doesFileExist (FP.joinPath dirs </> "elm.json")
98+ if exists_
99+ then return (Just (FP.joinPath dirs))
100+ else getRootHelp (init dirs)
101+
102+
103+
104+-- PACKAGE CACHES
105+
106+
107+newtype PackageCache = PackageCache FilePath
108+
109+
110+getPackageCache :: IO PackageCache
111+getPackageCache =
112+ PackageCache <$> getCacheDir "packages"
113+
114+
115+registry :: PackageCache -> FilePath
116+registry (PackageCache dir) =
117+ dir </> "registry.dat"
118+
119+
120+metadata :: PackageCache -> Pkg.Name -> V.Version -> String -> FilePath
121+metadata (PackageCache dir) name version path =
122+ dir </> Pkg.toFilePath name </> V.toChars version </> path
123+
124+
125+
126+-- CACHE
127+
128+
129+getReplCache :: IO FilePath
130+getReplCache =
131+ getCacheDir "repl"
132+
133+
134+getCacheDir :: FilePath -> IO FilePath
135+getCacheDir projectName =
136+ do home <- getElmHome
137+ let root = home </> compilerVersion </> projectName
138+ Dir.createDirectoryIfMissing True root
139+ return root
140+
141+
142+getElmHome :: IO FilePath
143+getElmHome =
144+ do maybeCustomHome <- Env.lookupEnv "ELM_HOME"
145+ case maybeCustomHome of
146+ Just customHome -> return customHome
147+ Nothing -> Dir.getAppUserDataDirectory "elm"