Skip to content

Commit 6a8ba65

Browse files
committed
Rewrite Terminal.Package.Validate, and remove unused haskell code.
1 parent f6dd512 commit 6a8ba65

File tree

20 files changed

+256
-1606
lines changed

20 files changed

+256
-1606
lines changed

builder/src/BackgroundWriter.hs

Lines changed: 0 additions & 37 deletions
This file was deleted.

builder/src/Build.hs

Lines changed: 23 additions & 197 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66
module Build
77
( fromExposed,
8-
fromExposedSources,
98
fromPaths,
10-
fromPathsSources,
119
fromMainModules,
1210
fromRepl,
1311
Artifacts (..),
@@ -63,7 +61,7 @@ import Reporting.Error.Syntax qualified as Syntax
6361
import Reporting.Exit qualified as Exit
6462
import Reporting.Render.Type.Localizer qualified as L
6563
import System.Directory qualified as Dir
66-
import System.FilePath ((<.>), (</>))
64+
import System.FilePath ((<.>))
6765
import System.FilePath qualified as FP
6866

6967
-- ENVIRONMENT
@@ -110,8 +108,8 @@ forkWithKey func dict =
110108

111109
-- FROM EXPOSED
112110

113-
fromExposed :: Reporting.Style -> FilePath -> Details.Details -> DocsGoal docs -> NE.List ModuleName.Raw -> IO (Either Exit.BuildProblem docs)
114-
fromExposed style root details docsGoal exposed@(NE.List e es) =
111+
fromExposed :: Reporting.Style -> FilePath -> Details.Details -> Map ModuleName.Raw ByteString -> DocsGoal docs -> NE.List ModuleName.Raw -> IO (Either Exit.BuildProblem docs)
112+
fromExposed style root details sources docsGoal exposed@(NE.List e es) =
115113
Reporting.trackBuild style $ \key ->
116114
do
117115
env <- makeEnv key root details
@@ -120,36 +118,7 @@ fromExposed style root details docsGoal exposed@(NE.List e es) =
120118
-- crawl
121119
mvar <- newEmptyMVar
122120
let docsNeed = toDocsNeed docsGoal
123-
roots <- Map.fromKeysA (fork . crawlModule env mvar docsNeed) (e : es)
124-
putMVar mvar roots
125-
mapM_ readMVar roots
126-
statuses <- traverse readMVar =<< readMVar mvar
127-
128-
-- compile
129-
midpoint <- checkMidpoint dmvar statuses
130-
case midpoint of
131-
Left problem ->
132-
return (Left (Exit.BuildProjectProblem problem))
133-
Right foreigns ->
134-
do
135-
rmvar <- newEmptyMVar
136-
resultMVars <- forkWithKey (checkModule env foreigns rmvar) statuses
137-
putMVar rmvar resultMVars
138-
results <- traverse readMVar resultMVars
139-
writeDetails root details results
140-
finalizeExposed root docsGoal exposed results
141-
142-
fromExposedSources :: Reporting.Style -> FilePath -> Details.Details -> Map ModuleName.Raw ByteString -> DocsGoal docs -> NE.List ModuleName.Raw -> IO (Either Exit.BuildProblem docs)
143-
fromExposedSources style root details sources docsGoal exposed@(NE.List e es) =
144-
Reporting.trackBuild style $ \key ->
145-
do
146-
env <- makeEnv key root details
147-
dmvar <- Details.loadInterfaces root details
148-
149-
-- crawl
150-
mvar <- newEmptyMVar
151-
let docsNeed = toDocsNeed docsGoal
152-
roots <- Map.fromKeysA (fork . crawlModuleSources env mvar sources docsNeed) (e : es)
121+
roots <- Map.fromKeysA (fork . crawlModule env mvar sources docsNeed) (e : es)
153122
putMVar mvar roots
154123
mapM_ readMVar roots
155124
statuses <- traverse readMVar =<< readMVar mvar
@@ -184,8 +153,8 @@ data Module
184153
type Dependencies =
185154
Map.Map ModuleName.Canonical I.DependencyInterface
186155

187-
fromPaths :: Reporting.Style -> FilePath -> Details.Details -> NE.List FilePath -> IO (Either Exit.BuildProblem Artifacts)
188-
fromPaths style root details paths =
156+
fromPaths :: Reporting.Style -> FilePath -> Details.Details -> Map ModuleName.Raw ByteString -> NE.List FilePath -> IO (Either Exit.BuildProblem Artifacts)
157+
fromPaths style root details sources paths =
189158
Reporting.trackBuild style $ \key ->
190159
do
191160
env <- makeEnv key root details
@@ -199,41 +168,7 @@ fromPaths style root details paths =
199168
-- crawl
200169
dmvar <- Details.loadInterfaces root details
201170
smvar <- newMVar Map.empty
202-
srootMVars <- traverse (fork . crawlRoot env smvar) lroots
203-
sroots <- traverse readMVar srootMVars
204-
statuses <- traverse readMVar =<< readMVar smvar
205-
206-
midpoint <- checkMidpointAndRoots dmvar statuses sroots
207-
case midpoint of
208-
Left problem ->
209-
return (Left (Exit.BuildProjectProblem problem))
210-
Right foreigns ->
211-
do
212-
-- compile
213-
rmvar <- newEmptyMVar
214-
resultsMVars <- forkWithKey (checkModule env foreigns rmvar) statuses
215-
putMVar rmvar resultsMVars
216-
rrootMVars <- traverse (fork . checkRoot env resultsMVars) sroots
217-
results <- traverse readMVar resultsMVars
218-
writeDetails root details results
219-
toArtifacts env foreigns results <$> traverse readMVar rrootMVars
220-
221-
fromPathsSources :: Reporting.Style -> FilePath -> Details.Details -> Map ModuleName.Raw ByteString -> NE.List FilePath -> IO (Either Exit.BuildProblem Artifacts)
222-
fromPathsSources style root details sources paths =
223-
Reporting.trackBuild style $ \key ->
224-
do
225-
env <- makeEnv key root details
226-
227-
elroots <- findRoots env paths
228-
case elroots of
229-
Left problem ->
230-
return (Left (Exit.BuildProjectProblem problem))
231-
Right lroots ->
232-
do
233-
-- crawl
234-
dmvar <- Details.loadInterfaces root details
235-
smvar <- newMVar Map.empty
236-
srootMVars <- traverse (fork . crawlRootSources env smvar sources) lroots
171+
srootMVars <- traverse (fork . crawlRoot env smvar sources) lroots
237172
sroots <- traverse readMVar srootMVars
238173
statuses <- traverse readMVar =<< readMVar smvar
239174

@@ -305,8 +240,8 @@ data Status
305240
| SForeign Pkg.Name
306241
| SKernel
307242

308-
crawlDeps :: Env -> MVar StatusDict -> [ModuleName.Raw] -> a -> IO a
309-
crawlDeps env mvar deps blockedValue =
243+
crawlDeps :: Env -> MVar StatusDict -> Map ModuleName.Raw ByteString -> [ModuleName.Raw] -> a -> IO a
244+
crawlDeps env mvar sources deps blockedValue =
310245
do
311246
statusDict <- takeMVar mvar
312247
let depsDict = Map.fromKeys (\_ -> ()) deps
@@ -316,64 +251,12 @@ crawlDeps env mvar deps blockedValue =
316251
mapM_ readMVar statuses
317252
return blockedValue
318253
where
319-
crawlNew name () = fork (crawlModule env mvar (DocsNeed False) name)
320-
321-
crawlDepsSources :: Env -> MVar StatusDict -> Map ModuleName.Raw ByteString -> [ModuleName.Raw] -> a -> IO a
322-
crawlDepsSources env mvar sources deps blockedValue =
323-
do
324-
statusDict <- takeMVar mvar
325-
let depsDict = Map.fromKeys (\_ -> ()) deps
326-
let newsDict = Map.difference depsDict statusDict
327-
statuses <- Map.traverseWithKey crawlNew newsDict
328-
putMVar mvar (Map.union statuses statusDict)
329-
mapM_ readMVar statuses
330-
return blockedValue
331-
where
332-
crawlNew name () = fork (crawlModuleSources env mvar sources (DocsNeed False) name)
333-
334-
crawlModule :: Env -> MVar StatusDict -> DocsNeed -> ModuleName.Raw -> IO Status
335-
crawlModule env@(Env _ root projectType _ srcDirs buildID locals foreigns) mvar docsNeed name =
336-
do
337-
let fileName = ModuleName.toFilePath name <.> "gren"
338-
339-
paths <- filterM File.exists (map (`AbsoluteSrcDir.addRelative` fileName) srcDirs)
340-
341-
case paths of
342-
[path] ->
343-
case Map.lookup name foreigns of
344-
Just (Details.Foreign dep deps) ->
345-
return $ SBadImport $ Import.Ambiguous path [] dep deps
346-
Nothing ->
347-
do
348-
newTime <- File.getTime path
349-
case Map.lookup name locals of
350-
Nothing ->
351-
crawlFile env mvar docsNeed name path newTime buildID
352-
Just local@(Details.Local oldPath oldTime deps _ lastChange _) ->
353-
if path /= oldPath || oldTime /= newTime || needsDocs docsNeed
354-
then crawlFile env mvar docsNeed name path newTime lastChange
355-
else crawlDeps env mvar deps (SCached local)
356-
p1 : p2 : ps ->
357-
return $ SBadImport $ Import.AmbiguousLocal (FP.makeRelative root p1) (FP.makeRelative root p2) (map (FP.makeRelative root) ps)
358-
[] ->
359-
case Map.lookup name foreigns of
360-
Just (Details.Foreign dep deps) ->
361-
case deps of
362-
[] ->
363-
return $ SForeign dep
364-
d : ds ->
365-
return $ SBadImport $ Import.AmbiguousForeign dep d ds
366-
Nothing ->
367-
if Name.isKernel name && Parse.isKernel projectType
368-
then do
369-
exists <- File.exists ("src" </> ModuleName.toFilePath name <.> "js")
370-
return $ if exists then SKernel else SBadImport Import.NotFound
371-
else return $ SBadImport Import.NotFound
254+
crawlNew name () = fork (crawlModule env mvar sources (DocsNeed False) name)
372255

373256
-- TODO: Use (slimmed down) locals to avoid compiling a module twice
374257
-- TODO: Pass on path from frontend
375-
crawlModuleSources :: Env -> MVar StatusDict -> Map ModuleName.Raw ByteString -> DocsNeed -> ModuleName.Raw -> IO Status
376-
crawlModuleSources env@(Env _ _ projectType _ _ buildID _ foreigns) mvar sources docsNeed name =
258+
crawlModule :: Env -> MVar StatusDict -> Map ModuleName.Raw ByteString -> DocsNeed -> ModuleName.Raw -> IO Status
259+
crawlModule env@(Env _ _ projectType _ _ buildID _ foreigns) mvar sources docsNeed name =
377260
let path = ModuleName.toFilePath name <.> "gren"
378261
in case Map.lookup name sources of
379262
Just source ->
@@ -386,7 +269,7 @@ crawlModuleSources env@(Env _ _ projectType _ _ buildID _ foreigns) mvar sources
386269
if Parse.isKernel projectType
387270
then return SKernel
388271
else return $ SBadImport Import.NotFound
389-
else crawlFileSources env mvar sources docsNeed name path source buildID
272+
else crawlFile env mvar sources docsNeed name path source buildID
390273
Nothing ->
391274
case Map.lookup name foreigns of
392275
Just (Details.Foreign dep deps) ->
@@ -398,28 +281,8 @@ crawlModuleSources env@(Env _ _ projectType _ _ buildID _ foreigns) mvar sources
398281
Nothing ->
399282
return $ SBadImport Import.NotFound
400283

401-
crawlFile :: Env -> MVar StatusDict -> DocsNeed -> ModuleName.Raw -> FilePath -> File.Time -> Details.BuildID -> IO Status
402-
crawlFile env@(Env _ root projectType _ _ buildID _ _) mvar docsNeed expectedName path time lastChange =
403-
do
404-
source <- File.readUtf8 (root </> path)
405-
406-
case Parse.fromByteString projectType source of
407-
Left err ->
408-
return $ SBadSyntax path time source err
409-
Right modul@(Src.Module maybeActualName _ _ imports values _ _ _ _ _ _) ->
410-
case maybeActualName of
411-
Nothing ->
412-
return $ SBadSyntax path time source (Syntax.ModuleNameUnspecified expectedName)
413-
Just name@(A.At _ actualName) ->
414-
if expectedName == actualName
415-
then
416-
let deps = map (Src.getImportName . snd) imports
417-
local = Details.Local path time deps (any (isMain . snd) values) lastChange buildID
418-
in crawlDeps env mvar deps (SChanged local source modul docsNeed)
419-
else return $ SBadSyntax path time source (Syntax.ModuleNameMismatch expectedName name)
420-
421-
crawlFileSources :: Env -> MVar StatusDict -> Map ModuleName.Raw ByteString -> DocsNeed -> ModuleName.Raw -> FilePath -> ByteString -> Details.BuildID -> IO Status
422-
crawlFileSources env@(Env _ _ projectType _ _ buildID _ _) mvar sources docsNeed expectedName path source lastChange =
284+
crawlFile :: Env -> MVar StatusDict -> Map ModuleName.Raw ByteString -> DocsNeed -> ModuleName.Raw -> FilePath -> ByteString -> Details.BuildID -> IO Status
285+
crawlFile env@(Env _ _ projectType _ _ buildID _ _) mvar sources docsNeed expectedName path source lastChange =
423286
case Parse.fromByteString projectType source of
424287
Left err ->
425288
return $ SBadSyntax path File.zeroTime source err
@@ -432,7 +295,7 @@ crawlFileSources env@(Env _ _ projectType _ _ buildID _ _) mvar sources docsNeed
432295
then
433296
let deps = map (Src.getImportName . snd) imports
434297
local = Details.Local path File.zeroTime deps (any (isMain . snd) values) lastChange buildID
435-
in crawlDepsSources env mvar sources deps (SChanged local source modul docsNeed)
298+
in crawlDeps env mvar sources deps (SChanged local source modul docsNeed)
436299
else return $ SBadSyntax path File.zeroTime source (Syntax.ModuleNameMismatch expectedName name)
437300

438301
isMain :: A.Located Src.Value -> Bool
@@ -920,7 +783,7 @@ fromRepl root details rootSources source =
920783

921784
let deps = map (Src.getImportName . snd) imports
922785
mvar <- newMVar Map.empty
923-
crawlDepsSources env mvar rootSources deps ()
786+
crawlDeps env mvar rootSources deps ()
924787

925788
statuses <- traverse readMVar =<< readMVar mvar
926789
midpoint <- checkMidpoint dmvar statuses
@@ -1090,63 +953,26 @@ data RootStatus
1090953
| SOutsideOk Details.Local B.ByteString Src.Module
1091954
| SOutsideErr Error.Module
1092955

1093-
crawlRoot :: Env -> MVar StatusDict -> RootLocation -> IO RootStatus
1094-
crawlRoot env@(Env _ _ projectType _ _ buildID _ _) mvar root =
1095-
case root of
1096-
LInside name ->
1097-
do
1098-
statusMVar <- newEmptyMVar
1099-
statusDict <- takeMVar mvar
1100-
putMVar mvar (Map.insert name statusMVar statusDict)
1101-
putMVar statusMVar =<< crawlModule env mvar (DocsNeed False) name
1102-
return (SInside name)
1103-
LOutside path ->
1104-
do
1105-
time <- File.getTime path
1106-
source <- File.readUtf8 path
1107-
case Parse.fromByteString projectType source of
1108-
Right modul@(Src.Module _ _ _ imports values _ _ _ _ _ _) ->
1109-
do
1110-
let deps = map (Src.getImportName . snd) imports
1111-
let local = Details.Local path time deps (any (isMain . snd) values) buildID buildID
1112-
crawlDeps env mvar deps (SOutsideOk local source modul)
1113-
Left syntaxError ->
1114-
return $
1115-
SOutsideErr $
1116-
Error.Module "???" path time source (Error.BadSyntax syntaxError)
1117-
1118-
crawlRootSources :: Env -> MVar StatusDict -> Map ModuleName.Raw ByteString -> RootLocation -> IO RootStatus
1119-
crawlRootSources env@(Env _ _ projectType _ _ buildID _ _) mvar sources root =
956+
crawlRoot :: Env -> MVar StatusDict -> Map ModuleName.Raw ByteString -> RootLocation -> IO RootStatus
957+
crawlRoot env mvar sources root =
1120958
case root of
1121959
LInside name ->
1122960
do
1123961
statusMVar <- newEmptyMVar
1124962
statusDict <- takeMVar mvar
1125963
putMVar mvar (Map.insert name statusMVar statusDict)
1126-
putMVar statusMVar =<< crawlModuleSources env mvar sources (DocsNeed False) name
964+
putMVar statusMVar =<< crawlModule env mvar sources (DocsNeed False) name
1127965
return (SInside name)
1128-
LOutside path ->
1129-
do
1130-
time <- File.getTime path
1131-
source <- File.readUtf8 path
1132-
case Parse.fromByteString projectType source of
1133-
Right modul@(Src.Module _ _ _ imports values _ _ _ _ _ _) ->
1134-
do
1135-
let deps = map (Src.getImportName . snd) imports
1136-
let local = Details.Local path time deps (any (isMain . snd) values) buildID buildID
1137-
crawlDeps env mvar deps (SOutsideOk local source modul)
1138-
Left syntaxError ->
1139-
return $
1140-
SOutsideErr $
1141-
Error.Module "???" path time source (Error.BadSyntax syntaxError)
966+
LOutside _ ->
967+
error "Bad assumption"
1142968

1143969
crawlRootModule :: Env -> MVar StatusDict -> Map ModuleName.Raw ByteString -> ModuleName.Raw -> IO RootStatus
1144970
crawlRootModule env mvar sources root =
1145971
do
1146972
statusMVar <- newEmptyMVar
1147973
statusDict <- takeMVar mvar
1148974
putMVar mvar (Map.insert root statusMVar statusDict)
1149-
putMVar statusMVar =<< crawlModuleSources env mvar sources (DocsNeed False) root
975+
putMVar statusMVar =<< crawlModule env mvar sources (DocsNeed False) root
1150976
return (SInside root)
1151977

1152978
-- CHECK ROOTS

0 commit comments

Comments
 (0)