@@ -2,6 +2,7 @@ module Spago.Command.Init
2
2
( DefaultConfigOptions (..)
3
3
, DefaultConfigPackageOptions
4
4
, DefaultConfigWorkspaceOptions
5
+ , InitMode (..)
5
6
, InitOptions
6
7
, defaultConfig
7
8
, defaultConfig'
@@ -14,65 +15,79 @@ module Spago.Command.Init
14
15
import Spago.Prelude
15
16
16
17
import Data.Map as Map
18
+ import Data.String as String
17
19
import Node.Path as Path
18
20
import Registry.PackageName as PackageName
19
21
import Registry.Version as Version
20
22
import Spago.Config (Dependencies (..), SetAddress (..), Config )
21
23
import Spago.Config as Config
22
24
import Spago.FS as FS
25
+ import Spago.Log as Log
26
+ import Spago.Paths as Paths
23
27
import Spago.Registry (RegistryEnv )
24
28
import Spago.Registry as Registry
25
29
30
+ data InitMode
31
+ = InitWorkspace { packageName :: Maybe String }
32
+ | InitSubpackage { packageName :: String }
33
+
26
34
type InitOptions =
27
35
-- TODO: we should allow the `--package-set` flag to alternatively pass in a URL
28
36
{ setVersion :: Maybe Version
29
- , packageName :: PackageName
37
+ , mode :: InitMode
30
38
, useSolver :: Boolean
31
39
}
32
40
33
41
-- TODO run git init? Is that desirable?
34
42
35
43
run :: ∀ a . InitOptions -> Spago (RegistryEnv a ) Config
36
44
run opts = do
37
- logInfo " Initializing a new project..."
38
-
39
45
-- Use the specified version of the package set (if specified).
40
46
-- Otherwise, get the latest version of the package set for the given compiler
41
47
packageSetVersion <- Registry .findPackageSet opts.setVersion
42
48
49
+ packageName <- getPackageName
50
+ withWorkspace <- getWithWorkspace packageSetVersion
51
+ projectDir <- getProjectDir packageName
52
+
43
53
{ purs } <- ask
54
+ logInfo " Initializing a new project..."
44
55
logInfo $ " Found PureScript " <> Version .print purs.version <> " , will use package set " <> Version .print packageSetVersion
45
56
46
- -- Write config
47
57
let
48
- config = defaultConfig
49
- { name: opts.packageName
50
- , withWorkspace: Just
51
- { setVersion: case opts.useSolver of
52
- true -> Nothing
53
- false -> Just packageSetVersion
54
- }
55
- , testModuleName: " Test.Main"
56
- }
57
- let configPath = " spago.yaml"
58
+ mainModuleName = " Main"
59
+ testModuleName = " Test.Main"
60
+ srcDir = Path .concat [ projectDir, " src" ]
61
+ testDir = Path .concat [ projectDir, " test" ]
62
+ configPath = Path .concat [ projectDir, " spago.yaml" ]
63
+ config = defaultConfig { name: packageName, withWorkspace, testModuleName }
64
+
65
+ -- Write config
58
66
(FS .exists configPath) >>= case _ of
59
67
true -> logInfo $ foundExistingProject configPath
60
68
false -> liftAff $ FS .writeYamlFile Config .configCodec configPath config
61
69
62
70
-- If these directories (or files) exist, we skip copying "sample sources"
63
71
-- Because you might want to just init a project with your own source files,
64
72
-- or just migrate a psc-package project
65
- let mainModuleName = " Main"
66
- whenDirNotExists " src" do
67
- copyIfNotExists (" src" <> Path .sep <> mainModuleName <> " .purs" ) (srcMainTemplate mainModuleName)
73
+ whenDirNotExists srcDir do
74
+ copyIfNotExists (Path .concat [ srcDir, mainModuleName <> " .purs" ]) (srcMainTemplate mainModuleName)
68
75
69
- whenDirNotExists " test " $ do
70
- FS .mkdirp (Path .concat [ " test " , " Test" ])
71
- copyIfNotExists (Path .concat [ " test " , " Test" , " Main.purs" ]) (testMainTemplate " Test.Main " )
76
+ whenDirNotExists testDir $ do
77
+ FS .mkdirp (Path .concat [ testDir , " Test" ])
78
+ copyIfNotExists (Path .concat [ testDir , " Test" , " Main.purs" ]) (testMainTemplate testModuleName )
72
79
73
- copyIfNotExists " .gitignore" gitignoreTemplate
80
+ case opts.mode of
81
+ InitWorkspace _ -> do
82
+ copyIfNotExists " .gitignore" gitignoreTemplate
83
+ copyIfNotExists pursReplFile.name pursReplFile.content
84
+ InitSubpackage _ ->
85
+ pure unit
74
86
75
- copyIfNotExists pursReplFile.name pursReplFile.content
87
+ logInfo " Set up a new Spago project."
88
+ case opts.mode of
89
+ InitWorkspace _ -> logInfo " Try running `spago run`"
90
+ InitSubpackage _ -> logInfo $ " Try running `spago run -p " <> PackageName .print packageName <> " `"
76
91
77
92
pure config
78
93
@@ -87,6 +102,46 @@ run opts = do
87
102
true -> logInfo $ foundExistingFile dest
88
103
false -> FS .writeTextFile dest srcTemplate
89
104
105
+ getPackageName :: Spago (RegistryEnv a ) PackageName
106
+ getPackageName = do
107
+ let
108
+ candidateName = case opts.mode of
109
+ InitWorkspace { packageName: Nothing } -> String .take 150 $ Path .basename Paths .cwd
110
+ InitWorkspace { packageName: Just n } -> n
111
+ InitSubpackage { packageName: n } -> n
112
+ logDebug [ show Paths .cwd, show candidateName ]
113
+ pname <- case PackageName .parse (PackageName .stripPureScriptPrefix candidateName) of
114
+ Left err -> die
115
+ [ toDoc " Could not figure out a name for the new package. Error:"
116
+ , Log .break
117
+ , Log .indent2 $ toDoc err
118
+ ]
119
+ Right p -> pure p
120
+ logDebug [ " Got packageName and setVersion:" , PackageName .print pname, unsafeStringify opts.setVersion ]
121
+ pure pname
122
+
123
+ getWithWorkspace :: Version -> Spago (RegistryEnv a ) (Maybe { setVersion :: Maybe Version } )
124
+ getWithWorkspace setVersion = case opts.mode of
125
+ InitWorkspace _ ->
126
+ pure $ Just
127
+ { setVersion: case opts.useSolver of
128
+ true -> Nothing
129
+ false -> Just setVersion
130
+ }
131
+ InitSubpackage _ -> do
132
+ when (isJust opts.setVersion || opts.useSolver) do
133
+ logWarn " The --package-set and --use-solver flags are ignored when initializing a subpackage"
134
+ pure Nothing
135
+
136
+ getProjectDir :: PackageName -> Spago (RegistryEnv a ) FilePath
137
+ getProjectDir packageName = case opts.mode of
138
+ InitWorkspace _ ->
139
+ pure " ."
140
+ InitSubpackage _ -> do
141
+ let dirPath = PackageName .print packageName
142
+ unlessM (FS .exists dirPath) $ FS .mkdirp dirPath
143
+ pure dirPath
144
+
90
145
-- TEMPLATES -------------------------------------------------------------------
91
146
92
147
type TemplateConfig =
@@ -234,10 +289,10 @@ pursReplFile = { name: ".purs-repl", content: "import Prelude\n" }
234
289
-- ERROR TEXTS -----------------------------------------------------------------
235
290
236
291
foundExistingProject :: FilePath -> String
237
- foundExistingProject path = " Found a " <> show path <> " file, skipping copy."
292
+ foundExistingProject path = " Found a \" " <> path <> " \ " file, skipping copy."
238
293
239
294
foundExistingDirectory :: FilePath -> String
240
- foundExistingDirectory dir = " Found existing directory " <> show dir <> " , skipping copy of sample sources"
295
+ foundExistingDirectory dir = " Found existing directory \" " <> dir <> " \ " , skipping copy of sample sources"
241
296
242
297
foundExistingFile :: FilePath -> String
243
- foundExistingFile file = " Found existing file " <> show file <> " , not overwriting it"
298
+ foundExistingFile file = " Found existing file \" " <> file <> " \ " , not overwriting it"
0 commit comments