godot/modules/mono/editor/GodotTools/GodotTools.ProjectEditor/ProjectUtils.cs
Ignacio Etcheverry 270af6fa08 Re-write mono module editor code in C#
Make the build system automatically build the C# Api assemblies to be shipped with the editor.
Make the editor, editor player and debug export templates use Api assemblies built with debug symbols.
Always run MSBuild to build the editor tools and Api assemblies when building Godot.
Several bugs fixed related to assembly hot reloading and restoring state.
Fix StringExtensions internal calls not being registered correctly, resulting in MissingMethodException.
2019-07-05 09:38:23 +02:00

73 lines
2.3 KiB
C#

using GodotTools.Core;
using System.Collections.Generic;
using System.IO;
using DotNet.Globbing;
using Microsoft.Build.Construction;
namespace GodotTools.ProjectEditor
{
public static class ProjectUtils
{
public static void AddItemToProjectChecked(string projectPath, string itemType, string include)
{
var dir = Directory.GetParent(projectPath).FullName;
var root = ProjectRootElement.Open(projectPath);
var normalizedInclude = include.RelativeToPath(dir).Replace("/", "\\");
if (root.AddItemChecked(itemType, normalizedInclude))
root.Save();
}
private static string[] GetAllFilesRecursive(string rootDirectory, string mask)
{
string[] files = Directory.GetFiles(rootDirectory, mask, SearchOption.AllDirectories);
// We want relative paths
for (int i = 0; i < files.Length; i++) {
files[i] = files[i].RelativeToPath(rootDirectory);
}
return files;
}
public static string[] GetIncludeFiles(string projectPath, string itemType)
{
var result = new List<string>();
var existingFiles = GetAllFilesRecursive(Path.GetDirectoryName(projectPath), "*.cs");
GlobOptions globOptions = new GlobOptions();
globOptions.Evaluation.CaseInsensitive = false;
var root = ProjectRootElement.Open(projectPath);
foreach (var itemGroup in root.ItemGroups)
{
if (itemGroup.Condition.Length != 0)
continue;
foreach (var item in itemGroup.Items)
{
if (item.ItemType != itemType)
continue;
string normalizedInclude = item.Include.NormalizePath();
var glob = Glob.Parse(normalizedInclude, globOptions);
// TODO Check somehow if path has no blob to avoid the following loop...
foreach (var existingFile in existingFiles)
{
if (glob.IsMatch(existingFile))
{
result.Add(existingFile);
}
}
}
}
return result.ToArray();
}
}
}