Skip to content
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

option to add/remove package for a single project #610

Merged
merged 1 commit into from
Feb 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions src/Paket.Core/AddProcess.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,59 @@ module Paket.AddProcess
open Paket
open System.IO
open Paket.Domain
open Paket.Logging

let Add(dependenciesFileName, package, version, force, hard, interactive, installAfter) =
let private notInstalled (project : ProjectFile) package = project.HasPackageInstalled(NormalizedPackageName package) |> not

let private addToProject project package =
ProjectFile.FindOrCreateReferencesFile(FileInfo(project.FileName))
.AddNuGetReference(package)
.Save()

let private add addToProjects dependenciesFileName package version force hard installAfter =
let existingDependenciesFile = DependenciesFile.ReadFromFile(dependenciesFileName)
let dependenciesFile =
existingDependenciesFile
.Add(package,version)
.Add(package,version)

dependenciesFile.Save()

let lockFile = UpdateProcess.SelectiveUpdate(dependenciesFile,Some(NormalizedPackageName package),force)
let projects = seq { for p in ProjectFile.FindAllProjects(Path.GetDirectoryName lockFile.FileName) -> p } // lazy sequence in case no project install required

if interactive then
for project in ProjectFile.FindAllProjects(Path.GetDirectoryName lockFile.FileName) do
let notInstalled = project.HasPackageInstalled(NormalizedPackageName package) |> not
if notInstalled && Utils.askYesNo(sprintf " Install to %s?" project.Name) then
ProjectFile.FindOrCreateReferencesFile(FileInfo(project.FileName))
.AddNuGetReference(package)
.Save()
package |> addToProjects projects

if installAfter then
let sources = dependenciesFile.GetAllPackageSources()
InstallProcess.Install(sources, force, hard, false, lockFile)
InstallProcess.Install(sources, force, hard, false, lockFile)

// add a package with the option to add it to a specified project
let AddToProject(dependenciesFileName, package, version, force, hard, projectName, installAfter) =

let addToSpecifiedProject (projects : ProjectFile seq) package =
let project =
projects
|> Seq.tryFind (fun p ->
let name = p.Name.Split('.').[0]
name = projectName || p.Name = projectName)

match project with
| Some p ->
if package |> notInstalled p then
package |> addToProject p
else traceWarnfn "Package %s already installed in project %s" package.Id p.Name
| None ->
traceErrorfn "Could not install package in specified project %s. Project not found" projectName

add addToSpecifiedProject dependenciesFileName package version force hard installAfter

// add a package with the option to interactively add it to multiple projects
let Add(dependenciesFileName, package, version, force, hard, interactive, installAfter) =

let addToProjects (projects : ProjectFile seq) package =
if interactive then
for project in projects do
if package |> notInstalled project && Utils.askYesNo(sprintf " Install to %s?" project.Name) then
package |> addToProject project

add addToProjects dependenciesFileName package version force hard installAfter
12 changes: 12 additions & 0 deletions src/Paket.Core/PublicAPI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ type Dependencies(dependenciesFileName: string) =
Utils.RunInLockedAccessMode(
this.RootPath,
fun () -> AddProcess.Add(dependenciesFileName, PackageName package, version, force, hard, interactive, installAfter))

/// Adds the given package with the given version to the dependencies file.
member this.AddToProject(package: string,version: string,force: bool,hard: bool,projectName: string,installAfter: bool): unit =
Utils.RunInLockedAccessMode(
this.RootPath,
fun () -> AddProcess.AddToProject(dependenciesFileName, PackageName package, version, force, hard, projectName, installAfter))

/// Adds credentials for a Nuget feed
member this.AddCredentials(source: string, username: string) : unit =
Expand Down Expand Up @@ -230,6 +236,12 @@ type Dependencies(dependenciesFileName: string) =
this.RootPath,
fun () -> RemoveProcess.Remove(dependenciesFileName, PackageName package, force, hard, interactive, installAfter))

/// Removes the given package from the specified project
member this.RemoveFromProject(package: string,force: bool,hard: bool,projectName: string,installAfter: bool): unit =
Utils.RunInLockedAccessMode(
this.RootPath,
fun () -> RemoveProcess.RemoveFromProject(dependenciesFileName, PackageName package, force, hard, projectName, installAfter))

/// Shows all references for the given packages.
member this.ShowReferencesFor(packages: string list): unit =
FindReferences.ShowReferencesFor (packages |> List.map PackageName) |> this.Process
Expand Down
49 changes: 41 additions & 8 deletions src/Paket.Core/RemoveProcess.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ module Paket.RemoveProcess
open Paket
open System.IO
open Paket.Domain
open Paket.Logging

let Remove(dependenciesFileName, package:PackageName, force, hard, interactive, installAfter) =
let private removePackageFromProject project package =
ProjectFile.FindOrCreateReferencesFile(FileInfo(project.FileName))
.RemoveNuGetReference(package)
.Save()

let private remove removeFromProjects dependenciesFileName (package: PackageName) force hard installAfter =
let (PackageName name) = package
let root = Path.GetDirectoryName dependenciesFileName
let allProjects = ProjectFile.FindAllProjects root
for project in allProjects do
if project.HasPackageInstalled(NormalizedPackageName package) then
if (not interactive) || Utils.askYesNo(sprintf " Remove from %s?" project.Name) then
ProjectFile.FindOrCreateReferencesFile(FileInfo(project.FileName))
.RemoveNuGetReference(package)
.Save()

