Browse Source

Change PowerShell working directory to solution directory when solution is opened.

pull/15/head
Matt Ward 15 years ago
parent
commit
d56517ab61
  1. 10
      src/AddIns/BackendBindings/Scripting/Test/Utils/FakeScriptingConsole.cs
  2. 19
      src/AddIns/Misc/PackageManagement/Cmdlets/Project/Src/InvokeInitializePackagesCmdlet.cs
  3. 2
      src/AddIns/Misc/PackageManagement/Cmdlets/Project/Src/PackageManagementCmdlet.cs
  4. 7
      src/AddIns/Misc/PackageManagement/Cmdlets/Test/Helpers/TestableInvokeInitializePackagesCmdlet.cs
  5. 15
      src/AddIns/Misc/PackageManagement/Cmdlets/Test/Src/InvokeInitializePackagesCmdletTests.cs
  6. 1
      src/AddIns/Misc/PackageManagement/Project/PackageManagement.csproj
  7. 27
      src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PowerShellWorkingDirectory.cs
  8. 1
      src/AddIns/Misc/PackageManagement/Test/PackageManagement.Tests.csproj
  9. 20
      src/AddIns/Misc/PackageManagement/Test/Src/Scripting/PackageInitializationScriptsRunnerForOpenedSolutionTests.cs
  10. 52
      src/AddIns/Misc/PackageManagement/Test/Src/Scripting/PowerShellWorkingDirectoryTests.cs

10
src/AddIns/BackendBindings/Scripting/Test/Utils/FakeScriptingConsole.cs

@ -11,6 +11,7 @@ namespace ICSharpCode.Scripting.Tests.Utils
public event EventHandler LineReceived; public event EventHandler LineReceived;
public string TextPassedToSendLine; public string TextPassedToSendLine;
public List<string> AllTextPassedToSendLine = new List<string>();
public string TextPassedToSendText; public string TextPassedToSendText;
public bool IsWriteLineCalled; public bool IsWriteLineCalled;
public string TextPassedToWriteLine; public string TextPassedToWriteLine;
@ -31,11 +32,20 @@ namespace ICSharpCode.Scripting.Tests.Utils
get { return AllTextPassedToWriteLine[AllTextPassedToWriteLine.Count - 1]; } get { return AllTextPassedToWriteLine[AllTextPassedToWriteLine.Count - 1]; }
} }
public string FirstLinePassedToSendLine {
get { return AllTextPassedToSendLine[0]; }
}
public string LastLinePassedToSendLine {
get { return AllTextPassedToSendLine[AllTextPassedToSendLine.Count - 1]; }
}
public bool ScrollToEndWhenTextWritten { get; set; } public bool ScrollToEndWhenTextWritten { get; set; }
public void SendLine(string text) public void SendLine(string text)
{ {
TextPassedToSendLine = text; TextPassedToSendLine = text;
AllTextPassedToSendLine.Add(text);
} }
public void SendText(string text) public void SendText(string text)

19
src/AddIns/Misc/PackageManagement/Cmdlets/Project/Src/InvokeInitializePackagesCmdlet.cs

@ -36,9 +36,28 @@ namespace ICSharpCode.PackageManagement.Cmdlets
protected override void ProcessRecord() protected override void ProcessRecord()
{ {
UpdateWorkingDirectory();
RunPackageInitializationScripts(); RunPackageInitializationScripts();
} }
void UpdateWorkingDirectory()
{
string directory = GetWorkingDirectory();
UpdateWorkingDirectory(directory);
}
string GetWorkingDirectory()
{
var workingDirectory = new PowerShellWorkingDirectory(projectService);
return workingDirectory.GetWorkingDirectory();
}
void UpdateWorkingDirectory(string directory)
{
string command = String.Format("Set-Location '{0}'", directory);
InvokeScript(command);
}
void RunPackageInitializationScripts() void RunPackageInitializationScripts()
{ {
IPackageInitializationScripts scripts = GetPackageInitializationScripts(); IPackageInitializationScripts scripts = GetPackageInitializationScripts();

2
src/AddIns/Misc/PackageManagement/Cmdlets/Project/Src/PackageManagementCmdlet.cs

@ -86,7 +86,7 @@ namespace ICSharpCode.PackageManagement.Cmdlets
SessionState.PSVariable.Remove(name); SessionState.PSVariable.Remove(name);
} }
public void InvokeScript(string script) public virtual void InvokeScript(string script)
{ {
try { try {
InvokeCommand.InvokeScript(script); InvokeCommand.InvokeScript(script);

7
src/AddIns/Misc/PackageManagement/Cmdlets/Test/Helpers/TestableInvokeInitializePackagesCmdlet.cs

@ -47,5 +47,12 @@ namespace PackageManagement.Cmdlets.Tests.Helpers
{ {
base.ProcessRecord(); base.ProcessRecord();
} }
public string ScriptPassedToInvokeScript;
public override void InvokeScript(string script)
{
ScriptPassedToInvokeScript = script;
}
} }
} }

