-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add uninstall command * Add initial round of tests * Modify doc more correctly --------- Co-authored-by: Fabrizio Ferrai <fabrizio.ferrai@gmail.com>
- Loading branch information
1 parent
23697f4
commit 3c65f88
Showing
15 changed files
with
309 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
module Spago.Command.Uninstall | ||
( run | ||
, UninstallEnv | ||
, UninstallArgs | ||
) where | ||
|
||
import Spago.Prelude | ||
|
||
import Data.Array as Array | ||
import Data.Array.NonEmpty as NEA | ||
import Data.FoldableWithIndex (foldlWithIndex) | ||
import Data.Map as Map | ||
import Data.Set.NonEmpty as NonEmptySet | ||
import Node.Path as Path | ||
import Registry.PackageName as PackageName | ||
import Spago.Config (Dependencies, PackageConfig, Workspace) | ||
import Spago.Config as Config | ||
import Spago.Config as Core | ||
import Spago.FS as FS | ||
|
||
type UninstallArgs = | ||
{ dependenciesToRemove :: Set PackageName | ||
, testDeps :: Boolean | ||
} | ||
|
||
type UninstallEnv = | ||
{ workspace :: Workspace | ||
, logOptions :: LogOptions | ||
} | ||
|
||
run :: UninstallArgs -> Spago UninstallEnv Unit | ||
run args = do | ||
logDebug "Running `spago uninstall`" | ||
{ workspace } <- ask | ||
let | ||
modifyConfig | ||
:: FilePath | ||
-> YamlDoc Core.Config | ||
-> String | ||
-> NonEmptyArray PackageName | ||
-> Spago UninstallEnv Unit | ||
modifyConfig configPath yamlDoc sourceOrTestString = \removedPackages -> do | ||
logInfo | ||
[ "Removing the following " <> sourceOrTestString <> " dependencies:" | ||
, " " <> intercalateMap ", " PackageName.print removedPackages | ||
] | ||
logDebug $ "Editing config file at path: " <> configPath | ||
liftEffect $ Config.removePackagesFromConfig yamlDoc args.testDeps $ NonEmptySet.fromFoldable1 removedPackages | ||
liftAff $ FS.writeYamlDocFile configPath yamlDoc | ||
where | ||
intercalateMap sep f = _.val <<< foldl go { init: true, val: "" } | ||
where | ||
go acc next = { init: false, val: if acc.init then f next else acc.val <> sep <> f next } | ||
|
||
toContext | ||
:: FilePath | ||
-> YamlDoc Core.Config | ||
-> PackageConfig | ||
-> Either | ||
PackageName | ||
{ name :: PackageName | ||
, deps :: Dependencies | ||
, sourceOrTestString :: String | ||
, modifyDoc :: NonEmptyArray PackageName -> Spago UninstallEnv Unit | ||
} | ||
toContext configPath yamlDoc pkgConfig | ||
| args.testDeps = case pkgConfig.test of | ||
Nothing -> | ||
Left pkgConfig.name | ||
Just { dependencies } -> do | ||
let sourceOrTestString = "test" | ||
Right | ||
{ name: pkgConfig.name | ||
, deps: dependencies | ||
, sourceOrTestString | ||
, modifyDoc: modifyConfig configPath yamlDoc sourceOrTestString | ||
} | ||
| otherwise = do | ||
let sourceOrTestString = "source" | ||
Right | ||
{ name: pkgConfig.name | ||
, deps: pkgConfig.dependencies | ||
, sourceOrTestString | ||
, modifyDoc: modifyConfig configPath yamlDoc sourceOrTestString | ||
} | ||
missingTestConfigOrContext <- case workspace.selected of | ||
Just p -> | ||
pure $ toContext (Path.concat [ p.path, "spago.yaml" ]) p.doc p.package | ||
Nothing -> do | ||
case workspace.rootPackage of | ||
Nothing -> | ||
die "No package was selected. Please select a package." | ||
Just p -> | ||
pure $ toContext "spago.yaml" workspace.doc p | ||
case missingTestConfigOrContext of | ||
Left pkgName -> | ||
logWarn $ "Could not uninstall test dependencies for " <> PackageName.print pkgName <> " because it does not have a test configuration." | ||
Right context -> do | ||
logDebug $ "Existing " <> context.sourceOrTestString <> " dependencies are: " <> (Array.intercalate ", " $ foldlWithIndex (\k a _ -> Array.snoc a $ PackageName.print k) [] $ unwrap context.deps) | ||
let | ||
{ warn, removed } = foldl separate init args.dependenciesToRemove | ||
where | ||
init = { warn: [], removed: [] } | ||
|
||
separate :: _ -> PackageName -> _ | ||
separate acc next | ||
| Map.member next $ unwrap context.deps = acc { removed = Array.snoc acc.removed next } | ||
| otherwise = acc { warn = Array.snoc acc.warn next } | ||
for_ (NEA.fromArray warn) \undeclaredPkgs -> | ||
logWarn | ||
[ "The following packages cannot be uninstalled because they are not declared in the package's " <> context.sourceOrTestString <> " dependencies:" | ||
, " " <> NEA.intercalate ", " (map PackageName.print undeclaredPkgs) | ||
] | ||
|
||
case NEA.fromArray removed of | ||
Nothing -> | ||
logInfo $ "The package config for " <> PackageName.print context.name <> " was not updated." | ||
Just removed' -> | ||
context.modifyDoc removed' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Reading Spago workspace configuration... | ||
Read the package set from the registry | ||
|
||
✅ Selecting package to build: uninstall-tests | ||
|
||
⚠️ The following packages cannot be uninstalled because they are not declared in the package's source dependencies: | ||
either | ||
The package config for uninstall-tests was not updated. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Reading Spago workspace configuration... | ||
Read the package set from the registry | ||
|
||
✅ Selecting package to build: uninstall-tests | ||
|
||
⚠️ The following packages cannot be uninstalled because they are not declared in the package's test dependencies: | ||
either | ||
The package config for uninstall-tests was not updated. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Reading Spago workspace configuration... | ||
Read the package set from the registry | ||
|
||
✅ Selecting 2 packages to build: | ||
bar | ||
foo | ||
|
||
|
||
❌ No package was selected. Please select a package. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Reading Spago workspace configuration... | ||
Read the package set from the registry | ||
|
||
✅ Selecting package to build: uninstall-tests | ||
|
||
⚠️ Could not uninstall test dependencies for uninstall-tests because it does not have a test configuration. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Reading Spago workspace configuration... | ||
Read the package set from the registry | ||
|
||
✅ Selecting package to build: uninstall-tests | ||
|
||
Removing the following source dependencies: | ||
either |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Reading Spago workspace configuration... | ||
Read the package set from the registry | ||
|
||
✅ Selecting package to build: uninstall-tests | ||
|
||
Removing the following test dependencies: | ||
either |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.