-
Notifications
You must be signed in to change notification settings - Fork 4
/
inline-task.cs
137 lines (111 loc) · 5.09 KB
/
inline-task.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18051
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace InlineCode {
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
public class ReadPublishIgnoreFile : Microsoft.Build.Utilities.Task {
private bool _Success = true;
public virtual bool Success {
get {
return _Success;
}
set {
_Success = value;
}
}
private string _FilePath;
public virtual string FilePath {
get {
return _FilePath;
}
set {
_FilePath = value;
}
}
private string[] _LinesFromFile;
public virtual string[] LinesFromFile {
get {
return _LinesFromFile;
}
set {
_LinesFromFile = value;
}
}
public override bool Execute() {
// read the file line by line and exclude any lines which start with # or
// just contain whitespace
Log.LogMessage("Starting to read publish.ignore file at [{0}]", this.FilePath);
if (!File.Exists(FilePath)) {
string msg = string.Format("Unable to find the publish.ignore file at [{0}]",this.FilePath);
Log.LogError(msg);
return false;
}
// TODO: do this better
string[] allLinesRaw = File.ReadAllLines(this.FilePath);
// System.Collections.Generic.List<ITaskItem> linesNotComments = new System.Collections.Generic.List<ITaskItem>();
System.Collections.Generic.List<string> linesNotComments = new System.Collections.Generic.List<string>();
foreach(string line in allLinesRaw){
if (string.IsNullOrEmpty(line))
continue;
// trim the line and see if it starts with #
string pattern = line;
if (pattern.StartsWith("#"))
continue;
#region ConvertPatternIfNecessary - if this was defiend in an assembly this would be it's own method
if (!string.IsNullOrEmpty(pattern)) {
pattern = pattern.Trim();
if (pattern.StartsWith("!"))
{
throw new NotSupportedException("The ! operator is not currently supported in publish.ignore");
}
// for patterns that match file and that do not start with \ or / we should prepend **\
if (!(pattern.EndsWith(@"\") || pattern.EndsWith(@"/"))) {
if (!(pattern.StartsWith(@"\") || pattern.StartsWith(@"/"))) {
pattern = string.Format(@"**\{0}",pattern);
}
}
// for file patterns that start with \ or / we should
// remove the leading slash because MSBuild assumes that
// don't match \\servername\file.name
string fileThatStartsWithASlashPattern = @"^[\\,/]{1}[^\\,/].*\.[^/]*";
if (System.Text.RegularExpressions.Regex.IsMatch(pattern, fileThatStartsWithASlashPattern)) {
// \sample.txt or \folder\sub\somefile.txt
pattern = pattern.Substring(1);
}
// if its a directory we should append **\* to the end
if (pattern.EndsWith(@"/") || pattern.EndsWith(@"\"))
{
pattern = string.Format(@"{0}**\*", pattern);
}
}
#endregion
// add it to the list to be returned
// linesNotComments.Add(new TaskItem(pattern));
linesNotComments.Add(pattern);
}
// doesn't work from an inline task for some reason
// this.LinesFromFile = linesNotComments.ToArray();
this.LinesFromFile = new ITaskItem[linesNotComments.Count];
//this.LinesFromFile = new string [linesNotComments.Count];
for (int i = 0; i < linesNotComments.Count; i++) {
this.LinesFromFile[i] = new TaskItem(linesNotComments[i]);
}
Log.LogMessage("Finished reading publish.ignore file at [{0}]. Found [{0}] lines which are not comments or blank.", this.FilePath, this.LinesFromFile.Length);
return !Log.HasLoggedErrors;
return _Success;
}
}
}