BuildCommandAutoRule: expose more information

We expose the list of candidate script paths and the chosen script path
so that we can inspect them for diagnostics purposes.
This commit is contained in:
Michael B. Gale
2023-02-14 12:51:36 +00:00
parent 60afa6e9f0
commit b88382e3e7

View File

@@ -10,23 +10,51 @@ namespace Semmle.Autobuild.Shared
public class BuildCommandAutoRule : IBuildRule<AutobuildOptionsShared>
{
private readonly WithDotNet<AutobuildOptionsShared> withDotNet;
private IEnumerable<string> candidatePaths;
private string? scriptPath;
/// <summary>
/// A list of paths to files in the project directory which we classified as scripts.
/// </summary>
public IEnumerable<string> CandidatePaths
{
get { return this.candidatePaths; }
}
/// <summary>
/// The path of the script we decided to run, if any.
/// </summary>
public string? ScriptPath
{
get { return this.scriptPath; }
}
public BuildCommandAutoRule(WithDotNet<AutobuildOptionsShared> withDotNet)
{
this.withDotNet = withDotNet;
this.candidatePaths = new List<string>();
}
/// <summary>
/// A list of extensions that we consider to be for scripts on Windows.
/// </summary>
private readonly IEnumerable<string> winExtensions = new List<string> {
".bat",
".cmd",
".exe"
};
/// <summary>
/// A list of extensions that we consider to be for scripts on Linux.
/// </summary>
private readonly IEnumerable<string> linuxExtensions = new List<string> {
"",
".sh"
};
/// <summary>
/// A list of filenames without extensions that we think might be build scripts.
/// </summary>
private readonly IEnumerable<string> buildScripts = new List<string> {
"build"
};
@@ -35,9 +63,16 @@ namespace Semmle.Autobuild.Shared
{
builder.Log(Severity.Info, "Attempting to locate build script");
// a list of extensions for files that we consider to be scripts on the current platform
var extensions = builder.Actions.IsWindows() ? winExtensions : linuxExtensions;
// a list of combined base script names with the current platform's script extensions
// e.g. for Linux: build, build.sh
var scripts = buildScripts.SelectMany(s => extensions.Select(e => s + e));
var scriptPath = builder.Paths.Where(p => scripts.Any(p.Item1.ToLower().EndsWith)).OrderBy(p => p.Item2).Select(p => p.Item1).FirstOrDefault();
// search through the files in the project directory for paths which end in one of
// the names given by `scripts`, then order them by their distance from the root
this.candidatePaths = builder.Paths.Where(p => scripts.Any(p.Item1.ToLower().EndsWith)).OrderBy(p => p.Item2).Select(p => p.Item1);
// pick the first matching path, if there is one
this.scriptPath = candidatePaths.FirstOrDefault();
if (scriptPath is null)
return BuildScript.Failure;