godot/modules/mono/editor/GodotSharpTools/Project/ProjectExtensions.cs

61 lines
1.6 KiB
C#
Raw Normal View History

2017-10-02 23:24:00 +02:00
using System;
2018-10-22 19:20:29 +02:00
using DotNet.Globbing;
2017-10-02 23:24:00 +02:00
using Microsoft.Build.Construction;
namespace GodotSharpTools.Project
{
public static class ProjectExtensions
{
public static bool HasItem(this ProjectRootElement root, string itemType, string include)
{
2018-10-22 19:20:29 +02:00
GlobOptions globOptions = new GlobOptions();
globOptions.Evaluation.CaseInsensitive = false;
string normalizedInclude = include.NormalizePath();
2017-10-02 23:24:00 +02:00
foreach (var itemGroup in root.ItemGroups)
{
if (itemGroup.Condition.Length != 0)
continue;
foreach (var item in itemGroup.Items)
{
2018-10-22 19:20:29 +02:00
if (item.ItemType != itemType)
continue;
var glob = Glob.Parse(item.Include.NormalizePath(), globOptions);
if (glob.IsMatch(normalizedInclude))
2017-10-02 23:24:00 +02:00
{
2018-10-22 19:20:29 +02:00
return true;
2017-10-02 23:24:00 +02:00
}
}
}
return false;
}
public static bool AddItemChecked(this ProjectRootElement root, string itemType, string include)
2017-10-02 23:24:00 +02:00
{
if (!root.HasItem(itemType, include))
{
root.AddItem(itemType, include);
return true;
2017-10-02 23:24:00 +02:00
}
return false;
2017-10-02 23:24:00 +02:00
}
public static Guid GetGuid(this ProjectRootElement root)
{
foreach (var property in root.Properties)
{
if (property.Name == "ProjectGuid")
return Guid.Parse(property.Value);
}
return Guid.Empty;
}
}
}