Skip to content

Commit

Permalink
+Simplified notification for stable versions
Browse files Browse the repository at this point in the history
Also updates namespaces for ddNS
  • Loading branch information
3F committed Apr 15, 2020
1 parent 848e19b commit 1305599
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 29 deletions.
49 changes: 49 additions & 0 deletions Wizard/Extensions/ThreadingExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2020 Denis Kuzmin < x-3F@outlook.com > GitHub/3F
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System.Threading;
using System.Threading.Tasks;

namespace net.r_eg.DllExport.Wizard.Extensions
{
internal static class ThreadingExtension
{
internal static CancellationTokenSource CancelAndResetIfRunning(this CancellationTokenSource cts, Task task, int signalLimit)
{
var ret = new CancellationTokenSource();
if(cts == null) {
return ret;
}

if(cts.Token.CanBeCanceled == true && task?.Status == TaskStatus.Running)
{
cts.Cancel();
task.Wait(signalLimit);
}

cts.Dispose();
return ret;
}
}
}
43 changes: 33 additions & 10 deletions Wizard/PackageInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using net.r_eg.MvsSln.Extensions;
using net.r_eg.MvsSln.Log;
Expand All @@ -40,16 +41,22 @@ internal class PackageInfo

public string Activated => exec.Config.PkgVer;

public Task<IEnumerable<string>> GetFromGitHubAsync()
=> GetFromRemoteAsync("https://3F.github.io/DllExport/data/pkgrel");
public Task<IEnumerable<string>> GetFromGitHubAsync(CancellationToken ct = default)
=> GetFromRemoteAsync("https://3F.github.io/DllExport/data/pkgrel", ct);

public Task<IEnumerable<string>> GetFromRemoteAsync(string url)
=> RcvStringOrActivatedAsync(url)
.ContinueWith(r => Detect301(r.Result))
public Task<IEnumerable<string>> GetFromRemoteAsync(string url, CancellationToken ct = default)
=> RcvStringOrActivatedAsync(url, ct)
.ContinueWith(r => Detect301(r.Result, ct))
.ContinueWith(r => Regex.Matches(r.Result, @"^\s*([^#\r\n].+)$", RegexOptions.Multiline)
.Cast<Match>()
.Select(x => x.Groups[1].Value));

internal bool IsNewStableVersionFrom(IEnumerable<string> versions, out Version found)
{
found = FindStableVersion(versions);
return (found != null && found > new Version(Activated));
}

public PackageInfo(IExecutor exec)
{
this.exec = exec ?? throw new ArgumentNullException(nameof(exec));
Expand All @@ -58,13 +65,27 @@ public PackageInfo(IExecutor exec)
DefineSecurityProtocol();
}

protected Version FindStableVersion(IEnumerable<string> versions)
{
foreach(var ver in versions ?? Enumerable.Empty<string>())
{
// No RC or beta releases
if(Version.TryParse(ver, out Version remote))
{
return remote;
}
}

return null;
}

/// <summary>
/// Emulates an emergency 301 through special command due to unsupported servers like GitHub pages.
/// Format: `@301=url` EOF
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
protected string Detect301(string input)
protected string Detect301(string input, CancellationToken ct)
{
const string R301 = "@301=";

Expand All @@ -74,14 +95,14 @@ protected string Detect301(string input)
var url = input.Substring(R301.Length);

LSender.Send(this, $"{R301}{url}", Message.Level.Debug);
return RcvStringOrActivatedAsync(url).Result;
return RcvStringOrActivatedAsync(url, ct).Result;
}

protected Task<string> RcvStringOrActivatedAsync(string target)
=> RcvStringAsync(target, (ex) => Activated);
protected Task<string> RcvStringOrActivatedAsync(string target, CancellationToken ct)
=> RcvStringAsync(target, (ex) => Activated, ct);

