public List <ReleaseInfo> GetGitHubUpdates() { GetCurrentRelease(); //githubReleases using (var client = new WebClientPx()) { client.Headers.Add("user-agent", "HomeGenieUpdater/1.0 (compatible; MSIE 7.0; Windows NT 6.0)"); try { bool collectingComplete = false; string releaseJson = client.DownloadString(githubReleases); var deserializerSettings = new JsonSerializerSettings() { DateParseHandling = Newtonsoft.Json.DateParseHandling.None }; dynamic releases = JsonConvert.DeserializeObject(releaseJson, deserializerSettings) as JArray; if (remoteUpdates != null) { remoteUpdates.Clear(); } else { remoteUpdates = new List <ReleaseInfo>(); } foreach (var rel in releases) { foreach (dynamic relFile in (rel.assets as JArray)) { if (relFile.browser_download_url.ToString().EndsWith(".tgz")) { var releaseDate = DateTime.ParseExact(relFile.updated_at.ToString(), "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal); if (currentRelease.ReleaseDate < releaseDate && remoteUpdates.Count == 0) { var r = new ReleaseInfo(); r.Name = githubRepository; r.Version = rel.tag_name.ToString(); r.Description = rel.name.ToString(); r.ReleaseNote = rel.body.ToString(); r.RequireRestart = false; // this flag is now useless since "restart" flag is dynamically computed by update process r.UpdateBreak = true; // TODO: store this flag somewhere in the github entry r.DownloadUrl = relFile.browser_download_url.ToString(); r.ReleaseDate = releaseDate; remoteUpdates.Add(r); } else if (currentRelease.ReleaseDate < releaseDate) { string relInfo = String.Format("\r\n\r\n[{0} {1:yyyy-MM-dd}]\r\n{2}", rel.tag_name.ToString(), releaseDate, rel.body.ToString()); remoteUpdates[0].ReleaseNote += relInfo; } else { collectingComplete = true; } } } // updates from github contains the whole HG bundle so we always consider the most recent one if (collectingComplete) { break; } } } catch (Exception e) { Console.WriteLine(e.Message); remoteUpdates = null; } finally { client.Dispose(); } } return(remoteUpdates); }
public List <string> DownloadAndUncompress(ReleaseInfo releaseInfo) { if (ArchiveDownloadUpdate != null) { ArchiveDownloadUpdate(this, new ArchiveDownloadEventArgs(releaseInfo, ArchiveDownloadStatus.DOWNLOADING)); } // string destinationFolder = Path.Combine(updateFolder, "files"); string archiveName = Path.Combine(updateFolder, "archives", "hg_update_" + releaseInfo.Version.Replace(" ", "_").Replace(".", "_") + ".zip"); if (!Directory.Exists(Path.GetDirectoryName(archiveName))) { Directory.CreateDirectory(Path.GetDirectoryName(archiveName)); } using (var client = new WebClientPx()) { client.Headers.Add("user-agent", "HomeGenieUpdater/1.0 (compatible; MSIE 7.0; Windows NT 6.0)"); try { client.DownloadFile(releaseInfo.DownloadUrl, archiveName); } catch (Exception) { if (ArchiveDownloadUpdate != null) { ArchiveDownloadUpdate(this, new ArchiveDownloadEventArgs(releaseInfo, ArchiveDownloadStatus.ERROR)); } return(null); // throw; } finally { client.Dispose(); } } // Unarchive (unzip) if (ArchiveDownloadUpdate != null) { ArchiveDownloadUpdate(this, new ArchiveDownloadEventArgs(releaseInfo, ArchiveDownloadStatus.DECOMPRESSING)); } bool errorOccurred = false; var files = Utility.UncompressTgz(archiveName, destinationFolder); errorOccurred = (files.Count == 0); if (ArchiveDownloadUpdate != null) { if (errorOccurred) { ArchiveDownloadUpdate(this, new ArchiveDownloadEventArgs(releaseInfo, ArchiveDownloadStatus.ERROR)); } else { ArchiveDownloadUpdate(this, new ArchiveDownloadEventArgs(releaseInfo, ArchiveDownloadStatus.COMPLETED)); } } // update release_info.xml file with last releaseInfo ReleaseDate field in order to reflect github release date if (files.Contains(Path.Combine("homegenie", releaseFile))) { var ri = GetReleaseFile(Path.Combine(destinationFolder, "homegenie", releaseFile)); ri.ReleaseDate = releaseInfo.ReleaseDate.ToUniversalTime(); XmlSerializer serializer = new XmlSerializer(typeof(ReleaseInfo)); using (TextWriter writer = new StreamWriter(Path.Combine(destinationFolder, "homegenie", releaseFile))) { serializer.Serialize(writer, ri); } } return(files); }
public bool InstallPackage(string pkgFolderUrl, string tempFolderPath) { string installFolder = Path.Combine(tempFolderPath, "pkg"); dynamic pkgData = null; bool success = true; // Download package specs homegenie.RaiseEvent( Domains.HomeGenie_System, Domains.HomeGenie_PackageInstaller, SourceModule.Master, "HomeGenie Package Installer", Properties.InstallProgressMessage, "= Downloading: package.json" ); using (var client = new WebClientPx()) { try { string pkgJson = "[" + client.DownloadString(pkgFolderUrl + "/package.json") + "]"; pkgData = (JsonConvert.DeserializeObject(pkgJson) as JArray)[0]; } catch (Exception e) { homegenie.RaiseEvent( Domains.HomeGenie_System, Domains.HomeGenie_PackageInstaller, SourceModule.Master, "HomeGenie Package Installer", Properties.InstallProgressMessage, "= ERROR: '" + e.Message + "'" ); success = false; } client.Dispose(); } // Download and install package files if (success && pkgData != null) { // Import Automation Programs in package foreach (var program in pkgData.programs) { homegenie.RaiseEvent( Domains.HomeGenie_System, Domains.HomeGenie_PackageInstaller, SourceModule.Master, "HomeGenie Package Installer", Properties.InstallProgressMessage, "= Downloading: " + program.file.ToString() ); Utility.FolderCleanUp(installFolder); string programFile = Path.Combine(installFolder, program.file.ToString()); if (File.Exists(programFile)) { File.Delete(programFile); } using (var client = new WebClientPx()) { try { client.DownloadFile(pkgFolderUrl + "/" + program.file.ToString(), programFile); } catch (Exception e) { homegenie.RaiseEvent( Domains.HomeGenie_System, Domains.HomeGenie_PackageInstaller, SourceModule.Master, "HomeGenie Package Installer", Properties.InstallProgressMessage, "= ERROR: '" + e.Message + "'" ); success = false; } client.Dispose(); } if (success) { homegenie.RaiseEvent( Domains.HomeGenie_System, Domains.HomeGenie_PackageInstaller, SourceModule.Master, "HomeGenie Package Installer", Properties.InstallProgressMessage, "= Installing: " + program.name.ToString() ); int pid = homegenie.ProgramManager.GeneratePid(); if (program.uid == null || !int.TryParse(program.uid.ToString(), out pid)) { program.uid = pid; } // by default enable package programs after installing them var enabled = true; var oldProgram = homegenie.ProgramManager.ProgramGet(pid); if (oldProgram != null) { homegenie.RaiseEvent( Domains.HomeGenie_System, Domains.HomeGenie_PackageInstaller, SourceModule.Master, "HomeGenie Package Installer", Properties.InstallProgressMessage, "= Replacing: '" + oldProgram.Name + "' with pid " + pid ); // if the program was already installed, inherit IsEnabled enabled = oldProgram.IsEnabled; homegenie.ProgramManager.ProgramRemove(oldProgram); } var programBlock = ProgramImport(pid, programFile, program.group.ToString()); if (programBlock != null) { string groupName = programBlock.Group; if (!String.IsNullOrWhiteSpace(groupName)) { // Add automation program group if does not exist Group newGroup = new Group() { Name = groupName }; if (homegenie.AutomationGroups.Find(g => g.Name == newGroup.Name) == null) { homegenie.AutomationGroups.Add(newGroup); homegenie.UpdateGroupsDatabase("Automation"); } } programBlock.IsEnabled = enabled; homegenie.RaiseEvent( Domains.HomeGenie_System, Domains.HomeGenie_PackageInstaller, SourceModule.Master, "HomeGenie Package Installer", Properties.InstallProgressMessage, "= Installed: '" + program.name.ToString() + "' as pid " + pid ); } else { // TODO: report error and stop the package install procedure success = false; } } } // Import Widgets in package foreach (var widget in pkgData.widgets) { homegenie.RaiseEvent( Domains.HomeGenie_System, Domains.HomeGenie_PackageInstaller, SourceModule.Master, "HomeGenie Package Installer", Properties.InstallProgressMessage, "= Downloading: " + widget.file.ToString() ); Utility.FolderCleanUp(installFolder); string widgetFile = Path.Combine(installFolder, widget.file.ToString()); if (File.Exists(widgetFile)) { File.Delete(widgetFile); } using (var client = new WebClientPx()) { try { client.DownloadFile(pkgFolderUrl + "/" + widget.file.ToString(), widgetFile); } catch (Exception e) { homegenie.RaiseEvent( Domains.HomeGenie_System, Domains.HomeGenie_PackageInstaller, SourceModule.Master, "HomeGenie Package Installer", Properties.InstallProgressMessage, "= ERROR: '" + e.Message + "'" ); success = false; } client.Dispose(); } if (success && WidgetImport(widgetFile, installFolder)) { homegenie.RaiseEvent( Domains.HomeGenie_System, Domains.HomeGenie_PackageInstaller, SourceModule.Master, "HomeGenie Package Installer", Properties.InstallProgressMessage, "= Installed: '" + widget.name.ToString() + "'" ); } else { // TODO: report error and stop the package install procedure success = false; } } // Import MIG Interfaces in package foreach (var migface in pkgData.interfaces) { homegenie.RaiseEvent( Domains.HomeGenie_System, Domains.HomeGenie_PackageInstaller, SourceModule.Master, "HomeGenie Package Installer", Properties.InstallProgressMessage, "= Downloading: " + migface.file.ToString() ); Utility.FolderCleanUp(installFolder); string migfaceFile = Path.Combine(installFolder, migface.file.ToString()); if (File.Exists(migfaceFile)) { File.Delete(migfaceFile); } using (var client = new WebClientPx()) { try { client.DownloadFile(pkgFolderUrl + "/" + migface.file.ToString(), migfaceFile); Utility.UncompressZip(migfaceFile, installFolder); File.Delete(migfaceFile); } catch (Exception e) { homegenie.RaiseEvent( Domains.HomeGenie_System, Domains.HomeGenie_PackageInstaller, SourceModule.Master, "HomeGenie Package Installer", Properties.InstallProgressMessage, "= ERROR: '" + e.Message + "'" ); success = false; } client.Dispose(); } if (success && InterfaceInstall(installFolder)) { homegenie.RaiseEvent( Domains.HomeGenie_System, Domains.HomeGenie_PackageInstaller, SourceModule.Master, "HomeGenie Package Installer", Properties.InstallProgressMessage, "= Installed: '" + migface.name.ToString() + "'" ); } else { // TODO: report error and stop the package install procedure success = false; } } } else { success = false; } if (success) { pkgData.folder_url = pkgFolderUrl; pkgData.install_date = DateTime.UtcNow; AddInstalledPackage(pkgData); homegenie.RaiseEvent( Domains.HomeGenie_System, Domains.HomeGenie_PackageInstaller, SourceModule.Master, "HomeGenie Package Installer", Properties.InstallProgressMessage, "= Status: Package Install Successful" ); } else { homegenie.RaiseEvent( Domains.HomeGenie_System, Domains.HomeGenie_PackageInstaller, SourceModule.Master, "HomeGenie Package Installer", Properties.InstallProgressMessage, "= Status: Package Install Error" ); } return(success); }