forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NuGetUtils.cs
86 lines (76 loc) · 2.94 KB
/
NuGetUtils.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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NuGet.Frameworks;
using NuGet.Packaging.Core;
using NuGet.ProjectModel;
namespace Microsoft.NET.Build.Tasks
{
internal static class NuGetUtils
{
public static bool IsPlaceholderFile(string path)
{
return string.Equals(Path.GetFileName(path), PackagingCoreConstants.EmptyFolder, StringComparison.Ordinal);
}
public static IEnumerable<LockFileItem> FilterPlaceHolderFiles(this IEnumerable<LockFileItem> files)
{
return files.Where(f => !IsPlaceholderFile(f.Path));
}
public static string GetLockFileLanguageName(string projectLanguage)
{
switch (projectLanguage)
{
case "C#": return "cs";
case "F#": return "fs";
default: return projectLanguage?.ToLowerInvariant();
}
}
public static NuGetFramework ParseFrameworkName(string frameworkName)
{
return frameworkName == null ? null : NuGetFramework.Parse(frameworkName);
}
/// <summary>
/// Gets PackageId from sourcePath.
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public static string GetPackageIdFromSourcePath(string sourcePath)
{
string packageId, unused;
GetPackageParts(sourcePath, out packageId, out unused);
return packageId;
}
/// <summary>
/// Gets PackageId and package subpath from source path
/// </summary>
/// <param name="fullPath">full path to package file</param>
/// <param name="packageId">package ID</param>
/// <param name="packageSubPath">subpath of asset within package</param>
public static void GetPackageParts(string fullPath, out string packageId, out string packageSubPath)
{
packageId = null;
packageSubPath = null;
try
{
// this method is just a temporary heuristic until we flow the NuGet metadata through the right items
// https://github.com/dotnet/sdk/issues/1091
for (var dir = Directory.GetParent(fullPath); dir != null; dir = dir.Parent)
{
var nuspecs = dir.GetFiles("*.nuspec");
if (nuspecs.Length > 0)
{
packageId = Path.GetFileNameWithoutExtension(nuspecs[0].Name);
packageSubPath = fullPath.Substring(dir.FullName.Length + 1).Replace('\\', '/');
break;
}
}
}
catch (Exception)
{ }
return;
}
}
}