// while we're still using .netfx 4.0
protected Task<string> RcvStringAsync(string target, Func<Exception, string> failed)
protected Task<string> RcvStringAsync(string target, Func<Exception, string> failed, CancellationToken ct)
{
var tcs = new TaskCompletionSource<string>();
var url = new Uri(target);
Expand Down Expand Up @@ -120,6 +141,8 @@ protected Task<string> RcvStringAsync(string target, Func<Exception, string> fai

wc.DownloadProgressChanged += (sender, e) => bytesReceived = e.BytesReceived;

ct.Register(() => wc.CancelAsync());

LSender.Send(this, $"Get data: {url}", Message.Level.Debug);
wc.DownloadStringAsync(url);
return tcs.Task;
Expand Down
12 changes: 11 additions & 1 deletion Wizard/UI/ConfiguratorForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 53 additions & 16 deletions Wizard/UI/ConfiguratorForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ internal sealed partial class ConfiguratorForm: Form, IRender
private readonly IConfFormater confFormater;
private int prevSlnItemIndex = 0;
private volatile bool _suspendCbSln;
private readonly string updaterInitName;
private CancellationTokenSource ctsUpdater;
private Task tUpdater;
private readonly object sync = new object();

private string UpdToVersion => cbPackages.Text.Trim();
Expand Down Expand Up @@ -92,6 +95,7 @@ public ConfiguratorForm(IExecutor exec)

InitializeComponent();

updaterInitName = tabUpdating.Text;
Text = GetVersionInfo();

projectItems.Browse =
Expand Down Expand Up @@ -130,28 +134,61 @@ private void ConfiguratorForm_Load(object sender, EventArgs e)

private void UpdateListOfPackages()
{
const int _ANI_DELAY = 550; //ms

if(tUpdater != null && tUpdater.Status != TaskStatus.Running
&& !(tUpdater.IsCompleted || tUpdater.IsCanceled || tUpdater.IsFaulted)) { return; }

ctsUpdater = ctsUpdater.CancelAndResetIfRunning(tUpdater, _ANI_DELAY * 2);

tUpdater?.Dispose();
cbPackages.Items.Clear();
((Control)tabUpdating).Enabled = false;

Task.Factory
.StartNew(() => pkgVer.GetFromGitHubAsync())
.ContinueWith(t =>
{
var rctask = t.Result;
var releases = rctask.Result.ToArray();
tUpdater = Task.Factory
.StartNew(() => pkgVer.GetFromGitHubAsync(ctsUpdater.Token), ctsUpdater.Token)
.ContinueWith(t =>
{
var rctask = t.Result;
var releases = rctask.Result.ToArray();

cbPackages.UIAction(x => x.Items.AddRange(releases));
cbPackages.UIAction(x => x.Items.AddRange(releases));

int pos = cbPackages.FindString(pkgVer.Activated);
cbPackages.UIAction(x =>
int pos = cbPackages.FindString(pkgVer.Activated);
cbPackages.UIAction(x =>
{
if(pos == -1) {
x.Text = pkgVer.Activated;
}
else {
x.SelectedIndex = pos;
}
});
return releases;

}, ctsUpdater.Token)
.ContinueWith(t =>
{
if(pos == -1) {
x.Text = pkgVer.Activated;
}
else {
x.SelectedIndex = pos;
tabUpdating.UIAction(x => x.Enabled = true);
if(!pkgVer.IsNewStableVersionFrom(t.Result, out Version remote)) {
return;
}
});
});

tabUpdating.UIBlinkText
(
_ANI_DELAY,
$" Up to {remote}",
ctsUpdater.Token,
"^....",
".^...",
"..^..",
"...^.",
"....^",
"..... "
);
tabUpdating.UIAction(x => x.Text = updaterInitName);

}, ctsUpdater.Token);
}

private string GetVersionInfo(bool urlinfo = true)
Expand Down
15 changes: 15 additions & 0 deletions Wizard/UI/Extensions/ControlExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

using System;
using System.Threading;
using System.Windows.Forms;
using net.r_eg.Conari.Accessors.WinAPI;

Expand Down Expand Up @@ -124,5 +125,19 @@ internal static void AppendData(this TextBox control, string text, bool newline
control.SelectionStart = control.Text.Length;
control.ScrollToCaret();
}

internal static void UIBlinkText(this Control ctrl, int delay, string text, CancellationToken ct, params string[] effects)
{
while(!ct.IsCancellationRequested)
{
foreach(var ef in effects)
{
ctrl.UIAction(x => x.Text = ef + text);
Thread.Sleep(delay);

if(ct.IsCancellationRequested) { return; }
}
}
}
}
}
5 changes: 3 additions & 2 deletions Wizard/UserConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ public UserConfig(IWizardConfig cfg)

Namespaces = new List<string>() {
NS_DEFAULT_VALUE,
"RGiesecke.DllExport",
"net.r_eg.DllExport"
"net.r_eg.DllExport",
"com.github._3F.DllExport",
string.Empty, //https://github.com/3F/DllExport/issues/47
};
}

Expand Down
1 change: 1 addition & 0 deletions Wizard/Wizard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Reference Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<Compile Include="Extensions\ThreadingExtension.cs" />
<Compile Include="PackageInfo.cs" />
<Compile Include="Caller.cs" />
<Compile Include="Guids.cs" />
Expand Down

0 comments on commit 1305599

Please sign in to comment.