1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-08 12:40:44 +00:00

[3.2] Sync csproj when files are changed from the Godot FileSystem dock

This commit is contained in:
Ignacio Etcheverry
2020-03-18 15:05:50 +01:00
parent 110523fecc
commit d1a5f8dbf2
7 changed files with 186 additions and 33 deletions

View File

@@ -1,5 +1,7 @@
using GodotTools.Core;
using System;
using System.Collections.Generic;
using System.IO;
using DotNet.Globbing;
using Microsoft.Build.Construction;
@@ -7,16 +9,15 @@ namespace GodotTools.ProjectEditor
{
public static class ProjectExtensions
{
public static bool HasItem(this ProjectRootElement root, string itemType, string include)
public static ProjectItemElement FindItemOrNull(this ProjectRootElement root, string itemType, string include, bool noCondition = false)
{
GlobOptions globOptions = new GlobOptions();
globOptions.Evaluation.CaseInsensitive = false;
GlobOptions globOptions = new GlobOptions {Evaluation = {CaseInsensitive = false}};
string normalizedInclude = include.NormalizePath();
foreach (var itemGroup in root.ItemGroups)
{
if (itemGroup.Condition.Length != 0)
if (noCondition && itemGroup.Condition.Length != 0)
continue;
foreach (var item in itemGroup.Items)
@@ -27,20 +28,79 @@ namespace GodotTools.ProjectEditor
var glob = Glob.Parse(item.Include.NormalizePath(), globOptions);
if (glob.IsMatch(normalizedInclude))
{
return true;
}
return item;
}
}
return null;
}
public static ProjectItemElement FindItemOrNullAbs(this ProjectRootElement root, string itemType, string include, bool noCondition = false)
{
GlobOptions globOptions = new GlobOptions {Evaluation = {CaseInsensitive = false}};
string normalizedInclude = Path.GetFullPath(include).NormalizePath();
foreach (var itemGroup in root.ItemGroups)
{
if (noCondition && itemGroup.Condition.Length != 0)
continue;
foreach (var item in itemGroup.Items)
{
if (item.ItemType != itemType)
continue;
var glob = Glob.Parse(Path.GetFullPath(item.Include).NormalizePath(), globOptions);
if (glob.IsMatch(normalizedInclude))
return item;
}
}
return null;
}
public static IEnumerable<ProjectItemElement> FindAllItemsInFolder(this ProjectRootElement root, string itemType, string folder)
{
string absFolderNormalizedWithSep = Path.GetFullPath(folder).NormalizePath() + Path.DirectorySeparatorChar;
foreach (var itemGroup in root.ItemGroups)
{
foreach (var item in itemGroup.Items)
{
if (item.ItemType != itemType)
continue;
string absPathNormalized = Path.GetFullPath(item.Include).NormalizePath();
if (absPathNormalized.StartsWith(absFolderNormalizedWithSep))
yield return item;
}
}
}
public static bool HasItem(this ProjectRootElement root, string itemType, string include, bool noCondition = false)
{
return root.FindItemOrNull(itemType, include, noCondition) != null;
}
public static bool AddItemChecked(this ProjectRootElement root, string itemType, string include)
{
if (!root.HasItem(itemType, include, noCondition: true))
{
root.AddItem(itemType, include);
return true;
}
return false;
}
public static bool AddItemChecked(this ProjectRootElement root, string itemType, string include)
public static bool RemoveItemChecked(this ProjectRootElement root, string itemType, string include)
{
if (!root.HasItem(itemType, include))
var item = root.FindItemOrNullAbs(itemType, include);
if (item != null)
{
root.AddItem(itemType, include);
item.Parent.RemoveChild(item);
return true;
}