So I searched all over the web and it seems that there is no way to add an Import element via automation. The closest technique that works is to unload the project, edit it manually, then reload it. I have created a method to do this but trying to do it
in PowerShell is very difficult.
Here's the method:
private static void AddImport(string targetPath, DTE dte, IVsSolution solution, Project project)
string fullName = project.FullName;
solution.GetProjectOfUniqueName(project.UniqueName, out selectedHierarchy);
var ns = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003");
var doc = System.Xml.Linq.XDocument.Load("file://" + fullName);
var element = new XElement(ns + "Import");
var attribute = new XAttribute("Project", targetPath);
I did this in a sample VSPackage I created to test this stuff out (since debugging those ps1 scripts during the install is evil!). It works but I don't know how to get the IVsHierarchy and call that crud from the script. You can just call ExecuteCommand("Project.UnloadProject")
but it fails if the project node isn't selected (which it won't be if they're clicking on the references node).
So would it be unreasonable to ask for this as a type of helper method I can call from my script? I can get the path I need for the targets file easily enough, I'm just not sure there's a better way. The lack of support for inserting Imports is pretty dissapointing
(in VS automation).
Maybe I'll fork the code and submit a patch...?