Exemplo n.º 1
0
        public DirectoryInfo GetCurrentUpdateSubfolder(IEnumerable <UpdatePackageData> packagesToDownload)
        {
            DirectoryInfo     dir       = this.GetUpdatesParentFolder();
            UpdatePackageData latestPkg = packagesToDownload.OrderByDescending(x => x.Version, new TelimenaVersionStringComparer()).First();

            return(new DirectoryInfo(Path.Combine(dir.FullName, latestPkg.Version)));
        }
Exemplo n.º 2
0
        private async Task <FileInfo> InstallUpdater(UpdatePackageData pkgData)
        {
            var updaterExecutable = this.locator.GetUpdater(this.programInfo);

            if (pkgData == null)
            {
                return(updaterExecutable);
            }

            FileDownloadResult result = await this.messenger.DownloadFile(pkgData.DownloadUrl).ConfigureAwait(false);

            FileInfo pkgFile = this.locator.GetInstallerPackagePath(result);

            if (pkgFile.Exists)
            {
                pkgFile.Delete();
            }

            try
            {
                await SaveStreamToPath(pkgData, pkgFile, result.Stream).ConfigureAwait(false);
            }
            catch (IOException)
            {
            }
            await this.updateInstaller.InstallUpdaterUpdate(pkgFile, updaterExecutable).ConfigureAwait(false);

            return(updaterExecutable);
        }
Exemplo n.º 3
0
        protected async Task StoreUpdatePackage(UpdatePackageData pkgData, DirectoryInfo updatesFolder)
        {
            FileDownloadResult result = await this.messenger.DownloadFile(pkgData.DownloadUrl).ConfigureAwait(false);

            FileInfo pkgFile = this.locator.BuildUpdatePackagePath(updatesFolder, pkgData);

            Trace.WriteLine($"{result.FileName} to be stored into {pkgFile.FullName}");


            await SaveStreamToPath(pkgData, pkgFile, result.Stream).ConfigureAwait(false);
        }
Exemplo n.º 4
0
        public void TestLocator()
        {
            Locator locator =
                new Locator(new ProgramInfo {
                Name = "TestApp"
            });
            var pkg = new UpdatePackageData()
            {
                FileName = "Updates.zip", Version = "1.2.0"
            };

            var parent = locator.GetCurrentUpdateSubfolder(new[] { pkg });


            var updatePackage = locator.BuildUpdatePackagePath(parent, pkg);

            Assert.AreEqual($@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\Telimena\TestApp\Updates\1.2.0\1.2.0\Updates.zip", updatePackage.FullName);
        }
Exemplo n.º 5
0
        private async Task <Tuple <bool, FileInfo> > DownloadUpdatesIfNeeded(UpdatePromptingModes confirmationMode, IReadOnlyList <UpdatePackageData> packagesToInstall
                                                                             , UpdatePackageData updaterUpdate)
        {
            string maxVersion = packagesToInstall.GetMaxVersion();

            bool     installUpdatesNow;
            FileInfo updaterFile = null;

            switch (confirmationMode)
            {
            case UpdatePromptingModes.PromptAfterDownload:
                updaterFile = await this.InstallUpdater(updaterUpdate).ConfigureAwait(false);

                await this.DownloadUpdatePackages(packagesToInstall).ConfigureAwait(false);

                installUpdatesNow = this.inputReceiver.ShowInstallUpdatesNowQuestion(maxVersion, packagesToInstall.Sum(x => x.FileSizeBytes), this.programInfo);
                return(new Tuple <bool, FileInfo>(installUpdatesNow, updaterFile));

            case UpdatePromptingModes.PromptBeforeDownload:
                installUpdatesNow = this.inputReceiver.ShowDownloadAndInstallUpdatesQuestion(maxVersion, packagesToInstall.Sum(x => x.FileSizeBytes), this.programInfo);
                break;

            case UpdatePromptingModes.DontPrompt:
                installUpdatesNow = true;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(confirmationMode), confirmationMode, null);
            }

            if (installUpdatesNow)
            {
                updaterFile = await this.InstallUpdater(updaterUpdate).ConfigureAwait(false);

                await this.DownloadUpdatePackages(packagesToInstall).ConfigureAwait(false);
            }

            return(new Tuple <bool, FileInfo>(installUpdatesNow, updaterFile));
        }
