Skip to content

Commit

Permalink
Fixed #22 Check for updates on FXB start
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Rapp committed Mar 17, 2015
1 parent abe3ef7 commit c5cc598
Show file tree
Hide file tree
Showing 6 changed files with 451 additions and 82 deletions.
159 changes: 82 additions & 77 deletions FetchXmlBuilder/AppCode/GithubVersionChecker.cs
Expand Up @@ -5,72 +5,77 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Web.Script.Serialization;

namespace Cinteros.Xrm.FetchXmlBuilder.AppCode
{
internal class GithubVersionChecker
{
private static int currentMajorVersion;
private static int currentMinorVersion;
private static int currentBuildVersion;
private static int currentRevisionVersion;
public GithubVersionChecker(string currentVersion)
{
var versionParts = currentVersion.Split('.');
currentMajorVersion = int.Parse(versionParts[0]);
currentMinorVersion = int.Parse(versionParts[1]);
currentBuildVersion = int.Parse(versionParts[2]);
currentRevisionVersion = int.Parse(versionParts[3]);
}

public static GithubInformation Cpi { get; set; }

public void Run()
{
RunAsync().Wait();
}

static async Task RunAsync()
{
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.github.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("User-Agent", "Cinteros");

HttpResponseMessage response =
await
client.GetAsync("repos/Cinteros/FetchXMLBuilder/releases")
.ConfigureAwait(continueOnCapturedContext: false);
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsStringAsync();
var jSserializer = new JavaScriptSerializer();
var releases = jSserializer.Deserialize<List<RootObject>>(data.Result);

var lastRelease = releases.OrderByDescending(r => r.created_at).FirstOrDefault();
if (lastRelease != null)
{
var version = lastRelease.tag_name.Replace("v.", "");
var versionParts = version.Split('.');
int majorVersion = int.Parse(versionParts[0]);
int minorVersion = int.Parse(versionParts[1]);
int buildVersion = int.Parse(versionParts[2]);
int revisionVersion = int.Parse(versionParts[3]);

if (currentMajorVersion < majorVersion
|| currentMajorVersion == majorVersion && currentMinorVersion < minorVersion
||
currentMajorVersion == majorVersion && currentMinorVersion == minorVersion &&
currentBuildVersion < buildVersion
||
currentMajorVersion == majorVersion && currentMinorVersion == minorVersion &&
currentBuildVersion == buildVersion && currentRevisionVersion < revisionVersion)
namespace Cinteros.Xrm.FetchXmlBuilder.AppCode
{
internal class GithubVersionChecker
{
private static int currentMajorVersion;
private static int currentMinorVersion;
private static int currentBuildVersion;
private static int currentRevisionVersion;
private static string ghUser;
private static string ghRepo;

public GithubVersionChecker(string currentVersion, string githubUser, string githubRepo)
{
var versionParts = currentVersion.Split('.');
currentMajorVersion = int.Parse(versionParts[0]);
currentMinorVersion = int.Parse(versionParts[1]);
currentBuildVersion = int.Parse(versionParts[2]);
currentRevisionVersion = int.Parse(versionParts[3]);
ghUser = githubUser;
ghRepo = githubRepo;
}

public static GithubInformation Cpi { get; set; }

public void Run()
{
RunAsync().Wait();
}

static async Task RunAsync()
{
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.github.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("User-Agent", ghUser);

HttpResponseMessage response =
await
client.GetAsync(string.Format("repos/{0}/{1}/releases", ghUser, ghRepo))
.ConfigureAwait(continueOnCapturedContext: false);
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsStringAsync();
var jSserializer = new JavaScriptSerializer();
var releases = jSserializer.Deserialize<List<RootObject>>(data.Result);

var lastRelease = releases.OrderByDescending(r => r.created_at).FirstOrDefault();
if (lastRelease != null)
{
var version = lastRelease.tag_name.Replace("v.", "");
var versionParts = version.Split('.');
int majorVersion = int.Parse(versionParts[0]);
int minorVersion = int.Parse(versionParts[1]);
int buildVersion = int.Parse(versionParts[2]);
int revisionVersion = int.Parse(versionParts[3]);

if (currentMajorVersion < majorVersion
|| currentMajorVersion == majorVersion && currentMinorVersion < minorVersion
||
currentMajorVersion == majorVersion && currentMinorVersion == minorVersion &&
currentBuildVersion < buildVersion
||
currentMajorVersion == majorVersion && currentMinorVersion == minorVersion &&
currentBuildVersion == buildVersion && currentRevisionVersion < revisionVersion)
{
var mdth = new Markdown();
var html = mdth.Transform(lastRelease.body).Replace("h1", "div");
Expand All @@ -80,15 +85,15 @@ static async Task RunAsync()
Description = html,
Version = version
};
}
}
}
}
}
catch
{
// Do nothing as we don't want to throw exception if something goes wrong with checking update
}
}
}
}
}
}
}
}
}
catch
{
// Do nothing as we don't want to throw exception if something goes wrong with checking update
}
}
}
}
23 changes: 18 additions & 5 deletions FetchXmlBuilder/FetchXmlBuilder.cs
Expand Up @@ -31,6 +31,7 @@ public partial class FetchXmlBuilder : XrmToolBox.PluginBase, XrmToolBox.IGitHub
{
#region Declarations
const string settingfile = "Cinteros.Xrm.FetchXmlBuilder.Settings.xml";
private string dontRemindVersion = "";
internal Clipboard clipboard = new Clipboard();
private XmlDocument fetchDoc;
private static Dictionary<string, EntityMetadata> entities;
Expand Down Expand Up @@ -555,6 +556,7 @@ private void SaveSetting()
var map = new ExeConfigurationFileMap { ExeConfigFilename = settingfile };
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
config.AppSettings.Settings.Clear();
config.AppSettings.Settings.Add("HideNewVersion", dontRemindVersion);
if (!string.IsNullOrWhiteSpace(xml))
{
config.AppSettings.Settings.Add("FetchXML", xml);
Expand Down Expand Up @@ -629,6 +631,10 @@ private void LoadSetting()
{
var map = new ExeConfigurationFileMap { ExeConfigFilename = settingfile };
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
if (config.AppSettings.Settings["HideNewVersion"] != null)
{
dontRemindVersion = config.AppSettings.Settings["HideNewVersion"].Value;
}
if (config.AppSettings.Settings["FetchXML"] != null)
{
var xml = config.AppSettings.Settings["FetchXML"].Value;
Expand Down Expand Up @@ -1715,17 +1721,24 @@ private Task LaunchVersionCheck()
return new Task(() =>
{
var currentVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
var cvc = new GithubVersionChecker(currentVersion);
var cvc = new GithubVersionChecker(currentVersion, "Cinteros", "FetchXMLBuilder");
cvc.Run();
if (GithubVersionChecker.Cpi != null && !string.IsNullOrEmpty(GithubVersionChecker.Cpi.Version))
{
this.Invoke(new Action(() =>
if (GithubVersionChecker.Cpi.Version != dontRemindVersion)
{
var nvForm = new NewVersionForm(currentVersion, GithubVersionChecker.Cpi.Version, GithubVersionChecker.Cpi.Description);
nvForm.ShowDialog(this);
}));
this.Invoke(new Action(() =>
{
var nvForm = new Cinteros.Xrm.FetchXmlBuilder.Forms.NewVersionForm(currentVersion, GithubVersionChecker.Cpi.Version, GithubVersionChecker.Cpi.Description,
new Uri("http://fxb.xrmtoolbox.com"));
if (nvForm.ShowDialog(this) == DialogResult.Ignore)
{
dontRemindVersion = GithubVersionChecker.Cpi.Version;
}
}));
}
}
});
}
Expand Down
9 changes: 9 additions & 0 deletions FetchXmlBuilder/FetchXmlBuilder.csproj
Expand Up @@ -138,6 +138,12 @@
<DependentUpon>fetchControl.cs</DependentUpon>
</Compile>
<Compile Include="AppCode\Prompt.cs" />
<Compile Include="Forms\NewVersionForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Forms\NewVersionForm.designer.cs">
<DependentUpon>NewVersionForm.cs</DependentUpon>
</Compile>
<Compile Include="Forms\ResultGrid.cs">
<SubType>Form</SubType>
</Compile>
Expand Down Expand Up @@ -203,6 +209,9 @@
<EmbeddedResource Include="FetchXmlBuilder.resx">
<DependentUpon>FetchXmlBuilder.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Forms\NewVersionForm.resx">
<DependentUpon>NewVersionForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Forms\ResultGrid.resx">
<DependentUpon>ResultGrid.cs</DependentUpon>
</EmbeddedResource>
Expand Down
38 changes: 38 additions & 0 deletions FetchXmlBuilder/Forms/NewVersionForm.cs
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Cinteros.Xrm.FetchXmlBuilder.Forms
{
public partial class NewVersionForm : Form
{
private const string style = "<style>*{font-family:Segoe UI;}</style>";
private Uri download;

public NewVersionForm(string currentVersion, string newVersion, string description, Uri downloadurl)
{
InitializeComponent();
download = downloadurl;
lblCurrentVersion.Text = string.Format(lblCurrentVersion.Text, currentVersion);
lblNewVersion.Text = string.Format(lblNewVersion.Text, newVersion);
webBrowser1.DocumentText = style + description;
webBrowser1.ScriptErrorsSuppressed = true;
}

private void btnClose_Click(object sender, EventArgs e)
{
Close();
}

private void btnDownload_Click(object sender, EventArgs e)
{
Process.Start(download.ToString());
}
}
}

0 comments on commit c5cc598

Please sign in to comment.