Browse Source

Implement EnvDTE.Solution.Globals.VariableValue

Allows reading/writing of ExtensibilityGlobals solution items. Used by Unity NuGet package.
pull/28/head
Matt Ward 13 years ago
parent
commit
ca45dd501d
  1. 1
      src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj
  2. 20
      src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/Globals.cs
  3. 95
      src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/SolutionExtensibilityGlobals.cs
  4. 111
      src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/SolutionGlobalsTests.cs
  5. 19
      src/AddIns/Misc/PackageManagement/Test/Src/Helpers/SolutionHelper.cs

1
src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj

@ -127,6 +127,7 @@
<Compile Include="Src\EnvDTE\ProjectKind.cs" /> <Compile Include="Src\EnvDTE\ProjectKind.cs" />
<Compile Include="Src\EnvDTE\Projects.cs" /> <Compile Include="Src\EnvDTE\Projects.cs" />
<Compile Include="Src\EnvDTE\Reference3.cs" /> <Compile Include="Src\EnvDTE\Reference3.cs" />
<Compile Include="Src\EnvDTE\SolutionExtensibilityGlobals.cs" />
<Compile Include="Src\EnvDTE\SourceControl.cs" /> <Compile Include="Src\EnvDTE\SourceControl.cs" />
<Compile Include="Src\EnvDTE\TextPoint.cs" /> <Compile Include="Src\EnvDTE\TextPoint.cs" />
<Compile Include="Src\EnvDTE\vsCMAccess.cs" /> <Compile Include="Src\EnvDTE\vsCMAccess.cs" />

20
src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/Globals.cs

@ -12,29 +12,21 @@ namespace ICSharpCode.PackageManagement.EnvDTE
public class Globals public class Globals
{ {
SD.Solution solution; SD.Solution solution;
SolutionExtensibilityGlobals extensibilityGlobals;
public Globals(SD.Solution solution) public Globals(SD.Solution solution)
{ {
this.solution = solution; this.solution = solution;
this.extensibilityGlobals = new SolutionExtensibilityGlobals(solution);
} }
public virtual bool VariableExists(string name) public virtual SolutionExtensibilityGlobals VariableValue {
{ get { return extensibilityGlobals; }
SD.ProjectSection section = GetExtensibilityGlobalsSection();
if (section != null) {
return section.Items.Any(item => IsMatchIgnoringCase(item.Name, name));
}
return false;
} }
bool IsMatchIgnoringCase(string a, string b) public virtual bool VariableExists(string name)
{
return String.Equals(a, b, StringComparison.OrdinalIgnoreCase);
}
SD.ProjectSection GetExtensibilityGlobalsSection()
{ {
return solution.Sections.SingleOrDefault(section => section.Name == "ExtensibilityGlobals"); return extensibilityGlobals.GetItemFromSolution(name) != null;
} }
} }
} }

95
src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/SolutionExtensibilityGlobals.cs

