Add support for accepting directories to add and remove reference commands.

This commit adds support for specifying directories containing a single
project to both the `add reference` and `remove reference` commands.

Fixes issue #7343.
This commit is contained in:
Peter Huene 2017-12-07 17:19:51 -08:00
parent bcf13b93cc
commit 1ddf5c87c7
No known key found for this signature in database
GPG key ID: E1D265D820213D6A
19 changed files with 125 additions and 76 deletions

View file

@ -0,0 +1,2 @@
This directory is intentionally empty.

View file

@ -345,9 +345,6 @@
<data name="SolutionDoesNotExist" xml:space="preserve">
<value>Specified solution file {0} does not exist, or there is no solution file in the directory.</value>
</data>
<data name="ReferenceDoesNotExist" xml:space="preserve">
<value>Reference {0} does not exist.</value>
</data>
<data name="ReferenceIsInvalid" xml:space="preserve">
<value>Reference `{0}` is invalid.</value>
</data>

View file

@ -45,9 +45,9 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
var frameworkString = _appliedCommand.ValueOrDefault<string>("framework");
PathUtility.EnsureAllPathsExist(_appliedCommand.Arguments, CommonLocalizableStrings.ReferenceDoesNotExist);
PathUtility.EnsureAllPathsExist(_appliedCommand.Arguments, CommonLocalizableStrings.CouldNotFindProjectOrDirectory, true);
List<MsbuildProject> refs = _appliedCommand.Arguments
.Select((r) => MsbuildProject.FromFile(projects, r))
.Select((r) => MsbuildProject.FromFileOrDirectory(projects, r))
.ToList();
if (frameworkString == null)
@ -90,9 +90,10 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
}
}
var relativePathReferences = _appliedCommand.Arguments.Select((r) =>
Path.GetRelativePath(msbuildProj.ProjectDirectory, Path.GetFullPath(r)))
.ToList();
var relativePathReferences = refs.Select((r) =>
Path.GetRelativePath(
msbuildProj.ProjectDirectory,
r.ProjectRootElement.FullPath)).ToList();
int numberOfAddedReferences = msbuildProj.AddProjectToProjectReferences(
frameworkString,

View file

@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Linq;
using Microsoft.Build.Evaluation;
using Microsoft.DotNet.Cli;
@ -42,10 +43,22 @@ namespace Microsoft.DotNet.Tools.Remove.ProjectToProjectReference
public override int Execute()
{
var msbuildProj = MsbuildProject.FromFileOrDirectory(new ProjectCollection(), _fileOrDirectory);
var references = _appliedCommand.Arguments.Select(p => {
var fullPath = Path.GetFullPath(p);
if (!Directory.Exists(fullPath))
{
return p;
}
return Path.GetRelativePath(
msbuildProj.ProjectRootElement.FullPath,
MsbuildProject.GetProjectFileFromDirectory(fullPath).FullName
);
});
int numberOfRemovedReferences = msbuildProj.RemoveProjectToProjectReferences(
_appliedCommand.ValueOrDefault<string>("framework"),
_appliedCommand.Arguments);
references);
if (numberOfRemovedReferences != 0)
{

View file

@ -97,11 +97,6 @@
<target state="translated">Aplikace</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">Odkaz na {0} neexistuje.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">Odkaz na {0} byl přidán do projektu.</target>

View file

@ -97,11 +97,6 @@
<target state="translated">Anwendung</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">Der Verweis "{0}" ist nicht vorhanden.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">Der Verweis "{0}" wurde dem Projekt hinzugefügt.</target>

View file

@ -97,11 +97,6 @@
<target state="translated">Aplicación</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">La referencia {0} no existe.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">Se ha agregado la referencia "{0}" al proyecto.</target>

View file

@ -97,11 +97,6 @@
<target state="translated">Application</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">La référence {0} n'existe pas.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">Référence '{0}' ajoutée au projet.</target>

View file

@ -97,11 +97,6 @@
<target state="translated">Applicazione</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">Il riferimento {0} non esiste.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">Il riferimento `{0}` è stato aggiunto al progetto.</target>

View file

@ -97,11 +97,6 @@
<target state="translated">アプリケーション</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">参照 {0} は存在しません。</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">参照 `{0}` がプロジェクトに追加されました。</target>

View file

@ -97,11 +97,6 @@
<target state="translated">응용 프로그램</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">{0} 참조가 없습니다.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">프로젝트에 '{0}' 참조가 추가되었습니다.</target>

View file

@ -97,11 +97,6 @@
<target state="translated">Aplikacja</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">Odwołanie {0} nie istnieje.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">Do projektu zostało dodane odwołanie „{0}”.</target>

View file

@ -97,11 +97,6 @@
<target state="translated">Aplicativo</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">A referência {0} não existe.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">A referência {0} foi adicionada ao projeto.</target>

View file

@ -97,11 +97,6 @@
<target state="translated">Приложение</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">Ссылка {0} не существует.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">Ссылка "{0}" добавлена в проект.</target>

View file

@ -97,11 +97,6 @@
<target state="translated">Uygulama</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">{0} başvurusu yok.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">`{0}` başvurusu projeye eklendi.</target>

View file

@ -97,11 +97,6 @@
<target state="translated">应用程序</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">引用 {0} 不存在。</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">已将引用“{0}”添加到项目。</target>

View file

@ -97,11 +97,6 @@
<target state="translated">應用程式</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">參考 {0} 不存在。</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">參考 `{0}` 已新增至專案。</target>

View file

@ -559,7 +559,7 @@ Commands:
.WithProject(lib.CsProjName)
.Execute("\"IDoNotExist.csproj\"");
cmd.Should().Fail();
cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.ReferenceDoesNotExist, "IDoNotExist.csproj"));
cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.CouldNotFindProjectOrDirectory, "IDoNotExist.csproj"));
lib.CsProjContent().Should().BeEquivalentTo(contentBefore);
}
@ -575,7 +575,7 @@ Commands:
.WithProject(lib.CsProjPath)
.Execute($"\"{setup.ValidRefCsprojPath}\" \"IDoNotExist.csproj\"");
cmd.Should().Fail();
cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.ReferenceDoesNotExist, "IDoNotExist.csproj"));
cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.CouldNotFindProjectOrDirectory, "IDoNotExist.csproj"));
lib.CsProjContent().Should().BeEquivalentTo(contentBefore);
}
@ -693,5 +693,55 @@ Commands:
cmd.StdErr.Should().MatchRegex(" - net45");
net45lib.CsProjContent().Should().BeEquivalentTo(csProjContent);
}
[Fact]
public void WhenDirectoryContainingProjectIsGivenReferenceIsAdded()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
var result = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"\"{Path.GetDirectoryName(setup.ValidRefCsprojPath)}\"");
result.Should().Pass();
result.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"ValidRef\ValidRef.csproj"));
result.StdErr.Should().BeEmpty();
}
[Fact]
public void WhenDirectoryContainsNoProjectsItCancelsWholeOperation()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
var reference = "Empty";
var result = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute(reference);
result.Should().Fail();
result.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText);
result.StdErr.Should().Be(string.Format(CommonLocalizableStrings.CouldNotFindAnyProjectInDirectory, reference));
}
[Fact]
public void WhenDirectoryContainsMultipleProjectsItCancelsWholeOperation()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
var reference = "MoreThanOne";
var result = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute(reference);
result.Should().Fail();
result.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText);
result.StdErr.Should().Be(string.Format(CommonLocalizableStrings.MoreThanOneProjectInDirectory, reference));
}
}
}

