Skip to content

Commit 4d42e1a

Browse files
committed
Add project search wizard to robot template
Means instructions no longer require any replacements.
1 parent ff9badd commit 4d42e1a

File tree

7 files changed

+182
-2
lines changed

7 files changed

+182
-2
lines changed

FRC-Extension/FRC-Extension.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@
111111
<Private>True</Private>
112112
<Private>False</Private>
113113
</Reference>
114+
<Reference Include="Microsoft.VisualStudio.TemplateWizardInterface, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
115+
<HintPath>..\packages\VSSDK.TemplateWizardInterface.9.0.4\lib\net20\Microsoft.VisualStudio.TemplateWizardInterface.dll</HintPath>
116+
<Private>True</Private>
117+
<Private>False</Private>
118+
</Reference>
114119
<Reference Include="Microsoft.VisualStudio.TextManager.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
115120
<HintPath>..\packages\VSSDK.TextManager.Interop.7.0.4\lib\net20\Microsoft.VisualStudio.TextManager.Interop.dll</HintPath>
116121
<Private>True</Private>
@@ -188,6 +193,7 @@
188193
<Compile Include="Settings.cs">
189194
<SubType>Component</SubType>
190195
</Compile>
196+
<Compile Include="SimulatorWizards\MainProjectSearchWizard.cs" />
191197
<Compile Include="WPILibFolder\WPILibFolderStructure.cs" />
192198
</ItemGroup>
193199
<ItemGroup>

