Skip to content

Commit ca00e32

Browse files
committed
WIP - stubbing out code
1 parent 81fcbe5 commit ca00e32

File tree

2 files changed

+82
-9
lines changed

2 files changed

+82
-9
lines changed

src/CodingWithCalvin.ProjectRenamifier/CodingWithCalvin.ProjectRenamifier.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@
6161
</ItemGroup>
6262
<ItemGroup>
6363
<None Include="Resources\extension.manifest.json" />
64-
<None Include="Resources\LICENSE" />
64+
<Content Include="Resources\LICENSE">
65+
<IncludeInVSIX>true</IncludeInVSIX>
66+
</Content>
6567
<None Include="source.extension.vsixmanifest">
6668
<SubType>Designer</SubType>
6769
<Generator>VsixManifestGenerator</Generator>

src/CodingWithCalvin.ProjectRenamifier/Commands/RenamifyProjectCommand.cs

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using EnvDTE;
22
using EnvDTE80;
3+
using Microsoft.VisualStudio.Shell.Interop;
4+
using System.Collections.Generic;
35
using System.ComponentModel.Design;
46
using System.IO;
57
using System.Windows.Forms;
8+
using VSLangProj;
69

710
namespace CodingWithCalvin.ProjectRenamifier
811
{
@@ -77,27 +80,95 @@ void RenameProject(Project project, DTE2 dte)
7780
{
7881
ThreadHelper.ThrowIfNotOnUIThread();
7982

80-
var projectFileName = Path.GetFileName(project.FullName);
83+
var oldProjectFileName = Path.GetFileName(project.FullName);
8184
var projectFileNameNoExtension = Path.GetFileNameWithoutExtension(project.FullName);
85+
var projectExtension = Path.GetExtension(oldProjectFileName);
86+
87+
var newProjectFileName = $"{projectFileNameNoExtension}-NEW{projectExtension}";
88+
8289
var projectPath =
8390
Path.GetDirectoryName(project.FullName)
8491
?? throw new InvalidOperationException();
8592
var parentDirectoryName = new DirectoryInfo(projectPath).Parent.Name;
86-
var grandparentDirectory = new DirectoryInfo(parentDirectoryName).Parent.FullName;
93+
var oldParentDirectory = new DirectoryInfo(projectPath).Parent.FullName;
94+
95+
var grandparentDirectory = new DirectoryInfo(parentDirectoryName).Parent.FullName;
96+
var newParentDirectory = Path.Combine(grandparentDirectory, $"{parentDirectoryName}-NEW");
97+
98+
// hold onto all projects that reference this project
99+
var referencingProjects = new List<Project>();
100+
foreach (Project p in dte.Solution.Projects)
101+
{
102+
if(p.Object is VSProject referencingVSProject)
103+
{
104+
foreach(Reference reference in referencingVSProject.References)
105+
{
106+
if(reference.SourceProject != null && reference.SourceProject.UniqueName == project.UniqueName)
107+
{
108+
referencingProjects.Add(p);
109+
}
110+
}
111+
}
112+
}
113+
114+
// hold onto all projects that this project references
115+
var referencedProjects = new List<Project>();
116+
117+
if (project.Object is VSProject referencedVSProject)
118+
{
119+
foreach (Reference reference in referencedVSProject.References)
120+
{
121+
if (reference.SourceProject != null)
122+
{
123+
referencedProjects.Add(reference.SourceProject);
124+
}
125+
}
126+
}
127+
128+
// unload project, then do the work
129+
dte.Solution.Remove(project);
87130

131+
//rename parent directory, if necessary
88132
if (parentDirectoryName.Equals(projectFileNameNoExtension))
89133
{
90-
// rename parent directory
134+
Directory.Move(oldParentDirectory, newParentDirectory);
91135
}
92136

93137
// rename project
94-
95-
// fix solution?
96-
// remove old project?
97-
// add new project?
138+
File.Move(oldProjectFileName, newProjectFileName);
139+
140+
// add new project back to solution
141+
dte.Solution.AddFromFile(newProjectFileName, true);
98142

99-
// fix references?
143+
// need handle to new project now that its back
144+
Project newProjectReference = null;
145+
foreach (Project p in dte.Solution.Projects)
146+
{
147+
if(p.UniqueName == newProjectFileName)
148+
{
149+
newProjectReference = p;
150+
break;
151+
}
152+
}
100153

154+
// fix projects that reference the old project to reference the new one
155+
foreach (var referencingProject in referencingProjects)
156+
{
157+
if(referencingProject.Object is VSProject vsProject)
158+
{
159+
vsProject.References.AddProject(newProjectReference);
160+
}
161+
}
162+
163+
// fix this project's references
164+
foreach (var referencedProject in referencedProjects)
165+
{
166+
if(newProjectReference.Object is VSProject vsProject2)
167+
{
168+
vsProject2.References.AddProject(referencedProject);
169+
}
170+
}
171+
101172
// sync new namespace?
102173
}
103174
}

0 commit comments

Comments
 (0)