private static void SelfUpdate(GitHubUpdater updater) { // var updater = new GitHubUpdater(); updater.SetRepo("jcaillon", "GithubUpdater"); updater.UseCancellationToken(new CancellationTokenSource(3000).Token); updater.UseMaxNumberOfReleasesToFetch(10); var currentVersion = UpdaterHelper.StringToVersion(FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion); Console.WriteLine($"Our current version is: {currentVersion}."); var releases = updater.FetchNewReleases(currentVersion); Console.WriteLine($"We found {releases.Count} new releases on github."); Console.WriteLine($"The latest release if {releases[0].Name}."); Console.WriteLine($"Downloading the latest release asset: {releases[0].Assets[0].BrowserDownloadUrl}."); var tempFilePath = updater.DownloadToTempFile(releases[0].Assets[0].BrowserDownloadUrl, progress => { Console.WriteLine($"Downloading... {progress.NumberOfBytesDoneTotal} / {progress.NumberOfBytesTotal} bytes."); }); var fileUpdater = SimpleFileUpdater.Instance; Console.WriteLine("We will replace this .exe with the one on the github release after this program has exited."); fileUpdater.AddFileToMove(tempFilePath, Assembly.GetExecutingAssembly().Location); fileUpdater.Start(); }
/// <summary> /// Returns a list of new releases based on the local version versus the tag version of distant releases. /// The returned list is sorted by descending order, the first release being the most up-to-date. /// </summary> /// <param name="localVersion">See <see cref="UpdaterHelper.StringToVersion"/> to get the version from a string.</param> /// <returns></returns> public List <GitLabTag> FetchNewReleases(Version localVersion) { return(FetchNewReleases(release => UpdaterHelper.StringToVersion(release.TagName).CompareTo(localVersion) > 0, release => UpdaterHelper.StringToVersion(release.TagName))); }
public void Test() { // hostname to use // we need something different than 127.0.0.1 or localhost for the proxy! IPHostEntry hostEntry; try { hostEntry = Dns.GetHostEntry("mylocalhost"); } catch (Exception) { hostEntry = null; } var host = hostEntry == null ? "127.0.0.1" : "mylocalhost"; var baseDir = Path.Combine(TestFolder, "http"); Directory.CreateDirectory(baseDir); var gitLabServer = new SimpleGitLabServer(baseDir, "admin"); var proxyServer = new SimpleHttpProxyServer("jucai69d", "julien caillon"); var cts = new CancellationTokenSource(); var task1 = HttpServer.ListenAsync(8086, cts.Token, gitLabServer.OnHttpRequest, true); var task2 = HttpServer.ListenAsync(8087, cts.Token, proxyServer.OnHttpRequest, true); // do gitLabServer.Tags = new List <GitLabTag> { new GitLabTag { TagName = "v1.0.1-beta", TagMessage = "rel1", TagSha1 = "1", Release = new GitLabRelease { TagName = "", Description = "Description" } }, new GitLabTag { TagName = "v1.1.0", TagMessage = "rel2", TagSha1 = "2" }, new GitLabTag { TagName = "v1.2.1-beta", TagMessage = "rel3", TagSha1 = "3" }, new GitLabTag { TagName = "v3.0.0", TagMessage = "rel5", TagSha1 = "5" }, new GitLabTag { TagName = "v2.0.0", TagMessage = "rel4", TagSha1 = "4" } }; var updater = new GitLabUpdater($"http://{host}:8086"); updater.UsePrivateToken("admin"); updater.UseProxy($"http://{host}:8087/", "jucai69d", "julien caillon"); updater.SetProjectId("test/truc"); updater.UseMaxNumberOfTagsToFetch(10); var tags = updater.FetchNewReleases(UpdaterHelper.StringToVersion("0")); Assert.AreEqual(5, tags.Count); Assert.AreEqual("rel5", tags[0].TagMessage); tags = updater.FetchNewReleases(UpdaterHelper.StringToVersion("3")); Assert.AreEqual(0, tags.Count); tags = updater.FetchNewReleases(UpdaterHelper.StringToVersion("1.2")); Assert.AreEqual(3, tags.Count); Assert.AreEqual("rel5", tags[0].TagMessage); File.WriteAllText(Path.Combine(baseDir, "testFile"), "cc"); var countProgress = 0; var dlPath = updater.DownloadToTempFile("testFile", progress => countProgress++); Assert.IsTrue(countProgress > 0); Assert.IsTrue(File.Exists(dlPath)); Assert.AreEqual(File.ReadAllText(Path.Combine(baseDir, "testFile")), File.ReadAllText(dlPath)); File.Delete(dlPath); countProgress = 0; dlPath = updater.DownloadRepositoryArchive("mysha1", progress => countProgress++); Assert.IsTrue(countProgress > 0); Assert.IsTrue(File.Exists(dlPath)); Assert.AreEqual(new FileInfo(dlPath).Length, 10); File.Delete(dlPath); if (!host.Equals("127.0.0.1")) { Assert.IsTrue(proxyServer.NbRequestsHandledOk > 0); } HttpServer.Stop(cts, task1, task2); }
public void Test() { // hostname to use // we need something different than 127.0.0.1 or localhost for the proxy! IPHostEntry hostEntry; try { hostEntry = Dns.GetHostEntry("mylocalhost"); } catch (Exception) { hostEntry = null; } var host = hostEntry == null ? "127.0.0.1" : "mylocalhost"; var baseDir = Path.Combine(TestFolder, "http"); Directory.CreateDirectory(baseDir); var githubServer = new SimpleGithubServer(baseDir, "admin"); var proxyServer = new SimpleHttpProxyServer("jucai69d", "julien caillon"); githubServer.Releases = new List <GitHubRelease> { new GitHubRelease { CreatedAt = $"{DateTime.UtcNow:s}Z" } }; var cts = new CancellationTokenSource(); var task1 = HttpServer.ListenAsync(8084, cts.Token, githubServer.OnHttpRequest, true); var task2 = HttpServer.ListenAsync(8085, cts.Token, proxyServer.OnHttpRequest, true); // do githubServer.Releases = new List <GitHubRelease> { new GitHubRelease { Name = "rel1", TagName = "v1.0.1-beta", Prerelease = true, ZipballUrl = "file.v1.0", CreatedAt = $"{DateTime.UtcNow:s}Z", Assets = new List <GitHubAsset> { new GitHubAsset { Name = "asset1" }, new GitHubAsset { Name = "asset2" } } }, new GitHubRelease { Name = "rel2", TagName = "v1.1.0", Prerelease = false }, new GitHubRelease { Name = "rel3", TagName = "v1.2.1-beta", Prerelease = true }, new GitHubRelease { Name = "rel5", TagName = "v3.0.0", Prerelease = false }, new GitHubRelease { Name = "rel4", TagName = "v2.0.0", Prerelease = false } }; var updater = new GitHubUpdater(); updater.UseAuthorizationToken("admin"); updater.UseAlternativeBaseUrl($"http://{host}:8084"); updater.UseProxy($"http://{host}:8085/", "jucai69d", "julien caillon"); updater.SetRepo("3pUser", "yolo"); updater.UseMaxNumberOfReleasesToFetch(10); var releases = updater.FetchNewReleases(UpdaterHelper.StringToVersion("0")); Assert.AreEqual(5, releases.Count); Assert.AreEqual("rel5", releases[0].Name); releases = updater.FetchNewReleases(UpdaterHelper.StringToVersion("3")); Assert.AreEqual(0, releases.Count); releases = updater.FetchNewReleases(UpdaterHelper.StringToVersion("1.2")); Assert.AreEqual(3, releases.Count); Assert.AreEqual("rel5", releases[0].Name); File.WriteAllText(Path.Combine(baseDir, "testFile"), "cc"); var countProgress = 0; var dlPath = updater.DownloadToTempFile("testFile", progress => countProgress++); Assert.IsTrue(countProgress > 0); Assert.IsTrue(File.Exists(dlPath)); Assert.AreEqual(File.ReadAllText(Path.Combine(baseDir, "testFile")), File.ReadAllText(dlPath)); File.Delete(dlPath); if (!host.Equals("127.0.0.1")) { Assert.IsTrue(proxyServer.NbRequestsHandledOk > 0); } HttpServer.Stop(cts, task1, task2); }