|
| 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 | +} |
0 commit comments