forked from DotNetOpenAuth/DotNetOpenAuth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopyWithTokenSubstitution.cs
115 lines (99 loc) · 4.32 KB
/
CopyWithTokenSubstitution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//-----------------------------------------------------------------------
// <copyright file="CopyWithTokenSubstitution.cs" company="Outercurve Foundation">
// Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.BuildTasks {
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
/// <summary>
/// Copies files and performs a search and replace for given tokens in their contents during the process.
/// </summary>
public class CopyWithTokenSubstitution : Task {
/// <summary>
/// Gets or sets the files to copy.
/// </summary>
/// <value>The files to copy.</value>
[Required]
public ITaskItem[] SourceFiles { get; set; }
/// <summary>
/// Gets or sets a list of files to copy the source files to.
/// </summary>
/// <value>The list of files to copy the source files to.</value>
[Required]
public ITaskItem[] DestinationFiles { get; set; }
/// <summary>
/// Gets or sets the destination files actually copied to.
/// </summary>
/// <remarks>
/// In the case of error partway through, or files not copied due to already being up to date,
/// this can be a subset of the <see cref="DestinationFiles"/> array.
/// </remarks>
[Output]
public ITaskItem[] CopiedFiles { get; set; }
/// <summary>
/// Executes this instance.
/// </summary>
/// <returns><c>true</c> if the operation was successful.</returns>
public override bool Execute() {
if (this.SourceFiles.Length != this.DestinationFiles.Length) {
Log.LogError("{0} inputs and {1} outputs given.", this.SourceFiles.Length, this.DestinationFiles.Length);
return false;
}
var copiedFiles = new List<ITaskItem>(this.DestinationFiles.Length);
for (int i = 0; i < this.SourceFiles.Length; i++) {
string sourcePath = this.SourceFiles[i].ItemSpec;
string destPath = this.DestinationFiles[i].ItemSpec;
bool skipUnchangedFiles;
bool.TryParse(this.SourceFiles[i].GetMetadata("SkipUnchangedFiles"), out skipUnchangedFiles);
if (!Directory.Exists(Path.GetDirectoryName(destPath))) {
Directory.CreateDirectory(Path.GetDirectoryName(destPath));
}
if (string.IsNullOrEmpty(this.SourceFiles[i].GetMetadata("BeforeTokens"))) {
// this is just a standard copy without token substitution
if (skipUnchangedFiles && File.GetLastWriteTimeUtc(sourcePath) == File.GetLastWriteTimeUtc(destPath)) {
Log.LogMessage(MessageImportance.Low, "Skipping \"{0}\" -> \"{1}\" because the destination is up to date.", sourcePath, destPath);
continue;
}
Log.LogMessage(MessageImportance.Normal, "Copying file from \"{0}\" to \"{1}\"", sourcePath, destPath);
File.Copy(sourcePath, destPath, true);
} else {
// We deliberably consider newer destination files to be up-to-date rather than
// requiring equality because this task modifies the destination file while copying.
if (skipUnchangedFiles && File.GetLastWriteTimeUtc(sourcePath) < File.GetLastWriteTimeUtc(destPath)) {
Log.LogMessage(MessageImportance.Low, "Skipping \"{0}\" -> \"{1}\" because the destination is up to date.", sourcePath, destPath);
continue;
}
Log.LogMessage(MessageImportance.Normal, "Transforming \"{0}\" -> \"{1}\"", sourcePath, destPath);
string[] beforeTokens = this.SourceFiles[i].GetMetadata("BeforeTokens").Split(';');
string[] afterTokens = this.SourceFiles[i].GetMetadata("AfterTokens").Split(';');
if (beforeTokens.Length != afterTokens.Length) {
Log.LogError("Unequal number of before and after tokens. Before: \"{0}\". After \"{1}\".", beforeTokens, afterTokens);
return false;
}
using (StreamReader sr = File.OpenText(sourcePath)) {
using (StreamWriter sw = File.CreateText(destPath)) {
StringBuilder line = new StringBuilder();
while (!sr.EndOfStream) {
line.Length = 0;
line.Append(sr.ReadLine());
for (int j = 0; j < beforeTokens.Length; j++) {
line.Replace(beforeTokens[j], afterTokens[j]);
}
sw.WriteLine(line);
}
}
}
}
copiedFiles.Add(this.DestinationFiles[i]);
}
this.CopiedFiles = copiedFiles.ToArray();
return true;
}
}
}