removeFromProjects allProjects

// check we have it removed from all paket.references files
let stillInstalled =
Expand Down Expand Up @@ -44,4 +46,35 @@ let Remove(dependenciesFileName, package:PackageName, force, hard, interactive,

if installAfter then
let sources = DependenciesFile.ReadFromFile(dependenciesFileName).GetAllPackageSources()
InstallProcess.Install(sources, force, hard, false, lockFile)
InstallProcess.Install(sources, force, hard, false, lockFile)

// remove a package with the option to remove it from a specified project
let RemoveFromProject(dependenciesFileName, package:PackageName, force, hard, projectName, installAfter) =

let removeFromSpecifiedProject (projects : ProjectFile seq) =
let project =
projects
|> Seq.tryFind (fun p ->
let name = p.Name.Split('.').[0]
name = projectName || p.Name = projectName)

match project with
| Some p ->
if p.HasPackageInstalled(NormalizedPackageName package) then
package |> removePackageFromProject p
else traceWarnfn "Package %s was not installed in project %s" package.Id p.Name
| None ->
traceErrorfn "Could not install package in specified project %s. Project not found" projectName

remove removeFromSpecifiedProject dependenciesFileName package force hard installAfter

// remove a package with the option to interactively remove it from multiple projects
let Remove(dependenciesFileName, package:PackageName, force, hard, interactive, installAfter) =

let removeFromProjects (projects: ProjectFile seq) =
for project in projects do
if project.HasPackageInstalled(NormalizedPackageName package) then
if (not interactive) || Utils.askYesNo(sprintf " Remove from %s?" project.Name) then
package |> removePackageFromProject project

remove removeFromProjects dependenciesFileName package force hard installAfter
2 changes: 2 additions & 0 deletions src/Paket/Commands.fs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ with
type AddArgs =
| [<CustomCommandLine("nuget")>][<Mandatory>] Nuget of string
| [<CustomCommandLine("version")>] Version of string
| [<CustomCommandLine("project")>] Project of string
| [<AltCommandLine("-f")>] Force
| [<AltCommandLine("-i")>] Interactive
| Hard
Expand Down Expand Up @@ -88,6 +89,7 @@ with

type RemoveArgs =
| [<CustomCommandLine("nuget")>][<Mandatory>] Nuget of string
| [<CustomCommandLine("project")>] Project of string
| [<AltCommandLine("-f")>] Force
| [<AltCommandLine("-i")>] Interactive
| Hard
Expand Down
16 changes: 15 additions & 1 deletion src/Paket/HelpTexts.fs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,14 @@ Options:

`--hard`: Replaces package references within project files even if they are not yet adhering to to Paket's conventions (and hence considered manually managed). See [convert from NuGet](paket-convert-from-nuget.html).

See also [paket remove](paket-remove.html).
## Adding to a single project

It's also possible to add a package to a specified project only:

[lang=batchfile]
$ paket add nuget PACKAGENAME [version VERSION] [project PROJECT] [--force] [--hard]

See also [paket remove](paket-remove.html).

## Sample

Expand Down Expand Up @@ -318,6 +325,13 @@ Options:

`--hard`: Replaces package references within project files even if they are not yet adhering to to Paket's conventions (and hence considered manually managed). See [convert from NuGet](paket-convert-from-nuget.html).

## Removing from a single project

It's also possible to remove a package from a specified project only:

[lang=batchfile]
$ paket remove nuget PACKAGENAME [project PROJECT] [--force] [--hard]

See also [paket add](paket-add.html)."""}

"install",
Expand Down
16 changes: 12 additions & 4 deletions src/Paket/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ try
let version = defaultArg (results.TryGetResult <@ AddArgs.Version @>) ""
let force = results.Contains <@ AddArgs.Force @>
let hard = results.Contains <@ AddArgs.Hard @>
let interactive = results.Contains <@ AddArgs.Interactive @>
let noInstall = results.Contains <@ AddArgs.No_Install @>
Dependencies.Locate().Add(packageName, version, force, hard, interactive, noInstall |> not)
match results.TryGetResult <@ AddArgs.Project @> with
| Some projectName ->
Dependencies.Locate().AddToProject(packageName, version, force, hard, projectName, noInstall |> not)
| None ->
let interactive = results.Contains <@ AddArgs.Interactive @>
Dependencies.Locate().Add(packageName, version, force, hard, interactive, noInstall |> not)

| Command(Config, args) ->
let results = commandArgs<ConfigArgs> args
Expand Down Expand Up @@ -174,9 +178,13 @@ try
let packageName = results.GetResult <@ RemoveArgs.Nuget @>
let force = results.Contains <@ RemoveArgs.Force @>
let hard = results.Contains <@ RemoveArgs.Hard @>
let interactive = results.Contains <@ RemoveArgs.Interactive @>
let noInstall = results.Contains <@ RemoveArgs.No_Install @>
Dependencies.Locate().Remove(packageName, force, hard, interactive, noInstall |> not)
match results.TryGetResult <@ RemoveArgs.Project @> with
| Some projectName ->
Dependencies.Locate().RemoveFromProject(packageName, force, hard, projectName, noInstall |> not)
| None ->
let interactive = results.Contains <@ RemoveArgs.Interactive @>
Dependencies.Locate().Remove(packageName, force, hard, interactive, noInstall |> not)

| Command(Restore, args) ->
let results = commandArgs<RestoreArgs> args
Expand Down