-
Notifications
You must be signed in to change notification settings - Fork 131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add uninstall command #1111
Merged
Merged
Add uninstall command #1111
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
packages
here are additional packages to add to the config, which is the opposite of what we want to achieve here 😄There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAICT via
mkFetchEnv
, that's not true. Rather, we just validate there that the strings we got from the CLI can be parsed intoPackageName
values. The resultingpackageNames
value is never used anywhere else and is just stored infetchOpts
. And for context, the only reason why I'm usingmkFetchEnv
instead of doing this outside of that is because I need aworkspace
value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah sorry - I got confused about
mkFetchEnv
vsFetch.run
, all good