|
| 1 | +// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. |
| 2 | + |
| 3 | +namespace FSharp.Build |
| 4 | + |
| 5 | +open System |
| 6 | +open System.Collections |
| 7 | +open System.IO |
| 8 | +open Microsoft.Build.Framework |
| 9 | +open Microsoft.Build.Utilities |
| 10 | + |
| 11 | +type SubstituteText () = |
| 12 | + |
| 13 | + let mutable _buildEngine : IBuildEngine = null |
| 14 | + let mutable _hostObject : ITaskHost = null |
| 15 | + |
| 16 | + let mutable copiedFiles = new ResizeArray<ITaskItem>() |
| 17 | + let mutable embeddedResources : ITaskItem[] = [||] |
| 18 | + |
| 19 | + [<Required>] |
| 20 | + member this.EmbeddedResources |
| 21 | + with get() = embeddedResources |
| 22 | + and set(value) = embeddedResources <- value |
| 23 | + |
| 24 | + [<Output>] |
| 25 | + member this.CopiedFiles |
| 26 | + with get() = copiedFiles.ToArray() |
| 27 | + |
| 28 | + interface ITask with |
| 29 | + member this.BuildEngine |
| 30 | + with get() = _buildEngine |
| 31 | + and set(value) = _buildEngine <- value |
| 32 | + |
| 33 | + member this.HostObject |
| 34 | + with get() = _hostObject |
| 35 | + and set(value) = _hostObject <- value |
| 36 | + |
| 37 | + member this.Execute() = |
| 38 | + copiedFiles.Clear() |
| 39 | + if not(isNull embeddedResources) then |
| 40 | + for item in embeddedResources do |
| 41 | + // Update ITaskItem metadata to point to new location |
| 42 | + let sourcePath = item.GetMetadata("FullPath") |
| 43 | + |
| 44 | + let pattern1 = item.GetMetadata("Pattern1") |
| 45 | + let pattern2 = item.GetMetadata("Pattern2") |
| 46 | + |
| 47 | + // Is there any replacement to do? |
| 48 | + if not (String.IsNullOrWhiteSpace(pattern1) && String.IsNullOrWhiteSpace(pattern2)) then |
| 49 | + if not(String.IsNullOrWhiteSpace(sourcePath)) then |
| 50 | + try |
| 51 | + let getTargetPathFrom key = |
| 52 | + let md = item.GetMetadata(key) |
| 53 | + let path = Path.GetDirectoryName(md) |
| 54 | + let filename = Path.GetFileName(md) |
| 55 | + let target = Path.Combine(path, @"..\resources", filename) |
| 56 | + target |
| 57 | + |
| 58 | + // Copy from the location specified in Identity |
| 59 | + let sourcePath=item.GetMetadata("Identity") |
| 60 | + |
| 61 | + // Copy to the location specified in TargetPath unless no TargetPath is provided, then use Identity |
| 62 | + let targetPath= |
| 63 | + let identityPath = getTargetPathFrom "Identity" |
| 64 | + let intermediateTargetPath = item.GetMetadata("IntermediateTargetPath") |
| 65 | + if not (String.IsNullOrWhiteSpace(intermediateTargetPath)) then |
| 66 | + let filename = Path.GetFileName(identityPath) |
| 67 | + let target = Path.Combine(intermediateTargetPath, filename) |
| 68 | + target |
| 69 | + else |
| 70 | + identityPath |
| 71 | + |
| 72 | + item.ItemSpec <- targetPath |
| 73 | + |
| 74 | + // Transform file |
| 75 | + let mutable contents = File.ReadAllText(sourcePath) |
| 76 | + if not (String.IsNullOrWhiteSpace(pattern1)) then |
| 77 | + let replacement = item.GetMetadata("Replacement1") |
| 78 | + contents <- contents.Replace(pattern1, replacement) |
| 79 | + if not (String.IsNullOrWhiteSpace(pattern2)) then |
| 80 | + let replacement = item.GetMetadata("Replacement2") |
| 81 | + contents <- contents.Replace(pattern2, replacement) |
| 82 | + |
| 83 | + let directory = Path.GetDirectoryName(targetPath) |
| 84 | + if not(Directory.Exists(directory)) then |
| 85 | + Directory.CreateDirectory(directory) |>ignore |
| 86 | + |
| 87 | + File.WriteAllText(targetPath, contents) |
| 88 | + with |
| 89 | + | _ -> () |
| 90 | + |
| 91 | + copiedFiles.Add(item) |
| 92 | + true |
0 commit comments