Skip to content

Commit 86bc2f9

Browse files
committed
Updates to upgrade process
1 parent 94951c1 commit 86bc2f9

File tree

6 files changed

+96
-42
lines changed

6 files changed

+96
-42
lines changed

build/build.cake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ Task("Build").IsDependentOn("Clean").Does(() =>
2323
}
2424

2525
CopyFileToDirectory("../plugins/Common/bin/Release/netcoreapp2.1/Common.dll", "./publish");
26+
27+
CopyFileToDirectory("../src/Upgrade/bin/Release/netcoreapp2.1/Upgrade.dll", "./publish");
28+
CopyFileToDirectory("../src/Upgrade/bin/Release/netcoreapp2.1/Upgrade.deps.json", "./publish");
29+
CopyFileToDirectory("../src/Upgrade/bin/Release/netcoreapp2.1/Upgrade.runtimeconfig.json", "./publish");
2630
});
2731

2832
Task("Test").IsDependentOn("Build").Does(() =>

src/App/App.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
55
<MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish>
6-
<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
6+
<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
77
</PropertyGroup>
88

99
<ItemGroup>
@@ -36,8 +36,10 @@
3636
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
3737
<PropertyGroup>
3838
<PluginBaseDir>$(SolutionDir)plugins\Common\</PluginBaseDir>
39+
<UpgradeBaseDir>$(SolutionDir)src\Upgrade\</UpgradeBaseDir>
3940
</PropertyGroup>
4041
<Copy SourceFiles="$(PluginBaseDir)\bin\$(Configuration)\$(TargetFramework)\Common.dll" DestinationFolder="$(TargetDir)" Condition="Exists('$(PluginBaseDir)bin\$(Configuration)\$(TargetFramework)\Common.dll')" />
42+
<Copy SourceFiles="$(UpgradeBaseDir)\bin\$(Configuration)\$(TargetFramework)\Upgrade.dll" DestinationFolder="$(TargetDir)" Condition="Exists('$(UpgradeBaseDir)bin\$(Configuration)\$(TargetFramework)\Upgrade.dll')" />
4143
</Target>
4244

4345
</Project>
Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,50 @@
1-
using Microsoft.AspNetCore.Hosting;
1+
using Core.Services;
2+
using Microsoft.AspNetCore.Hosting;
23
using System;
34
using System.Diagnostics;
5+
using System.Threading.Tasks;
46

57
namespace App.Pages.Admin.Upgrade
68
{
79
public class UpgradeModel : AdminPageModel
810
{
9-
private IApplicationLifetime ApplicationLifetime { get; set; }
11+
private IApplicationLifetime _app;
12+
private IWebService _ws;
1013

1114
public bool IsUpgrading = false;
1215

13-
public UpgradeModel(IApplicationLifetime appLifetime)
16+
public UpgradeModel(IApplicationLifetime app, IWebService ws)
1417
{
15-
ApplicationLifetime = appLifetime;
18+
_app = app;
19+
_ws = ws;
1620
}
1721

1822
public void OnGet()
1923
{
2024
// check for newer version
2125
}
2226

23-
public void OnPost()
27+
public async Task OnPost()
2428
{
2529
IsUpgrading = true;
2630

27-
// Download new version
28-
//string zipPath = @"c:\demo\foo.zip";
29-
//string extractPath = @"c:\demo\extract";
30-
//ZipFile.ExtractToDirectory(zipPath, extractPath);
31-
32-
// start upgrade process
33-
//Process p = new Process();
34-
//p.StartInfo.FileName = "dotnet";
35-
//p.StartInfo.Arguments = "Upgrade.dll";
36-
//p.StartInfo.UseShellExecute = false;
37-
//p.StartInfo.CreateNoWindow = false;
38-
//p.Start();
39-
40-
ApplicationLifetime.StopApplication();
41-
42-
//Program.Main(null);
43-
44-
//Process.GetCurrentProcess().Kill();
45-
//Environment.Exit(0);
46-
//Program.Shutdown();
31+
if (string.IsNullOrEmpty(await _ws.DownloadLatestRelease()))
32+
{
33+
// start upgrade process
34+
//Process p = new Process();
35+
//p.StartInfo.FileName = "dotnet";
36+
//p.StartInfo.Arguments = "Upgrade.dll";
37+
//p.StartInfo.UseShellExecute = false;
38+
//p.StartInfo.CreateNoWindow = false;
39+
//p.Start();
40+
41+
//_app.StopApplication();
42+
43+
//Program.Main(null);
44+
//Process.GetCurrentProcess().Kill();
45+
//Environment.Exit(0);
46+
//Program.Shutdown();
47+
}
4748
}
4849
}
4950
}

src/Core/Services/NotificationService.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ public interface INotificationService
1515

1616
public class NotificationService : INotificationService
1717
{
18-
static DateTime _lastChecked;
18+
static DateTime _checkPoint;
1919
IDataService _db;
2020
IWebService _web;
2121

2222
public NotificationService(IDataService db, IWebService web)
2323
{
2424
_db = db;
2525
_web = web;
26-
_lastChecked = SystemClock.Now();
2726
}
2827

2928
public async Task<int> AddNotification(AlertType aType, int authorId, string notifier, string content)
@@ -47,9 +46,9 @@ public async Task<int> AddNotification(AlertType aType, int authorId, string not
4746

4847
public async Task<IEnumerable<Notification>> GetNotifications(int authorId)
4948
{
50-
if(SystemClock.Now() >= _lastChecked)
49+
if(SystemClock.Now() >= _checkPoint)
5150
{
52-
_lastChecked = SystemClock.Now().AddMinutes(10);
51+
_checkPoint = SystemClock.Now().AddMinutes(30);
5352
var result = await _web.CheckForLatestRelease();
5453

5554
if (!string.IsNullOrEmpty(result))

src/Core/Services/WebService.cs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
using System.Net.Http;
1+
using System.IO;
2+
using System.IO.Compression;
3+
using System.Net.Http;
24
using System.Threading.Tasks;
35

46
namespace Core.Services
57
{
68
public interface IWebService
79
{
810
Task<string> CheckForLatestRelease();
11+
Task<string> DownloadLatestRelease();
912
}
1013

1114
public class WebService : IWebService
1215
{
1316
IDataService _db;
1417
static HttpClient client = new HttpClient();
18+
static string _repoUrl = "https://api.github.com/repos/blogifierdotnet/Blogifier/releases/latest";
1519

1620
public WebService(IDataService db)
1721
{
@@ -27,9 +31,8 @@ public WebService(IDataService db)
2731
public async Task<string> CheckForLatestRelease()
2832
{
2933
string result = "";
30-
var url = "https://api.github.com/repos/blogifierdotnet/Blogifier/releases/latest";
34+
HttpResponseMessage response = await client.GetAsync(_repoUrl);
3135

32-
HttpResponseMessage response = await client.GetAsync(url);
3336
if (response.IsSuccessStatusCode)
3437
{
3538
var repo = await response.Content.ReadAsAsync<Data.Github.Repository>();
@@ -48,5 +51,45 @@ public async Task<string> CheckForLatestRelease()
4851

4952
return await Task.FromResult(result);
5053
}
54+
55+
public async Task<string> DownloadLatestRelease()
56+
{
57+
var uloadDir = "_upgrade";
58+
var msg = "";
59+
try
60+
{
61+
HttpResponseMessage response = await client.GetAsync(_repoUrl);
62+
if (response.IsSuccessStatusCode)
63+
{
64+
var repo = await response.Content.ReadAsAsync<Data.Github.Repository>();
65+
var zipUrl = repo.assets[0].browser_download_url;
66+
var zipPath = $"{uloadDir}{Path.DirectorySeparatorChar.ToString()}{repo.tag_name}.zip";
67+
68+
using (var client = new HttpClient())
69+
{
70+
using (var result = await client.GetAsync(zipUrl))
71+
{
72+
if (result.IsSuccessStatusCode)
73+
{
74+
var zipBites = await result.Content.ReadAsByteArrayAsync();
75+
76+
if (!Directory.Exists(uloadDir))
77+
Directory.CreateDirectory(uloadDir);
78+
79+
File.WriteAllBytes(zipPath, zipBites);
80+
81+
ZipFile.ExtractToDirectory(zipPath, uloadDir);
82+
}
83+
}
84+
}
85+
}
86+
}
87+
catch (System.Exception ex)
88+
{
89+
msg = ex.Message;
90+
}
91+
92+
return await Task.FromResult(msg);
93+
}
5194
}
5295
}

src/Upgrade/Program.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,31 @@ class Program
1616
static void Main(string[] args)
1717
{
1818
_appDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
19-
_upgDir = $"{_appDir}{_slash}upgrade";
19+
_upgDir = $"{_appDir}{_slash}_upgrade";
2020
_items = new List<string>();
2121

2222
_items.Add($"STARTING UPGRADE IN: {_appDir}");
2323

2424
// wait for web app to stop
2525
System.Threading.Thread.Sleep(3000);
2626

27-
var files = Directory.GetFiles(_appDir);
27+
try
28+
{
29+
var files = Directory.GetFiles(_appDir);
2830

29-
foreach (var file in GetCoreFiles())
31+
foreach (var file in GetCoreFiles())
32+
{
33+
ReplaceFile(file);
34+
}
35+
36+
ReplaceFolder($"wwwroot{_slash}admin");
37+
ReplaceFolder($"wwwroot{_slash}lib");
38+
}
39+
catch (Exception ex)
3040
{
31-
ReplaceFile(file);
41+
_items.Add(ex.Message);
3242
}
3343

34-
ReplaceFolder($"wwwroot{_slash}admin");
35-
ReplaceFolder($"wwwroot{_slash}lib");
36-
3744
using (StreamWriter writer = new StreamWriter("upgrade.log"))
3845
{
3946
foreach (var item in _items)
@@ -100,8 +107,6 @@ static List<string> GetCoreFiles()
100107
"App.deps.json",
101108
"App.dll",
102109
"App.pdb",
103-
"App.PrecompiledViews.dll",
104-
"App.PrecompiledViews.pdb",
105110
"App.runtimeconfig.json",
106111
"Common.dll",
107112
"Core.dll",

0 commit comments

Comments
 (0)