Replace smart-download of GCM Core with dumb-download

We get rate limited querying the GitHub API for the release assets to
download GCM Core for Mac. Replace this with a simple hardcoding of the
GCM Core macOS installer package URL.
This commit is contained in:
Matthew John Cheetham 2019-10-30 12:40:06 +00:00
Родитель 31d4dabefa
Коммит 9325c729e9
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: C64BA810D2B517ED
3 изменённых файлов: 10 добавлений и 192 удалений

Просмотреть файл

@ -3,30 +3,35 @@
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<DistributionOutputPath>$(ProjectOutPath)dist\$(Configuration)\</DistributionOutputPath>
<GcmCoreReleaseTag>v2.0.79-beta</GcmCoreReleaseTag>
<GcmCorePackageFilename>gcmcore-osx-2.0.79.64449.pkg</GcmCorePackageFilename>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Scalar.Installer.Mac\Scalar.Installer.Mac.csproj" ReferenceOutputAssembly="false" Private="false" />
<PackageReference Include="GitForMac.GVFS.Installer" />
<GitHubAssetReference Include="microsoft/Git-Credential-Manager-Core" Version="v2.0.79-beta" Filter=".*\.pkg" PropertyName="GcmCoreAssets" />
</ItemGroup>
<Target Name="BuildDistribution" AfterTargets="Publish" Condition="'$(OSPlatform)' == 'osx'">
<!-- Download GCM Core -->
<DownloadFile DestinationFolder="$(DistributionOutputPath)\GCM"
SourceUrl="https://github.com/microsoft/Git-Credential-Manager-Core/releases/download/$(GcmCoreReleaseTag)/$(GcmCorePackageFilename)"
SkipUnchangedFiles="true" />
<!-- Copy Scalar and Git packages to distribution output directory -->
<ItemGroup>
<ScalarPackage Include="$(RepoOutPath)Scalar.Installer.Mac\installer\$(Configuration)\*.pkg" LinkBase="Scalar" />
<GitPackage Include="$(PkgGitForMac_GVFS_Installer)\tools\*.pkg" LinkBase="Git" />
<GcmCorePackage Include="$(GcmCoreAssets)\*.pkg" LinkBase="GCM" />
</ItemGroup>
<Copy SourceFiles="@(ScalarPackage);@(GitPackage);@(GcmCorePackage)"
<Copy SourceFiles="@(ScalarPackage);@(GitPackage)"
DestinationFolder="$(DistributionOutputPath)%(LinkBase)"
SkipUnchangedFiles="true" />
<!-- Generate installation script -->
<PropertyGroup>
<ScalarPackageFilename>%(ScalarPackage.Filename)%(ScalarPackage.Extension)</ScalarPackageFilename>
<GitPackageFilename>%(GitPackage.Filename)%(GitPackage.Extension)</GitPackageFilename>
<GcmCorePackageFilename>%(GcmCorePackage.Filename)%(GcmCorePackage.Extension)</GcmCorePackageFilename>
</PropertyGroup>
<ItemGroup>
<ScriptTemplate Include="InstallScalar.template.sh" TargetPath="InstallScalar.sh"
Properties="##SCALAR_INSTALLER_PKG_PLACEHOLDER##=$(ScalarPackageFilename);

Просмотреть файл

