mirror of
https://github.com/ckaczor/Common.git
synced 2026-02-16 10:58:34 -05:00
Add support for updates from GitHub
This commit is contained in:
@@ -170,6 +170,7 @@
|
|||||||
<Compile Include="Trace\Tracer.cs" />
|
<Compile Include="Trace\Tracer.cs" />
|
||||||
<Compile Include="Settings\Extensions.cs" />
|
<Compile Include="Settings\Extensions.cs" />
|
||||||
<Compile Include="Settings\GenericSettingsProvider.cs" />
|
<Compile Include="Settings\GenericSettingsProvider.cs" />
|
||||||
|
<Compile Include="Update\GitHubRelease.cs" />
|
||||||
<Compile Include="Update\UpdateCheck.cs" />
|
<Compile Include="Update\UpdateCheck.cs" />
|
||||||
<Compile Include="Update\VersionInfo.cs" />
|
<Compile Include="Update\VersionInfo.cs" />
|
||||||
<Compile Include="Xml\XmlExtensions.cs" />
|
<Compile Include="Xml\XmlExtensions.cs" />
|
||||||
@@ -219,6 +220,11 @@
|
|||||||
<None Include="LICENSE.md" />
|
<None Include="LICENSE.md" />
|
||||||
<None Include="README.md" />
|
<None Include="README.md" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Newtonsoft.Json">
|
||||||
|
<Version>11.0.2</Version>
|
||||||
|
</PackageReference>
|
||||||
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSHARP.Targets" />
|
<Import Project="$(MSBuildBinPath)\Microsoft.CSHARP.Targets" />
|
||||||
<ProjectExtensions>
|
<ProjectExtensions>
|
||||||
<VisualStudio>
|
<VisualStudio>
|
||||||
|
|||||||
177
Update/GitHubRelease.cs
Normal file
177
Update/GitHubRelease.cs
Normal file
@@ -0,0 +1,177 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Converters;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
|
namespace Common.Update
|
||||||
|
{
|
||||||
|
public partial class GitHubRelease
|
||||||
|
{
|
||||||
|
[JsonProperty("url")]
|
||||||
|
public string Url { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("assets_url")]
|
||||||
|
public string AssetsUrl { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("upload_url")]
|
||||||
|
public string UploadUrl { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("html_url")]
|
||||||
|
public string HtmlUrl { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("id")]
|
||||||
|
public long Id { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("tag_name")]
|
||||||
|
public string TagName { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("target_commitish")]
|
||||||
|
public string TargetCommitish { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("name")]
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("draft")]
|
||||||
|
public bool Draft { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("author")]
|
||||||
|
public Uploader Author { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("prerelease")]
|
||||||
|
public bool Prerelease { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("created_at")]
|
||||||
|
public DateTimeOffset CreatedAt { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("published_at")]
|
||||||
|
public DateTimeOffset PublishedAt { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("assets")]
|
||||||
|
public List<Asset> Assets { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("tarball_url")]
|
||||||
|
public string TarballUrl { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("zipball_url")]
|
||||||
|
public string ZipballUrl { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("body")]
|
||||||
|
public string Body { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Asset
|
||||||
|
{
|
||||||
|
[JsonProperty("url")]
|
||||||
|
public string Url { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("id")]
|
||||||
|
public long Id { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("name")]
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("label")]
|
||||||
|
public string Label { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("uploader")]
|
||||||
|
public Uploader Uploader { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("content_type")]
|
||||||
|
public string ContentType { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("state")]
|
||||||
|
public string State { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("size")]
|
||||||
|
public long Size { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("download_count")]
|
||||||
|
public long DownloadCount { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("created_at")]
|
||||||
|
public DateTimeOffset CreatedAt { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("updated_at")]
|
||||||
|
public DateTimeOffset UpdatedAt { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("browser_download_url")]
|
||||||
|
public string BrowserDownloadUrl { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Uploader
|
||||||
|
{
|
||||||
|
[JsonProperty("login")]
|
||||||
|
public string Login { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("id")]
|
||||||
|
public long Id { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("avatar_url")]
|
||||||
|
public string AvatarUrl { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("gravatar_id")]
|
||||||
|
public string GravatarId { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("url")]
|
||||||
|
public string Url { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("html_url")]
|
||||||
|
public string HtmlUrl { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("followers_url")]
|
||||||
|
public string FollowersUrl { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("following_url")]
|
||||||
|
public string FollowingUrl { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("gists_url")]
|
||||||
|
public string GistsUrl { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("starred_url")]
|
||||||
|
public string StarredUrl { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("subscriptions_url")]
|
||||||
|
public string SubscriptionsUrl { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("organizations_url")]
|
||||||
|
public string OrganizationsUrl { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("repos_url")]
|
||||||
|
public string ReposUrl { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("events_url")]
|
||||||
|
public string EventsUrl { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("received_events_url")]
|
||||||
|
public string ReceivedEventsUrl { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("type")]
|
||||||
|
public string Type { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("site_admin")]
|
||||||
|
public bool SiteAdmin { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial class GitHubRelease
|
||||||
|
{
|
||||||
|
public static GitHubRelease FromJson(string json) => JsonConvert.DeserializeObject<GitHubRelease>(json, Converter.Settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Serialize
|
||||||
|
{
|
||||||
|
public static string ToJson(this GitHubRelease self) => JsonConvert.SerializeObject(self, Converter.Settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class Converter
|
||||||
|
{
|
||||||
|
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
|
||||||
|
DateParseHandling = DateParseHandling.None,
|
||||||
|
Converters = {
|
||||||
|
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,12 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace Common.Update
|
namespace Common.Update
|
||||||
{
|
{
|
||||||
|
public enum ServerType
|
||||||
|
{
|
||||||
|
Generic,
|
||||||
|
GitHub
|
||||||
|
}
|
||||||
|
|
||||||
public static class UpdateCheck
|
public static class UpdateCheck
|
||||||
{
|
{
|
||||||
public delegate void ApplicationShutdownDelegate();
|
public delegate void ApplicationShutdownDelegate();
|
||||||
@@ -18,6 +24,7 @@ namespace Common.Update
|
|||||||
public static ApplicationCurrentMessageDelegate ApplicationCurrentMessage = null;
|
public static ApplicationCurrentMessageDelegate ApplicationCurrentMessage = null;
|
||||||
public static ApplicationUpdateMessageDelegate ApplicationUpdateMessage = null;
|
public static ApplicationUpdateMessageDelegate ApplicationUpdateMessage = null;
|
||||||
|
|
||||||
|
public static ServerType UpdateServerType;
|
||||||
public static string UpdateServer;
|
public static string UpdateServer;
|
||||||
public static string UpdateFile;
|
public static string UpdateFile;
|
||||||
public static string ApplicationName { get; set; }
|
public static string ApplicationName { get; set; }
|
||||||
@@ -26,14 +33,11 @@ namespace Common.Update
|
|||||||
public static string LocalInstallFile { get; private set; }
|
public static string LocalInstallFile { get; private set; }
|
||||||
public static bool UpdateAvailable { get; private set; }
|
public static bool UpdateAvailable { get; private set; }
|
||||||
|
|
||||||
public static Version LocalVersion
|
public static Version LocalVersion => Assembly.GetEntryAssembly().GetName().Version;
|
||||||
{
|
|
||||||
get { return Assembly.GetEntryAssembly().GetName().Version; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool CheckForUpdate()
|
public static bool CheckForUpdate()
|
||||||
{
|
{
|
||||||
RemoteVersion = VersionInfo.Load(UpdateServer, UpdateFile);
|
RemoteVersion = VersionInfo.Load(UpdateServerType, UpdateServer, UpdateFile);
|
||||||
|
|
||||||
if (RemoteVersion == null)
|
if (RemoteVersion == null)
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Common.Debug;
|
using Common.Debug;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Net;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
|
|
||||||
namespace Common.Update
|
namespace Common.Update
|
||||||
@@ -10,7 +11,20 @@ namespace Common.Update
|
|||||||
public string InstallFile { get; set; }
|
public string InstallFile { get; set; }
|
||||||
public DateTime InstallCreated { get; set; }
|
public DateTime InstallCreated { get; set; }
|
||||||
|
|
||||||
public static VersionInfo Load(string server, string file)
|
public static VersionInfo Load(ServerType updateServerType, string server, string file)
|
||||||
|
{
|
||||||
|
switch (updateServerType)
|
||||||
|
{
|
||||||
|
case ServerType.Generic:
|
||||||
|
return LoadFile(server, file);
|
||||||
|
case ServerType.GitHub:
|
||||||
|
return LoadGitHub(server);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static VersionInfo LoadFile(string server, string file)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -44,5 +58,38 @@ namespace Common.Update
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static VersionInfo LoadGitHub(string server)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ServicePointManager.Expect100Continue = true;
|
||||||
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
|
||||||
|
|
||||||
|
using (var webClient = new WebClient())
|
||||||
|
{
|
||||||
|
webClient.Headers.Add("User-Agent", UpdateCheck.ApplicationName);
|
||||||
|
|
||||||
|
var json = webClient.DownloadString(server);
|
||||||
|
|
||||||
|
var release = GitHubRelease.FromJson(json);
|
||||||
|
|
||||||
|
var versionInfo = new VersionInfo
|
||||||
|
{
|
||||||
|
Version = Version.Parse(release.TagName),
|
||||||
|
InstallFile = release.Assets[0].BrowserDownloadUrl,
|
||||||
|
InstallCreated = release.Assets[0].CreatedAt.LocalDateTime
|
||||||
|
};
|
||||||
|
|
||||||
|
return versionInfo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
Tracer.WriteException(exception);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user