Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Antlr4BuildTasks/Antlr4BuildTasks.targets
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
<NodeDownloadDirectory>USERPROFILE/.node/</NodeDownloadDirectory>
<AntlrNgPath></AntlrNgPath>
<Visitor>true</Visitor>
<WorkingDirectory></WorkingDirectory>
</Antlr4>
</ItemDefinitionGroup>

Expand Down Expand Up @@ -169,6 +170,7 @@
NodeDownloadDirectory="%(Antlr4.NodeDownloadDirectory)"
AntlrNgPath="%(Antlr4.AntlrNgPath)"
Visitor="%(Antlr4.Visitor)"
WorkingDirectory="%(Antlr4.WorkingDirectory)"
>
<Output ItemName="ToCompileFiles" TaskParameter="GeneratedCodeFiles" />
<Output ItemName="AllFiles" TaskParameter="GeneratedFiles" />
Expand Down
7 changes: 5 additions & 2 deletions Antlr4BuildTasks/Tasks/RunAntlrTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ [Output] public ITaskItem[] GeneratedDirectories
public string NodeExec { get; set; }
public string NodeDownloadDirectory { get; set; }
public string AntlrNgPath { get; set; }
public string WorkingDirectory { get; set; }

public async System.Threading.Tasks.Task DownloadFileAsync(string uri, string outputPath)
{
Expand Down Expand Up @@ -219,7 +220,8 @@ public override bool Execute()
Package = Package,
DOptions = DOptions,
TreatWarningsAsErrors = Error,
ForceATN = ForceAtn
ForceATN = ForceAtn,
WorkingDirectory = WorkingDirectory
};

tool = ngTool;
Expand Down Expand Up @@ -253,7 +255,8 @@ public override bool Execute()
Package = Package,
DOptions = DOptions,
TreatWarningsAsErrors = Error,
ForceATN = ForceAtn
ForceATN = ForceAtn,
WorkingDirectory = WorkingDirectory
};

tool = javaTool;
Expand Down
4 changes: 2 additions & 2 deletions Antlr4BuildTasks/Tools/AntlrNgTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ private List<string> BuildGenerationArguments(IEnumerable<string> grammarFiles)
// Add separator to prevent grammar files from being interpreted as option values
arguments.Add("--");

// Grammar files
arguments.AddRange(grammarFiles.Select(NormalizePath));
// Grammar files - use relative path so "Generated from" comment is stable
arguments.AddRange(grammarFiles.Select(MakeGrammarRelativePath));

return arguments;
}
Expand Down
37 changes: 37 additions & 0 deletions Antlr4BuildTasks/Tools/AntlrToolBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
Expand Down Expand Up @@ -31,6 +32,7 @@ protected AntlrToolBase()
public string DOptions { get; set; }
public bool TreatWarningsAsErrors { get; set; }
public bool ForceATN { get; set; }
public string WorkingDirectory { get; set; }

public abstract bool Setup();

Expand Down Expand Up @@ -89,6 +91,11 @@ protected bool ExecuteProcess(
RedirectStandardError = true,
};

if (ShouldUseWorkingDirectory(WorkingDirectory))
{
startInfo.WorkingDirectory = WorkingDirectory;
}

MessageQueue.EnqueueMessage(Message.BuildInfoMessage(
$"Executing command: \"{startInfo.FileName}\" {startInfo.Arguments}"));

Expand Down Expand Up @@ -133,6 +140,28 @@ protected string NormalizePath(string path)
return path?.Replace("\\", "/");
}

/// <summary>
/// Converts an absolute grammar file path to be relative to WorkingDirectory.
/// If WorkingDirectory is not set or does not exist, returns the path as-is (normalized).
/// </summary>
protected string MakeGrammarRelativePath(string grammarFile)
{
if (string.IsNullOrEmpty(grammarFile) || !ShouldUseWorkingDirectory(WorkingDirectory))
return NormalizePath(grammarFile);

var fullPath = Path.GetFullPath(grammarFile);
var workDir = Path.GetFullPath(WorkingDirectory);
if (!workDir.EndsWith(Path.DirectorySeparatorChar.ToString()))
workDir += Path.DirectorySeparatorChar;

if (fullPath.StartsWith(workDir, StringComparison.OrdinalIgnoreCase))
{
return NormalizePath(fullPath.Substring(workDir.Length));
}

return NormalizePath(grammarFile);
}

/// <summary>
/// Splits a semicolon-separated list and filters empty entries
/// </summary>
Expand All @@ -145,5 +174,13 @@ protected IEnumerable<string> SplitAndFilterList(string list)
.Select(s => s.Trim())
.Where(s => !string.IsNullOrWhiteSpace(s));
}

/// <summary>
/// Returns true if WorkingDirectory is set and the directory exists on disk.
/// </summary>
private static bool ShouldUseWorkingDirectory(string workingDirectory)
{
return !string.IsNullOrEmpty(workingDirectory) && Directory.Exists(workingDirectory);
}
}
}
7 changes: 7 additions & 0 deletions Antlr4BuildTasks/Tools/IAntlrTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,12 @@ bool DiscoverGeneratedFiles(
/// Gets or sets whether to force ATN for all decisions
/// </summary>
bool ForceATN { get; set; }

/// <summary>
/// Gets or sets the working directory for the tool process.
/// When set, grammar file paths are converted to be relative to this directory
/// so that the "Generated from" comment in output files uses relative paths.
/// </summary>
string WorkingDirectory { get; set; }
}
}
8 changes: 4 additions & 4 deletions Antlr4BuildTasks/Tools/JavaAntlrTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ private List<string> BuildDependencyArguments(string grammarFile)
// Common arguments
AddCommonArguments(grammarFile.EndsWith("Parser.g4"), arguments);

// Grammar files
arguments.Add(grammarFile);
// Grammar files - use relative path so "Generated from" comment is stable
arguments.Add(MakeGrammarRelativePath(grammarFile));

return arguments;
}
Expand All @@ -167,8 +167,8 @@ private List<string> BuildGenerationArguments(string grammarFile)
if (EnableLogging)
arguments.Add("-Xlog");

// Grammar files
arguments.Add(grammarFile);
// Grammar files - use relative path so "Generated from" comment is stable
arguments.Add(MakeGrammarRelativePath(grammarFile));

return arguments;
}
Expand Down