From c77a330b6d75a9033c6129af281ed323477b379f Mon Sep 17 00:00:00 2001 From: TigRig Date: Sat, 30 Mar 2024 18:11:11 +0900 Subject: [PATCH 1/3] fix: nsMsbuild missing issue in Unity 2022 --- .../Editor/Features/InsertAdditionalImportFeature.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CsprojModifier/Assets/CsprojModifier/Editor/Features/InsertAdditionalImportFeature.cs b/src/CsprojModifier/Assets/CsprojModifier/Editor/Features/InsertAdditionalImportFeature.cs index c1827cf..3a14f3e 100644 --- a/src/CsprojModifier/Assets/CsprojModifier/Editor/Features/InsertAdditionalImportFeature.cs +++ b/src/CsprojModifier/Assets/CsprojModifier/Editor/Features/InsertAdditionalImportFeature.cs @@ -145,7 +145,7 @@ public override string OnGeneratedCSProject(string path, string content) var baseDir = Path.GetDirectoryName(path); var xDoc = XDocument.Parse(content); var nsMsbuild = (XNamespace)"http://schemas.microsoft.com/developer/msbuild/2003"; - var projectE = xDoc.Element(nsMsbuild + "Project"); + var projectE = xDoc.Element(nsMsbuild + "Project") ?? xDoc.Element("Project"); foreach (var target in settings.AdditionalImports) { From 0f87862390e620d2c542097e0143376050a830a2 Mon Sep 17 00:00:00 2001 From: TigRig Date: Thu, 12 Sep 2024 22:51:24 +0900 Subject: [PATCH 2/3] fix: remove namespace settings --- .../Editor/Features/InsertAdditionalImportFeature.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/CsprojModifier/Assets/CsprojModifier/Editor/Features/InsertAdditionalImportFeature.cs b/src/CsprojModifier/Assets/CsprojModifier/Editor/Features/InsertAdditionalImportFeature.cs index 3a14f3e..9a94acf 100644 --- a/src/CsprojModifier/Assets/CsprojModifier/Editor/Features/InsertAdditionalImportFeature.cs +++ b/src/CsprojModifier/Assets/CsprojModifier/Editor/Features/InsertAdditionalImportFeature.cs @@ -144,8 +144,7 @@ public override string OnGeneratedCSProject(string path, string content) var baseDir = Path.GetDirectoryName(path); var xDoc = XDocument.Parse(content); - var nsMsbuild = (XNamespace)"http://schemas.microsoft.com/developer/msbuild/2003"; - var projectE = xDoc.Element(nsMsbuild + "Project") ?? xDoc.Element("Project"); + var projectE = xDoc.Element("Project"); foreach (var target in settings.AdditionalImports) { @@ -154,11 +153,11 @@ public override string OnGeneratedCSProject(string path, string content) if (target.Position == ImportProjectPosition.Append) { projectE.Add(new XComment($"{target.Path}:{hash}")); - projectE.Add(new XElement(nsMsbuild + "Import", new XAttribute("Project", target.Path))); + projectE.Add(new XElement("Import", new XAttribute("Project", target.Path))); } else if (target.Position == ImportProjectPosition.Prepend) { - projectE.AddFirst(new XElement(nsMsbuild + "Import", new XAttribute("Project", target.Path))); + projectE.AddFirst(new XElement("Import", new XAttribute("Project", target.Path))); projectE.AddFirst(new XComment($"{target.Path}:{hash}")); } else if (target.Position == ImportProjectPosition.AppendContent) From 6cd118decff1a27299998fe4b1554a06bbacd7cf Mon Sep 17 00:00:00 2001 From: Kitsuensaki Mezumona Date: Sat, 23 Nov 2024 14:08:43 +0900 Subject: [PATCH 3/3] fix: Support for both classic-style and SDK-style csproj --- .../Editor/Features/InsertAdditionalImportFeature.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/CsprojModifier/Assets/CsprojModifier/Editor/Features/InsertAdditionalImportFeature.cs b/src/CsprojModifier/Assets/CsprojModifier/Editor/Features/InsertAdditionalImportFeature.cs index 9a94acf..3be17dc 100644 --- a/src/CsprojModifier/Assets/CsprojModifier/Editor/Features/InsertAdditionalImportFeature.cs +++ b/src/CsprojModifier/Assets/CsprojModifier/Editor/Features/InsertAdditionalImportFeature.cs @@ -144,7 +144,13 @@ public override string OnGeneratedCSProject(string path, string content) var baseDir = Path.GetDirectoryName(path); var xDoc = XDocument.Parse(content); - var projectE = xDoc.Element("Project"); + var nsMsbuild = (XNamespace)"http://schemas.microsoft.com/developer/msbuild/2003"; + var projectE = xDoc.Element(nsMsbuild + "Project") ?? xDoc.Element("Project"); + if (projectE == null) + { + throw new Exception($"Unknown csproj format: {path}"); + } + var projectNs = projectE.GetDefaultNamespace(); foreach (var target in settings.AdditionalImports) { @@ -153,11 +159,11 @@ public override string OnGeneratedCSProject(string path, string content) if (target.Position == ImportProjectPosition.Append) { projectE.Add(new XComment($"{target.Path}:{hash}")); - projectE.Add(new XElement("Import", new XAttribute("Project", target.Path))); + projectE.Add(new XElement(projectNs + "Import", new XAttribute("Project", target.Path))); } else if (target.Position == ImportProjectPosition.Prepend) { - projectE.AddFirst(new XElement("Import", new XAttribute("Project", target.Path))); + projectE.AddFirst(new XElement(projectNs + "Import", new XAttribute("Project", target.Path))); projectE.AddFirst(new XComment($"{target.Path}:{hash}")); } else if (target.Position == ImportProjectPosition.AppendContent)