Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] add F# WriteCodeFragment #2132

Closed
wants to merge 3 commits into from
Closed
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
36 changes: 36 additions & 0 deletions src/Tasks.UnitTests/WriteCodeFragment_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,32 @@ public void OneAttributePositionalAndNamedParamsVisualBasic()
File.Delete(task.OutputFile.ItemSpec);
}

/// <summary>
/// Test with the VB language
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F#, not VB

/// </summary>
[Fact]
[Trait("Category", "mono-osx-failing")]
[Trait("Category", "fsharp")]
public void OneAttributeNoParamsFSharp()
{
WriteCodeFragment task = new WriteCodeFragment();
MockEngine engine = new MockEngine(true);
task.BuildEngine = engine;
TaskItem attribute = new TaskItem("System.AssemblyTrademarkAttribute");
task.AssemblyAttributes = new TaskItem[] { attribute };
task.Language = "f#";
task.OutputDirectory = new TaskItem(Path.GetTempPath());
bool result = task.Execute();

Assert.Equal(true, result);

string content = File.ReadAllText(task.OutputFile.ItemSpec);
Console.WriteLine(content);

CheckContentFSharp(content, "[<assembly: System.AssemblyTrademarkAttribute()>]");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe test with multiple attributes and attributes with parameters?

}


private static void CheckContentCSharp(string actualContent, params string[] expectedAttributes)
{
CheckContent(
Expand All @@ -663,6 +689,16 @@ private static void CheckContentVB(string actualContent, params string[] expecte
"Imports System.Reflection");
}

private static void CheckContentFSharp(string actualContent, params string[] expectedAttributes)
{
CheckContent(
actualContent,
expectedAttributes,
"//",
"open System",
"open System.Reflection");
}

private static void CheckContent(string actualContent, string[] expectedAttributes, string commentStart, params string[] expectedHeader)
{
string expectedContent = string.Join(Environment.NewLine, expectedHeader.Concat(expectedAttributes));
Expand Down
32 changes: 32 additions & 0 deletions src/Tasks/WriteCodeFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ public override bool Execute()
[SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.IO.StringWriter.#ctor(System.Text.StringBuilder)", Justification = "Reads fine to me")]
private string GenerateCode(out string extension)
{
if (Language.ToLowerInvariant() == "f#")
{
//no codedoom for F#, fallback to coreclr version
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Code DOM" not "codedoom"

return GenerateCodeCoreClr(out extension);
}

extension = null;
bool haveGeneratedContent = false;

Expand Down Expand Up @@ -333,6 +339,32 @@ private string GenerateCodeCoreClr(out string extension)
code.AppendLine(string.Format($"<Assembly: {attributeItem.ItemSpec}({args})>"));
haveGeneratedContent = true;
}
break;
case "f#":
if (AssemblyAttributes == null) return string.Empty;

extension = "fs";
code.AppendLine("// " + ResourceUtilities.FormatResourceString("WriteCodeFragment.Comment"));
code.AppendLine();
code.AppendLine("open System");
code.AppendLine("open System.Reflection");
code.AppendLine();

foreach (ITaskItem attributeItem in AssemblyAttributes)
{
string args = GetAttributeArguments(attributeItem, "=", QuoteSnippetStringCSharp);
if (args == null) return null;

code.AppendLine(string.Format($"[<assembly: {attributeItem.ItemSpec}({args})>]"));
haveGeneratedContent = true;
}

if (haveGeneratedContent)
{
code.AppendLine("()");
code.AppendLine();
}

break;
default:
Log.LogErrorWithCodeFromResources("WriteCodeFragment.CouldNotCreateProvider", Language, string.Empty);
Expand Down