private void Initialize() { this.versionInfo = new VersionInfo() { Version = ProductInformation.AssemblyVersion, Configuration = Configuration.Release }; #if DEBUG this.versionInfo.Configuration = Configuration.Debug; #endif this.installablePackageName = PackagingInformation.GetPackageFileName(this.versionInfo) + PackagingInformation.GetInstallablePackageFileExtesion(); this.portablePackageName = PackagingInformation.GetPackageFileName(this.versionInfo) + " - Portable" + PackagingInformation.GetPortablePackageFileExtesion(); this.updatePackageName = PackagingInformation.GetPackageFileName(this.versionInfo) + PackagingInformation.GetUpdatePackageFileExtension(); this.packagerDoc = XDocument.Load("PackagerConfiguration.xml"); this.packageDirectory = (from p in this.packagerDoc.Element("Packager").Element("Packaging").Elements("PackageDirectory") select p.Value).FirstOrDefault(); if (!System.IO.Directory.Exists(this.packageDirectory)) { System.IO.Directory.CreateDirectory(this.packageDirectory); } }
async Task CreatePortableVersionAsync() { Console.ForegroundColor = ConsoleColor.Gray; Console.Write(Environment.NewLine + " - Creating portable version"); bool success = true; await Task.Run(() => { try { string currentFolder = Core.IO.ApplicationPaths.ExecutionFolder; // Delete all portable files if they exist foreach (FileInfo f in new DirectoryInfo(currentFolder).GetFiles(@"*" + PackagingInformation.GetPortablePackageFileExtesion())) { f.Delete(); } // Make sure we're in portable mode this.SetPortableMode(true); // Create the portable file using (var zip = new ZipFile()) { // Workaround for issue "ZipFile.AddFile fails depending of the file size" in DotNetZip. // When a file has a size which is a multiple of the default 128k buffer size for parallel compression, // extracting the zip file indicates a CRC error for that file and extracting the file fails. // "zip.ParallelDeflateThreshold = -1" disables parallel compression. // See: https://dotnetzip.codeplex.com/workitem/14087 zip.ParallelDeflateThreshold = -1; // Add directories List <string> directories = (from p in this.packagerDoc.Element("Packager").Element("Packaging").Element("Portable").Element("Directories").Elements("Directory") select p.Value).ToList(); foreach (string d in directories) { this.AddDirectoryToZip(zip, currentFolder, d); } // Add files List <string> files = (from p in this.packagerDoc.Element("Packager").Element("Packaging").Element("Portable").Element("Files").Elements("File") select p.Value).ToList(); foreach (string f in files) { this.AddFileToZip(zip, currentFolder, f); } // Save portable file package zip.Save(this.portablePackageName); } File.Copy(this.portablePackageName, Path.Combine(this.packageDirectory, this.portablePackageName), true); } catch (Exception) { success = false; } }); if (success) { Console.ForegroundColor = ConsoleColor.Green; Console.Write("\tOK"); Console.ForegroundColor = ConsoleColor.Gray; } else { Console.ForegroundColor = ConsoleColor.Red; Console.Write("\tERROR"); Console.ForegroundColor = ConsoleColor.Gray; } }
async Task CreateInstallableVersionAsync() { Console.ForegroundColor = ConsoleColor.Gray; Console.Write(Environment.NewLine + " - Creating installable version"); bool success = true; await Task.Run(() => { try { // Delete all installable files if they exist foreach (FileInfo f in new DirectoryInfo(Core.IO.ApplicationPaths.ExecutionFolder).GetFiles(@"*" + PackagingInformation.GetInstallablePackageFileExtesion())) { f.Delete(); } // Delete all portable files if they exist foreach (FileInfo f in new DirectoryInfo(Core.IO.ApplicationPaths.ExecutionFolder).GetFiles(@"*" + PackagingInformation.GetPortablePackageFileExtesion())) { f.Delete(); } // Make sure we're not in portable mode this.SetPortableMode(false); // Get the bin directory for the WIX runtimes var wixBinDirectory = (from p in this.packagerDoc.Element("Packager").Element("Packaging").Element("Installable").Elements("WixBinDirectory") select p.Value).FirstOrDefault(); // Create the .bat file for WIX if (File.Exists("CreateMsiInstaller.bat")) { File.Delete("CreateMsiInstaller.bat"); } using (TextWriter writer = File.CreateText("CreateMsiInstaller.bat")) { writer.WriteLine(@"DEL *.wixobj"); writer.WriteLine(@"DEL *.wixpdb"); writer.WriteLine(@"DEL *" + PackagingInformation.GetInstallablePackageFileExtesion()); writer.WriteLine(@"DEL *" + PackagingInformation.GetPortablePackageFileExtesion()); writer.WriteLine(@"""" + wixBinDirectory + @"\candle.exe"" *.wxs"); writer.WriteLine(String.Format(@"""" + wixBinDirectory + @"\light.exe"" -ext WixUIExtension -ext WixUtilExtension -out ""{0}"" *.wixobj", this.installablePackageName)); writer.WriteLine("PAUSE"); } Process.Start("CreateMsiInstaller.bat"); // Wait until the installable file is created while (!File.Exists(this.installablePackageName)) { Task.Delay(1000); } // Copy the installable version to the destination directory (this is a loop because the files can be in use by the .bat file) bool copySuccess = false; while (!copySuccess) { try { File.Copy(this.installablePackageName, Path.Combine(this.packageDirectory, this.installablePackageName), true); copySuccess = true; } catch (Exception) { copySuccess = false; } Task.Delay(1000); } } catch (Exception) { success = false; } }); if (success) { Console.ForegroundColor = ConsoleColor.Green; Console.Write("\tOK"); Console.ForegroundColor = ConsoleColor.Gray; } else { Console.ForegroundColor = ConsoleColor.Red; Console.Write("\tERROR"); Console.ForegroundColor = ConsoleColor.Gray; } }
private void DownloadOrInstallUpdate() { if (!string.IsNullOrEmpty(this.destinationPath)) { try { // A file was downloaded. Start the installer. System.IO.FileInfo msiFileInfo = new System.IO.DirectoryInfo(this.destinationPath).GetFiles("*" + PackagingInformation.GetInstallablePackageFileExtesion()).First(); Process.Start(msiFileInfo.FullName); } catch (Exception ex) { LogClient.Instance.Logger.Error("Could not start the MSI installer. Download link was opened instead. Exception: {0}", ex.Message); this.OpenDownloadLink(); } } else { // Nothing was downloaded, forward to the download site. this.OpenDownloadLink(); } }
private async Task CheckForUpdatesAsync() { // Indicate for the rest of the class that we are checking for updates // ------------------------------------------------------------------- this.checkingForUpdates = true; // We start checking for updates: stop the timer // --------------------------------------------- this.checkNewVersionTimer.Stop(); // Get the current version // ----------------------- var currentVersion = new VersionInfo { Version = ProductInformation.AssemblyVersion }; LogClient.Instance.Logger.Info("Update check: current version = {0}.{1}.{2}.{3}", currentVersion.Version.Major, currentVersion.Version.Minor, currentVersion.Version.Build, currentVersion.Version.Revision); // Get the latest version online // ----------------------------- if (!this.canCheckForUpdates) { return; // Stop here if the update check was disabled while we were running } VersionInfo latestOnlineVersion = await this.GetLatestOnlineVersionAsync(); LogClient.Instance.Logger.Info("Update check: latest online version = {0}.{1}.{2}.{3}", latestOnlineVersion.Version.Major, latestOnlineVersion.Version.Minor, latestOnlineVersion.Version.Build, latestOnlineVersion.Version.Revision); // Compare the versions // -------------------- if (currentVersion.IsOlder(latestOnlineVersion)) { if (this.automaticDownload) { // Automatic download is enabled // ----------------------------- // Define the name of the file to which we will download the update string updatePackageExtractedDirectoryFullPath = Path.Combine(this.updatesSubDirectory, PackagingInformation.GetPackageFileName(latestOnlineVersion)); string updatePackageDownloadedFileFullPath = Path.Combine(this.updatesSubDirectory, PackagingInformation.GetPackageFileName(latestOnlineVersion) + PackagingInformation.GetUpdatePackageFileExtension()); // Check if there is a directory with the name of the update package: // that means the file was already downloaded and extracted. if (Directory.Exists(updatePackageExtractedDirectoryFullPath)) { // The folder exists, that means that the new version was already extracted previously. // Raise an event that a new version is available for installation. if (this.canCheckForUpdates) { this.NewDownloadedVersionAvailable(latestOnlineVersion, updatePackageExtractedDirectoryFullPath); } } else { // Check if there is a package with the name of the update package: that would mean the update was already downloaded. if (!this.IsDownloadedPackageAvailable(updatePackageDownloadedFileFullPath)) { // No package available yet: download it. OperationResult downloadResult = await this.DownloadAsync(latestOnlineVersion, updatePackageDownloadedFileFullPath); if (downloadResult.Result) { OperationResult processResult = await this.ProcessDownloadedPackageAsync(); if (processResult.Result) { // Processing the downloaded file was successful. Raise an event that a new version is available for installation. if (this.canCheckForUpdates) { this.NewDownloadedVersionAvailable(latestOnlineVersion, updatePackageExtractedDirectoryFullPath); } } else { // Processing the downloaded file failed. Log the failure reason. LogClient.Instance.Logger.Error("Update check: could not process downloaded files. User is notified that there is a new version online. Exception: {0}", processResult.GetFirstMessage()); // Raise an event that there is a new version available online. if (this.canCheckForUpdates) { this.NewOnlineVersionAvailable(latestOnlineVersion); } } } else { // Downloading failed: log the failure reason. LogClient.Instance.Logger.Error("Update check: could not download the file. Exception: {0}", downloadResult.GetFirstMessage()); } } else { OperationResult extractResult = this.ExtractDownloadedPackage(updatePackageDownloadedFileFullPath); if (extractResult.Result) { // Extracting was successful. Raise an event that a new version is available for installation. if (this.canCheckForUpdates) { this.NewDownloadedVersionAvailable(latestOnlineVersion, updatePackageExtractedDirectoryFullPath); } } else { // Extracting failed: log the failure reason. LogClient.Instance.Logger.Error("Update check: could not extract the package. Exception: {0}", extractResult.GetFirstMessage()); } } } } else { // Automatic download is not enabled // --------------------------------- // Raise an event that a New version Is available for download if (this.canCheckForUpdates) { this.NewOnlineVersionAvailable(latestOnlineVersion); } } } else { this.NoNewVersionAvailable(latestOnlineVersion); LogClient.Instance.Logger.Info("Update check: no newer version was found."); } // Indicate for the rest of the class that we have finished checking for updates // ----------------------------------------------------------------------------- this.checkingForUpdates = false; // We're finished checking for updates: start the timer // ---------------------------------------------------- this.checkNewVersionTimer.Start(); }
public void Execute() { // Initialize // ---------- this.Initialize(); Console.WriteLine("Packager"); Console.WriteLine("========"); Console.WriteLine(Environment.NewLine + "Creating packages for '" + PackagingInformation.GetPackageFileName(this.versionInfo) + "'"); // Clean up the destination directory // ---------------------------------- foreach (string f in Directory.GetFiles(this.packageDirectory)) { File.Delete(f); } // Create the installable version // ------------------------------ Task createInstallableVersionTask = this.CreateInstallableVersionAsync(); createInstallableVersionTask.Wait(); // Create the update package // ------------------------- Task createUpdatePackageTask = this.CreateUpdatePackageAsync(); createUpdatePackageTask.Wait(); // Create the portable version // --------------------------- Task createPortableVersionTask = this.CreatePortableVersionAsync(); createPortableVersionTask.Wait(); // Do you wish to publish this package? // ------------------------------------ Console.ForegroundColor = ConsoleColor.White; Console.Write(Environment.NewLine + Environment.NewLine + "Do you wish to publish this package? [Y/N]"); ConsoleKeyInfo info = Console.ReadKey(); if (info.Key == ConsoleKey.Y) { Console.Write(Environment.NewLine + Environment.NewLine + "Please provide the publishing FTP Server:"); string server = Console.ReadLine(); Console.Write(Environment.NewLine + Environment.NewLine + "Please provide the publishing FTP Port:"); string port = Console.ReadLine(); Console.Write(Environment.NewLine + Environment.NewLine + "Please provide the publishing username:"******"Please provide the publishing password:"******"Package published"); } else { Console.ForegroundColor = ConsoleColor.Red; Console.Write(Environment.NewLine + Environment.NewLine + "Package not published"); } Console.ForegroundColor = ConsoleColor.White; Console.Write(Environment.NewLine + Environment.NewLine + "Press any key to close this window and go to the package directory"); Console.ReadKey(); Process.Start(@"explorer.exe", @"/select, """ + this.packageDirectory + @""""); Console.ReadKey(); }