From a24cbc3ff1413a1b1210e64a365e6b3b82d0509b Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Mon, 12 Sep 2022 19:54:13 +0300 Subject: [PATCH 1/3] Use MSBuild's logging facilities instead of writing to console. --- src/FSharp.Build/FSharpEmbedResXSource.fs | 42 ++++--- src/FSharp.Build/FSharpEmbedResourceText.fs | 87 +++++++------- src/FSharp.Build/SubstituteText.fs | 119 +++++++++----------- src/FSharp.Build/WriteCodeFragment.fs | 112 +++++++++--------- 4 files changed, 166 insertions(+), 194 deletions(-) diff --git a/src/FSharp.Build/FSharpEmbedResXSource.fs b/src/FSharp.Build/FSharpEmbedResXSource.fs index 4b34660b2a8..6d80e263e64 100644 --- a/src/FSharp.Build/FSharpEmbedResXSource.fs +++ b/src/FSharp.Build/FSharpEmbedResXSource.fs @@ -10,13 +10,16 @@ open System.Xml.Linq open Microsoft.Build.Framework open Microsoft.Build.Utilities -type FSharpEmbedResXSource() = - let mutable _buildEngine: IBuildEngine MaybeNull = null - let mutable _hostObject: ITaskHost MaybeNull = null +type FSharpEmbedResXSource() as this = + inherit Task() let mutable _embeddedText: ITaskItem[] = [||] let mutable _generatedSource: ITaskItem[] = [||] let mutable _outputPath: string = "" let mutable _targetFramework: string = "" + + let failTask fmt = Printf.ksprintf (fun msg -> + this.Log.LogError msg + raise TaskFailed) fmt let boilerplate = @"// @@ -36,7 +39,7 @@ module internal {1} = let generateSource (resx: string) (fullModuleName: string) (generateLegacy: bool) (generateLiteral: bool) = try - let printMessage = printfn "FSharpEmbedResXSource: %s" + let printMessage fmt = Printf.ksprintf this.Log.LogMessage fmt let justFileName = Path.GetFileNameWithoutExtension(resx) let sourcePath = Path.Combine(_outputPath, justFileName + ".fs") @@ -46,7 +49,7 @@ module internal {1} = && File.Exists(sourcePath) && File.GetLastWriteTimeUtc(resx) <= File.GetLastWriteTimeUtc(sourcePath) then - printMessage (sprintf "Skipping generation: '%s' since it is up-to-date." sourcePath) + printMessage "Skipping generation: '%s' since it is up-to-date." sourcePath Some(sourcePath) else let namespaceName, moduleName = @@ -63,7 +66,7 @@ module internal {1} = || _targetFramework.StartsWith("netcoreapp1.") ) - printMessage (sprintf "Generating code for target framework %s" _targetFramework) + printMessage "Generating code for target framework %s" _targetFramework let sb = StringBuilder() @@ -72,7 +75,7 @@ module internal {1} = if generateGetObject then sb.AppendLine(boilerplateGetObject) |> ignore - printMessage <| sprintf "Generating: %s" sourcePath + printMessage "Generating: %s" sourcePath let body = let xname = XName.op_Implicit @@ -82,12 +85,12 @@ module internal {1} = (fun (sb: StringBuilder) (node: XElement) -> let name = match node.Attribute(xname "name") with - | null -> failwith (sprintf "Missing resource name on element '%s'" (node.ToString())) + | null -> failTask "Missing resource name on element '%O'" node | attr -> attr.Value let docComment = match node.Elements(xname "value").FirstOrDefault() with - | null -> failwith <| sprintf "Missing resource value for '%s'" name + | null -> failTask "Missing resource value for '%s'" name | element -> element.Value.Trim() let identifier = @@ -118,7 +121,7 @@ module internal {1} = sb File.WriteAllText(sourcePath, body.ToString()) - printMessage <| sprintf "Done: %s" sourcePath + printMessage "Done: %s" sourcePath Some(sourcePath) with e -> printf "An exception occurred when processing '%s'\n%s" resx (e.ToString()) @@ -140,17 +143,8 @@ module internal {1} = [] member _.GeneratedSource = _generatedSource - - interface ITask with - member _.BuildEngine - with get () = _buildEngine - and set (value) = _buildEngine <- value - - member _.HostObject - with get () = _hostObject - and set (value) = _hostObject <- value - - member this.Execute() = + override this.Execute() = + try let getBooleanMetadata (metadataName: string) (defaultValue: bool) (item: ITaskItem) = match item.GetMetadata(metadataName) with | value when String.IsNullOrWhiteSpace(value) -> defaultValue @@ -158,7 +152,7 @@ module internal {1} = match value.ToLowerInvariant() with | "true" -> true | "false" -> false - | _ -> failwith (sprintf "Expected boolean value for '%s' found '%s'" metadataName value) + | _ -> failTask "Expected boolean value for '%s' found '%s'" metadataName value let mutable success = true @@ -181,4 +175,6 @@ module internal {1} = |] _generatedSource <- generatedSource - success + success && not this.Log.HasLoggedErrors + with + | TaskFailed -> false diff --git a/src/FSharp.Build/FSharpEmbedResourceText.fs b/src/FSharp.Build/FSharpEmbedResourceText.fs index 77002aa4f54..fe838d9a8a7 100644 --- a/src/FSharp.Build/FSharpEmbedResourceText.fs +++ b/src/FSharp.Build/FSharpEmbedResourceText.fs @@ -6,24 +6,29 @@ open System.IO open Microsoft.Build.Framework open Microsoft.Build.Utilities -type FSharpEmbedResourceText() = - let mutable _buildEngine: IBuildEngine MaybeNull = null - let mutable _hostObject: ITaskHost MaybeNull = null +/// A special exception that when thrown signifies that +/// the task should end with failure. It is assumed that +/// the task has already emitted the error message. +exception TaskFailed + +type FSharpEmbedResourceText() as this = + inherit Task() let mutable _embeddedText: ITaskItem[] = [||] let mutable _generatedSource: ITaskItem[] = [||] let mutable _generatedResx: ITaskItem[] = [||] let mutable _outputPath: string = "" let PrintErr (fileName, line, msg) = - printfn "%s(%d): error : %s" fileName line msg + this.Log.LogError(null, null, null, fileName, line, 0, 0, 0, msg, Array.empty) let Err (fileName, line, msg) = PrintErr(fileName, line, msg) - printfn "Note that the syntax of each line is one of these three alternatives:" - printfn "# comment" - printfn "ident,\"string\"" - printfn "errNum,ident,\"string\"" - failwith (sprintf "there were errors in the file '%s'" fileName) + let hint = "Note that the syntax of each line is one of these three alternatives: +# comment +ident,\"string\" +errNum,ident,\"string\"" + this.Log.LogMessage(MessageImportance.High, hint) + raise TaskFailed let xmlBoilerPlateString = @" @@ -192,7 +197,7 @@ type FSharpEmbedResourceText() = if s.StartsWith "\"" && s.EndsWith "\"" then s.Substring(1, s.Length - 2) else - failwith "error message string should be quoted" + Err(null, 0, "error message string should be quoted") let ParseLine fileName lineNum (txt: string) = let mutable errNum = None @@ -361,8 +366,7 @@ open Printf let generateResxAndSource (fileName: string) = try - let printMessage message = - printfn "FSharpEmbedResourceText: %s" message + let printMessage fmt = Printf.ksprintf this.Log.LogMessage fmt let justFileName = Path.GetFileNameWithoutExtension(fileName) // .txt @@ -391,35 +395,33 @@ open Printf && (File.GetLastWriteTimeUtc(fileName) <= File.GetLastWriteTimeUtc(outXmlFileName)) if condition5 then - printMessage (sprintf "Skipping generation of %s and %s from %s since up-to-date" outFileName outXmlFileName fileName) + printMessage "Skipping generation of %s and %s from %s since up-to-date" outFileName outXmlFileName fileName Some(fileName, outFileName, outXmlFileName) else - printMessage ( - sprintf - "Generating %s and %s from %s, because condition %d is false, see FSharpEmbedResourceText.fs in the F# source" - outFileName - outXmlFileName - fileName - (if not condition1 then 1 - elif not condition2 then 2 - elif not condition3 then 3 - elif not condition4 then 4 - else 5) - ) - - printMessage (sprintf "Reading %s" fileName) + printMessage + "Generating %s and %s from %s, because condition %d is false, see FSharpEmbedResourceText.fs in the F# source" + outFileName + outXmlFileName + fileName + (if not condition1 then 1 + elif not condition2 then 2 + elif not condition3 then 3 + elif not condition4 then 4 + else 5) + + printMessage "Reading %s" fileName let lines = File.ReadAllLines(fileName) |> Array.mapi (fun i s -> i, s) // keep line numbers |> Array.filter (fun (i, s) -> not (s.StartsWith "#")) // filter out comments - printMessage (sprintf "Parsing %s" fileName) + printMessage "Parsing %s" fileName let stringInfos = lines |> Array.map (fun (i, s) -> ParseLine fileName i s) // now we have array of (lineNum, ident, str, holes, netFormatString) // str has %d, netFormatString has {0} - printMessage (sprintf "Validating %s" fileName) + printMessage "Validating %s" fileName // validate that all the idents are unique let allIdents = new System.Collections.Generic.Dictionary() @@ -436,7 +438,7 @@ open Printf allIdents.Add(ident, line) - printMessage (sprintf "Validating uniqueness of %s" fileName) + printMessage "Validating uniqueness of %s" fileName // validate that all the strings themselves are unique let allStrs = new System.Collections.Generic.Dictionary() @@ -456,7 +458,7 @@ open Printf allStrs.Add(str, (line, ident)) - printMessage (sprintf "Generating %s" outFileName) + printMessage "Generating %s" outFileName use outStream = File.Create outFileName use out = new StreamWriter(outStream) fprintfn out "// This is a generated file; the original input is '%s'" fileName @@ -466,7 +468,7 @@ open Printf let theResourceName = justFileName fprintfn out "%s" (StringBoilerPlate theResourceName) - printMessage (sprintf "Generating resource methods for %s" outFileName) + printMessage "Generating resource methods for %s" outFileName // gen each resource method stringInfos |> Seq.iter (fun (lineNum, (optErrNum, ident), str, holes, netFormatString) -> @@ -520,7 +522,7 @@ open Printf justPercentsFromFormatString (actualArgs.ToString())) - printMessage (sprintf "Generating .resx for %s" outFileName) + printMessage "Generating .resx for %s" outFileName fprintfn out "" // gen validation method fprintfn out " /// Call this method once to validate that all known resources are valid; throws if not" @@ -548,7 +550,7 @@ open Printf use outXmlStream = File.Create outXmlFileName xd.Save outXmlStream - printMessage (sprintf "Done %s" outFileName) + printMessage "Done %s" outFileName Some(fileName, outFileName, outXmlFileName) with e -> PrintErr(fileName, 0, sprintf "An exception occurred when processing '%s'\n%s" fileName (e.ToString())) @@ -569,18 +571,9 @@ open Printf [] member _.GeneratedResx = _generatedResx + override this.Execute() = - interface ITask with - member _.BuildEngine - with get () = _buildEngine - and set (value) = _buildEngine <- value - - member _.HostObject - with get () = _hostObject - and set (value) = _hostObject <- value - - member this.Execute() = - + try let generatedFiles = this.EmbeddedText |> Array.choose (fun item -> generateResxAndSource item.ItemSpec) @@ -609,4 +602,6 @@ open Printf _generatedSource <- generatedSource _generatedResx <- generatedResx - generatedResult + generatedResult && not this.Log.HasLoggedErrors + with + | TaskFailed -> false diff --git a/src/FSharp.Build/SubstituteText.fs b/src/FSharp.Build/SubstituteText.fs index 0f036da0a86..ce0377186e9 100644 --- a/src/FSharp.Build/SubstituteText.fs +++ b/src/FSharp.Build/SubstituteText.fs @@ -5,11 +5,10 @@ namespace FSharp.Build open System open System.IO open Microsoft.Build.Framework +open Microsoft.Build.Utilities type SubstituteText() = - - let mutable _buildEngine: IBuildEngine MaybeNull = null - let mutable _hostObject: ITaskHost MaybeNull = null + inherit Task() let mutable copiedFiles = new ResizeArray() let mutable embeddedResources: ITaskItem[] = [||] @@ -21,75 +20,65 @@ type SubstituteText() = [] member _.CopiedFiles = copiedFiles.ToArray() - - interface ITask with - member _.BuildEngine - with get () = _buildEngine - and set (value) = _buildEngine <- value - - member _.HostObject - with get () = _hostObject - and set (value) = _hostObject <- value - - member _.Execute() = - copiedFiles.Clear() - - if not (isNull embeddedResources) then - for item in embeddedResources do - // Update ITaskItem metadata to point to new location - let sourcePath = item.GetMetadata("FullPath") - - let pattern1 = item.GetMetadata("Pattern1") - let pattern2 = item.GetMetadata("Pattern2") - - // Is there any replacement to do? - if not (String.IsNullOrWhiteSpace(pattern1) && String.IsNullOrWhiteSpace(pattern2)) then - if not (String.IsNullOrWhiteSpace(sourcePath)) then - try - let getTargetPathFrom key = - let md = item.GetMetadata(key) - let path = Path.GetDirectoryName(md) - let fileName = Path.GetFileName(md) - let target = Path.Combine(path, @"..\resources", fileName) + override _.Execute() = + copiedFiles.Clear() + + if not (isNull embeddedResources) then + for item in embeddedResources do + // Update ITaskItem metadata to point to new location + let sourcePath = item.GetMetadata("FullPath") + + let pattern1 = item.GetMetadata("Pattern1") + let pattern2 = item.GetMetadata("Pattern2") + + // Is there any replacement to do? + if not (String.IsNullOrWhiteSpace(pattern1) && String.IsNullOrWhiteSpace(pattern2)) then + if not (String.IsNullOrWhiteSpace(sourcePath)) then + try + let getTargetPathFrom key = + let md = item.GetMetadata(key) + let path = Path.GetDirectoryName(md) + let fileName = Path.GetFileName(md) + let target = Path.Combine(path, @"..\resources", fileName) + target + + // Copy from the location specified in Identity + let sourcePath = item.GetMetadata("Identity") + + // Copy to the location specified in TargetPath unless no TargetPath is provided, then use Identity + let targetPath = + let identityPath = getTargetPathFrom "Identity" + let intermediateTargetPath = item.GetMetadata("IntermediateTargetPath") + + if not (String.IsNullOrWhiteSpace(intermediateTargetPath)) then + let fileName = Path.GetFileName(identityPath) + let target = Path.Combine(intermediateTargetPath, fileName) target + else + identityPath - // Copy from the location specified in Identity - let sourcePath = item.GetMetadata("Identity") - - // Copy to the location specified in TargetPath unless no TargetPath is provided, then use Identity - let targetPath = - let identityPath = getTargetPathFrom "Identity" - let intermediateTargetPath = item.GetMetadata("IntermediateTargetPath") - - if not (String.IsNullOrWhiteSpace(intermediateTargetPath)) then - let fileName = Path.GetFileName(identityPath) - let target = Path.Combine(intermediateTargetPath, fileName) - target - else - identityPath - - item.ItemSpec <- targetPath + item.ItemSpec <- targetPath - // Transform file - let mutable contents = File.ReadAllText(sourcePath) + // Transform file + let mutable contents = File.ReadAllText(sourcePath) - if not (String.IsNullOrWhiteSpace(pattern1)) then - let replacement = item.GetMetadata("Replacement1") - contents <- contents.Replace(pattern1, replacement) + if not (String.IsNullOrWhiteSpace(pattern1)) then + let replacement = item.GetMetadata("Replacement1") + contents <- contents.Replace(pattern1, replacement) - if not (String.IsNullOrWhiteSpace(pattern2)) then - let replacement = item.GetMetadata("Replacement2") - contents <- contents.Replace(pattern2, replacement) + if not (String.IsNullOrWhiteSpace(pattern2)) then + let replacement = item.GetMetadata("Replacement2") + contents <- contents.Replace(pattern2, replacement) - let directory = Path.GetDirectoryName(targetPath) + let directory = Path.GetDirectoryName(targetPath) - if not (Directory.Exists(directory)) then - Directory.CreateDirectory(directory) |> ignore + if not (Directory.Exists(directory)) then + Directory.CreateDirectory(directory) |> ignore - File.WriteAllText(targetPath, contents) - with _ -> - () + File.WriteAllText(targetPath, contents) + with _ -> + () - copiedFiles.Add(item) + copiedFiles.Add(item) - true + true diff --git a/src/FSharp.Build/WriteCodeFragment.fs b/src/FSharp.Build/WriteCodeFragment.fs index ab390ae4a59..3e5659c1dcb 100644 --- a/src/FSharp.Build/WriteCodeFragment.fs +++ b/src/FSharp.Build/WriteCodeFragment.fs @@ -9,13 +9,17 @@ open System.Text open Microsoft.Build.Framework open Microsoft.Build.Utilities -type WriteCodeFragment() = - let mutable _buildEngine: IBuildEngine MaybeNull = null - let mutable _hostObject: ITaskHost MaybeNull = null +type WriteCodeFragment() as this = + inherit Task() let mutable _outputDirectory: ITaskItem MaybeNull = null let mutable _outputFile: ITaskItem MaybeNull = null let mutable _language: string = "" let mutable _assemblyAttributes: ITaskItem[] = [||] + + let failTask fmt = + Printf.ksprintf (fun msg -> + this.Log.LogError msg + raise TaskFailed) fmt static let escapeString (str: string) = let sb = @@ -37,7 +41,7 @@ type WriteCodeFragment() = sb.Append("\"").ToString() - static member GenerateAttribute(item: ITaskItem, language: string) = + member _.GenerateAttribute(item: ITaskItem, language: string) = let attributeName = item.ItemSpec let args = @@ -70,7 +74,7 @@ type WriteCodeFragment() = match Int32.TryParse indexString with | (true, index) -> (index, value) - | (false, _) -> failwith (sprintf "Unable to parse '%s' as an index" indexString)) + | (false, _) -> failTask "Unable to parse '%s' as an index" indexString) |> List.sortBy fst // assign ordered parameters to array let orderedParametersArray = @@ -96,7 +100,7 @@ type WriteCodeFragment() = | "f#" -> sprintf "[]" attributeName args | "c#" -> sprintf "[assembly: %s(%s)]" attributeName args | "vb" -> sprintf "" attributeName args - | _ -> failwith "Language name must be one of F#, C# or VB" + | _ -> failTask "Language name must be one of F#, C# or VB" // adding this property to maintain API equivalence with the MSBuild task member _.Language @@ -115,57 +119,45 @@ type WriteCodeFragment() = member _.OutputFile with get () = _outputFile and set (value) = _outputFile <- value - - interface ITask with - member _.BuildEngine - with get () = _buildEngine - and set (value) = _buildEngine <- value - - member _.HostObject - with get () = _hostObject - and set (value) = _hostObject <- value - - member _.Execute() = - try - match _outputFile with - | Null -> failwith "Output location must be specified" - | NonNull outputFile -> - let boilerplate = - match _language.ToLowerInvariant() with - | "f#" -> - "// \n// Generated by the FSharp WriteCodeFragment class.\n// \nnamespace FSharp\n\nopen System\nopen System.Reflection\n" - | "c#" -> - "// \n// Generated by the FSharp WriteCodeFragment class.\n// \n\nusing System;\nusing System.Reflection;" - | "vb" -> - "'------------------------------------------------------------------------------\n' \n' Generated by the FSharp WriteCodeFragment class.\n' \n'------------------------------------------------------------------------------\n\nOption Strict Off\nOption Explicit On\n\nImports System\nImports System.Reflection" - | _ -> failwith "Language name must be one of F#, C# or VB" - - let sb = StringBuilder().AppendLine(boilerplate).AppendLine() - - let code = - (sb, _assemblyAttributes) - ||> Array.fold (fun (sb: StringBuilder) (item: ITaskItem) -> - sb.AppendLine(WriteCodeFragment.GenerateAttribute(item, _language.ToLowerInvariant()))) - - if _language.ToLowerInvariant() = "f#" then - code.AppendLine("do()") |> ignore - - let fileName = outputFile.ItemSpec - - let outputFileItem = - match _outputDirectory with - | Null -> outputFile - | NonNull outputDirectory -> - if Path.IsPathRooted(fileName) then - outputFile - else - TaskItem(Path.Combine(outputDirectory.ItemSpec, fileName)) :> ITaskItem - - let codeText = code.ToString() - File.WriteAllText(fileName, codeText) - _outputFile <- outputFileItem - true - - with e -> - printf "Error writing code fragment: %s" (e.ToString()) - false + override this.Execute() = + try + match _outputFile with + | Null -> failTask "Output location must be specified" + | NonNull outputFile -> + let boilerplate = + match _language.ToLowerInvariant() with + | "f#" -> + "// \n// Generated by the FSharp WriteCodeFragment class.\n// \nnamespace FSharp\n\nopen System\nopen System.Reflection\n" + | "c#" -> + "// \n// Generated by the FSharp WriteCodeFragment class.\n// \n\nusing System;\nusing System.Reflection;" + | "vb" -> + "'------------------------------------------------------------------------------\n' \n' Generated by the FSharp WriteCodeFragment class.\n' \n'------------------------------------------------------------------------------\n\nOption Strict Off\nOption Explicit On\n\nImports System\nImports System.Reflection" + | _ -> failTask "Language name must be one of F#, C# or VB" + + let sb = StringBuilder().AppendLine(boilerplate).AppendLine() + + let code = + (sb, _assemblyAttributes) + ||> Array.fold (fun (sb: StringBuilder) (item: ITaskItem) -> + sb.AppendLine(this.GenerateAttribute(item, _language.ToLowerInvariant()))) + + if _language.ToLowerInvariant() = "f#" then + code.AppendLine("do()") |> ignore + + let fileName = outputFile.ItemSpec + + let outputFileItem = + match _outputDirectory with + | Null -> outputFile + | NonNull outputDirectory -> + if Path.IsPathRooted(fileName) then + outputFile + else + TaskItem(Path.Combine(outputDirectory.ItemSpec, fileName)) :> ITaskItem + + let codeText = code.ToString() + File.WriteAllText(fileName, codeText) + _outputFile <- outputFileItem + not this.Log.HasLoggedErrors + with + | TaskFailed -> false From 5fe06421a5ee41170bda8021b6f63911a6b2046f Mon Sep 17 00:00:00 2001 From: Tomas Grosup Date: Wed, 12 Oct 2022 10:19:51 +0200 Subject: [PATCH 2/3] Changing usage of WriteCodeFragment in tests --- tests/FSharp.Build.UnitTests/WriteCodeFragmentTests.fs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/FSharp.Build.UnitTests/WriteCodeFragmentTests.fs b/tests/FSharp.Build.UnitTests/WriteCodeFragmentTests.fs index d295f60f4ee..49ad920c449 100644 --- a/tests/FSharp.Build.UnitTests/WriteCodeFragmentTests.fs +++ b/tests/FSharp.Build.UnitTests/WriteCodeFragmentTests.fs @@ -13,7 +13,7 @@ type WriteCodeFragmentFSharpTests() = let verifyAttribute (attributeName:string) (parameters:(string*string) list) (expectedAttributeText:string) = let taskItem = TaskItem(attributeName) parameters |> List.iter (fun (key, value) -> taskItem.SetMetadata(key, value)) - let actualAttributeText = WriteCodeFragment.GenerateAttribute (taskItem :> ITaskItem, "f#") + let actualAttributeText = (new WriteCodeFragment()).GenerateAttribute (taskItem :> ITaskItem, "f#") let fullExpectedAttributeText = "[]" Assert.AreEqual(fullExpectedAttributeText, actualAttributeText) @@ -43,7 +43,7 @@ type WriteCodeFragmentCSharpTests() = let verifyAttribute (attributeName:string) (parameters:(string*string) list) (expectedAttributeText:string) = let taskItem = TaskItem(attributeName) parameters |> List.iter (fun (key, value) -> taskItem.SetMetadata(key, value)) - let actualAttributeText = WriteCodeFragment.GenerateAttribute (taskItem :> ITaskItem, "c#") + let actualAttributeText = (new WriteCodeFragment()).GenerateAttribute (taskItem :> ITaskItem, "c#") let fullExpectedAttributeText = "[assembly: " + expectedAttributeText + "]" Assert.AreEqual(fullExpectedAttributeText, actualAttributeText) @@ -75,7 +75,7 @@ type WriteCodeFragmentVisualBasicTests() = let verifyAttribute (attributeName:string) (parameters:(string*string) list) (expectedAttributeText:string) = let taskItem = TaskItem(attributeName) parameters |> List.iter (fun (key, value) -> taskItem.SetMetadata(key, value)) - let actualAttributeText = WriteCodeFragment.GenerateAttribute (taskItem :> ITaskItem, "vb") + let actualAttributeText = (new WriteCodeFragment()).GenerateAttribute (taskItem :> ITaskItem, "vb") let fullExpectedAttributeText = "" Assert.AreEqual(fullExpectedAttributeText, actualAttributeText) From aee41a8bef749e1bee4514df757041e4d1c1da58 Mon Sep 17 00:00:00 2001 From: Tomas Grosup Date: Wed, 12 Oct 2022 10:22:27 +0200 Subject: [PATCH 3/3] Applying cleaner formatting --- src/FSharp.Build/FSharpEmbedResXSource.fs | 16 ++++++++++------ src/FSharp.Build/FSharpEmbedResourceText.fs | 12 ++++++++---- src/FSharp.Build/SubstituteText.fs | 1 + src/FSharp.Build/WriteCodeFragment.fs | 15 +++++++++------ 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/FSharp.Build/FSharpEmbedResXSource.fs b/src/FSharp.Build/FSharpEmbedResXSource.fs index 6d80e263e64..29a2ceefd19 100644 --- a/src/FSharp.Build/FSharpEmbedResXSource.fs +++ b/src/FSharp.Build/FSharpEmbedResXSource.fs @@ -16,10 +16,13 @@ type FSharpEmbedResXSource() as this = let mutable _generatedSource: ITaskItem[] = [||] let mutable _outputPath: string = "" let mutable _targetFramework: string = "" - - let failTask fmt = Printf.ksprintf (fun msg -> - this.Log.LogError msg - raise TaskFailed) fmt + + let failTask fmt = + Printf.ksprintf + (fun msg -> + this.Log.LogError msg + raise TaskFailed) + fmt let boilerplate = @"// @@ -143,6 +146,7 @@ module internal {1} = [] member _.GeneratedSource = _generatedSource + override this.Execute() = try let getBooleanMetadata (metadataName: string) (defaultValue: bool) (item: ITaskItem) = @@ -176,5 +180,5 @@ module internal {1} = _generatedSource <- generatedSource success && not this.Log.HasLoggedErrors - with - | TaskFailed -> false + with TaskFailed -> + false diff --git a/src/FSharp.Build/FSharpEmbedResourceText.fs b/src/FSharp.Build/FSharpEmbedResourceText.fs index fe838d9a8a7..7790a783271 100644 --- a/src/FSharp.Build/FSharpEmbedResourceText.fs +++ b/src/FSharp.Build/FSharpEmbedResourceText.fs @@ -23,10 +23,13 @@ type FSharpEmbedResourceText() as this = let Err (fileName, line, msg) = PrintErr(fileName, line, msg) - let hint = "Note that the syntax of each line is one of these three alternatives: + + let hint = + "Note that the syntax of each line is one of these three alternatives: # comment ident,\"string\" errNum,ident,\"string\"" + this.Log.LogMessage(MessageImportance.High, hint) raise TaskFailed @@ -399,7 +402,7 @@ open Printf Some(fileName, outFileName, outXmlFileName) else - printMessage + printMessage "Generating %s and %s from %s, because condition %d is false, see FSharpEmbedResourceText.fs in the F# source" outFileName outXmlFileName @@ -571,6 +574,7 @@ open Printf [] member _.GeneratedResx = _generatedResx + override this.Execute() = try @@ -603,5 +607,5 @@ open Printf _generatedSource <- generatedSource _generatedResx <- generatedResx generatedResult && not this.Log.HasLoggedErrors - with - | TaskFailed -> false + with TaskFailed -> + false diff --git a/src/FSharp.Build/SubstituteText.fs b/src/FSharp.Build/SubstituteText.fs index ce0377186e9..2e4950a62b3 100644 --- a/src/FSharp.Build/SubstituteText.fs +++ b/src/FSharp.Build/SubstituteText.fs @@ -20,6 +20,7 @@ type SubstituteText() = [] member _.CopiedFiles = copiedFiles.ToArray() + override _.Execute() = copiedFiles.Clear() diff --git a/src/FSharp.Build/WriteCodeFragment.fs b/src/FSharp.Build/WriteCodeFragment.fs index 3e5659c1dcb..7df19fad327 100644 --- a/src/FSharp.Build/WriteCodeFragment.fs +++ b/src/FSharp.Build/WriteCodeFragment.fs @@ -15,11 +15,13 @@ type WriteCodeFragment() as this = let mutable _outputFile: ITaskItem MaybeNull = null let mutable _language: string = "" let mutable _assemblyAttributes: ITaskItem[] = [||] - + let failTask fmt = - Printf.ksprintf (fun msg -> - this.Log.LogError msg - raise TaskFailed) fmt + Printf.ksprintf + (fun msg -> + this.Log.LogError msg + raise TaskFailed) + fmt static let escapeString (str: string) = let sb = @@ -119,6 +121,7 @@ type WriteCodeFragment() as this = member _.OutputFile with get () = _outputFile and set (value) = _outputFile <- value + override this.Execute() = try match _outputFile with @@ -159,5 +162,5 @@ type WriteCodeFragment() as this = File.WriteAllText(fileName, codeText) _outputFile <- outputFileItem not this.Log.HasLoggedErrors - with - | TaskFailed -> false + with TaskFailed -> + false