Skip to content

Commit

Permalink
#26: Replaced CodePlexUrl by new GitHub url for downloads.
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkapok committed Jun 15, 2017
1 parent 79fd33f commit 844d03d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 35 deletions.
7 changes: 6 additions & 1 deletion Source/Terminals/Terminals.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\Resources\Libraries\mstsc\AxInterop.MSTSCLib.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.2\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.configuration" />
Expand Down Expand Up @@ -1288,7 +1291,9 @@
<Link>Thumbs\wmimgmt.msc.jpg</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="packages.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
<None Include="Resources\amber.png" />
<Content Include="Resources\application_user.png" />
<Content Include="Resources\back.png" />
Expand Down
60 changes: 39 additions & 21 deletions Source/Terminals/Updates/UpdateManager.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,50 @@
using System;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Terminals.Properties;
using Unified.Rss;

namespace Terminals.Updates
{
internal class UpdateManager
public class Release
{
/// <summary>
/// Url to releases Rss feed, where the Terminals releases are published
/// </summary>
private const string RSS_URL = "http://terminals.codeplex.com/project/feeds/rss?ProjectRSSFeed=codeplex%3A%2F%2Frelease%2FTerminals&ProjectName=terminals";
[JsonProperty("tag_name")]
public string TagName { get; set; }

[JsonProperty("name")]

private readonly Func<RssFeed> readReleases;
public string Name { get; set; }

[JsonProperty("published_at")]

public DateTime Published { get; set; }
}

public UpdateManager() : this(ReadReleases)
internal class UpdateManager
{
private readonly Func<string> readReleases;

public UpdateManager() : this(DownloadReleases)
{
}

internal UpdateManager(Func<RssFeed> readReleases)
internal UpdateManager(Func<string> readReleases)
{
this.readReleases = readReleases;
}

private static RssFeed ReadReleases()
private static string DownloadReleases()
{
return RssFeed.Read(RSS_URL);
using (var client = new WebClient())
{
const string agent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident / 6.0)";
client.Headers.Add("Accept", "application/json");
client.Headers.Add("User-Agent", agent);
return client.DownloadString(Settings.Default.ReleasesUrl);
}
}

/// <summary>
Expand Down Expand Up @@ -82,23 +100,23 @@ private ReleaseInfo TryCheckForCodeplexRelease(DateTime buildDate)

private ReleaseInfo DownLoadLatestReleaseInfo(DateTime buildDate)
{
RssFeed feed = this.readReleases();
string downloaded = this.readReleases();
Release[] feed = JsonConvert.DeserializeObject<Release[]>(downloaded);

if (feed != null)
{
RssItem newvestRssItem = SelectNewvestRssRssItem(feed, buildDate);
Release newvestRssItem = SelectNewvestRssRssItem(feed, buildDate);
if (newvestRssItem != null)
return new ReleaseInfo(newvestRssItem.PubDate, newvestRssItem.Title);
return new ReleaseInfo(newvestRssItem.Published, newvestRssItem.TagName);
}

return ReleaseInfo.NotAvailable;
}

private static RssItem SelectNewvestRssRssItem(RssFeed feed, DateTime buildDate)
private static Release SelectNewvestRssRssItem(Release[] feed, DateTime buildDate)
{
return feed.Channels.OfType<RssChannel>()
.SelectMany(chanel => chanel.Items.OfType<RssItem>())
.Where(item => IsNewerThanCurrent(item, buildDate))
.OrderByDescending(selected => selected.PubDate)
return feed.Where(item => IsNewerThanCurrent(item, buildDate))
.OrderByDescending(selected => selected.Published)
.FirstOrDefault();
}

Expand All @@ -107,14 +125,14 @@ private static RssItem SelectNewvestRssRssItem(RssFeed feed, DateTime buildDate)
/// This filters the updates on release page after the release was published.
/// Obtains the real date from the title.
/// </summary>
private static bool IsNewerThanCurrent(RssItem item, DateTime buildDate)
private static bool IsNewerThanCurrent(Release item, DateTime buildDate)
{
// rss item published date
if (item.PubDate < buildDate)
if (item.Published < buildDate)
return false;

const string DATE_FILTER = @"([^\(]+)\((?<Date>[^\(\)]+)\)"; // Select string iside brackets as "Date" group
string titleDate = Regex.Match(item.Title, DATE_FILTER).Groups["Date"].Value;
string titleDate = Regex.Match(item.Name, DATE_FILTER).Groups["Date"].Value;
DateTime releaseDate;
// there is no culture specification when downloading the feed, so it is always EN
DateTime.TryParse(titleDate, out releaseDate);
Expand Down
1 change: 1 addition & 0 deletions Source/Terminals/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<packages>
<package id="EntityFramework" version="5.0.0" targetFramework="net40" />
<package id="FluentValidation" version="6.2.1.0" targetFramework="net40" />
<package id="Newtonsoft.Json" version="10.0.2" targetFramework="net40" />
<package id="SharpZipLib" version="0.86.0" targetFramework="net40" />
</packages>
23 changes: 10 additions & 13 deletions Source/Tests/UpdateManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Terminals.Configuration;
using Terminals.Updates;
using Tests.FilePersisted;
using Unified.Rss;

namespace Tests
{
Expand Down Expand Up @@ -73,21 +72,19 @@ private ReleaseInfo RunUpdateCheck()
return updateManager.CheckForCodeplexRelease(this.buildDate);
}

private RssFeed CreateRss()
private string CreateRss()
{
const string downloaded = @"
[
{{
""name"": ""Title({0})"",
""tag_name"": ""1.0.0"",
""published_at"": ""{0}""
}}
]";
DateTime currentRelease = new DateTime(2010, 1, 3, 1, 0, 0, DateTimeKind.Utc);

var feed = new RssFeed();
var chanel = new RssChannel();
var release = new RssItem()
{
PubDate = currentRelease,
Title = string.Format("Title ({0})", currentRelease)
};
chanel.Items.Add(release);
feed.Channels.Add(chanel);

return feed;
return string.Format(downloaded, currentRelease);
}

private void AssertLastUpdateCheck()
Expand Down

0 comments on commit 844d03d

Please sign in to comment.