Skip to content

Commit

Permalink
[Csproj] Add support for texttemplates that use Host.SetFileExtension…
Browse files Browse the repository at this point in the history
… instead of output extension tags.
  • Loading branch information
kudaba committed Feb 10, 2022
1 parent f3e7be5 commit 8d18cd4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sharpmake.Generators/VisualStudio/Csproj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2170,7 +2170,7 @@ List<string> skipFiles
bool runtimeTemplate = project.AdditionalRuntimeTemplates.Contains(ttFile);
string expectedExtension =
runtimeTemplate ? ".cs" :
Util.GetTextTemplateDirectiveParam(Path.Combine(_projectPath, ttFile), "output", "extension") ?? ".cs";
Util.GetTextTemplateOutputExtension(Path.Combine(_projectPath, ttFile)) ?? ".cs";
if (!expectedExtension.StartsWith(".", StringComparison.Ordinal))
expectedExtension = "." + expectedExtension;
string fileNameWithoutExtension = ttFile.Substring(0, ttFile.Length - TTExtension.Length);
Expand Down
36 changes: 35 additions & 1 deletion Sharpmake/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ public static int GetDeterministicHashCode(this string str)
}
}

private static string GetTextTemplateDirectiveParam(string[] templateText, string directive, string paramName)
{
Regex regex = new Regex(@"<#@\s*?" + directive + @"\s+?.*?" + paramName + @"=""(?<paramValue>.*?)"".*?#>");

foreach (var line in templateText)
{
Match m = regex.Match(line);
Group g = m.Groups["paramValue"];
if (g != null && g.Success)
return g.Value;
}

return null;
}

/// <summary>
/// Finds the first occurrence of directive and returns the
/// requested param value. Ex:
Expand All @@ -107,10 +122,29 @@ public static int GetDeterministicHashCode(this string str)
/// and return ".txt"
/// </summary>
public static string GetTextTemplateDirectiveParam(string filePath, string directive, string paramName)
{
return GetTextTemplateDirectiveParam(File.ReadAllLines(filePath), directive, paramName);
}

/// <summary>
/// Finds the output type of a template, looking for both the directive form and the Host.SetFileExtension form
/// will match:
/// <#@ output extension=".txt" #>
/// or
/// Host.SetFileExtension(".txt")
/// and return ".txt"
/// </summary>
public static string GetTextTemplateOutputExtension(string filePath)
{
string[] templateText = File.ReadAllLines(filePath);

Regex regex = new Regex(@"<#@\s*?" + directive + @"\s+?.*?" + paramName + @"=""(?<paramValue>.*?)"".*?#>");
var output = Util.GetTextTemplateDirectiveParam(templateText, "output", "extension");
if (output != null)
return output;

// alternatively look for host.SetFileExtension

Regex regex = new Regex(@"Host.SetFileExtension\(""(?<paramValue>.*?)""\)");

foreach (var line in templateText)
{
Expand Down

0 comments on commit 8d18cd4

Please sign in to comment.