@ -0,0 +1,95 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Linq;
using SD = ICSharpCode.SharpDevelop.Project;
namespace ICSharpCode.PackageManagement.EnvDTE
{
public class SolutionExtensibilityGlobals
{
SD.Solution solution;
const string ExtensibilityGlobalsSectionName = "ExtensibilityGlobals";
List<SD.SolutionItem> nonPersistedSolutionItems = new List<SD.SolutionItem>();
public SolutionExtensibilityGlobals(SD.Solution solution)
{
this.solution = solution;
}
public object this[string name] {
get {
SD.SolutionItem item = GetItemFromSolutionOrNonPersistedItems(name);
if (item == null) {
ThrowNoVariableExistsException(name);
}
return item.Location;
}
set {
GetOrCreateSolutionItem(name, value as string);
}
}
void ThrowNoVariableExistsException(string name)
{
throw new ArgumentException("Variable name does not exist.", name);
}
SD.SolutionItem GetItemFromSolutionOrNonPersistedItems(string name)
{
SD.SolutionItem item = GetNonPersistedSolutionItem(name);
if (item != null) {
return item;
}
return GetItemFromSolution(name);
}
SD.SolutionItem GetNonPersistedSolutionItem(string name)
{
return GetMatchingSolutionItem(nonPersistedSolutionItems, name);
}
SD.SolutionItem GetMatchingSolutionItem(List<SD.SolutionItem> items, string name)
{
return items.SingleOrDefault(item => IsMatchIgnoringCase(item.Name, name));
}
internal SD.SolutionItem GetItemFromSolution(string name)
{
SD.ProjectSection section = GetExtensibilityGlobalsSection();
if (section != null) {
return GetMatchingSolutionItem(section.Items, name);
}
return null;
}
SD.ProjectSection GetExtensibilityGlobalsSection()
{
return solution.Sections.SingleOrDefault(section => section.Name == ExtensibilityGlobalsSectionName);
}
bool IsMatchIgnoringCase(string a, string b)
{
return String.Equals(a, b, StringComparison.OrdinalIgnoreCase);
}
void GetOrCreateSolutionItem(string name, string value)
{
SD.SolutionItem item = GetItemFromSolution(name);
if (item != null) {
item.Location = value;
} else {
CreateNonPersistedSolutionItem(name, value);
}
}
void CreateNonPersistedSolutionItem(string name, string value)
{
var item = new SD.SolutionItem(name, value);
nonPersistedSolutionItems.Add(item);
}
}
}

111
src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/SolutionGlobalsTests.cs

@ -5,6 +5,7 @@ using System;
using ICSharpCode.PackageManagement.EnvDTE; using ICSharpCode.PackageManagement.EnvDTE;
using NUnit.Framework; using NUnit.Framework;
using PackageManagement.Tests.Helpers; using PackageManagement.Tests.Helpers;
using SD = ICSharpCode.SharpDevelop.Project;
namespace PackageManagement.Tests.EnvDTE namespace PackageManagement.Tests.EnvDTE
{ {
@ -35,6 +36,16 @@ namespace PackageManagement.Tests.EnvDTE
solutionHelper.AddExtensibilityGlobalsSection(); solutionHelper.AddExtensibilityGlobalsSection();
} }
SD.SolutionItem GetExtensibilityGlobalsSolutionItem(string name)
{
return solutionHelper.GetExtensibilityGlobalsSolutionItem(name);
}
SD.ProjectSection GetExtensibilityGlobalsSection()
{
return solutionHelper.GetExtensibilityGlobalsSection();
}
[Test] [Test]
public void VariableExists_VariableExistsInExtensibilityGlobalsSection_ReturnsTrue() public void VariableExists_VariableExistsInExtensibilityGlobalsSection_ReturnsTrue()
{ {
@ -79,5 +90,105 @@ namespace PackageManagement.Tests.EnvDTE
Assert.IsTrue(exists); Assert.IsTrue(exists);
} }
[Test]
public void VariableValue_VariableAddedToExtensibilityGlobalsSection_ReturnsVariableValue()
{
CreateSolution();
AddExtensibilityGlobalsSection();
AddVariableToExtensibilityGlobals("test", "test-value");
object value = globals.VariableValue["test"];
Assert.AreEqual("test-value", value);
}
[Test]
public void VariableValue_VariableAddedToExtensibilityGlobalsSectionWithDifferentCasing_ReturnsVariableValue()
{
CreateSolution();
AddExtensibilityGlobalsSection();
AddVariableToExtensibilityGlobals("TEST", "test-value");
object value = globals.VariableValue["test"];
Assert.AreEqual("test-value", value);
}
[Test]
public void VariableValue_NoExtensibilityGlobalsSection_ThrowInvalidArgumentException()
{
CreateSolution();
Assert.Throws<ArgumentException>(delegate { object value = globals.VariableValue["test"]; });
}
[Test]
public void VariableValue_ExtensibilityGlobalsSectionButNoVariable_ThrowInvalidArgumentException()
{
CreateSolution();
AddExtensibilityGlobalsSection();
Assert.Throws<ArgumentException>(delegate { object value = globals.VariableValue["test"]; });
}
[Test]
public void VariableValue_SetNewValueForVariableAddedToExtensibilityGlobalsSection_SolutionItemUpdated()
{
CreateSolution();
AddExtensibilityGlobalsSection();
AddVariableToExtensibilityGlobals("test", "value");
globals.VariableValue["test"] = "new-value";
SD.SolutionItem item = GetExtensibilityGlobalsSolutionItem("test");
Assert.AreEqual("new-value", item.Location);
}
[Test]
public void VariableValue_SetNewValueForVariableWhenExtensibilityGlobalsExistsButVariableDoesNotExist_SolutionItemUpdated()
{
CreateSolution();
AddExtensibilityGlobalsSection();
globals.VariableValue["test"] = "new-value";
object value = globals.VariableValue["test"];
Assert.AreEqual("new-value", value);
}
[Test]
public void VariableValue_SetNewValueForVariableWhenExtensibilityGlobalsExistsButVariableDoesNotExist_SolutionNotUpdatedWithNewSolutionItem()
{
CreateSolution();
AddExtensibilityGlobalsSection();
globals.VariableValue["test"] = "new-value";
SD.SolutionItem item = GetExtensibilityGlobalsSolutionItem("test");
Assert.IsNull(item);
}
[Test]
public void VariableValue_SetNewValueForVariableWhenExtensibilityGlobalsDoesNotExist_ExtensibilityGlobalsSectionCreated()
{
CreateSolution();
globals.VariableValue["test"] = "new-value";
SD.ProjectSection section = GetExtensibilityGlobalsSection();
Assert.IsNull(section);
}
[Test]
public void VariableValue_SetNewValueForVariableWhenExtensibilityGlobalsDoesNotExistAndRetrieveItUsingDifferentCase_NewValueReturned()
{
CreateSolution();
globals.VariableValue["test"] = "new-value";
object value = globals.VariableValue["TEST"];
Assert.AreEqual("new-value", value);
}
} }
} }

