forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackageManagerModule.cs
More file actions
168 lines (148 loc) · 6.51 KB
/
Copy pathPackageManagerModule.cs
File metadata and controls
168 lines (148 loc) · 6.51 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
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Web.Hosting;
namespace System.Web.WebPages.Administration.PackageManager
{
internal static class PackageManagerModule
{
private const string DefaultSourceUrl = @"http://go.microsoft.com/fwlink/?LinkID=226946";
private const string NuGetSourceUrl = @"http://go.microsoft.com/fwlink/?LinkID=226948";
internal static readonly string PackageSourceFilePath = VirtualPathUtility.Combine(SiteAdmin.AdminSettingsFolder, "PackageSources.config");
private static readonly object _sourceFileLock = new object();
private static readonly IEnumerable<WebPackageSource> _defaultSources = new[]
{
new WebPackageSource(name: PackageManagerResources.DefaultPackageSourceName, source: DefaultSourceUrl) { FilterPreferredPackages = true },
new WebPackageSource(name: PackageManagerResources.NuGetFeed, source: NuGetSourceUrl) { FilterPreferredPackages = false }
};
private static readonly PackageSourceFile _sourceFile = new PackageSourceFile(PackageSourceFilePath);
private static ISet<WebPackageSource> _packageSources;
public static IEnumerable<WebPackageSource> PackageSources
{
get
{
Debug.Assert(_packageSources != null, "InitFeedsFile must be called before Feeds can be accessed.");
return _packageSources.Any() ? _packageSources : _defaultSources;
}
}
/// <summary>
/// Gets the first available PackageSource
/// </summary>
public static WebPackageSource ActiveSource
{
get { return PackageSources.First(); }
}
public static IEnumerable<WebPackageSource> DefaultSources
{
get { return _defaultSources; }
}
public static string ModuleName
{
get { return PackageManagerResources.ModuleTitle; }
}
public static string ModuleDescription
{
get { return PackageManagerResources.ModuleDesc; }
}
internal static string SiteRoot
{
get { return HostingEnvironment.MapPath("~/"); }
}
internal static bool Available
{
get { return (HttpContext.Current != null) && HttpContext.Current.Request.IsLocal; }
}
public static bool InitPackageSourceFile()
{
return InitPackageSourceFile(_sourceFile, ref _packageSources);
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Any exception that occurs indicates that the source file cannot be initialized.")]
public static bool InitPackageSourceFile(IPackagesSourceFile sourceFile, ref ISet<WebPackageSource> packageSources)
{
if (packageSources != null)
{
return true;
}
try
{
lock (_sourceFileLock)
{
// This method is invoked from Page_Start and ensures we have a feed file to read/write.
// The call needs to be guarded against multiple simultaneous requests.
if (!sourceFile.Exists())
{
packageSources = new HashSet<WebPackageSource>(_defaultSources);
sourceFile.WriteSources(_packageSources);
}
else
{
packageSources = new HashSet<WebPackageSource>(sourceFile.ReadSources());
}
}
}
catch
{
return false;
}
return true;
}
public static WebPackageSource GetSource(string sourceName)
{
Debug.Assert(_packageSources != null, "InitFeedsFile must be called before this method can be called.");
return GetSource(PackageSources, sourceName);
}
public static WebPackageSource GetSource(IEnumerable<WebPackageSource> packageSources, string sourceName)
{
lock (_sourceFileLock)
{
return packageSources.Where(source => source.Name.Equals(sourceName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
}
}
public static bool AddPackageSource(string source, string name)
{
Debug.Assert(_packageSources != null, "InitFeedsFile must be called before this method can be called.");
name = String.IsNullOrEmpty(name) ? source.ToString() : name;
var packageSource = new WebPackageSource(source: source, name: name);
return AddPackageSource(_sourceFile, _packageSources, packageSource);
}
public static bool AddPackageSource(WebPackageSource packageSource)
{
Debug.Assert(!String.IsNullOrEmpty(packageSource.Name) && !String.IsNullOrEmpty(packageSource.Source));
return AddPackageSource(_sourceFile, _packageSources, packageSource);
}
public static bool AddPackageSource(IPackagesSourceFile sourceFile, ISet<WebPackageSource> packageSources, WebPackageSource packageSource)
{
if (GetSource(packageSources, packageSource.Name) != null)
{
return false;
}
lock (_sourceFileLock)
{
packageSources.Add(packageSource);
sourceFile.WriteSources(packageSources);
}
return true;
}
public static void RemovePackageSource(string sourceName)
{
Debug.Assert(_packageSources != null, "InitFeedsFile must be called before this method can be called.");
RemovePackageSource(_sourceFile, _packageSources, sourceName);
}
public static void RemovePackageSource(IPackagesSourceFile sourceFile, ISet<WebPackageSource> packageSources, string name)
{
var packageSource = GetSource(packageSources, name);
lock (_sourceFileLock)
{
if (packageSource == null)
{
return;
}
packageSources.Remove(packageSource);
sourceFile.WriteSources(packageSources);
}
}
}
}