View file

@ -506,5 +506,56 @@ Commands:
csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore - 1);
csproj.NumberOfProjectReferencesWithIncludeContaining(validref.Name).Should().Be(0);
}
[Fact]
public void WhenDirectoryContainingProjectIsGivenReferenceIsRemoved()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
var libref = AddLibRef(setup, lib);
var result = new RemoveReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"\"{libref.CsProjPath}\"");
result.Should().Pass();
result.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ProjectReferenceRemoved, Path.Combine("Lib", setup.LibCsprojName)));
result.StdErr.Should().BeEmpty();
}
[Fact]
public void WhenDirectoryContainsNoProjectsItCancelsWholeOperation()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
var reference = "Empty";
var result = new RemoveReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute(reference);
result.Should().Fail();
result.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText);
result.StdErr.Should().Be(string.Format(CommonLocalizableStrings.CouldNotFindAnyProjectInDirectory, Path.Combine(setup.TestRoot, reference)));
}
[Fact]
public void WhenDirectoryContainsMultipleProjectsItCancelsWholeOperation()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
var reference = "MoreThanOne";
var result = new RemoveReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute(reference);
result.Should().Fail();
result.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText);
result.StdErr.Should().Be(string.Format(CommonLocalizableStrings.MoreThanOneProjectInDirectory, Path.Combine(setup.TestRoot, reference)));
}
}
}