Skip to content
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 @@ -23,7 +23,9 @@ public enum NuGetToProjectMode
public class FromNuGetToProjectTransformation : ObservableObject
{
private string _toProjectPath;
private string _toTestProjectPath;
private ProjectModel _toProject;
private ProjectModel _toTestProject;

/// <summary>Initializes a new instance of the <see cref="FromNuGetToProjectTransformation"/> class. </summary>
/// <param name="projects">The projects. </param>
Expand All @@ -44,6 +46,12 @@ public FromNuGetToProjectTransformation(List<ProjectModel> projects, ReferenceMo
ToProjectPath = swi.FromProjectPath;
else
ToProject = targetProject;

var targetTestProject = projects.FirstOrDefault(p => p.Path == swi.FromTestProjectPath);
if (targetTestProject == null)
ToTestProjectPath = swi.FromTestProjectPath;
else
ToTestProject = targetProject;
}
else
SelectedMode = NuGetToProjectMode.Deactivated;
Expand Down Expand Up @@ -96,6 +104,36 @@ public ProjectModel ToProject
}
}

public ProjectModel ToTestProject
{
get { return _toTestProject; }
set
{
if (Set(ref _toTestProject, value))
{
if (_toTestProject == null)
SelectedMode = NuGetToProjectMode.ProjectPath;
else
SelectedMode = NuGetToProjectMode.Project;
}
}
}

public string ToTestProjectPath
{
get { return _toTestProjectPath; }
set
{
if (Set(ref _toTestProjectPath, value))
{
if (_toTestProjectPath == null)
SelectedMode = NuGetToProjectMode.Project;
else
SelectedMode = NuGetToProjectMode.ProjectPath;
}
}
}

/// <summary>Gets the evaluated to project path. </summary>
public string EvaluatedToProjectPath
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,11 @@ public class FromProjectToNuGetTransformation

/// <summary>Gets or sets the NuGet assembly path name to switch to. </summary>
public string ToAssemblyPath { get; set; }

/// <summary>Gets or sets the test project name. </summary>
public string FromTestProjectName { get; set; }

/// <summary>Gets or sets the test project path. </summary>
public string FromTestProjectPath { get; set; }
}
}
31 changes: 19 additions & 12 deletions src/NuGetReferenceSwitcher.Presentation/Models/ProjectModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ProjectModel(VSProject project, DTE application)
{
_vsProject = project;

Name = project.Project.Name;
Name = project.Project.Name;
SolutionFile = new FileInfo(application.Solution.FileName);
LoadReferences();
}
Expand All @@ -56,7 +56,7 @@ public string PreviousConfigurationPath
{
get { return GetConfigurationPath(".previous.nugetreferenceswitcher"); }
}

/// <summary>Gets the current project reference to NuGet reference transformations. </summary>
public List<FromProjectToNuGetTransformation> CurrentToNuGetTransformations
{
Expand All @@ -78,7 +78,7 @@ public string Path
/// <summary>Deletes the previous configuration file and renames the current
/// configuration file to the path of the previous configuration file. </summary>
public void DeleteConfigurationFile()
{
{
if (File.Exists(CurrentConfigurationPath))
{
if (File.Exists(PreviousConfigurationPath))
Expand All @@ -98,11 +98,15 @@ public void AddProjectReference(ProjectModel project)
/// <summary>Saves the project. </summary>
public void Save()
{
// TODO: This may lock up the UI => check and fix
Dispatcher.CurrentDispatcher.InvokeAsync(() =>
// The Dispatcher was not working, plus not all Project items were saved. This work fines (after also saving the solution)
// No need to save the solution manually afterward.
_vsProject.Project.Save();
for (int j = 1; j <= _vsProject.Project.ProjectItems.Count; j++)
{
_vsProject.Project.Save();
});
var item = _vsProject.Project.ProjectItems.Item(j);
if (!item.Saved)
item.Save();
}
}

/// <summary>Adds an assembly reference to the project.</summary>
Expand All @@ -118,7 +122,7 @@ public bool AddReference(string assemblyPath)

return true;
}
return false;
return false;
}

/// <summary>Removes the project from the solution. </summary>
Expand All @@ -144,15 +148,18 @@ private List<FromProjectToNuGetTransformation> LoadTransformationsFromFile(strin
{
var lines = File.ReadAllLines(configurationPath)
.Select(l => l.Split('\t'))
.Where(l => l.Length == 3).ToArray();
.Where(l => l.Length == 5).ToArray();




foreach (var line in lines)
list.Add(new FromProjectToNuGetTransformation {
list.Add(new FromProjectToNuGetTransformation
{
FromProjectName = line[0],
FromProjectPath = PathUtilities.MakeAbsolute(line[1], configurationPath),
ToAssemblyPath = PathUtilities.MakeAbsolute(line[2], configurationPath)
ToAssemblyPath = PathUtilities.MakeAbsolute(line[2], configurationPath),
FromTestProjectName = line[3],
FromTestProjectPath = PathUtilities.MakeAbsolute(line[4], configurationPath),
});
}
return list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,18 @@ await RunTaskAsync(token => Task.Run(() =>

project.AddProjectReference(assemblyToProjectSwitch.ToProject);
nuGetReferenceTransformationsForProject +=
assemblyToProjectSwitch.ToProject.Name + "\t" +
assemblyToProjectSwitch.ToProject.Name +
"\t" +
PathUtilities.MakeRelative(assemblyToProjectSwitch.ToProject.Path,
project.CurrentConfigurationPath) + "\t" +
project.CurrentConfigurationPath) +
"\t" +
PathUtilities.MakeRelative(fromAssemblyPath, project.CurrentConfigurationPath) +
"\n";
"\t" +
assemblyToProjectSwitch.ToTestProject.Name +
"\t" +
PathUtilities.MakeRelative(assemblyToProjectSwitch.ToTestProject.Path,
project.CurrentConfigurationPath) +
"\n";
}
else
{
Expand All @@ -153,9 +160,13 @@ await RunTaskAsync(token => Task.Run(() =>
File.AppendAllText(project.CurrentConfigurationPath, nuGetReferenceTransformationsForProject);
}
}
if (!Application.Solution.Saved)
Application.Solution.SaveAs(Application.Solution.FullName);
}, token));
}



/// <summary>Handles an exception which occured in the <see cref="M:MyToolkit.Mvvm.ViewModelBase.RunTaskAsync(System.Func{System.Threading.CancellationToken,System.Threading.Tasks.Task})"/> method. </summary>
/// <param name="exception">The exception. </param>
public override void HandleException(Exception exception)
Expand Down Expand Up @@ -186,7 +197,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,6 +208,8 @@ await RunTaskAsync(token => Task.Run(() =>
}

RemoveProjectsFromSolution(projectsToRemove);
if (!Application.Solution.Saved)
Application.Solution.SaveAs(Application.Solution.FullName);
}, token));
}