19
src/AddIns/Misc/PackageManagement/Test/Src/Helpers/SolutionHelper.cs

@ -2,6 +2,8 @@
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System; using System;
using System.Linq;
using ICSharpCode.PackageManagement.Design; using ICSharpCode.PackageManagement.Design;
using ICSharpCode.PackageManagement.EnvDTE; using ICSharpCode.PackageManagement.EnvDTE;
using SD = ICSharpCode.SharpDevelop.Project; using SD = ICSharpCode.SharpDevelop.Project;
@ -18,7 +20,6 @@ namespace PackageManagement.Tests.Helpers
public Solution Solution; public Solution Solution;
public FakePackageManagementProjectService FakeProjectService; public FakePackageManagementProjectService FakeProjectService;
public SD.Solution MSBuildSolution; public SD.Solution MSBuildSolution;
public SD.ProjectSection ExtensibilityGlobalsSection;
void OpenSolution() void OpenSolution()
{ {
@ -53,14 +54,24 @@ namespace PackageManagement.Tests.Helpers
public void AddExtensibilityGlobalsSection() public void AddExtensibilityGlobalsSection()
{ {
ExtensibilityGlobalsSection = new SD.ProjectSection("ExtensibilityGlobals", "postSolution"); var section = new SD.ProjectSection("ExtensibilityGlobals", "postSolution");
MSBuildSolution.Sections.Add(ExtensibilityGlobalsSection); MSBuildSolution.Sections.Add(section);
} }
public void AddVariableToExtensibilityGlobals(string name, string value) public void AddVariableToExtensibilityGlobals(string name, string value)
{ {
var solutionItem = new SD.SolutionItem(name, value); var solutionItem = new SD.SolutionItem(name, value);
ExtensibilityGlobalsSection.Items.Add(solutionItem); GetExtensibilityGlobalsSection().Items.Add(solutionItem);
}
public SD.SolutionItem GetExtensibilityGlobalsSolutionItem(string name)
{
return GetExtensibilityGlobalsSection().Items.SingleOrDefault(item => item.Name == name);
}
public SD.ProjectSection GetExtensibilityGlobalsSection()
{
return MSBuildSolution.Sections.SingleOrDefault(section => section.Name == "ExtensibilityGlobals");
} }
} }
} }

Loading…
Cancel
Save