Exemplo n.º 6
0
        private static async Task SaveStreamToPath(UpdatePackageData pkgData, FileInfo pkgFile, Stream stream)
        {
            FileStream fileStream = null;

            try
            {
                pkgFile.Directory?.Create();
                using (fileStream = new FileStream(pkgFile.FullName, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    await stream.CopyToAsync(fileStream).ContinueWith(copyTask => {
                        fileStream.Close();
                        stream.Close();
                    }
                                                                      ).ConfigureAwait(false);

                    pkgData.StoredFilePath = pkgFile.FullName;
                }
            }
            catch (Exception ex)
            {
                fileStream?.Close();
                using (fileStream = new FileStream(pkgFile.FullName + 2, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    await stream.CopyToAsync(fileStream).ContinueWith(copyTask => {
                        fileStream.Close();
                        stream.Close();
                    }
                                                                      ).ConfigureAwait(false);

                    pkgData.StoredFilePath = pkgFile.FullName;
                }
                throw new IOException("Error while saving stream to file", ex);
            }
        }
Exemplo n.º 7
0
        public async Task HandleUpdates(UpdatePromptingModes confirmationMode, IReadOnlyList <UpdatePackageData> packagesToInstall, UpdatePackageData updaterUpdate)
        {
            try
            {
                if (packagesToInstall != null && packagesToInstall.Any())
                {
                    var shouldInstallNow = await this.DownloadUpdatesIfNeeded(confirmationMode, packagesToInstall, updaterUpdate).ConfigureAwait(false);

                    if (!shouldInstallNow.Item1)
                    {
                        return;
                    }

                    FileInfo instructionsFile = UpdateInstructionCreator.CreateInstructionsFile(packagesToInstall, this.programInfo.Program, this.programInfo.Program.Name);
                    foreach (UpdatePackageData item in packagesToInstall)
                    {
                        this.telemetryModule.Event(BuiltInEventKeys.UpdateInstallation, properties: new Dictionary <string, string>()
                        {
                            { "PackageVersion", item.Version },
                            { "FromVersionAssembly", this.programInfo.Program.PrimaryAssembly.VersionData.AssemblyVersion },
                            { "FromVersionFile", this.programInfo.Program.PrimaryAssembly.VersionData.FileVersion },
                        });
                    }
                    this.telemetryModule.SendAllDataNow();
                    this.updateInstaller.InstallUpdates(instructionsFile, shouldInstallNow.Item2);
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"An error occured while handling updates", ex);
            }
        }
Exemplo n.º 8
0
        public async Task HandleUpdates(UpdatePromptingModes confirmationMode, IReadOnlyList <UpdatePackageData> packagesToInstall, UpdatePackageData updaterUpdate)
        {
            try
            {
                if (packagesToInstall != null && packagesToInstall.Any())
                {
                    var shouldInstallNow = await this.DownloadUpdatesIfNeeded(confirmationMode, packagesToInstall, updaterUpdate).ConfigureAwait(false);

                    if (!shouldInstallNow.Item1)
                    {
                        return;
                    }

                    FileInfo instructionsFile = UpdateInstructionCreator.CreateInstructionsFile(packagesToInstall, this.programInfo.Program, this.programInfo.Program.Name);

                    this.updateInstaller.InstallUpdates(instructionsFile, shouldInstallNow.Item2);
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"An error occured while handling updates", ex);
            }
        }
Exemplo n.º 9
0
 public FileInfo BuildUpdatePackagePath(DirectoryInfo updatesFolder, UpdatePackageData packageData)
 {
     return(Static.BuildUpdatePackagePath(updatesFolder, packageData));
 }
Exemplo n.º 10
0
 public static FileInfo BuildUpdatePackagePath(DirectoryInfo updatesFolder, UpdatePackageData packageData)
 {
     return(new FileInfo(Path.Combine(updatesFolder.FullName, packageData.Version, packageData.FileName)));
 }