Files
codeql/csharp/autobuilder/Semmle.Autobuild.Shared/ProjectOrSolution.cs
2020-07-01 20:06:46 +02:00

58 lines
1.9 KiB
C#

using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Semmle.Autobuild.Shared
{
/// <summary>
/// A file that can be the target in an invocation of `msbuild` or `dotnet build`.
/// Either a solution file or a project file (`.proj`, `.csproj`, or `.vcxproj`).
/// </summary>
public interface IProjectOrSolution
{
/// <summary>
/// Gets the full path of this file.
/// </summary>
string FullPath { get; }
/// <summary>
/// Gets a list of other projects directly included by this file.
/// </summary>
IEnumerable<IProjectOrSolution> IncludedProjects { get; }
}
public abstract class ProjectOrSolution : IProjectOrSolution
{
public string FullPath { get; private set; }
public string DirectoryName => Path.GetDirectoryName(FullPath) ?? "";
protected ProjectOrSolution(Autobuilder builder, string path)
{
FullPath = builder.Actions.GetFullPath(path);
}
public abstract IEnumerable<IProjectOrSolution> IncludedProjects { get; }
public override string ToString() => FullPath;
}
public static class IProjectOrSolutionExtensions
{
/// <summary>
/// Holds if this file includes a project with code from language <paramref name="l"/>.
/// </summary>
public static bool HasLanguage(this IProjectOrSolution p, Language l)
{
bool HasLanguage(IProjectOrSolution p0, HashSet<string> seen)
{
if (seen.Contains(p0.FullPath))
return false;
seen.Add(p0.FullPath); // guard against cyclic includes
return l.ProjectFileHasThisLanguage(p0.FullPath) || p0.IncludedProjects.Any(p1 => HasLanguage(p1, seen));
}
return HasLanguage(p, new HashSet<string>());
}
}
}