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

Add git support to utilize stashing to manage ephemeral reference changes #42

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -73,10 +74,20 @@ public string ExtensionVersion
get { return ExtensionAssembly != null ? ExtensionAssembly.GetVersionWithBuildTime() : "n/a"; }
}

/// <summary>Gets the current visual studio solution directory</summary>
public string SolutionDirectory { get; private set; }

/// <summary>
/// Gets or sets the current configuration to use git to stash changes made by this tool in order to
/// cleanup after reverting back to nuget references.
/// </summary>
public bool UseGit { get; set; } = true;

/// <summary>Initializes the view model. Must only be called once per view model instance
/// (after the InitializeComponent method of a <see cref="!:UserControl"/>). </summary>
public override async void Initialize()
{
SolutionDirectory = GetSolutionDirectory();
List<ProjectModel> projects = null;
await RunTaskAsync(token => Task.Run(() =>
{
Expand All @@ -99,6 +110,9 @@ await RunTaskAsync(token => Task.Run(() =>
/// <returns>The task. </returns>
public async Task SwitchToProjectReferencesAsync()
{
// stash the local solution to track changes
StashLocalSolution();

await RunTaskAsync(token => Task.Run(() =>
{
// first, add all required projects
Expand Down Expand Up @@ -134,7 +148,7 @@ await RunTaskAsync(token => Task.Run(() =>
PathUtilities.MakeRelative(assemblyToProjectSwitch.ToProject.Path,
project.CurrentConfigurationPath) + "\t" +
PathUtilities.MakeRelative(fromAssemblyPath, project.CurrentConfigurationPath) +
"\n";
"\n";
}
else
{
Expand All @@ -153,6 +167,7 @@ await RunTaskAsync(token => Task.Run(() =>
File.AppendAllText(project.CurrentConfigurationPath, nuGetReferenceTransformationsForProject);
}
}

}, token));
}

Expand Down Expand Up @@ -186,7 +201,7 @@ await RunTaskAsync(token => Task.Run(() =>
{
MessageBox.Show("The project '" + transformation.ToAssemblyPath + "' could not be added. " +
"\nSkipped.", "Could not add project", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}

Expand All @@ -197,7 +212,15 @@ await RunTaskAsync(token => Task.Run(() =>
}

RemoveProjectsFromSolution(projectsToRemove);
RevertLocalSolution();
}, token));

// delete all generated switcher files
var nugetReferenceSwitcherFiles = Directory.GetFiles(Directory.GetParent(GetSolutionDirectory()).FullName, "*.nugetreferenceswitcher", SearchOption.AllDirectories);
foreach (var file in nugetReferenceSwitcherFiles)
{
File.Delete(file);
}
}

private List<ProjectModel> GetAllProjects(IEnumerable<Project> objects)
Expand Down Expand Up @@ -272,5 +295,45 @@ private void RemoveProjectsFromSolution(List<ProjectModel> projectsToDelete)
}
}
}

private string GetSolutionDirectory()
{
return Directory.GetParent(Application.Solution.FullName).FullName;
}

private bool StashLocalSolution()
{
return ExecuteCommand("git stash");
}

private bool RevertLocalSolution()
{
StashLocalSolution();
return ExecuteCommand("git stash drop && git stash pop");
}

private bool ExecuteCommand(string command)
{
// Do not execute anything if the user has opted out of this feature
if (!UseGit) return false;

var process = new System.Diagnostics.Process()
{
StartInfo = new ProcessStartInfo()
{
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = "/C cd " + SolutionDirectory + " && " + command
}
};
process.Start();
process.WaitForExit();
var errors = process.StandardError.ReadToEnd();
var output = process.StandardOutput.ReadToEnd();
return process.ExitCode.Equals(0);
}
}
}
5 changes: 5 additions & 0 deletions src/NuGetReferenceSwitcher.Presentation/Views/MainDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
Expand Down Expand Up @@ -100,6 +101,10 @@

<Button Grid.Row="2" Margin="8" Padding="4" Grid.Column="0" Grid.ColumnSpan="2"
Content="Switch to Project references" Click="OnSwitchToProjectReferences" />
<CheckBox Grid.Row="3" Margin="8" Padding="4" Grid.Column="0" Grid.ColumnSpan="2"
Content="Use Smart Stash"
ToolTip="Stashes any current changes in order to utilize git to cleanup any changes from switching to project references."
IsChecked="{Binding UseGit, Mode=TwoWay}" />
</Grid>
</TabItem>
<TabItem Header="Switch to NuGet references">
Expand Down