Expand Down Expand Up @@ -245,15 +258,29 @@ private void AddProjectToSolutionIfNeeded(FromNuGetToProjectTransformation fromN
}
else
MessageBox.Show("The project '" + fromNuGetToProjectTransformation.ToProjectPath + "' could not be found. (ignored)", "Project not found", MessageBoxButton.OK, MessageBoxImage.Stop);
if (!string.IsNullOrEmpty(fromNuGetToProjectTransformation.ToTestProjectPath) && File.Exists(fromNuGetToProjectTransformation.ToTestProjectPath))
{
var project = Application.Solution.AddFromFile(fromNuGetToProjectTransformation.ToTestProjectPath);
var myProject = new ProjectModel((VSProject)project.Object, Application);
fromNuGetToProjectTransformation.ToTestProject = myProject;
}
}
}

private List<ProjectModel> GetCurrentProjectsToRemove()
{
return Projects
var pjToRemove = new List<ProjectModel>();
var projectsToRemove = Projects
.SelectMany(p => p.CurrentToNuGetTransformations.Select(s => s.FromProjectName))
.Select(name => Projects.SingleOrDefault(p => p.Name == name))
.ToList();
pjToRemove.AddRange(projectsToRemove);
var testProjectsToRemove = Projects
.SelectMany(p => p.CurrentToNuGetTransformations.Select(s => s.FromTestProjectName))
.Select(name => Projects.SingleOrDefault(p => p.Name == name))
.ToList();
pjToRemove.AddRange(testProjectsToRemove);
return pjToRemove;
}

private void RemoveProjectsFromSolution(List<ProjectModel> projectsToDelete)
Expand Down
7 changes: 7 additions & 0 deletions src/NuGetReferenceSwitcher.Presentation/Views/MainDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@
SelectedItem="{Binding ToProject, Mode=TwoWay}"
DisplayMemberPath="Name" />
</StackPanel>
<StackPanel Visibility="{Binding SelectedMode, Mode=OneWay, ConverterParameter='ProjectPath', Converter={StaticResource NuGetToProjectModeConverter}}">
<Button Content="Select a Test Project to add" Click="OnSelectTestProjectFile" Tag="{Binding}" />
<TextBlock Text="{Binding ToTestProjectPath}" TextWrapping="Wrap" Margin="0,4,0,0"
Visibility="{Binding ToTestProjectPath, Converter={StaticResource VisibilityConverter}}" />
</StackPanel>
<StackPanel Visibility="{Binding SelectedMode, Mode=OneWay, ConverterParameter='Project', Converter={StaticResource NuGetToProjectModeConverter}}">
</StackPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
Expand Down
19 changes: 19 additions & 0 deletions src/NuGetReferenceSwitcher.Presentation/Views/MainDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,24 @@ private void OnSelectProjectFile(object sender, RoutedEventArgs e)
if (_dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
fntpSwitch.ToProjectPath = _dlg.FileName;
}

private void OnSelectTestProjectFile(object sender, RoutedEventArgs e)
{
var fntpSwitch = (FromNuGetToProjectTransformation)((Button)sender).Tag;
if (_dlg == null)
{
_dlg = new OpenFileDialog();
_dlg.Filter = "CSharp Projects (*.csproj)|*.csproj|VB.NET Projects (*.vbproj)|*.vbproj";

// switch to VB if any VB project is already referenced
if (Model.Transformations.Any(t => t.ToTestProjectPath != null && t.ToTestProjectPath.EndsWith(".vbproj", System.StringComparison.OrdinalIgnoreCase)))
_dlg.FilterIndex = 2;
}

_dlg.Title = string.Format("Select Project for '{0}'", fntpSwitch.FromAssemblyName);

if (_dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
fntpSwitch.ToTestProjectPath = _dlg.FileName;
}
}
}