Browse Source

Created UpdateModule

flabbet 4 years ago
parent
commit
35faf65e3f

+ 7 - 0
PixiEditor.UpdateModule/PixiEditor.UpdateModule.csproj

@@ -0,0 +1,7 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>netcoreapp3.1</TargetFramework>
+  </PropertyGroup>
+
+</Project>

+ 24 - 0
PixiEditor.UpdateModule/ReleaseInfo.cs

@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Text.Json.Serialization;
+
+namespace PixiEditor.UpdateModule
+{
+    public class ReleaseInfo
+    {
+        [JsonPropertyName("tag_name")]
+        public string TagName { get; set; }
+        [JsonPropertyName("draft")]
+        public bool IsDraft { get; set; }
+        [JsonPropertyName("prerelease")]
+        public bool IsPrerelease { get; set; }
+        public bool WasDataFetchSuccessfull { get; set; } = true;
+
+        public ReleaseInfo() { }
+        public ReleaseInfo(bool dataFetchSuccessfull)
+        {
+            WasDataFetchSuccessfull = dataFetchSuccessfull;
+        }
+    }
+}

+ 43 - 0
PixiEditor.UpdateModule/UpdateChecker.cs

@@ -0,0 +1,43 @@
+using System;
+using System.Net.Http;
+using System.Text.Json;
+using System.Threading.Tasks;
+
+namespace PixiEditor.UpdateModule
+{
+    public class UpdateChecker
+    {
+        public string ReleaseApiUrl = "https://api.github.com/repos/PixiEditor/PixiEditor/releases/latest";
+        public string CurrentVersionTag { get; set; }
+
+        public UpdateChecker(string currentVersionTag)
+        {
+            CurrentVersionTag = currentVersionTag;
+        }
+
+        public async Task<bool> CheckUpdateAvailable()
+        {
+            return CheckUpdateAvailable(await GetLatestReleaseInfo());
+        }
+
+        public bool CheckUpdateAvailable(ReleaseInfo latestRelease)
+        {
+            return latestRelease.WasDataFetchSuccessfull && latestRelease.TagName != CurrentVersionTag;
+        }
+
+        public async Task<ReleaseInfo> GetLatestReleaseInfo()
+        {
+            using(HttpClient client = new HttpClient())
+            {
+                client.DefaultRequestHeaders.Add("User-Agent", "PixiEditor");
+                var response = await client.GetAsync(ReleaseApiUrl);
+                if(response.StatusCode == System.Net.HttpStatusCode.OK)
+                {
+                    string content = await response.Content.ReadAsStringAsync();
+                    return JsonSerializer.Deserialize<ReleaseInfo>(content);
+                }
+            }
+            return new ReleaseInfo(false);
+        }
+    }
+}

+ 11 - 0
PixiEditor.UpdateModule/UpdateDownloader.cs

@@ -0,0 +1,11 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace PixiEditor.UpdateModule
+{
+    public class UpdateDownloader
+    {
+
+    }
+}

+ 6 - 0
PixiEditor/PixiEditor.sln

@@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PixiEditor", "PixiEditor.cs
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PixiEditorTests", "..\PixiEditorTests\PixiEditorTests.csproj", "{D61922EA-3BF3-4AFA-8930-3A8B30A9A195}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PixiEditor.UpdateModule", "..\PixiEditor.UpdateModule\PixiEditor.UpdateModule.csproj", "{9A0AB1E7-435C-4567-9EAC-FF102A9F1B01}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
 		{D61922EA-3BF3-4AFA-8930-3A8B30A9A195}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{D61922EA-3BF3-4AFA-8930-3A8B30A9A195}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{D61922EA-3BF3-4AFA-8930-3A8B30A9A195}.Release|Any CPU.Build.0 = Release|Any CPU
+		{9A0AB1E7-435C-4567-9EAC-FF102A9F1B01}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{9A0AB1E7-435C-4567-9EAC-FF102A9F1B01}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{9A0AB1E7-435C-4567-9EAC-FF102A9F1B01}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{9A0AB1E7-435C-4567-9EAC-FF102A9F1B01}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 2 - 0
PixiEditorTests/PixiEditorTests.csproj

@@ -18,6 +18,7 @@
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
     </PackageReference>
     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
+    <PackageReference Include="Moq" Version="4.14.5" />
     <PackageReference Include="OpenCover" Version="4.7.922" />
     <PackageReference Include="xunit" Version="2.4.1" />
     <PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
@@ -28,6 +29,7 @@
   </ItemGroup>
 
   <ItemGroup>
+    <ProjectReference Include="..\PixiEditor.UpdateModule\PixiEditor.UpdateModule.csproj" />
     <ProjectReference Include="..\PixiEditor\PixiEditor.csproj" />
   </ItemGroup>
 

+ 25 - 0
PixiEditorTests/UpdateModuleTests/UpdateCheckerTests.cs

@@ -0,0 +1,25 @@
+using Moq;
+using PixiEditor.UpdateModule;
+using System;
+using System.Collections.Generic;
+using System.Net.Http;
+using System.Text;
+using System.Threading.Tasks;
+using Xunit;
+
+namespace PixiEditorTests.UpdateModuleTests
+{
+    public class UpdateCheckerTests
+    {
+        [Theory]
+        [InlineData("0.1.2", "0.1.2", false)]
+        [InlineData("0.5.", "0.1.2", true)]
+        [InlineData(null, "0.1.2", true)]
+        public void TestThatCheckUpdateAvailableChecksCorrectly(string newVersion, string currentVersion, bool expectedValue)
+        {
+            UpdateChecker checker = new UpdateChecker(currentVersion);
+            bool result = checker.CheckUpdateAvailable(new ReleaseInfo() { TagName = newVersion });
+            Assert.True(result == expectedValue);
+        }
+    }
+}