@@ -18,7 +18,6 @@ import qualified Data.List.NonEmpty as NonEmpty
18
18
import qualified Data.Map as Map
19
19
import qualified Data.Set as Set
20
20
import qualified Data.Text as Text
21
- import qualified Data.Versions as Version
22
21
import System.Directory (getCurrentDirectory )
23
22
import System.FilePath (splitDirectories )
24
23
import qualified System.FilePath.Glob as Glob
@@ -29,7 +28,6 @@ import qualified Turtle
29
28
import qualified System.Process as Process
30
29
import qualified Web.Browser as Browser
31
30
32
- import qualified Spago.Build.Parser as Parse
33
31
import qualified Spago.Command.Path as Path
34
32
import qualified Spago.RunEnv as Run
35
33
import qualified Spago.Config as Config
@@ -51,31 +49,19 @@ prepareBundleDefaults maybeModuleName maybeTargetPath = (moduleName, targetPath)
51
49
targetPath = fromMaybe (TargetPath " index.js" ) maybeTargetPath
52
50
53
51
-- eventually running some other action after the build
54
- build
55
- :: HasBuildEnv env
56
- => BuildOptions -> Maybe (RIO Env () )
57
- -> RIO env ()
58
- build BuildOptions {.. } maybePostBuild = do
52
+ build :: HasBuildEnv env => Maybe (RIO Env () ) -> RIO env ()
53
+ build maybePostBuild = do
59
54
logDebug " Running `spago build`"
55
+ BuildOptions {.. } <- view (the @ BuildOptions )
60
56
Config {.. } <- view (the @ Config )
61
- PursCmd { compilerVersion } <- view (the @ PursCmd )
62
57
deps <- Packages. getProjectDeps
63
- case noInstall of
64
- DoInstall -> Fetch. fetchPackages deps
65
- NoInstall -> pure ()
66
58
let partitionedGlobs@ (Packages. Globs {.. }) = Packages. getGlobs deps depsOnly configSourcePaths
67
59
allPsGlobs = Packages. getGlobsSourcePaths partitionedGlobs <> sourcePaths
68
60
allJsGlobs = Packages. getJsGlobs deps depsOnly configSourcePaths <> sourcePaths
69
61
70
- checkImports globs = do
71
- minVersion <- case Version. semver " 0.14.0" of
72
- Left _ -> die [ " Unable to parse min version for imports check" ]
73
- Right minVersion -> pure minVersion
74
- when (compilerVersion >= minVersion) $ do
75
- graph <- Purs. graph globs
76
- case graph of
77
- Left err -> logWarn $ displayShow err
78
- Right (Purs. ModuleGraph moduleGraph) -> do
62
+ checkImports = do
63
+ maybeGraph <- view (the @ Graph )
64
+ for_ maybeGraph $ \ (Purs. ModuleGraph moduleGraph) -> do
79
65
let
80
66
matchesGlob :: Sys. FilePath -> SourcePath -> Bool
81
67
matchesGlob path sourcePath =
@@ -88,11 +74,11 @@ build BuildOptions{..} maybePostBuild = do
88
74
projectModules :: [ModuleName ]
89
75
projectModules =
90
76
map fst
91
- $ filter (\ (_, Purs. ModuleGraphNode {.. }) -> isProjectFile (Text. unpack path ))
77
+ $ filter (\ (_, Purs. ModuleGraphNode {.. }) -> isProjectFile (Text. unpack graphNodePath ))
92
78
$ Map. toList moduleGraph
93
79
94
80
getImports :: ModuleName -> Set ModuleName
95
- getImports = maybe Set. empty (Set. fromList . Purs. depends ) . flip Map. lookup moduleGraph
81
+ getImports = maybe Set. empty (Set. fromList . graphNodeDepends ) . flip Map. lookup moduleGraph
96
82
97
83
-- All package modules that are imported from our project files
98
84
importedPackageModules :: Set ModuleName
@@ -113,7 +99,7 @@ build BuildOptions{..} maybePostBuild = do
113
99
importedPackages :: Set PackageName
114
100
importedPackages =
115
101
Set. fromList
116
- $ mapMaybe (getPackageFromPath . Purs. path <=< flip Map. lookup moduleGraph)
102
+ $ mapMaybe (getPackageFromPath . graphNodePath <=< flip Map. lookup moduleGraph)
117
103
$ Set. toList importedPackageModules
118
104
119
105
dependencyPackages :: Set PackageName
@@ -157,7 +143,7 @@ build BuildOptions{..} maybePostBuild = do
157
143
shell backendCmd empty >>= \ case
158
144
ExitSuccess -> pure ()
159
145
ExitFailure n -> die [ " Backend " <> displayShow backend <> " exited with error:" <> repr n ]
160
- checkImports globs
146
+ checkImports
161
147
162
148
buildAction globs = do
163
149
env <- Run. getEnv
@@ -254,38 +240,29 @@ repl newPackages sourcePaths pursArgs depsOnly = do
254
240
255
241
-- | Test the project: compile and run "Test.Main"
256
242
-- (or the provided module name) with node
257
- test
258
- :: HasBuildEnv env
259
- => Maybe ModuleName -> BuildOptions -> [BackendArg ]
260
- -> RIO env ()
261
- test maybeModuleName buildOpts extraArgs = do
243
+ test :: HasBuildEnv env => Maybe ModuleName -> [BackendArg ] -> RIO env ()
244
+ test maybeModuleName extraArgs = do
262
245
let moduleName = fromMaybe (ModuleName " Test.Main" ) maybeModuleName
263
- Config. Config { alternateBackend, configSourcePaths } <- view (the @ Config )
264
- liftIO (foldMapM (Glob. glob . Text. unpack . unSourcePath) configSourcePaths) >>= \ paths -> do
265
- results <- forM paths $ \ path -> do
266
- content <- readFileBinary path
267
- pure $ Parse. checkModuleNameMatches (encodeUtf8 $ unModuleName moduleName) content
268
- if or results
269
- then do
246
+ Config. Config { alternateBackend } <- view (the @ Config )
247
+ maybeGraph <- view (the @ Graph )
248
+ -- We check if the test module is included in the build and spit out a nice error if it isn't (see #383)
249
+ for_ maybeGraph $ \ (ModuleGraph moduleMap) -> case Map. lookup moduleName moduleMap of
250
+ Nothing -> die [ " Module '" <> (display . unModuleName) moduleName <> " ' not found! Are you including it in your build?" ]
251
+ Just _ -> do
270
252
sourceDir <- Turtle. pwd
271
253
let dirs = RunDirectories sourceDir sourceDir
272
- runBackend alternateBackend dirs moduleName (Just " Tests succeeded." ) " Tests failed: " buildOpts extraArgs
273
- else do
274
- die [ " Module '" <> (display . unModuleName) moduleName <> " ' not found! Are you including it in your build?" ]
254
+ runBackend alternateBackend dirs moduleName (Just " Tests succeeded." ) " Tests failed: " extraArgs
275
255
276
256
277
257
-- | Run the project: compile and run "Main"
278
258
-- (or the provided module name) with node
279
- run
280
- :: HasBuildEnv env
281
- => Maybe ModuleName -> BuildOptions -> [BackendArg ]
282
- -> RIO env ()
283
- run maybeModuleName buildOpts extraArgs = do
259
+ run :: HasBuildEnv env => Maybe ModuleName -> [BackendArg ] -> RIO env ()
260
+ run maybeModuleName extraArgs = do
284
261
Config. Config { alternateBackend } <- view (the @ Config )
285
262
let moduleName = fromMaybe (ModuleName " Main" ) maybeModuleName
286
263
sourceDir <- Turtle. pwd
287
264
let dirs = RunDirectories sourceDir sourceDir
288
- runBackend alternateBackend dirs moduleName Nothing " Running failed; " buildOpts extraArgs
265
+ runBackend alternateBackend dirs moduleName Nothing " Running failed; " extraArgs
289
266
290
267
291
268
-- | Run the select module as a script: init, compile, and run the provided module
@@ -296,7 +273,7 @@ script
296
273
-> [PackageName ]
297
274
-> ScriptBuildOptions
298
275
-> RIO env ()
299
- script modulePath tag packageDeps opts@ ScriptBuildOptions { .. } = do
276
+ script modulePath tag packageDeps opts = do
300
277
logDebug " Running `spago script`"
301
278
absoluteModulePath <- fmap Text. pack (makeAbsolute (Text. unpack modulePath))
302
279
currentDir <- Turtle. pwd
@@ -325,21 +302,10 @@ script modulePath tag packageDeps opts@ScriptBuildOptions{..} = do
325
302
let runDirs :: RunDirectories
326
303
runDirs = RunDirectories scriptDirPath currentDir
327
304
328
- Run. withBuildEnv' (Just config) NoPsa (runAction runDirs)
305
+ Run. withBuildEnv' (Just config) NoPsa buildOpts (runAction runDirs)
329
306
where
330
- runAction dirs = do
331
- let
332
- buildOpts = BuildOptions
333
- { shouldClear = NoClear
334
- , shouldWatch = BuildOnce
335
- , allowIgnored = DoAllowIgnored
336
- , sourcePaths = []
337
- , withSourceMap = WithoutSrcMap
338
- , noInstall = DoInstall
339
- , depsOnly = AllSources
340
- , ..
341
- }
342
- runBackend Nothing dirs (ModuleName " Main" ) Nothing " Script failed to run; " buildOpts []
307
+ buildOpts = fromScriptOptions defaultBuildOptions opts
308
+ runAction dirs = runBackend Nothing dirs (ModuleName " Main" ) Nothing " Script failed to run; " []
343
309
344
310
345
311
data RunDirectories = RunDirectories { sourceDir :: FilePath , executeDir :: FilePath }
@@ -353,13 +319,13 @@ runBackend
353
319
-> ModuleName
354
320
-> Maybe Text
355
321
-> Text
356
- -> BuildOptions
357
322
-> [BackendArg ]
358
323
-> RIO env ()
359
- runBackend maybeBackend RunDirectories { sourceDir, executeDir } moduleName maybeSuccessMessage failureMessage buildOpts @ BuildOptions {pursArgs} extraArgs = do
324
+ runBackend maybeBackend RunDirectories { sourceDir, executeDir } moduleName maybeSuccessMessage failureMessage extraArgs = do
360
325
logDebug $ display $ " Running with backend: " <> fromMaybe " nodejs" maybeBackend
326
+ BuildOptions { pursArgs } <- view (the @ BuildOptions )
361
327
let postBuild = maybe (nodeAction $ Path. getOutputPath pursArgs) backendAction maybeBackend
362
- build buildOpts (Just postBuild)
328
+ build (Just postBuild)
363
329
where
364
330
fromFilePath = Text. pack . Turtle. encodeString
365
331
runJsSource = fromFilePath (sourceDir Turtle. </> " .spago/run.js" )
@@ -409,7 +375,7 @@ bundleApp withMain maybeModuleName maybeTargetPath noBuild buildOpts usePsa =
409
375
let (moduleName, targetPath) = prepareBundleDefaults maybeModuleName maybeTargetPath
410
376
bundleAction = Purs. bundle withMain (withSourceMap buildOpts) moduleName targetPath
411
377
in case noBuild of
412
- DoBuild -> Run. withBuildEnv usePsa $ build buildOpts (Just bundleAction)
378
+ DoBuild -> Run. withBuildEnv usePsa buildOpts $ build (Just bundleAction)
413
379
NoBuild -> Run. getEnv >>= (flip runRIO) bundleAction
414
380
415
381
-- | Bundle into a CommonJS module
@@ -436,21 +402,20 @@ bundleModule maybeModuleName maybeTargetPath noBuild buildOpts usePsa = do
436
402
Right _ -> logInfo $ display $ " Make module succeeded and output file to " <> unTargetPath targetPath
437
403
Left (n :: SomeException ) -> die [ " Make module failed: " <> repr n ]
438
404
case noBuild of
439
- DoBuild -> Run. withBuildEnv usePsa $ build buildOpts (Just bundleAction)
405
+ DoBuild -> Run. withBuildEnv usePsa buildOpts $ build (Just bundleAction)
440
406
NoBuild -> Run. getEnv >>= (flip runRIO) bundleAction
441
407
442
408
443
409
-- | Generate docs for the `sourcePaths` and run `purescript-docs-search build-index` to patch them.
444
410
docs
445
- :: ( HasLogFunc env , HasConfig env )
411
+ :: HasBuildEnv env
446
412
=> Maybe Purs. DocsFormat
447
- -> [SourcePath ]
448
- -> Packages. DepsOnly
449
413
-> NoSearch
450
414
-> OpenDocs
451
415
-> RIO env ()
452
- docs format sourcePaths depsOnly noSearch open = do
416
+ docs format noSearch open = do
453
417
logDebug " Running `spago docs`"
418
+ BuildOptions { sourcePaths, depsOnly } <- view (the @ BuildOptions )
454
419
Config {.. } <- view (the @ Config )
455
420
deps <- Packages. getProjectDeps
456
421
logInfo " Generating documentation for the project. This might take a while..."
@@ -486,9 +451,7 @@ docs format sourcePaths depsOnly noSearch open = do
486
451
openLink link = liftIO $ Browser. openBrowser (Text. unpack link)
487
452
488
453
-- | Start a search REPL.
489
- search
490
- :: (HasPurs env , HasLogFunc env , HasConfig env )
491
- => RIO env ()
454
+ search :: HasBuildEnv env => RIO env ()
492
455
search = do
493
456
Config {.. } <- view (the @ Config )
494
457
deps <- Packages. getProjectDeps
0 commit comments