forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathUtils.cs
More file actions
139 lines (118 loc) · 4.76 KB
/
Copy pathPathUtils.cs
File metadata and controls
139 lines (118 loc) · 4.76 KB
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
138
139
// Copyright (c) Service Stack LLC. All Rights Reserved.
// License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using ServiceStack.Text;
namespace ServiceStack
{
public static class PathUtils
{
public static string MapAbsolutePath(this string relativePath, string appendPartialPathModifier)
{
return PclExport.Instance.MapAbsolutePath(relativePath, appendPartialPathModifier);
}
/// <summary>
/// Maps the path of a file in the context of a VS project
/// </summary>
/// <param name="relativePath">the relative path</param>
/// <returns>the absolute path</returns>
/// <remarks>Assumes static content is two directories above the /bin/ directory,
/// eg. in a unit test scenario the assembly would be in /bin/Debug/.</remarks>
public static string MapProjectPath(this string relativePath)
{
var mapPath = MapAbsolutePath(relativePath, string.Format("{0}..{0}..", PclExport.Instance.DirSep));
return mapPath;
}
/// <summary>
/// Maps the path of a file in a self-hosted scenario
/// </summary>
/// <param name="relativePath">the relative path</param>
/// <returns>the absolute path</returns>
/// <remarks>Assumes static content is copied to /bin/ folder with the assemblies</remarks>
public static string MapAbsolutePath(this string relativePath)
{
var mapPath = MapAbsolutePath(relativePath, null);
return mapPath;
}
/// <summary>
/// Maps the path of a file in an Asp.Net hosted scenario
/// </summary>
/// <param name="relativePath">the relative path</param>
/// <returns>the absolute path</returns>
/// <remarks>Assumes static content is in the parent folder of the /bin/ directory</remarks>
public static string MapHostAbsolutePath(this string relativePath)
{
var mapPath = MapAbsolutePath(relativePath, string.Format("{0}..", PclExport.Instance.DirSep));
return mapPath;
}
internal static string CombinePaths(StringBuilder sb, params string[] paths)
{
AppendPaths(sb, paths);
return sb.ToString();
}
public static void AppendPaths(StringBuilder sb, string[] paths)
{
foreach (var path in paths)
{
if (string.IsNullOrEmpty(path))
continue;
if (sb.Length > 0 && sb[sb.Length - 1] != '/')
sb.Append("/");
sb.Append(path.Replace('\\', '/').TrimStart('/'));
}
}
public static string CombinePaths(params string[] paths)
{
var sb = StringBuilderThreadStatic.Allocate();
AppendPaths(sb, paths);
return StringBuilderThreadStatic.ReturnAndFree(sb);
}
public static string AssertDir(this string dirPath)
{
if (!dirPath.DirectoryExists())
dirPath.CreateDirectory();
return dirPath;
}
public static string CombineWith(this string path, params string[] thesePaths)
{
if (path == null)
path = "";
if (thesePaths.Length == 1 && thesePaths[0] == null) return path;
var startPath = path.Length > 1 ? path.TrimEnd('/', '\\') : path;
var sb = StringBuilderThreadStatic.Allocate();
sb.Append(startPath);
AppendPaths(sb, thesePaths);
return StringBuilderThreadStatic.ReturnAndFree(sb);
}
public static string CombineWith(this string path, params object[] thesePaths)
{
if (thesePaths.Length == 1 && thesePaths[0] == null) return path;
var sb = StringBuilderThreadStatic.Allocate();
sb.Append(path.TrimEnd('/', '\\'));
AppendPaths(sb, ToStrings(thesePaths));
return StringBuilderThreadStatic.ReturnAndFree(sb);
}
public static string[] ToStrings(object[] thesePaths)
{
var to = new string[thesePaths.Length];
for (var i = 0; i < thesePaths.Length; i++)
{
to[i] = thesePaths[i].ToString();
}
return to;
}
internal static List<To> Map<To>(System.Collections.IEnumerable items, Func<object, To> converter)
{
if (items == null)
return new List<To>();
var list = new List<To>();
foreach (var item in items)
{
list.Add(converter(item));
}
return list;
}
}
}