2019-07-03 09:44:53 +02:00
|
|
|
using GodotTools.Core;
|
2017-10-02 23:24:00 +02:00
|
|
|
using System;
|
2020-03-18 15:05:50 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2017-10-02 23:24:00 +02:00
|
|
|
using Microsoft.Build.Construction;
|
2020-08-20 12:56:36 +02:00
|
|
|
using Microsoft.Build.Globbing;
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2019-07-03 09:44:53 +02:00
|
|
|
namespace GodotTools.ProjectEditor
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
public static class ProjectExtensions
|
|
|
|
{
|
2020-03-18 15:05:50 +01:00
|
|
|
public static ProjectItemElement FindItemOrNull(this ProjectRootElement root, string itemType, string include, bool noCondition = false)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2018-10-22 19:20:29 +02:00
|
|
|
string normalizedInclude = include.NormalizePath();
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
foreach (var itemGroup in root.ItemGroups)
|
|
|
|
{
|
2020-03-18 15:05:50 +01:00
|
|
|
if (noCondition && itemGroup.Condition.Length != 0)
|
2017-10-02 23:24:00 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
foreach (var item in itemGroup.Items)
|
|
|
|
{
|
2018-10-22 19:20:29 +02:00
|
|
|
if (item.ItemType != itemType)
|
|
|
|
continue;
|
|
|
|
|
2020-08-20 12:56:36 +02:00
|
|
|
var glob = MSBuildGlob.Parse(item.Include.NormalizePath());
|
2018-10-22 19:20:29 +02:00
|
|
|
|
|
|
|
if (glob.IsMatch(normalizedInclude))
|
2020-03-18 15:05:50 +01:00
|
|
|
return item;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 15:05:50 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
public static ProjectItemElement FindItemOrNullAbs(this ProjectRootElement root, string itemType, string include, bool noCondition = 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;
|
|
|
|
|
2020-08-20 12:56:36 +02:00
|
|
|
var glob = MSBuildGlob.Parse(Path.GetFullPath(item.Include).NormalizePath());
|
2020-03-18 15:05:50 +01:00
|
|
|
|
|
|
|
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;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 00:10:51 +02:00
|
|
|
public static bool AddItemChecked(this ProjectRootElement root, string itemType, string include)
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
2020-03-18 15:05:50 +01:00
|
|
|
if (!root.HasItem(itemType, include, noCondition: true))
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
root.AddItem(itemType, include);
|
2017-10-05 00:10:51 +02:00
|
|
|
return true;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
2017-10-05 00:10:51 +02:00
|
|
|
|
|
|
|
return false;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2020-03-18 15:05:50 +01:00
|
|
|
public static bool RemoveItemChecked(this ProjectRootElement root, string itemType, string include)
|
|
|
|
{
|
|
|
|
var item = root.FindItemOrNullAbs(itemType, include);
|
|
|
|
if (item != null)
|
|
|
|
{
|
|
|
|
item.Parent.RemoveChild(item);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|