Skip to content

Commit

Permalink
Add support for global REPL (#280)
Browse files Browse the repository at this point in the history
* Support global repl

* Update CHANGELOG

* Refactor

* Change flag name and use SomeException

* Remove unused import

Co-Authored-By: Fabrizio Ferrai <fabrizio.ferrai@gmail.com>
  • Loading branch information
eahlberg and f-f authored Jun 22, 2019
1 parent 76af253 commit 02a9625
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
17 changes: 10 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
Bugfixes:
- Fix `spago install` failing when version branch names differ only by case on case-insensitive filesystems (#258)

New features:
- Add support for starting a repl within a folder which has not been setup as a spago project (#168)

## [0.8.5] - 2019-06-18

ZuriHac edition 🎉
Expand Down Expand Up @@ -118,14 +121,14 @@ New features:
## [0.7.1] - 2019-03-19

New features:
- Add `--watch` flag to `build`, `test`, `run`, `bundle` and `make-module` commands (#65, #126, #153)
- Add `--watch` flag to `build`, `test`, `run`, `bundle` and `make-module` commands (#65, #126, #153)
- Add `spago docs` command, to generate documentation from the project and all dependencies (#127)
- Add `spago run` command, to run your project (#131, #137)

Other fixes and improvements:
- Automatically build in commands that require the project to be built (#146, #149)
- Don't automatically create a configuration if not found (#139, #144)
- Always ensure that the package-set has a hash on it, for speed and security reasons (#128)
- Don't automatically create a configuration if not found (#139, #144)
- Always ensure that the package-set has a hash on it, for speed and security reasons (#128)
- Improvements to documentation and FAQ (#132, #125, #119, #123, #104, #135)
- Improvements to errors, messages and logging (#143, #145, #133, #151, #148, #129, #130, #136)
- Improvements to tests (#95, #91, #138, #141, #140)
Expand All @@ -142,12 +145,12 @@ Breaking changes:
New features:
- Support Windows in NPM install (#121, #109)
- Add `spago freeze` command to recompute hashes of the package-set (#113)
- Add `spago verify` and `spago verify-set` commands (#108, #14)
- Add `spago verify` and `spago verify-set` commands (#108, #14)
- Add the `--filter` flag to `spago list-packages`, to filter by direct and transitive deps (#106, #108)
- Check that the version of the installed compiler is at least what the package-set requires (#101, #107, #117, #116)
- Check that the version of the installed compiler is at least what the package-set requires (#101, #107, #117, #116)

Other improvements:
- Improve the installation: do less work and print less useless stuff (#110, #112, #114)
- Improve the installation: do less work and print less useless stuff (#110, #112, #114)
- Skip the copy of template files if the source directories exist (#102, #105)

## [0.6.4] - 2019-02-07
Expand Down Expand Up @@ -187,7 +190,7 @@ New features:

Bugfixes:
- Don't overwrite files when doing `init`, just skip the copy if some file exists (#56)
- Print `git` output in case of failure when doing `install` (#54, #59)
- Print `git` output in case of failure when doing `install` (#54, #59)
- Include building `src/*` when running `test` (#50, #53)
- Make file embedding indipendent of the locale when compiling; now we just use Unicode

Expand Down
7 changes: 4 additions & 3 deletions app/Spago.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ data Command
| Sources

-- | Start a REPL.
| Repl [SourcePath] [ExtraArg]
| Repl (Maybe Int) (Maybe CacheFlag) [PackageName] [SourcePath] [ExtraArg]

-- | Generate documentation for the project and its dependencies
| Docs [SourcePath]
Expand Down Expand Up @@ -156,6 +156,7 @@ parser = do
sourcePaths = CLI.many (CLI.opt (Just . SourcePath) "path" 'p' "Source path to include")
packageName = CLI.arg (Just . PackageName) "package" "Specify a package name. You can list them with `list-packages`"
packageNames = CLI.many $ CLI.arg (Just . PackageName) "package" "Package name to add as dependency"
replPackageNames = CLI.many $ CLI.opt (Just . PackageName) "dependency" 'd' "Package name to add to the REPL as dependency"
passthroughArgs = many $ CLI.arg (Just . ExtraArg) " ..any `purs compile` option" "Options passed through to `purs compile`; use -- to separate"
buildOptions = BuildOptions <$> limitJobs <*> cacheFlag <*> watch <*> clearScreen <*> sourcePaths <*> noInstall <*> passthroughArgs
globalOptions = GlobalOptions <$> verbose
Expand Down Expand Up @@ -192,7 +193,7 @@ parser = do
repl =
( "repl"
, "Start a REPL"
, Repl <$> sourcePaths <*> passthroughArgs
, Repl <$> limitJobs <*> cacheFlag <*> replPackageNames <*> sourcePaths <*> passthroughArgs
)

test =
Expand Down Expand Up @@ -351,7 +352,7 @@ main = do
Build buildOptions -> Spago.Build.build buildOptions Nothing
Test modName buildOptions nodeArgs -> Spago.Build.test modName buildOptions nodeArgs
Run modName buildOptions nodeArgs -> Spago.Build.run modName buildOptions nodeArgs
Repl paths pursArgs -> Spago.Build.repl paths pursArgs
Repl limitJobs cacheConfig replPackageNames paths pursArgs -> Spago.Build.repl limitJobs cacheConfig replPackageNames paths pursArgs
BundleApp modName tPath shouldBuild buildOptions
-> Spago.Build.bundleApp WithMain modName tPath shouldBuild buildOptions
BundleModule modName tPath shouldBuild buildOptions
Expand Down
34 changes: 28 additions & 6 deletions src/Spago/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import qualified Spago.Packages as Packages
import qualified Spago.PackageSet as PackageSet
import qualified Spago.Purs as Purs
import qualified Spago.Watch as Watch
import qualified System.IO.Temp as Temp
import qualified Turtle as Turtle


data Watch = Watch | BuildOnce
Expand Down Expand Up @@ -84,13 +86,33 @@ build BuildOptions{..} maybePostBuild = do
Watch -> Watch.watch (Set.fromAscList $ fmap Glob.compile absoluteProjectGlobs) shouldClear buildAction

-- | Start a repl
repl :: Spago m => [Purs.SourcePath] -> [Purs.ExtraArg] -> m ()
repl sourcePaths passthroughArgs = do
repl :: Spago m => Maybe Int -> Maybe GlobalCache.CacheFlag -> [PackageSet.PackageName] -> [Purs.SourcePath] -> [Purs.ExtraArg] -> m ()
repl maybeLimit cacheFlag newPackages sourcePaths passthroughArgs = do
echoDebug "Running `spago repl`"
config <- Config.ensureConfig
deps <- Packages.getProjectDeps config
let globs = Packages.getGlobs deps <> Config.configSourcePaths config <> sourcePaths
Purs.repl globs passthroughArgs

try Config.ensureConfig >>= \case
Right config -> do
deps <- Packages.getProjectDeps config
let globs = Packages.getGlobs deps <> Config.configSourcePaths config <> sourcePaths
Purs.repl globs passthroughArgs
Left (err :: SomeException) -> do
echoDebug $ tshow err
cacheDir <- GlobalCache.getGlobalCacheDir
Temp.withTempDirectory cacheDir "spago-repl-tmp" $ \dir -> do
Turtle.cd (Turtle.decodeString dir)

Packages.initProject False

config@Config.Config{ packageSet = PackageSet.PackageSet{..}, ..} <- Config.ensureConfig

let updatedConfig = Config.Config name (dependencies <> newPackages) (Config.packageSet config) configSourcePaths

deps <- Packages.getProjectDeps updatedConfig
let globs = Packages.getGlobs deps <> Config.configSourcePaths updatedConfig <> sourcePaths

Fetch.fetchPackages maybeLimit cacheFlag deps packagesMinPursVersion

Purs.repl globs passthroughArgs

-- | Test the project: compile and run "Test.Main"
-- (or the provided module name) with node
Expand Down

0 comments on commit 02a9625

Please sign in to comment.