Skip to content

Commit ab47fd8

Browse files
authored
spago repl writes a .purs-repl file unless already there (#1255)
1 parent 3ac61f5 commit ab47fd8

File tree

9 files changed

+59
-25
lines changed

9 files changed

+59
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Other improvements:
1414
- Update README with info about depending on a freshly added library
1515
- Fixed globbing issue where `/.spago` behaves differently than `.spago` in `.gitignore`
1616
- Fixed empty output for `--verbose-stats` when there are no errors or warnings.
17+
- `spago repl` now writes a `.purs-repl` file, unless already there, containing `import Prelude`.
1718

1819
## [0.21.0] - 2023-05-04
1920

core/src/FS.purs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
module Spago.FS
22
( chmod
3+
, copyFile
4+
, copyFileSync
5+
, copyTree
6+
, unlink
37
, ensureFileSync
48
, exists
59
, getInBetweenPaths
610
, isLink
711
, ls
812
, mkdirp
913
, moveSync
10-
, copyFileSync
11-
, copyFile
12-
, copyTree
1314
, readJsonFile
1415
, readTextFile
1516
, readYamlDocFile
@@ -69,6 +70,9 @@ ensureFileSync file = liftEffect $ ensureFileSyncImpl file
6970
exists :: forall m. MonadEffect m => String -> m Boolean
7071
exists = liftEffect <<< FS.Sync.exists
7172

73+
unlink :: m. MonadAff m => String -> m Unit
74+
unlink = liftAff <<< FS.Aff.unlink
75+
7276
writeTextFile :: forall m. MonadAff m => FilePath -> String -> m Unit
7377
writeTextFile path text = liftAff $ FS.Aff.writeTextFile UTF8 path text
7478

src/Spago/Command/Init.purs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
module Spago.Command.Init
2-
( run
3-
, InitOptions
4-
, srcMainTemplate
5-
, testMainTemplate
6-
, defaultConfig
7-
, DefaultConfigOptions(..)
2+
( DefaultConfigOptions(..)
83
, DefaultConfigPackageOptions
94
, DefaultConfigWorkspaceOptions
5+
, InitOptions
6+
, defaultConfig
107
, defaultConfig'
8+
, pursReplFile
9+
, run
10+
, srcMainTemplate
11+
, testMainTemplate
1112
) where
1213

1314
import Spago.Prelude
@@ -31,7 +32,7 @@ type InitOptions =
3132

3233
-- TODO run git init? Is that desirable?
3334

34-
run :: InitOptions -> Spago (RegistryEnv _) Config
35+
run :: a. InitOptions -> Spago (RegistryEnv a) Config
3536
run opts = do
3637
logInfo "Initializing a new project..."
3738

@@ -71,7 +72,7 @@ run opts = do
7172

7273
copyIfNotExists ".gitignore" gitignoreTemplate
7374

74-
copyIfNotExists ".purs-repl" pursReplTemplate
75+
copyIfNotExists pursReplFile.name pursReplFile.content
7576

7677
pure config
7778

@@ -227,11 +228,8 @@ generated-docs/
227228
.spago
228229
"""
229230

230-
pursReplTemplate :: String
231-
pursReplTemplate =
232-
"""
233-
import Prelude
234-
"""
231+
pursReplFile :: { name :: String, content :: String }
232+
pursReplFile = { name: ".purs-repl", content: "import Prelude\n" }
235233

236234
-- ERROR TEXTS -----------------------------------------------------------------
237235

src/Spago/Command/Repl.purs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import Spago.Prelude
88
import Data.Map as Map
99
import Spago.Command.Build as Build
1010
import Spago.Command.Fetch as Fetch
11+
import Spago.Command.Init (pursReplFile)
1112
import Spago.Config (PackageMap, WorkspacePackage)
13+
import Spago.FS as FS
1214
import Spago.Purs (Purs)
1315
import Spago.Purs as Purs
1416

@@ -23,10 +25,13 @@ type ReplEnv a =
2325
| a
2426
}
2527

26-
run :: Spago (ReplEnv _) Unit
28+
run :: a. Spago (ReplEnv a) Unit
2729
run = do
2830
{ dependencies, pursArgs, selected, depsOnly, supportPackage } <- ask
2931

32+
unlessM (FS.exists pursReplFile.name) $
33+
FS.writeTextFile pursReplFile.name pursReplFile.content
34+
3035
let
3136
allDependencies = Map.unionWith (\l _ -> l) supportPackage $ Fetch.toAllDependencies dependencies
3237
globs = Build.getBuildGlobs

test/Spago.purs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Test.Spago.Lock as Lock
2020
import Test.Spago.Ls as Ls
2121
import Test.Spago.Publish as Publish
2222
import Test.Spago.Registry as Registry
23+
import Test.Spago.Repl as Repl
2324
import Test.Spago.Run as Run
2425
import Test.Spago.Sources as Sources
2526
import Test.Spago.Test as Test
@@ -47,6 +48,7 @@ main = Aff.launchAff_ $ void $ un Identity $ Spec.Runner.runSpecT testConfig [ S
4748
Uninstall.spec
4849
Ls.spec
4950
Build.spec
51+
Repl.spec
5052
Run.spec
5153
Test.spec
5254
Bundle.spec

test/Spago/Build.purs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ spec = Spec.around withTempDir do
4343

4444
Spec.it "can build with a local custom package set" \{ spago, fixture } -> do
4545
spago [ "init" ] >>= shouldBeSuccess
46-
FSA.unlink "spago.yaml"
46+
FS.unlink "spago.yaml"
4747
FS.copyFileSync { src: fixture "local-package-set-config.yaml", dst: "spago.yaml" }
4848
FS.copyFileSync { src: fixture "local-package-set.json", dst: "local-package-set.json" }
4949
spago [ "build" ] >>= shouldBeSuccess
@@ -53,7 +53,7 @@ spec = Spec.around withTempDir do
5353
FS.mkdirp "subdir"
5454
liftEffect $ Process.chdir "subdir"
5555
spago [ "init" ] >>= shouldBeSuccess
56-
FSA.unlink "spago.yaml"
56+
FS.unlink "spago.yaml"
5757
FS.copyFileSync { src: fixture "local-package-set-config2.yaml", dst: "spago.yaml" }
5858
spago [ "build" ] >>= shouldBeSuccess
5959

@@ -78,7 +78,7 @@ spec = Spec.around withTempDir do
7878
Spec.it "--strict causes build to fail if there are warnings" \{ spago, fixture } -> do
7979
spago [ "init" ] >>= shouldBeSuccess
8080
let srcMain = Path.concat [ "src", "Main.purs" ]
81-
FSA.unlink srcMain
81+
FS.unlink srcMain
8282
FS.copyFile
8383
{ src: fixture "check-strict.purs"
8484
, dst: srcMain
@@ -90,12 +90,12 @@ spec = Spec.around withTempDir do
9090
let
9191
srcMain = Path.concat [ "src", "Main.purs" ]
9292
spagoYaml = "spago.yaml"
93-
FSA.unlink srcMain
93+
FS.unlink srcMain
9494
FS.copyFile
9595
{ src: fixture "check-strict.purs"
9696
, dst: srcMain
9797
}
98-
FSA.unlink spagoYaml
98+
FS.unlink spagoYaml
9999
FS.copyFile
100100
{ src: fixture "check-strict.yaml"
101101
, dst: spagoYaml

test/Spago/Install.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ spec = Spec.around withTempDir do
4949
Spec.it "adds test dependencies to the config file when the test section does not exist" \{ spago, fixture } -> do
5050
spago [ "init", "--name", "aaa", "--package-set", "29.3.0" ] >>= shouldBeSuccess
5151
let spagoYaml = "spago.yaml"
52-
FSA.unlink spagoYaml
52+
FS.unlink spagoYaml
5353
FS.copyFile
5454
{ src: fixture "no-test-section.yaml"
5555
, dst: spagoYaml

test/Spago/Publish.purs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module Test.Spago.Publish (spec) where
22

33
import Test.Prelude
44

5-
import Node.FS.Aff as FSA
65
import Node.Platform as Platform
76
import Node.Process as Process
87
import Spago.Cmd as Cmd
@@ -34,7 +33,7 @@ spec = Spec.around withTempDir do
3433

3534
Spec.it "fails if the module is called Main" \{ spago, fixture } -> do
3635
spago [ "init", "--name", "aaaa" ] >>= shouldBeSuccess
37-
FSA.unlink "spago.yaml"
36+
FS.unlink "spago.yaml"
3837
FS.copyFile { src: fixture "spago-publish.yaml", dst: "spago.yaml" }
3938
spago [ "build" ] >>= shouldBeSuccess
4039
doTheGitThing

test/Spago/Repl.purs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Test.Spago.Repl where
2+
3+
import Test.Prelude
4+
5+
import Spago.FS as FS
6+
import Test.Spec (Spec)
7+
import Test.Spec as Spec
8+
9+
spec :: Spec Unit
10+
spec = Spec.around withTempDir do
11+
Spec.describe "repl" do
12+
13+
Spec.it "writes .purs-repl if not there" \{ spago, spago' } -> do
14+
spago [ "init" ] >>= shouldBeSuccess
15+
FS.readTextFile ".purs-repl" >>= shouldEqual "import Prelude\n"
16+
17+
FS.unlink ".purs-repl"
18+
spago' (StdinWrite ":q") [ "repl" ] >>= shouldBeSuccess
19+
FS.readTextFile ".purs-repl" >>= shouldEqual "import Prelude\n"
20+
21+
Spec.it "does not write .purs-repl if already there" \{ spago, spago' } -> do
22+
spago [ "init" ] >>= shouldBeSuccess
23+
FS.writeTextFile ".purs-repl" "import Data.Maybe\n"
24+
spago' (StdinWrite ":q") [ "repl" ] >>= shouldBeSuccess
25+
FS.readTextFile ".purs-repl" >>= shouldEqual "import Data.Maybe\n"

0 commit comments

Comments
 (0)