Skip to content

Commit

Permalink
Replace Bintray repo with Cloudsmith (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Mar 24, 2021
1 parent 6551547 commit b9ddf42
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 33 deletions.
2 changes: 1 addition & 1 deletion nanoFirmwareFlasher/CC13x26x2Firmware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace nanoFramework.Tools.FirmwareFlasher
{
/// <summary>
/// Class that handles the download of STM32 firmware files from Bintray.
/// Class that handles the download of STM32 firmware files from Cloudsmith.
/// </summary>
internal class CC13x26x2Firmware : FirmwarePackage
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
namespace nanoFramework.Tools.FirmwareFlasher
{
[Serializable]
internal class BintrayPackageInfo
internal class CloudsmithPackageInfo
{
[JsonProperty("latest_version")]
public string LatestVersion { get; set; }
[JsonProperty("version")]
public string Version { get; set; }

[JsonProperty("cdn_url")]
public string DownloadUrl { get; set; }
}
}
2 changes: 1 addition & 1 deletion nanoFirmwareFlasher/Esp32Firmware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace nanoFramework.Tools.FirmwareFlasher
{
/// <summary>
/// Class that handles the download of ESP32 firmware files from Bintray.
/// Class that handles the download of ESP32 firmware files from Cloudsmith.
/// </summary>
internal class Esp32Firmware : FirmwarePackage
{
Expand Down
4 changes: 2 additions & 2 deletions nanoFirmwareFlasher/ExitCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ public enum ExitCodes : int
E9004 = 9004,

/// <summary>
/// Can't find the target in the Bintray repository.
/// Can't find the target in Cloudsmith repository.
/// </summary>
[Display(Name = "Can't find the target in the Bintray repository.")]
[Display(Name = "Can't find the target in Cloudsmith repository.")]
E9005 = 9005,

/// <summary>
Expand Down
52 changes: 32 additions & 20 deletions nanoFirmwareFlasher/FirmwarePackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Web;

namespace nanoFramework.Tools.FirmwareFlasher
{
/// <summary>
/// Abstract base class that handles the download and extraction of firmware file from Bintray.
/// Abstract base class that handles the download and extraction of firmware file from Cloudsmith.
/// </summary>
internal abstract class FirmwarePackage : IDisposable
{
// HttpClient is intended to be instantiated once per application, rather than per-use.
static HttpClient _bintrayClient = new HttpClient();
static HttpClient _cloudsmithClient = new HttpClient();

/// <summary>
/// Uri of Bintray API
/// Uri of Cloudsmith API
/// </summary>
internal const string _bintrayApiPackages = "https://api.bintray.com/packages/nfbot";
internal const string _cloudsmithPackages = "https://api.cloudsmith.io/v1/packages/net-nanoframework";

internal const string _refTargetsDevRepo = "nanoframework-images-dev";
internal const string _refTargetsStableRepo = "nanoframework-images";
Expand Down Expand Up @@ -68,9 +69,17 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
{
string fwFileName = null;

// query URL
// https://api.cloudsmith.io/v1/packages/net-nanoframework/REPO-NAME-HERE/?page=1&query=/PACKAGE-NAME-HERE latest

// download URL
// https://dl.cloudsmith.io/public/net-nanoframework/REPO-NAME-HERE/raw/names/PACKAGE-NAME-HERE/versions/VERSION-HERE/ST_STM32F429I_DISCOVERY-1.6.2-preview.9.zip

// reference targets
var repoName = _stable ? _refTargetsStableRepo : _refTargetsDevRepo;
string requestUri = $"{_bintrayApiPackages}/{repoName}/{_targetName}";
string requestUri = $"{_cloudsmithPackages}/{repoName}/?page=1&query={_targetName} latest";

string downloadUrl = string.Empty;

// flag to signal if the work-flow step was successful
bool stepSuccesful = false;
Expand Down Expand Up @@ -134,9 +143,12 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
Console.Write($"Trying to find {_targetName} in {(_stable ? "stable" : "developement")} repository...");
}

HttpResponseMessage response = await _bintrayClient.GetAsync(requestUri);
HttpResponseMessage response = await _cloudsmithClient.GetAsync(requestUri);

if (response.StatusCode == HttpStatusCode.NotFound)
var responseBody = await response.Content.ReadAsStringAsync();

// check for empty array
if (responseBody == "[]")
{
if (Verbosity >= VerbosityLevel.Normal)
{
Expand All @@ -145,12 +157,12 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
}

// try with community targets
requestUri = $"{_bintrayApiPackages}/{_communityTargetsepo}/{_targetName}";
requestUri = $"{_cloudsmithPackages}/{_communityTargetsepo}/?page=1&query={_targetName} latest";
repoName = _communityTargetsepo;

response = await _bintrayClient.GetAsync(requestUri);
response = await _cloudsmithClient.GetAsync(requestUri);

if (response.StatusCode == HttpStatusCode.NotFound)
if (responseBody == "[]")
{
if (Verbosity >= VerbosityLevel.Normal)
{
Expand All @@ -167,24 +179,26 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
Console.WriteLine($"OK");
}

// read and parse response
string responseBody = await response.Content.ReadAsStringAsync();
BintrayPackageInfo packageInfo = JsonConvert.DeserializeObject<BintrayPackageInfo>(responseBody);
// parse response
List<CloudsmithPackageInfo> packageInfo = JsonConvert.DeserializeObject<List<CloudsmithPackageInfo>>(responseBody);

// if no specific version was requested, use latest available
if (string.IsNullOrEmpty(_fwVersion))
{
_fwVersion = packageInfo.LatestVersion;
_fwVersion = packageInfo.ElementAt(0).Version;
}

// grab download URL
downloadUrl = packageInfo.ElementAt(0).DownloadUrl;

// set exposed property
Version = _fwVersion;

stepSuccesful = true;
}
catch
{
// exception with download, assuming it's something with network connection or Bintray API
// exception with download, assuming it's something with network connection or Cloudsmith API
}
}

Expand Down Expand Up @@ -222,9 +236,7 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
try
{
// setup and perform download request
requestUri = $"https://dl.bintray.com/nfbot/{repoName}/{fwFileName}";

using (var fwFileResponse = await _bintrayClient.GetAsync(requestUri))
using (var fwFileResponse = await _cloudsmithClient.GetAsync(downloadUrl))
{
if (fwFileResponse.IsSuccessStatusCode)
{
Expand Down Expand Up @@ -253,7 +265,7 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
}
catch
{
// exception with download, assuming it's something with network connection or Bintray API
// exception with download, assuming it's something with network connection or Cloudsmith API
}
}
else
Expand Down
2 changes: 1 addition & 1 deletion nanoFirmwareFlasher/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public class Options
"target",
Required = false,
Default = null,
HelpText = "Target name. This is the target name used in the GitHub and Bintray repositories.")]
HelpText = "Target name. This is the target name used in the GitHub and Cloudsmith repositories.")]
public string TargetName { get; set; }

[Option(
Expand Down
6 changes: 3 additions & 3 deletions nanoFirmwareFlasher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
else
{
// other supported platforms will go here
// in case a wacky target is entered by the user, the package name will be checked against Bintray repo
// in case a wacky target is entered by the user, the package name will be checked against Cloudsmith repo
}
}

Expand Down Expand Up @@ -587,7 +587,7 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
// update operation requested?
if (o.Update)
{
// this to update the device with fw from Bintray
// this to update the device with fw from Cloudsmith

// need to take care of flash address
string appFlashAddress = null;
Expand Down Expand Up @@ -681,7 +681,7 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
// update operation requested?
if (o.Update)
{
// this to update the device with fw from Bintray
// this to update the device with fw from Cloudsmith

// need to take care of flash address
string appFlashAddress = null;
Expand Down
2 changes: 1 addition & 1 deletion nanoFirmwareFlasher/Stm32Firmware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace nanoFramework.Tools.FirmwareFlasher
{
/// <summary>
/// Class that handles the download of STM32 firmware files from Bintray.
/// Class that handles the download of STM32 firmware files from Cloudsmith.
/// </summary>
internal class Stm32Firmware : FirmwarePackage
{
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.18.0-preview.{height}",
"version": "1.19.0-preview.{height}",
"assemblyVersion": {
"precision": "revision"
},
Expand Down

0 comments on commit b9ddf42

Please sign in to comment.