Skip to content

Commit

Permalink
Detect and update .csproj when C# files are added, moved or removed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nufflee committed Jul 19, 2018
1 parent 76bfe14 commit 8fbce27
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ platform/windows/godot_res.res

# Visual Studio 2017 and Visual Studio Code workspace folder
/.vs
/.vscode
.vscode/

# Scons progress indicator
.scons_node_count
44 changes: 44 additions & 0 deletions modules/mono/editor/GodotSharpTools/Build/BuildSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
using System.Runtime.InteropServices;
using System.Security;
using Microsoft.Build.Framework;
using Microsoft.Build.Construction;
using GodotSharpTools.Project;
using System.Net;
using System.Linq;

namespace GodotSharpTools.Build
{
Expand Down Expand Up @@ -90,6 +94,8 @@ public bool Build(string loggerAssemblyPath, string loggerOutputDir, string[] cu
if (!string.IsNullOrEmpty(frameworkPath))
customPropertiesList.Add("FrameworkPathOverride=" + frameworkPath);

SynchronizeCsproj();

string compilerArgs = BuildArguments(loggerAssemblyPath, loggerOutputDir, customPropertiesList);

ProcessStartInfo startInfo = new ProcessStartInfo(GetMSBuildPath(), compilerArgs);
Expand Down Expand Up @@ -150,6 +156,8 @@ public bool BuildAsync(string loggerAssemblyPath, string loggerOutputDir, string
if (!string.IsNullOrEmpty(frameworkPath))
customPropertiesList.Add("FrameworkPathOverride=" + frameworkPath);

SynchronizeCsproj();

string compilerArgs = BuildArguments(loggerAssemblyPath, loggerOutputDir, customPropertiesList);

ProcessStartInfo startInfo = new ProcessStartInfo(GetMSBuildPath(), compilerArgs);
Expand Down Expand Up @@ -189,6 +197,42 @@ public bool BuildAsync(string loggerAssemblyPath, string loggerOutputDir, string
return true;
}

public void SynchronizeCsproj()
{
string projectDirectory = Path.GetDirectoryName(solution);
ProjectRootElement root = ProjectRootElement.Open(solution.Replace(".sln", ".csproj"));

foreach (var itemGroup in root.ItemGroups)
{
if (itemGroup.Condition.Length != 0)
continue;

foreach (var item in itemGroup.Items.ToArray())
{
if (item.ItemType == "Compile")
{
// We need to use WebUtility.UrlDecode because paths in .csproj are URL encoded from some reason
if (!File.Exists(Path.Combine(projectDirectory, WebUtility.UrlDecode(item.Include))))
{
itemGroup.RemoveChild(item);
}
}
}
}

foreach (string filePath in Directory.EnumerateFiles(projectDirectory, "*.*", SearchOption.AllDirectories))
{
if (Path.GetExtension(filePath) == ".cs")
{
string relativePath = filePath.RelativeToPath(projectDirectory);

root.AddItemChecked("Compile", relativePath);
}
}

root.Save();
}

private string BuildArguments(string loggerAssemblyPath, string loggerOutputDir, List<string> customProperties)
{
string arguments = string.Format(@"""{0}"" /v:normal /t:Build ""/p:{1}"" ""/l:{2},{3};{4}""",
Expand Down
18 changes: 18 additions & 0 deletions modules/mono/editor/GodotSharpTools/Project/ProjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,30 @@ public static bool AddItemChecked(this ProjectRootElement root, string itemType,
if (!root.HasItem(itemType, include))
{
root.AddItem(itemType, include);

return true;
}

return false;
}

public static void RemoveItem(this ProjectRootElement root, string itemType, string include)
{
foreach (var itemGroup in root.ItemGroups)
{
if (itemGroup.Condition.Length != 0)
continue;

foreach (var item in itemGroup.Items)
{
if (item.ItemType == itemType && item.Include == include)
{
itemGroup.RemoveChild(item);
}
}
}
}

public static Guid GetGuid(this ProjectRootElement root)
{
foreach (var property in root.Properties)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static void AddItemToProjectChecked(string projectPath, string itemType,
{
var dir = Directory.GetParent(projectPath).FullName;
var root = ProjectRootElement.Open(projectPath);

if (root.AddItemChecked(itemType, include.RelativeToPath(dir).Replace("/", "\\")))
root.Save();
}
Expand Down

0 comments on commit 8fbce27

Please sign in to comment.