Refactor PublishTargets.cs, add publishing of all installers and archives to azure, change all versions to nuget versions.

This commit is contained in:
Bryan Thornbury 2016-03-23 16:37:59 -07:00
parent 7486b45eeb
commit f596b8ddce
5 changed files with 339 additions and 143 deletions

View file

@ -0,0 +1,96 @@
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using Microsoft.DotNet.Cli.Build.Framework;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using static Microsoft.DotNet.Cli.Build.Framework.BuildHelpers;
namespace Microsoft.DotNet.Cli.Build
{
public class AzurePublisher
{
private static readonly string s_dotnetBlobRootUrl = "https://dotnetcli.blob.core.windows.net/dotnet/";
private static readonly string s_dotnetBlobContainerName = "dotnet";
private string _connectionString { get; set; }
private CloudBlobContainer _blobContainer { get; set; }
public AzurePublisher()
{
_connectionString = Environment.GetEnvironmentVariable("CONNECTION_STRING").Trim('"');
_blobContainer = GetDotnetBlobContainer(_connectionString);
}
private CloudBlobContainer GetDotnetBlobContainer(string connectionString)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
return blobClient.GetContainerReference(s_dotnetBlobContainerName);
}
public void PublishInstallerFileAndLatest(string installerFile, string channel, string version)
{
var installerFileBlob = CalculateInstallerBlob(installerFile, channel, version);
var latestInstallerFileName = installerFile.Replace(version, "latest");
var latestInstallerFileBlob = CalculateInstallerBlob(latestInstallerFileName, channel, "Latest");
PublishFile(installerFileBlob, installerFile);
PublishFile(latestInstallerFileBlob, installerFile);
}
public void PublishArchiveAndLatest(string archiveFile, string channel, string version)
{
var archiveFileBlob = CalculateArchiveBlob(archiveFile, channel, version);
var latestArchiveFileName = archiveFile.Replace(version, "latest");
var latestArchiveFileBlob = CalculateArchiveBlob(latestArchiveFileName, channel, "Latest");
PublishFile(archiveFileBlob, archiveFile);
PublishFile(latestArchiveFileBlob, archiveFile);
}
public void PublishFile(string blob, string file)
{
CloudBlockBlob blockBlob = _blobContainer.GetBlockBlobReference(blob);
blockBlob.UploadFromFileAsync(file, FileMode.Open).Wait();
SetBlobPropertiesBasedOnFileType(blockBlob);
}
private void SetBlobPropertiesBasedOnFileType(CloudBlockBlob blockBlob)
{
if (Path.GetExtension(blockBlob.Uri.AbsolutePath.ToLower()) == ".svg")
{
blockBlob.Properties.ContentType = "image/svg+xml";
blockBlob.Properties.CacheControl = "no-cache";
blockBlob.SetPropertiesAsync().Wait();
}
}
public string CalculateInstallerUploadUrl(string installerFile, string channel, string version)
{
return $"{s_dotnetBlobRootUrl}{CalculateInstallerBlob(installerFile, channel, version)}";
}
public string CalculateInstallerBlob(string installerFile, string channel, string version)
{
return $"{channel}/Installers/{version}/{Path.GetFileName(installerFile)}";
}
public string CalculateArchiveUploadUrl(string archiveFile, string channel, string version)
{
return $"{s_dotnetBlobRootUrl}{CalculateArchiveBlob(archiveFile, channel, version)}";
}
public string CalculateArchiveBlob(string archiveFile, string channel, string version)
{
return $"{channel}/Binaries/{version}/{Path.GetFileName(archiveFile)}";
}
}
}

View file

@ -0,0 +1,54 @@
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using Microsoft.DotNet.Cli.Build.Framework;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using static Microsoft.DotNet.Cli.Build.Framework.BuildHelpers;
namespace Microsoft.DotNet.Cli.Build
{
public class DebRepoPublisher
{
private string _repoID;
private string _uploadJsonDirectory;
public DebRepoPublisher(string uploadJsonDirectory)
{
_uploadJsonDirectory = uploadJsonDirectory;
_repoID = Environment.GetEnvironmentVariable("REPO_ID");
}
public void PublishDebFileToDebianRepo(string packageName, string packageVersion, string uploadUrl)
{
var uploadJson = GenerateUploadJsonFile(packageName, packageVersion, uploadUrl);
Cmd(Path.Combine(Dirs.RepoRoot, "scripts", "publish", "repoapi_client.sh"), "-addpkg", uploadJson)
.Execute()
.EnsureSuccessful();
}
private string GenerateUploadJsonFile(string packageName, string packageVersion, string uploadUrl)
{
var uploadJson = Path.Combine(_uploadJsonDirectory, "package_upload.json");
File.Delete(uploadJson);
using (var fileStream = File.Create(uploadJson))
{
using (StreamWriter sw = new StreamWriter(fileStream))
{
sw.WriteLine("{");
sw.WriteLine($" \"name\":\"{packageName}\",");
sw.WriteLine($" \"version\":\"{packageVersion}\",");
sw.WriteLine($" \"repositoryId\":\"{_repoID}\",");
sw.WriteLine($" \"sourceUrl\":\"{uploadUrl}\"");
sw.WriteLine("}");
}
}
return uploadJson;
}
}
}