/
ExtensionMethods.cs
241 lines (208 loc) · 8.74 KB
/
ExtensionMethods.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Mail;
using System.Runtime.Versioning;
using System.Security.Principal;
using System.ServiceModel.Activation;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.WebPages;
using NuGet;
namespace NuGetGallery
{
public static class ExtensionMethods
{
public static void MapServiceRoute(
this RouteCollection routes,
string routeName,
string routeUrl,
Type serviceType)
{
var serviceRoute = new ServiceRoute(routeUrl, new DataServiceHostFactory(), serviceType);
serviceRoute.Defaults = new RouteValueDictionary { { "serviceType", "odata" } };
serviceRoute.Constraints = new RouteValueDictionary { { "serviceType", "odata" } };
routes.Add(routeName, serviceRoute);
}
public static string ToStringSafe(this object obj)
{
if (obj != null)
{
return obj.ToString();
}
return String.Empty;
}
public static string Flatten(this IEnumerable<string> list)
{
if (list == null)
return String.Empty;
return String.Join(", ", list.ToArray());
}
public static string Flatten(this ICollection<PackageAuthor> authors)
{
return authors.Select(a => a.Name).Flatten();
}
public static string Flatten(this IEnumerable<PackageDependencySet> dependencySets)
{
var dependencies = new List<dynamic>();
foreach (var dependencySet in dependencySets)
{
if (dependencySet.Dependencies.Count == 0)
dependencies.Add(new
{
Id = (string)null,
VersionSpec = (string)null,
TargetFramework = dependencySet.TargetFramework == null ? null : VersionUtility.GetShortFrameworkName(dependencySet.TargetFramework)
});
else
foreach (var dependency in dependencySet.Dependencies.Select(d => new { d.Id, d.VersionSpec, dependencySet.TargetFramework }))
dependencies.Add(new
{
dependency.Id,
VersionSpec = dependency.VersionSpec == null ? null : dependency.VersionSpec.ToString(),
TargetFramework = dependency.TargetFramework == null ? null : VersionUtility.GetShortFrameworkName(dependency.TargetFramework)
});
}
return FlattenDependencies(dependencies);
}
public static string Flatten(this ICollection<PackageDependency> dependencies)
{
return FlattenDependencies(dependencies.Select(d => new { d.Id, VersionSpec = d.VersionSpec.ToStringSafe(), TargetFramework = d.TargetFramework.ToStringSafe() }));
}
static string FlattenDependencies(IEnumerable<dynamic> dependencies)
{
return String.Join("|", dependencies.Select(d => String.Format(CultureInfo.InvariantCulture, "{0}:{1}:{2}", d.Id, d.VersionSpec, d.TargetFramework)));
}
public static HelperResult Flatten<T>(this IEnumerable<T> items, Func<T, HelperResult> template)
{
if (items == null)
{
return null;
}
var formattedItems = items.Select(item => template(item).ToHtmlString());
return new HelperResult(writer =>
{
writer.Write(String.Join(", ", formattedItems.ToArray()));
});
}
public static bool AnySafe<T>(this IEnumerable<T> items)
{
if (items == null)
{
return false;
}
return items.Any();
}
public static bool AnySafe<T>(this IEnumerable<T> items, Func<T, bool> predicate)
{
if (items == null)
{
return false;
}
return items.Any(predicate);
}
public static bool IsOwner(this Package package, IPrincipal user)
{
return package.PackageRegistration.IsOwner(user);
}
public static bool IsOwner(this Package package, User user)
{
return package.PackageRegistration.IsOwner(user);
}
public static bool IsOwner(this PackageRegistration package, IPrincipal user)
{
if (package == null)
{
throw new ArgumentNullException("package");
}
if (user == null || user.Identity == null)
{
return false;
}
return user.IsInRole(Constants.AdminRoleName) || package.Owners.Any(u => u.Username == user.Identity.Name);
}
public static bool IsOwner(this PackageRegistration package, User user)
{
if (package == null)
{
throw new ArgumentNullException("package");
}
if (user == null)
{
return false;
}
return package.Owners.Any(u => u.Key == user.Key);
}
// apple polish!
public static string CardinalityLabel(this int count, string singular, string plural)
{
return count == 1 ? singular : plural;
}
public static bool IsInThePast(this DateTime? date)
{
return date.Value.IsInThePast();
}
public static bool IsInThePast(this DateTime date)
{
return date < DateTime.UtcNow;
}
public static IQueryable<T> SortBy<T>(this IQueryable<T> source, string sortExpression)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
int descIndex = sortExpression.IndexOf(" desc", StringComparison.OrdinalIgnoreCase);
if (descIndex != -1)
{
sortExpression = sortExpression.Substring(0, descIndex).Trim();
}
if (String.IsNullOrEmpty(sortExpression))
{
return source;
}
ParameterExpression parameter = Expression.Parameter(source.ElementType);
Expression property = sortExpression.Split('.')
.Aggregate<string, Expression>(parameter, Expression.Property);
LambdaExpression lambda = Expression.Lambda(property, parameter);
string methodName = descIndex == -1 ? "OrderBy" : "OrderByDescending";
Expression methodCallExpression = Expression.Call(typeof(Queryable),
methodName,
new Type[] {
source.ElementType,
property.Type
},
source.Expression,
Expression.Quote(lambda));
return source.Provider.CreateQuery<T>(methodCallExpression);
}
public static MailAddress ToMailAddress(this User user)
{
return new MailAddress(user.EmailAddress, user.Username);
}
public static bool IsError<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
var metadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
var modelState = htmlHelper.ViewData.ModelState[metadata.PropertyName];
return modelState != null && modelState.Errors != null && modelState.Errors.Count > 0;
}
public static string ToShortNameOrNull(this FrameworkName frameworkName)
{
return frameworkName == null ? null : VersionUtility.GetShortFrameworkName(frameworkName);
}
public static string ToFriendlyName(this FrameworkName frameworkName)
{
if (frameworkName == null)
throw new ArgumentNullException("frameworkName");
var sb = new StringBuilder();
sb.AppendFormat("{0} {1}", frameworkName.Identifier, frameworkName.Version);
if (string.IsNullOrEmpty(frameworkName.Profile))
sb.AppendFormat(" {0}", frameworkName.Profile);
return sb.ToString();
}
}
}