private void RemoveIceBuilderFromProject(IVsProject p) { String path = ProjectUtil.GetProjectFullPath(p); foreach (IVsProject p1 in _buildProjects) { if (path.Equals(ProjectUtil.GetProjectFullPath(p1))) { _buildProjects.Remove(p1); break; } } ProjectUtil.DeleteItems( ProjectUtil.GetGeneratedFiles(p).Aggregate( new List <String>(), (items, kv) => { items.AddRange(kv.Value); return(items); })); if (DTEUtil.IsCSharpProject(p)) { Directory.GetFiles(GetAssembliesDir(GetIceHome()), "*.dll") .ToList() .ForEach(item => { String name = Path.GetFileNameWithoutExtension(item); if (ProjectUtil.HasAssemblyReference(DTEUtil.GetProject(p as IVsHierarchy), name)) { ProjectUtil.RemoveAssemblyReference(DTEUtil.GetProject(p as IVsHierarchy), name); } }); } Microsoft.Build.Evaluation.Project project = MSBuildUtils.LoadedProject(path, DTEUtil.IsCppProject(p), true); MSBuildUtils.RemoveIceBuilderFromProject(project); ProjectUtil.SaveProject(p); Guid projectGUID = Guid.Empty; IVsHierarchy hier = p as IVsHierarchy; IVsSolution.GetGuidOfProject(hier, out projectGUID); IVsSolution.CloseSolutionElement((uint)__VSSLNCLOSEOPTIONS.SLNCLOSEOPT_UnloadProject, hier, 0); project.Save(); try { ProjectCollection.GlobalProjectCollection.UnloadProject(project); } catch (System.Exception) { //expected if the project is not in the global project collection } IVsSolution4.ReloadProject(ref projectGUID); }
private void RemoveIceBuilderFromProject(EnvDTE.Project p) { Microsoft.Build.Evaluation.Project project = MSBuildUtils.LoadedProject(p.FullName); MSBuildUtils.RemoveIceBuilderFromProject(project); foreach (EnvDTE.Project p1 in _buildProjects) { if (p.FullName.Equals(p.FullName)) { _buildProjects.Remove(p1); break; } } ProjectUtil.DeleteItems(p, ProjectUtil.GetGeneratedFiles(p).Aggregate( new List <String>(), (items, kv) => { items.AddRange(kv.Value); return(items); })); Directory.GetFiles(GetAssembliesDir(GetIceHome()), "*.dll") .ToList() .ForEach(item => { String name = Path.GetFileNameWithoutExtension(item); if (ProjectUtil.HasAssemblyReference(p, name)) { ProjectUtil.RemoveAssemblyReference(p, name); } }); p.Save(); Guid projectGUID = Guid.Empty; IVsHierarchy hier = DTEUtil.GetIVsHierarchy(p); IVsSolution.GetGuidOfProject(hier, out projectGUID); IVsSolution.CloseSolutionElement((uint)__VSSLNCLOSEOPTIONS.SLNCLOSEOPT_UnloadProject, hier, 0); project.Save(); IVsSolution4.ReloadProject(ref projectGUID); }
public static void Upgrade(Dictionary <string, IVsProject> projects, UpgradeProgressCallback progressCallback) { Dispatcher dispatcher = Dispatcher.CurrentDispatcher; Thread t = new Thread(() => { try { var NuGet = Package.Instance.NuGet; if (!NuGet.IsUserConsentGranted()) { Package.WriteMessage("Ice Builder cannot download the required NuGet packages because " + "\"Allow NuGet to download missing packages\" is disabled"); dispatcher.Invoke(new Action(() => progressCallback.Finished())); return; } var DTE = Package.Instance.DTE; var builder = Package.Instance; int i = 0; int total = projects.Count; foreach (var entry in projects) { var project = entry.Value; var dteProject = project.GetDTEProject(); var uniqueName = dteProject.UniqueName; var name = entry.Key; i++; if (progressCallback.Canceled) { break; } dispatcher.Invoke( new Action(() => { progressCallback.ReportProgress(name, i); })); try { NuGet.Restore(dteProject); } catch (Exception ex) { Package.WriteMessage( string.Format("\nNuGet package restore failed:\n{0}\n", ex.Message)); dispatcher.Invoke(new Action(() => progressCallback.Finished())); break; } if (!NuGet.IsPackageInstalled(dteProject, Package.NuGetBuilderPackageId)) { try { DTE.StatusBar.Text = string.Format("Installing NuGet package {0} in project {1}", Package.NuGetBuilderPackageId, name); NuGet.InstallLatestPackage(dteProject, Package.NuGetBuilderPackageId); } catch (Exception ex) { Package.WriteMessage( string.Format("\nNuGet package zeroc.icebuilder.msbuild install failed:\n{0}\n", ex.Message)); dispatcher.Invoke(new Action(() => progressCallback.Finished())); break; } } IceBuilderProjectType projectType = IsIceBuilderEnabled(project); if (projectType != IceBuilderProjectType.None) { bool cpp = projectType == IceBuilderProjectType.CppProjectType; DTE.StatusBar.Text = string.Format("Upgrading project {0} Ice Builder settings", project.GetDTEProject().UniqueName); var fullPath = project.GetProjectFullPath(); var assemblyDir = project.GetEvaluatedProperty("IceAssembliesDir"); var outputDir = project.GetEvaluatedProperty("IceBuilderOutputDir"); var outputDirUnevaluated = project.GetPropertyWithDefault("IceBuilderOutputDir", "generated"); var sourceExt = project.GetPropertyWithDefault("IceBuilderSourceExt", "cpp"); var headerOutputDirUnevaluated = project.GetProperty("IceBuilderHeaderOutputDir"); var headerExt = project.GetPropertyWithDefault("IceBuilderHeaderExt", "h"); var cppOutputDir = new List <string>(); var cppHeaderOutputDir = new List <string>(); if (cpp) { var waitEvent = new ManualResetEvent(false); dispatcher.BeginInvoke(new Action(() => { foreach (EnvDTE.Configuration configuration in dteProject.ConfigurationManager) { cppOutputDir.Add(Package.Instance.VCUtil.Evaluate(configuration, outputDirUnevaluated)); if (string.IsNullOrEmpty(headerOutputDirUnevaluated)) { cppHeaderOutputDir.Add(Package.Instance.VCUtil.Evaluate(configuration, outputDirUnevaluated)); } else { cppHeaderOutputDir.Add(Package.Instance.VCUtil.Evaluate(configuration, headerOutputDirUnevaluated)); } } waitEvent.Set(); })); waitEvent.WaitOne(); } else { foreach (VSLangProj80.Reference3 r in dteProject.GetProjectRererences()) { if (Package.AssemblyNames.Contains(r.Name)) { project.UpdateProject((MSProject msproject) => { var item = msproject.AllEvaluatedItems.FirstOrDefault(referenceItem => referenceItem.ItemType.Equals("Reference") && referenceItem.EvaluatedInclude.Split(",".ToCharArray()).ElementAt(0).Equals(r.Name)); if (item != null) { if (item.HasMetadata("HintPath")) { var hintPath = item.GetMetadata("HintPath").UnevaluatedValue; if (hintPath.Contains("$(IceAssembliesDir)")) { hintPath = hintPath.Replace("$(IceAssembliesDir)", FileUtil.RelativePath(Path.GetDirectoryName(r.ContainingProject.FullName), assemblyDir)); // // If the HintPath points to the NuGet zeroc.ice.net package we upgrade it to not // use IceAssembliesDir otherwise we remove it if (hintPath.Contains("packages\\zeroc.ice.net")) { item.SetMetadataValue("HintPath", hintPath); } else { item.RemoveMetadata("HintPath"); } } } } }); } } } project.UpdateProject((MSProject msproject) => { MSBuildUtils.UpgradeProjectImports(msproject); MSBuildUtils.UpgradeProjectProperties(msproject, cpp); MSBuildUtils.RemoveIceBuilderFromProject(msproject, true); MSBuildUtils.UpgradeProjectItems(msproject); MSBuildUtils.UpgradeCSharpGeneratedItems(msproject, outputDir); foreach (var d in cppOutputDir) { MSBuildUtils.UpgradeGeneratedItems(msproject, d, sourceExt, "ClCompile"); } foreach (var d in cppHeaderOutputDir) { MSBuildUtils.UpgradeGeneratedItems(msproject, d, headerExt, "ClInclude"); } var propertyGroup = msproject.Xml.PropertyGroups.FirstOrDefault(group => group.Label.Equals("IceBuilder")); if (propertyGroup != null) { propertyGroup.Parent.RemoveChild(propertyGroup); } if (cpp) { propertyGroup = msproject.Xml.AddPropertyGroup(); propertyGroup.Label = "IceBuilder"; propertyGroup.AddProperty("IceCppMapping", "cpp98"); } }); builder.ReloadProject(project); } } dispatcher.BeginInvoke(new Action(() => progressCallback.Finished())); } catch (Exception ex) { dispatcher.BeginInvoke(new Action(() => progressCallback.Canceled = true)); Package.UnexpectedExceptionWarning(ex); } }); t.Start(); }
public static void Upgrade(Dictionary <string, IVsProject> projects, UpgradeProgressCallback progressCallback) { Dispatcher dispatcher = Dispatcher.CurrentDispatcher; Thread t = new Thread(() => { var NuGet = Package.Instance.NuGet; var DTE = Package.Instance.DTE; var builder = Package.Instance; int i = 0; int total = projects.Count; foreach (var entry in projects) { var project = entry.Value; var dteProject = project.GetDTEProject(); var name = entry.Key; i++; if (progressCallback.Canceled) { break; } dispatcher.Invoke( new Action(() => { progressCallback.ReportProgress(name, i); })); NuGet.Restore(dteProject); if (!NuGet.IsPackageInstalled(dteProject, Package.NuGetBuilderPackageId)) { DTE.StatusBar.Text = string.Format("Installing NuGet package {0} in project {1}", Package.NuGetBuilderPackageId, name); NuGet.InstallLatestPackage(dteProject, Package.NuGetBuilderPackageId); } IceBuilderProjectType projectType = DTEUtil.IsIceBuilderEnabled(project); if (projectType != IceBuilderProjectType.None) { bool cpp = projectType == IceBuilderProjectType.CppProjectType; DTE.StatusBar.Text = string.Format("Upgrading project {0} Ice Builder settings", project.GetDTEProject().UniqueName); var msproject = project.GetMSBuildProject(); var fullPath = ProjectUtil.GetProjectFullPath(project); var assemblyDir = ProjectUtil.GetEvaluatedProperty(project, "IceAssembliesDir"); if (projectType == IceBuilderProjectType.CsharpProjectType) { foreach (VSLangProj80.Reference3 r in dteProject.GetProjectRererences()) { if (Package.AssemblyNames.Contains(r.Name)) { var item = msproject.AllEvaluatedItems.FirstOrDefault(referenceItem => referenceItem.ItemType.Equals("Reference") && referenceItem.EvaluatedInclude.Split(",".ToCharArray()).ElementAt(0).Equals(r.Name)); if (item != null) { if (item.HasMetadata("HintPath")) { var hintPath = item.GetMetadata("HintPath").UnevaluatedValue; if (hintPath.Contains("$(IceAssembliesDir)")) { hintPath = hintPath.Replace("$(IceAssembliesDir)", FileUtil.RelativePath(Path.GetDirectoryName(r.ContainingProject.FullName), assemblyDir)); // // If the HintPath points to the NuGet zeroc.ice.net package we upgrade it to not // use IceAssembliesDir otherwise we remove it if (hintPath.Contains("packages\\zeroc.ice.net")) { item.SetMetadataValue("HintPath", hintPath); } else { item.RemoveMetadata("HintPath"); } } } } } } } bool modified = MSBuildUtils.UpgradeProjectImports(msproject); modified = MSBuildUtils.UpgradeProjectProperties(msproject, cpp) || modified; modified = MSBuildUtils.RemoveIceBuilderFromProject(msproject, true) || modified; modified = MSBuildUtils.UpgradeProjectItems(msproject, cpp) || modified; if (modified) { builder.SaveProject(project); } } } dispatcher.BeginInvoke(new Action(() => progressCallback.Finished())); }); t.Start(); }