FRC-Extension/FRC-ExtensionPackage.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace RobotDotNet.FRC_Extension
4141
[ProvideAutoLoad("{f1536ef8-92ec-443c-9ed7-fdadf150da82}")]
4242
//This gives us an options page.
4343
[ProvideOptionPage(typeof(SettingsPageGrid), "FRC Options", "FRC Options", 0, 0, true)]
44+
[ProvideBindingPath]
4445
public sealed class Frc_ExtensionPackage : Package
4546
{
4647
/// <summary>

FRC-Extension/Properties/AssemblyInfo.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Reflection;
33
using System.Resources;
4-
using System.Runtime.CompilerServices;
54
using System.Runtime.InteropServices;
65

76
// General Information about an assembly is controlled through the following
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using EnvDTE;
8+
using Microsoft.VisualStudio.Shell;
9+
using Microsoft.VisualStudio.TemplateWizard;
10+
using VSLangProj;
11+
12+
namespace RobotDotNet.FRC_Extension.SimulatorWizards
13+
{
14+
/// <summary>
15+
/// This wizard is used when creating the Simulator project in order to search for the main robot project and fill out the replacements properly
16+
/// </summary>
17+
public class MainProjectSearchWizard : IWizard
18+
{
19+
/// <summary>
20+
/// Called at the start of the wizard being called, which happens while the project is getting created.
21+
/// </summary>
22+
/// <param name="automationObject"></param>
23+
/// <param name="replacementsDictionary"></param>
24+
/// <param name="runKind"></param>
25+
/// <param name="customParams"></param>
26+
public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
27+
{
28+
var dte = automationObject as DTE;
29+
//Do nothing if we cannot access our automation object
30+
if (dte == null)
31+
{
32+
return;
33+
}
34+
35+
//Force a try, even though we dont really need to. VS does not like exceptions.
36+
try
37+
{
38+
//Loop through all projects found
39+
foreach (Project project in dte.Solution.Projects)
40+
{
41+
//Find the project with the RobotProject global, and also make sure it references WPILib.
42+
if (project.Globals.VariableExists["RobotProject"])
43+
{
44+
var vsproject = project.Object as VSLangProj.VSProject;
45+
46+
if (vsproject != null)
47+
{
48+
if ((from Reference reference in vsproject.References where reference.SourceProject == null select reference.Name).Any(name => name.Contains("WPILib")))
49+
{
50+
//If everything checks out, Search for the robot namespace and class, and store
51+
//a reference to the project so we can add it to the new project later.
52+
FindRobotNameAndNamespace(replacementsDictionary, project);
53+
m_robotProject = project;
54+
break;
55+
}
56+
}
57+
}
58+
}
59+
}
60+
catch (Exception ex)
61+
{
62+
OutputWriter.Instance.WriteLine(ex.StackTrace);
63+
}
64+
}
65+
66+
private Project m_robotProject = null;
67+
68+
//Find and add our robot namespace and class to the replacement dictionary.
69+
private void FindRobotNameAndNamespace(Dictionary<string, string> replacementsDictionary, Project robotProject)
70+
{
71+
string ns = null;
72+
string cls = null;
73+
foreach (ProjectItem projectItem in robotProject.ProjectItems)
74+
{
75+
//Search all items in project for our Program.cs
76+
if (projectItem.Name == "Program.cs")
77+
{
78+
//Found the program file. Load it, and search for our namespace and class
79+
string fileName = projectItem.FileNames[1];
80+
try
81+
{
82+
string[] lines = File.ReadAllLines(fileName);
83+
foreach (var line in lines)
84+
{
85+
if (ns != null && cls != null)
86+
{
87+
break;
88+
}
89+
if (line.StartsWith("namespace"))
90+
{
91+
//Its our namespace
92+
string[] split = line.Split(' ');
93+
if (split.Length > 1)
94+
{
95+
ns = split[1];
96+
}
97+
continue;
98+
}
99+
if (line.Contains("RobotBase.Main"))
100+
{
101+
//Its the way to find our main class
102+
int typeofIndex = line.IndexOf("typeof");
103+
string sub = line.Substring(typeofIndex);
104+
int startParam = sub.IndexOf('(');
105+
int endParam = sub.IndexOf(')', startParam + 1);
106+
cls = sub.Substring(startParam + 1, endParam - startParam - 1);
107+
continue;
108+
}
109+
}
110+
111+
}
112+
catch (Exception)
113+
{
114+
}
115+
if (ns != null && cls != null)
116+
{
117+
break;
118+
}
119+
}
120+
else if (projectItem.Name == "Program.vb")
121+
{
122+
//TODO Do this
123+
}
124+
}
125+
126+
//If we found both, add both to the dictionary.
127+
if (ns != null && cls != null)
128+
{
129+
replacementsDictionary.Add("$robotnamespace$", ns);
130+
replacementsDictionary.Add("$robotclass$", cls);
131+
}
132+
}
133+
134+
/// <summary>
135+
/// Called after the project is finished generating. Use this to automatically add the main robot project
136+
/// to the simulator as a reference.
137+
/// </summary>
138+
/// <param name="project"></param>
139+
public void ProjectFinishedGenerating(Project project)
140+
{
141+
var vsproject = project.Object as VSLangProj.VSProject;
142+
143+
if (vsproject != null && m_robotProject != null)
144+
{
145+
vsproject.References.AddProject(m_robotProject);
146+
}
147+
}
148+
149+
public void ProjectItemFinishedGenerating(ProjectItem projectItem)
150+
{
151+
152+
}
153+
154+
public bool ShouldAddProjectItem(string filePath)
155+
{
156+
return true;
157+
}
158+
159+
public void BeforeOpeningFile(ProjectItem projectItem)
160+
{
161+
162+
}
163+
164+
public void RunFinished()
165+
{
166+
167+
}
168+
}
169+
}

FRC-Extension/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<package id="VSSDK.Shell.Interop" version="7.0.4" targetFramework="net45" />
1818
<package id="VSSDK.Shell.Interop.8" version="8.0.4" targetFramework="net45" />
1919
<package id="VSSDK.Shell.Interop.9" version="9.0.4" targetFramework="net45" />
20+
<package id="VSSDK.TemplateWizardInterface" version="9.0.4" targetFramework="net45" />
2021
<package id="VSSDK.TextManager.Interop" version="7.0.4" targetFramework="net45" />
2122
<package id="VSSDK.TextManager.Interop.8" version="8.0.4" targetFramework="net45" />
2223
<package id="VSSDK.Threading" version="12.0.4" targetFramework="net45" />

Templates/CSharp/Project-Templates/WindowsMonoGameSimulator/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public static class Program
77
{
88
public static void Main()
99
{
10-
RobotBase.Main(null, typeof($mainrobotnamespace.$mainrobotclass));
10+
RobotBase.Main(null, typeof($robotnamespace$.$robotclass$));
1111
}
1212
}
1313

Templates/CSharp/Project-Templates/WindowsMonoGameSimulator/WindowsMonoGameSimulator.vstemplate

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
<Assembly>NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly>
2727
<FullClassName>NuGet.VisualStudio.TemplateWizard</FullClassName>
2828
</WizardExtension>
29+
<WizardExtension>
30+
<Assembly>FRC-Extension</Assembly>
31+
<FullClassName>RobotDotNet.FRC_Extension.SimulatorWizards.MainProjectSearchWizard</FullClassName>
32+
</WizardExtension>
2933
<WizardData>
3034
<packages repository="extension"
3135
repositoryId="FRC_Extension">

0 commit comments

Comments
 (0)