15
src/AddIns/Misc/PackageManagement/Cmdlets/Test/Src/InvokeInitializePackagesCmdletTests.cs

@ -89,5 +89,20 @@ namespace PackageManagement.Cmdlets.Tests
Assert.AreEqual(cmdlet, session); Assert.AreEqual(cmdlet, session);
} }
[Test]
public void ProcessRecord_SolutionHasPackageInitializationScripts_PowerShellWorkingDirectoryIsSetToSolutionDirectory()
{
CreateCmdlet();
SolutionHasPackageInitializationScripts();
fakeProjectService.OpenSolution.FileName = @"d:\projects\MySolution\MySolution.sln";
RunCmdlet();
string commandExecuted = cmdlet.ScriptPassedToInvokeScript;
string expectedCommandExecuted = @"Set-Location 'd:\projects\MySolution'";
Assert.AreEqual(expectedCommandExecuted, commandExecuted);
}
} }
} }

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

@ -199,6 +199,7 @@
<Compile Include="Src\Scripting\PackageInitializationScripts.cs" /> <Compile Include="Src\Scripting\PackageInitializationScripts.cs" />
<Compile Include="Src\Scripting\PackageInitializationScriptsConsole.cs" /> <Compile Include="Src\Scripting\PackageInitializationScriptsConsole.cs" />
<Compile Include="Src\Scripting\PackageInitializationScriptsFactory.cs" /> <Compile Include="Src\Scripting\PackageInitializationScriptsFactory.cs" />
<Compile Include="Src\Scripting\PowerShellWorkingDirectory.cs" />
<Compile Include="Src\SolutionPackageRepositoryPath.cs" /> <Compile Include="Src\SolutionPackageRepositoryPath.cs" />
<Compile Include="Src\PackageSourceConverter.cs" /> <Compile Include="Src\PackageSourceConverter.cs" />
<Compile Include="Src\PackageSourceViewModel.cs" /> <Compile Include="Src\PackageSourceViewModel.cs" />

27
src/AddIns/Misc/PackageManagement/Project/Src/Scripting/PowerShellWorkingDirectory.cs

@ -0,0 +1,27 @@
// 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 ICSharpCode.SharpDevelop.Project;
namespace ICSharpCode.PackageManagement.Scripting
{
public class PowerShellWorkingDirectory
{
IPackageManagementProjectService projectService;
public PowerShellWorkingDirectory(IPackageManagementProjectService projectService)
{
this.projectService = projectService;
}
public string GetWorkingDirectory()
{
Solution solution = projectService.OpenSolution;
if (solution != null) {
return solution.Directory;
}
return "$env:USERPROFILE";
}
}
}

1
src/AddIns/Misc/PackageManagement/Test/PackageManagement.Tests.csproj

@ -177,6 +177,7 @@
<Compile Include="Src\PackageRepositoryCacheTests.cs" /> <Compile Include="Src\PackageRepositoryCacheTests.cs" />
<Compile Include="Src\Scripting\PackageInitializationScriptsConsoleTests.cs" /> <Compile Include="Src\Scripting\PackageInitializationScriptsConsoleTests.cs" />
<Compile Include="Src\Scripting\PackageInitializationScriptsTests.cs" /> <Compile Include="Src\Scripting\PackageInitializationScriptsTests.cs" />
<Compile Include="Src\Scripting\PowerShellWorkingDirectoryTests.cs" />
<Compile Include="Src\SolutionPackageRepositoryPathTests.cs" /> <Compile Include="Src\SolutionPackageRepositoryPathTests.cs" />
<Compile Include="Src\PackageSourceViewModelTests.cs" /> <Compile Include="Src\PackageSourceViewModelTests.cs" />
<Compile Include="Src\PackagesViewModelTests.cs" /> <Compile Include="Src\PackagesViewModelTests.cs" />

