-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathSwiftComponent.cs
More file actions
76 lines (65 loc) · 2.83 KB
/
SwiftComponent.cs
File metadata and controls
76 lines (65 loc) · 2.83 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
#nullable disable
namespace Microsoft.ComponentDetection.Contracts.TypedComponent;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using PackageUrl;
/// <summary>
/// Represents a Swift package manager component.
/// </summary>
public class SwiftComponent : TypedComponent
{
private readonly Uri packageUrl;
private readonly string hash;
/// <summary>
/// Initializes a new instance of the <see cref="SwiftComponent"/> class.
/// </summary>
/// <param name="name">The name of the component.</param>
/// <param name="version">The version of the component.</param>
/// <param name="packageUrl">The package URL of the component.</param>
/// <param name="hash">The hash of the component.</param>
public SwiftComponent(string name, string version, string packageUrl, string hash)
{
this.Name = this.ValidateRequiredInput(name, nameof(name), nameof(ComponentType.Swift));
this.Version = this.ValidateRequiredInput(version, nameof(version), nameof(ComponentType.Swift));
this.ValidateRequiredInput(packageUrl, nameof(packageUrl), nameof(ComponentType.Swift));
this.packageUrl = new Uri(packageUrl);
this.hash = this.ValidateRequiredInput(hash, nameof(hash), nameof(ComponentType.Swift));
}
[JsonPropertyName("name")]
public string Name { get; }
[JsonPropertyName("version")]
public string Version { get; }
[JsonIgnore]
public override ComponentType Type => ComponentType.Swift;
// Example PackageUrl -> pkg:swift/github.com/apple/swift-asn1
// type: swift
// namespace: github.com/apple
// name: swift-asn1
[JsonPropertyName("packageUrl")]
public override PackageURL PackageUrl => new PackageURL(
type: "swift",
@namespace: this.GetNamespaceFromPackageUrl(),
name: this.Name,
version: this.Version,
qualifiers: new SortedDictionary<string, string>
{
{ "repository_url", this.packageUrl.AbsoluteUri },
},
subpath: null);
protected override string ComputeBaseId() => $"{this.Name} {this.Version} - {this.Type}";
private string GetNamespaceFromPackageUrl()
{
// In the case of github.com, the namespace should contain the user/organization
// See https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst#swift
var uppercaseHost = this.packageUrl.Host.ToUpperInvariant();
if (uppercaseHost.Contains("GITHUB.COM"))
{
// The first segment of the URL will contain the user or organization for GitHub
var firstSegment = this.packageUrl.Segments[1].Trim('/');
return $"{this.packageUrl.Host}/{firstSegment}";
}
// In the default case of a generic host, the namespace should be the just the host
return this.packageUrl.Host;
}
}