@ -1,159 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Scalar.MSBuild
{
public class GetGitHubReleaseAssets : Task
{
private const string GitHubApiBase = "https://api.github.com/";
[Required]
public string Repository { get; set; }
[Required]
public string Version { get; set; }
public string Filter { get; set; }
[Output]
public ITaskItem[] Assets { get; set; }
public override bool Execute()
{
var assetItems = new List<ITaskItem>();
ReleaseInfo releaseInfo;
try
{
this.Log.LogMessage(MessageImportance.Normal, $"Getting release information for '{this.Repository}' (tag: '{this.Version}')");
releaseInfo = GetReleaseInfo(this.Repository, this.Version);
}
catch (Exception ex)
{
var errorMessage = new StringBuilder();
errorMessage.AppendLine($"Failed to retrieve release information for '{this.Repository}' (tag: {this.Version}):");
errorMessage.AppendLine(ex.ToString());
if (ex is WebException wex)
{
using (var rs = wex.Response.GetResponseStream())
{
if (rs != null)
{
using (var reader = new StreamReader(rs))
{
errorMessage.AppendLine("Response:");
errorMessage.Append(reader.ReadToEnd());
}
}
}
}
this.Log.LogError(errorMessage.ToString());
return false;
}
this.Log.LogMessage(MessageImportance.Low, $"Release contains {releaseInfo.Assets.Length} assets:");
if (!string.IsNullOrWhiteSpace(this.Filter))
{
this.Log.LogMessage(MessageImportance.Low, $"(Filtering assets with pattern: '{this.Filter}')");
}
foreach (AssetInfo asset in releaseInfo.Assets)
{
if (!string.IsNullOrWhiteSpace(this.Filter) && !Regex.IsMatch(asset.Name, this.Filter))
{
this.Log.LogMessage(MessageImportance.Low, $" [skipped] {asset.Name} ({asset.Size} bytes)");
}
else
{
this.Log.LogMessage(MessageImportance.Low, $" [include] {asset.Name} ({asset.Size} bytes)");
var item = new TaskItem(asset.Name, new Dictionary<string, string>
{
["Repository"] = this.Repository,
["Version"] = this.Version,
["Size"] = asset.Size.ToString(),
["Url"] = asset.DownloadUrl
});
assetItems.Add(item);
}
}
this.Assets = assetItems.ToArray();
return true;
}
private ReleaseInfo GetReleaseInfo(string repository, string version)
{
var client = new WebClient
{
Headers = new WebHeaderCollection
{
{HttpRequestHeader.UserAgent, "MSBuild"},
{HttpRequestHeader.Accept, "application/json"}
}
};
Uri releaseTagUri = CreateApiUri($"repos/{repository}/releases/tags/{version}");
this.Log.LogMessage(MessageImportance.Low, $"GET {releaseTagUri}");
string releaseJson = client.DownloadString(releaseTagUri);
this.Log.LogMessage(MessageImportance.Low, $"Response: {releaseJson}");
return Deserialize<ReleaseInfo>(releaseJson);
}
private static Uri CreateApiUri(string uriStem)
{
return new Uri(new Uri(GitHubApiBase), uriStem);
}
private static T Deserialize<T>(string json)
{
var serializer = new DataContractJsonSerializer(typeof(T), new DataContractJsonSerializerSettings
{
DateTimeFormat = new DateTimeFormat("yyyy-MM-dd'T'HH:mm:ssZ")
});
byte[] bytes = Encoding.UTF8.GetBytes(json);
using (var ms = new MemoryStream(bytes))
{
return (T)serializer.ReadObject(ms);
}
}
#pragma warning disable CS0649
[DataContract]
private class ReleaseInfo
{
[DataMember(Name = "assets")]
public AssetInfo[] Assets;
}
[DataContract]
private class AssetInfo
{
[DataMember(Name = "name")]
public string Name;
[DataMember(Name = "browser_download_url")]
public string DownloadUrl;
[DataMember(Name = "size")]
public int Size;
}
#pragma warning restore CS0649
}
}

Просмотреть файл

@ -20,32 +20,4 @@
<FileWrites Include="$(IntermediateOutputPath)app.manifest" />
</ItemGroup>
</Target>
<!-- Location to cache GitHub assets -->
<PropertyGroup>
<GitHubAssetsPath Condition="'$(GitHubAssetsPath)' == '' AND '$(OS)' != 'Windows_NT'">$(HOME)\.githubassets\</GitHubAssetsPath>
<GitHubAssetsPath Condition="'$(GitHubAssetsPath)' == '' AND '$(OS)' == 'Windows_NT'">$(USERPROFILE)\.githubassets\</GitHubAssetsPath>
</PropertyGroup>
<!-- Download GitHub assets before build -->
<Target Name="DownloadGitHubReleaseAssets"
AfterTargets="BeforeBuild"
Inputs="@(GitHubAssetReference)"
Outputs="%(GitHubAssetReference.PropertyName);$(GitHubAssetsPath)\%(GitHubAssetReference.Identity)\%(GitHubAssetReference.Version)\*">
<CreateProperty Value="$(GitHubAssetsPath)\%(GitHubAssetReference.Identity)\%(GitHubAssetReference.Version)\">
<Output TaskParameter="Value" PropertyName="%(GitHubAssetReference.PropertyName)" />
</CreateProperty>
<GetGitHubReleaseAssets Repository="%(GitHubAssetReference.Identity)"
Version="%(GitHubAssetReference.Version)"
Filter="%(GitHubAssetReference.Filter)">
<Output TaskParameter="Assets" ItemName="_Asset" />
</GetGitHubReleaseAssets>
<DownloadFile DestinationFolder="$(GitHubAssetsPath)\%(_Asset.Repository)\%(_Asset.Version)"
SourceUrl="%(_Asset.Url)"
SkipUnchangedFiles="true">
<Output TaskParameter="DownloadedFile" ItemName="GitHubAsset" />
</DownloadFile>
</Target>
</Project>