예제 #1
0
        public static async Task InstallPackage(ChocolateyPackage Package)
        {
            await Task.Run(() =>
            {
                ChocoLogs logs = new ChocoLogs();

                Lets.GetChocolatey().Set(x =>
                {
                    x.CommandName = "install";
                    x.PackageNames = Package.Id;
                    x.AcceptLicense = true;
                    x.Force = true;
                    x.ForceDependencies = true;
                    x.Version = Package.Version.ToString();
                    x.Sources = $"{Package.PackageFile.DirectoryName}";

                }).SetCustomLogging(logs).Run();

                if (logs.ErrorMessages.Count > 0)
                {
                    if (!logs.WarningMessages.Any(x => x.Contains("Chocolatey installed 1/1 package(s). 0 package(s) failed")))
                    {
                        throw new Exception("Installation Failed!");
                    }
                }
            });
        }
예제 #2
0
 public async Task LoadPackageData()
 {
     this.State = SetupState.LoadPackageData;
     this.Package = await Chocolatey.GetPackage(this.PackageId, this.Version);
     this.State = SetupState.ReadyToInstall;
     this.PackageId = Package.Id;
     this.Title = Package.Title;
 }
예제 #3
0
        public static async Task<ChocolateyPackage> GetPackage(string PackageId, string Version = null)
        {
            return await Task<ChocolateyPackage>.Run(() =>
            {
                ChocolateyPackage package = new ChocolateyPackage();

                string downloadUrl = $"https://chocolatey.org/api/v2/package/{PackageId}";
                if(!string.IsNullOrEmpty(Version))
                {
                    downloadUrl += $"/{Version}";
                }
                XDocument nuspec = null;

                //download package and save it temporary
                using (var web = new WebClient())
                {
                    string tempPackageFile = Path.GetTempFileName() + ".nupkg";
                    web.DownloadFile(downloadUrl, tempPackageFile);
                    package.PackageFile = new FileInfo(tempPackageFile);

                    //unzip <PackageId>.nuspec
                    using (ZipFile zip = ZipFile.Read(package.PackageFile.FullName))
                    {
                        ZipEntry e = zip[$"{PackageId}.nuspec"];

                        using (MemoryStream output = new MemoryStream())
                        {
                            e.Extract(output);
                            var data = output.ToArray();
                            string encoded = Encoding.UTF8.GetString(data);
                            nuspec = XDocument.Parse(encoded);
                        }
                    }
                }

                var metadata = nuspec.Root.FindElement("metadata");

                package.Id = metadata.FindElement("id")?.Value;
                package.Version = metadata.FindElement("version")?.Value;
                package.Title = metadata.FindElement("title")?.Value;
                package.Author = metadata.FindElement("authors")?.Value;
                package.Owner = metadata.FindElement("owners")?.Value;
                package.LicenseUrl = metadata.FindElement("licenseUrl")?.Value;
                package.ProjectUrl = metadata.FindElement("projectUrl")?.Value;
                package.IconUrl = metadata.FindElement("iconUrl")?.Value;
                package.Tags = metadata.FindElement("tags")?.Value;

                string newFileName = $"{Path.GetTempPath()}chocopkg\\{package.Id}.{package.Version}.nupkg";
                Directory.CreateDirectory($"{Path.GetTempPath()}chocopkg\\");
                File.Delete(newFileName);
                package.PackageFile.MoveTo(newFileName);
                Directory.CreateDirectory($"{Path.GetTempPath()}chocopkg\\");
                return package;
            });
        }
예제 #4
0
 public async Task LoadPackage()
 {
     this.Package = await Chocolatey.GetPackage(this.PackageId);
 }