-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExtensions.cs
102 lines (101 loc) · 3.27 KB
/
Extensions.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
using Cake.Core.IO;
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace Dnn.CakeUtils
{
public static class Extensions
{
public static string ToNormalizedVersion(this Version version)
{
return string.Format("{0:00}.{1:00}.{2:00}", version.Major, version.Minor, version.Build);
}
public static string ToNormalizedVersion(this string version)
{
return new Version(version).ToNormalizedVersion();
}
public static string ToShortVersion(this string version)
{
return new Version(version).ToString();
}
public static string Otherwise(this string input, string alternative)
{
return string.IsNullOrEmpty(input) ? alternative : input;
}
public static string Serialize<T>(this T value)
{
if (value == null)
{
return string.Empty;
}
try
{
var xmlserializer = new XmlSerializer(typeof(T));
var stringWriter = new StringWriter();
using (var writer = XmlWriter.Create(stringWriter))
{
xmlserializer.Serialize(writer, value);
return stringWriter.ToString();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
throw new Exception("An error occurred", ex);
}
}
public static XmlDocument ToXmlDocument(this string value)
{
var res = new XmlDocument();
res.LoadXml(value);
return res;
}
public static bool Contains(this FilePathCollection input, FilePath filePath)
{
foreach (var fp in input)
{
if (fp.FullPath == filePath.FullPath)
{
return true;
}
}
return false;
}
public static string EnsureStartsWith(this string input, string start)
{
if (!input.StartsWith(start))
{
return start + input;
}
return input;
}
public static string EnsureEndsWith(this string input, string end)
{
if (!input.EndsWith(end))
{
return input + end;
}
return input;
}
public static string NoSlashes(this string input)
{
return input.Replace('/', '.').Replace('\\', '.');
}
public static Project FindProjectForPath(this FilePath input, Solution solution)
{
var fileFolder = System.IO.Path.GetDirectoryName(input.FullPath);
foreach (var pf in solution.dnn.projectFolders)
{
var projectPath = System.IO.Path.Combine(solution.BasePath, pf.Replace("/", "\\"));
if (fileFolder.StartsWith(projectPath, StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("Bingo");
return solution.dnn.projects[pf];
}
}
return null;
}
}
}