diff --git a/src/Tasks.UnitTests/WriteCodeFragment_Tests.cs b/src/Tasks.UnitTests/WriteCodeFragment_Tests.cs index 67028d33b09..d21605d2f94 100644 --- a/src/Tasks.UnitTests/WriteCodeFragment_Tests.cs +++ b/src/Tasks.UnitTests/WriteCodeFragment_Tests.cs @@ -641,6 +641,32 @@ public void OneAttributePositionalAndNamedParamsVisualBasic() File.Delete(task.OutputFile.ItemSpec); } + /// + /// Test with the VB language + /// + [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, "[]"); + } + + private static void CheckContentCSharp(string actualContent, params string[] expectedAttributes) { CheckContent( @@ -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)); diff --git a/src/Tasks/WriteCodeFragment.cs b/src/Tasks/WriteCodeFragment.cs index d55298099a8..2d85d5fe4bf 100644 --- a/src/Tasks/WriteCodeFragment.cs +++ b/src/Tasks/WriteCodeFragment.cs @@ -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 + return GenerateCodeCoreClr(out extension); + } + extension = null; bool haveGeneratedContent = false; @@ -333,6 +339,32 @@ private string GenerateCodeCoreClr(out string extension) code.AppendLine(string.Format($"")); 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($"[]")); + haveGeneratedContent = true; + } + + if (haveGeneratedContent) + { + code.AppendLine("()"); + code.AppendLine(); + } + break; default: Log.LogErrorWithCodeFromResources("WriteCodeFragment.CouldNotCreateProvider", Language, string.Empty);