20
src/AddIns/Misc/PackageManagement/Test/Src/Scripting/PackageInitializationScriptsRunnerForOpenedSolutionTests.cs

@ -40,6 +40,16 @@ namespace PackageManagement.Tests.Scripting
return solution; return solution;
} }
void SolutionHasPackageInitializationScripts()
{
fakeScriptsFactory.FakePackageInitializationScripts.AnyReturnValue = true;
}
void SolutionHasNoPackageInitializationScripts()
{
fakeScriptsFactory.FakePackageInitializationScripts.AnyReturnValue = false;
}
[Test] [Test]
public void Instance_SolutionIsOpened_PackageInitializationScriptsCreatedUsingSolution() public void Instance_SolutionIsOpened_PackageInitializationScriptsCreatedUsingSolution()
{ {
@ -55,11 +65,11 @@ namespace PackageManagement.Tests.Scripting
public void Instance_SolutionOpenedAndHasPackageInitializationScripts_InvokeInitializePackagesCmdletIsRun() public void Instance_SolutionOpenedAndHasPackageInitializationScripts_InvokeInitializePackagesCmdletIsRun()
{ {
CreateRunner(); CreateRunner();
fakeScriptsFactory.FakePackageInitializationScripts.AnyReturnValue = true; SolutionHasPackageInitializationScripts();
fakeConsoleHost.IsRunning = true; fakeConsoleHost.IsRunning = true;
OpenSolution(); OpenSolution();
string command = fakeScriptingConsole.TextPassedToSendLine; string command = fakeScriptingConsole.LastLinePassedToSendLine;
string expectedCommand = "Invoke-InitializePackages"; string expectedCommand = "Invoke-InitializePackages";
Assert.AreEqual(expectedCommand, command); Assert.AreEqual(expectedCommand, command);
@ -69,13 +79,13 @@ namespace PackageManagement.Tests.Scripting
public void Instance_SolutionOpenedAndHasNoPackageInitializationScripts_InvokeInitializePackagesCmdletIsNotRun() public void Instance_SolutionOpenedAndHasNoPackageInitializationScripts_InvokeInitializePackagesCmdletIsNotRun()
{ {
CreateRunner(); CreateRunner();
fakeScriptsFactory.FakePackageInitializationScripts.AnyReturnValue = false; SolutionHasNoPackageInitializationScripts();
fakeConsoleHost.IsRunning = true; fakeConsoleHost.IsRunning = true;
OpenSolution(); OpenSolution();
string command = fakeScriptingConsole.TextPassedToSendLine; bool contains = fakeScriptingConsole.AllTextPassedToSendLine.Contains("Invoke-InitializePackages");
Assert.IsNull(command); Assert.IsFalse(contains);
} }
} }
} }

52
src/AddIns/Misc/PackageManagement/Test/Src/Scripting/PowerShellWorkingDirectoryTests.cs

@ -0,0 +1,52 @@
// 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 ICSharpCode.PackageManagement.Design;
using ICSharpCode.PackageManagement.Scripting;
using ICSharpCode.SharpDevelop.Project;
using NUnit.Framework;
namespace PackageManagement.Tests.Scripting
{
[TestFixture]
public class PowerShellWorkingDirectoryTests
{
FakePackageManagementProjectService fakeProjectService;
PowerShellWorkingDirectory workingDirectory;
void CreateWorkingDirectory()
{
fakeProjectService = new FakePackageManagementProjectService();
workingDirectory = new PowerShellWorkingDirectory(fakeProjectService);
}
[Test]
public void GetWorkingDirectory_NoSolutionOpen_ReturnsUserProfileFolder()
{
CreateWorkingDirectory();
fakeProjectService.OpenSolution = null;
string directory = workingDirectory.GetWorkingDirectory();
string expectedDirectory = "$env:USERPROFILE";
Assert.AreEqual(expectedDirectory, directory);
}
[Test]
public void GetWorkingDirectory_SolutionOpen_ReturnsSolutionDirectory()
{
CreateWorkingDirectory();
var solution = new Solution();
solution.FileName = @"d:\projects\MyProject\myproject.sln";
fakeProjectService.OpenSolution = solution;
string directory = workingDirectory.GetWorkingDirectory();
string expectedDirectory = @"d:\projects\MyProject";
Assert.AreEqual(expectedDirectory, directory);
}
}
}
Loading…
Cancel
Save