Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
build: UpdateInvertedDependencies: stop on success, add logging
Browse files Browse the repository at this point in the history
Make sure we stop on the first source that is successful when fetching
nuspecs to avoid extra unnecessary HTTP.

Add task logging.
  • Loading branch information
abock committed Jun 26, 2018
1 parent 0428c7c commit fd20029
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 24 deletions.
100 changes: 76 additions & 24 deletions build/Xamarin.Build/MSBuild/UpdateInvertedDependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,33 @@ sealed class NuGetResources
public NuGetResource [] Resources { get; set; }
}

HttpClient httpClient;

(Stream stream, string contentType) HttpGet (string uri)
{
if (httpClient == null)
httpClient = new HttpClient ();

Log.LogMessage ("-> HTTP GET {0}", uri);

var response = httpClient.GetAsync (uri).GetAwaiter ().GetResult ();

Log.LogMessage (
" {0} ({1}) {2}: {3} {4} bytes",
response.StatusCode,
(int)response.StatusCode,
response.ReasonPhrase,
response.Content.Headers.ContentType,
response.Content.Headers.ContentLength);

response.EnsureSuccessStatusCode ();

return (
response.Content.ReadAsStreamAsync ().GetAwaiter ().GetResult (),
response.Content.Headers.ContentType.MediaType
);
}

IEnumerable<InvertedDependency> GetNuGetDependencies ()
{
var packagesToProjects = new Dictionary<PackageReference, List<Project>> ();
Expand Down Expand Up @@ -130,8 +157,8 @@ void DownloadNuSpec (PackageReference packageReference)
.Where (source => source.StartsWith ("E ", StringComparison.Ordinal))
.Select (source => source.Substring (2))
.Select (source => {
using (var client = new HttpClient ())
using (var stream = client.GetStreamAsync (source).GetAwaiter ().GetResult ())
var (stream, contentType) = HttpGet (source);
using (stream)
using (var streamReader = new StreamReader (stream))
using (var jsonReader = new JsonTextReader (streamReader))
return serializer
Expand All @@ -151,13 +178,15 @@ void DownloadNuSpec (PackageReference packageReference)
var uri = $"{baseUri}{relativePath.Replace (Path.DirectorySeparatorChar, '/')}";

try {
using (var client = new HttpClient ())
using (var stream = client.GetStreamAsync (uri).GetAwaiter ().GetResult ()) {
var (stream, contentType) = HttpGet (uri);
using (stream) {
Directory.CreateDirectory (Path.GetDirectoryName (localPath));
using (var fileStream = File.Create (localPath))
stream.CopyTo (fileStream);
}
} catch {

break;
} catch (HttpRequestException) {
}
}
}
Expand All @@ -176,14 +205,15 @@ void DownloadNuSpec (PackageReference packageReference)
.OrderBy (pr => pr)
.Distinct ()
.Select (packageReference => {
var invertedDependency = new InvertedDependency {
Kind = DependencyKind.NuGet,
Name = packageReference.Id,
Version = packageReference.Version,
DependentProjects = packagesToProjects [packageReference]
.Select (p => Path.GetFileNameWithoutExtension (p.FullPath))
.ToArray ()
};
var invertedDependency = InvertedDependency.Create (
this,
DependencyKind.NuGet,
packageReference.Id,
packageReference.Version);
invertedDependency.DependentProjects = packagesToProjects [packageReference]
.Select (p => Path.GetFileNameWithoutExtension (p.FullPath))
.ToArray ();
var nuspecPath = GetNuSpecPath (packageReference);
if (nuspecPath == null) {
Expand All @@ -192,6 +222,7 @@ void DownloadNuSpec (PackageReference packageReference)
}
if (nuspecPath != null) {
Log.LogMessage ("-> Parsing nuspec {0}", nuspecPath);
var nuspec = XDocument.Load (nuspecPath);
var ns = nuspec.Root.GetDefaultNamespace ();
var metadata = nuspec.Root.Element (ns + "metadata");
Expand All @@ -213,11 +244,12 @@ IEnumerable<InvertedDependency> GetNpmDependencies ()
.Root
.Element ("dependencies")
.Descendants ()
.Select (e => new InvertedDependency {
Kind = DependencyKind.Npm,
Name = e.Name.ToString (),
Version = e.Value
})).OrderBy (e => e.Name).ThenBy (e => e.Version);
.Select (e => InvertedDependency.Create (
this,
DependencyKind.Npm,
e.Name.ToString (),
e.Value))
).OrderBy (e => e.Name).ThenBy (e => e.Version);
}

IEnumerable<InvertedDependency> GetGitSubmoduleDependencies ()
Expand All @@ -230,12 +262,12 @@ IEnumerable<InvertedDependency> GetGitSubmoduleDependencies ()
if (parts == null || parts.Length < 2)
continue;

yield return new InvertedDependency {
Kind = DependencyKind.GitSubmodule,
Name = parts [1],
Version = parts [0]
};
};
yield return InvertedDependency.Create (
this,
DependencyKind.GitSubmodule,
parts [1],
parts [0]);
}
}

public override bool Execute ()
Expand Down Expand Up @@ -321,6 +353,26 @@ sealed class InvertedDependency
public string [] DependentProjects { get; set; }
public string LicenseUrl { get; set; }
public string ProjectUrl { get; set; }

public static InvertedDependency Create (
Task task,
DependencyKind kind,
string name,
string version)
{
task.Log.LogMessage (
MessageImportance.High,
"Processing {0} {1}/{2}",
kind,
name,
version);

return new InvertedDependency {
Kind = kind,
Name = name,
Version = version
};
}
}

struct PackageReference : IEquatable<PackageReference>, IComparable<PackageReference>
Expand Down
Binary file modified build/Xamarin.Build/Xamarin.Build.dll
Binary file not shown.

0 comments on commit fd20029

Please sign in to comment.