-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement GetArtifactDownloadUris in the BaseProjectManager (#329)
* Implement GetArtifactDownloadUris in the BaseProjectManager to get any relevant URIs associated with a package version. Implement it in NPM, NuGet and PyPI. Create tests for it, and create a new PyPIProjectManagerTests class. Fix scoped NPM name handling. Scoped npm purls should have the namespace prefixed with %40. * Create an ArtifactUri struct that contains the Type, Uri, and Extension. This gets used instead of string for the IEnumerable returned in GetArtifactDownloadUris. * Added remarks to PackageUrlExtension.HasNamespace to explain that it will just return the given namespace if it isn't an NPM package. * Added a remark in BaseProjectManager.GetArtifactDownloadUris that clarify that this method doesn't check that the returning URI(s) actually exist. * Added a generic to ArtifactUri<T> where T is the enum for the artifact type. TypedManager takes in another generic `TArtifactUriType` that has to be an enum. Create a NoOpPackageActions to use with NPM and PyPI as we don't have PackageActions implementations for them yet. Remove GetArtifactDownloadUris from BaseProjectManager and moved it to the TypedManager. NPM and PyPI now implement TypedManager. * Remove extension from ArtifactUri constructor as it wasn't being used anyways. It now gets calculated from the Uri.AbsolutePath Added the nuspecUri for NuGet packages as well. Added UriExistsAsync logic to TypedManager. * Created a new Extensions helper "UriExtension" that has a method GetExtension in it, so that way we can get special extensions such as ".tar.gz" and ".tar.bz2" because the Path.GetExtension method would only return ".gz" or ".bz2" respectively. * Add a TODO to track merging the ArtifactUriType into the PackageVersionMetadata.
- Loading branch information
Showing
21 changed files
with
530 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) Microsoft Corporation. Licensed under the MIT License. | ||
|
||
namespace Microsoft.CST.OpenSource.Extensions; | ||
|
||
using System; | ||
using System.IO; | ||
|
||
public static class UriExtension | ||
{ | ||
private static readonly string[] SpecialExtensions = { ".tar.gz", ".tar.bz2" }; | ||
|
||
/// <summary> | ||
/// Gets the extension from a <see cref="Uri.AbsolutePath"/>. | ||
/// </summary> | ||
/// <param name="uri">The <see cref="Uri"/> to get the extension from.</param> | ||
/// <returns>The extension from the <paramref name="uri"/>, or an empty string if it doesn't have one.</returns> | ||
public static string GetExtension(this Uri uri) | ||
{ | ||
string absolutePath = uri.AbsolutePath; | ||
|
||
foreach (string specialExtension in SpecialExtensions) | ||
{ | ||
if (absolutePath.EndsWith(specialExtension)) | ||
{ | ||
return specialExtension; | ||
} | ||
} | ||
|
||
return Path.GetExtension(absolutePath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) Microsoft Corporation. Licensed under the MIT License. | ||
|
||
namespace Microsoft.CST.OpenSource.Model; | ||
|
||
using Extensions; | ||
using System; | ||
using System.IO; | ||
|
||
/// <summary> | ||
/// A record to represent the type, uri, and extension for an artifact associated with a package. | ||
/// </summary> | ||
/// <typeparam name="T">The enum to represent the artifact type.</typeparam> | ||
public record ArtifactUri<T> where T : Enum | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of <see cref="ArtifactUri{T}"/>. | ||
/// </summary> | ||
/// <param name="type">The type of artifact for this <see cref="ArtifactUri{T}"/>.</param> | ||
/// <param name="uri">The <see cref="Uri"/> this artifact can be found at.</param> | ||
public ArtifactUri(T type, Uri uri) | ||
{ | ||
Type = type; | ||
Uri = uri; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of <see cref="ArtifactUri{T}"/>. | ||
/// </summary> | ||
/// <param name="type">The type of artifact for this <see cref="ArtifactUri{T}"/>.</param> | ||
/// <param name="uri">The string of the uri this artifact can be found at.</param> | ||
public ArtifactUri(T type, string uri) : this(type, new Uri(uri)) { } | ||
|
||
/// <summary> | ||
/// The enum representing the artifact type for the project manager associated with this artifact. | ||
/// </summary> | ||
public T Type { get; } | ||
|
||
/// <summary> | ||
/// The <see cref="Uri"/> for where this artifact can be found online. | ||
/// </summary> | ||
public Uri Uri { get; } | ||
|
||
/// <summary> | ||
/// The file extension for this artifact file. Including the '.' at the beginning. | ||
/// </summary> | ||
/// <remarks>If the file has no extension, it will just be an empty string.</remarks> | ||
public string Extension => Uri.GetExtension(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) Microsoft Corporation. Licensed under the MIT License. | ||
|
||
namespace Microsoft.CST.OpenSource.PackageActions; | ||
|
||
using Contracts; | ||
using PackageUrl; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
public class NoOpPackageActions : IManagerPackageActions<IManagerPackageVersionMetadata> | ||
{ | ||
public Task<string?> DownloadAsync( | ||
PackageURL packageUrl, | ||
string topLevelDirectory, | ||
string targetPath, | ||
bool doExtract, | ||
bool cached = false, | ||
CancellationToken cancellationToken = default) => Task.FromResult<string?>(null); | ||
|
||
public Task<bool> DoesPackageExistAsync( | ||
PackageURL packageUrl, | ||
bool useCache = true, | ||
CancellationToken cancellationToken = default) => Task.FromResult(false); | ||
|
||
public Task<IEnumerable<string>> GetAllVersionsAsync( | ||
PackageURL packageUrl, | ||
bool includePrerelease = true, | ||
bool useCache = true, | ||
CancellationToken cancellationToken = default) => Task.FromResult(Enumerable.Empty<string>()); | ||
|
||
public Task<string?> GetLatestVersionAsync( | ||
PackageURL packageUrl, | ||
bool includePrerelease = false, | ||
bool useCache = true, | ||
CancellationToken cancellationToken = default) => Task.FromResult<string?>(null); | ||
|
||
public Task<IManagerPackageVersionMetadata?> GetMetadataAsync( | ||
PackageURL packageUrl, | ||
bool useCache = true, | ||
CancellationToken cancellationToken = default) => Task.FromResult<IManagerPackageVersionMetadata?>(null); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.