Example #1
0
        protected virtual bool InstallInternal(string repoName, string packageName, string version = null, bool isUpdate = false)
        {
            if (isUpdate != PluginExists(repoName, packageName))
            {
                return(false);
            }

            if (isUpdate)
            {
                Uninstall(repoName, packageName);
            }

            var configRepo = Repositories
                             .FirstOrDefault(c => c.Name.Equals(repoName, StringComparison.OrdinalIgnoreCase));

            if (configRepo == null)
            {
                throw new ArgumentException("Repo not found: " + repoName, nameof(repoName));
            }

            if (!configRepo.Enabled)
            {
                throw new ArgumentException("Repo not enabled: " + repoName, nameof(repoName));
            }

            var repo = client.FetchRepository(configRepo.Url);
            List <NuGetPackage> packages = client.QueryPackages(repo, new NuGetQuery
            {
                Name = packageName
            })
                                           //Todo: remove after queries are fixed
                                           .Where(c => c.Id.ToLower().Contains(packageName.ToLower())).ToList();

            if (packages.Count == 0)
            {
                return(false);
            }

            if (packages.Count > 1)
            {
                throw new Exception("Multiple packages matched.");
            }

            var package = packages.First();

            version = version ?? package.Version;
            var targetVersion =
                package.Versions.FirstOrDefault(c => c.Version.Equals(version, StringComparison.OrdinalIgnoreCase));

            if (targetVersion == null)
            {
                throw new Exception("Version not found");
            }

            byte[] data = client.DownloadPackage(repo, targetVersion);

            string uid       = package.Id + "." + targetVersion.Version;
            var    targetDir = Path.Combine(Path.Combine(RepositoriesDirectory, configRepo.Name), uid);

            if (!Directory.Exists(targetDir))
            {
                Directory.CreateDirectory(targetDir);
            }

            File.WriteAllBytes(Path.Combine(targetDir, uid + ".nupkg"), data);
            return(true);
        }
Example #2
0
        protected virtual NuGetInstallResult InstallOrUpdate(string repoName, string packageName, string version = null, bool isPreRelease = false, bool isUpdate = false)
        {
            if (isUpdate && !PluginExists(repoName, packageName))
            {
                return(NuGetInstallResult.PackageNotFound);
            }

            if (isUpdate ||
                Packages.NugetPackages.Any(c => c.Id.Equals(packageName, StringComparison.OrdinalIgnoreCase) && c.Repository.Equals(repoName, StringComparison.OrdinalIgnoreCase)) ||
                PluginExists(repoName, packageName))
            {
                Uninstall(repoName, packageName);
            }

            var configRepo = Repositories
                             .FirstOrDefault(c => c.Name.Equals(repoName, StringComparison.OrdinalIgnoreCase));

            if (configRepo == null)
            {
                throw new ArgumentException("Repo not found: " + repoName, nameof(repoName));
            }

            if (!configRepo.Enabled)
            {
                throw new ArgumentException("Repo not enabled: " + repoName, nameof(repoName));
            }

            var repo = client.FetchRepository(configRepo.Url);
            List <NuGetPackage> packages = client.QueryPackages(repo, new NuGetQuery
            {
                Name = packageName
            }, isPreRelease)
                                           //Todo: remove after queries are fixed
                                           .Where(c => c.Id.ToLower().Contains(packageName.ToLower())).ToList();

            if (packages.Count == 0)
            {
                return(NuGetInstallResult.PackageNotFound);
            }

            if (packages.Count > 1)
            {
                throw new Exception("Multiple packages matched.");
            }

            var package = packages.First();

            version = version ?? package.Version;
            var targetVersion =
                package.Versions.FirstOrDefault(c => c.Version.Equals(version, StringComparison.OrdinalIgnoreCase));

            if (targetVersion == null)
            {
                return(NuGetInstallResult.VersionNotFound);
            }

            byte[] data = client.DownloadPackage(repo, targetVersion);

            string uid       = package.Id + "." + targetVersion.Version;
            var    targetDir = Path.Combine(Path.Combine(RepositoriesDirectory, configRepo.Name), uid);

            if (!Directory.Exists(targetDir))
            {
                Directory.CreateDirectory(targetDir);
            }

            File.WriteAllBytes(Path.Combine(targetDir, uid + ".nupkg"), data);
            PackagesConfiguration config = Packages;

            bool wasInstalled = false;

            foreach (var np in config.NugetPackages)
            {
                if (!np.Id.Equals(package.Id, StringComparison.OrdinalIgnoreCase) || np.Repository.Equals(repoName, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                wasInstalled = true;
                np.Version   = package.Version;
            }

            if (!wasInstalled)
            {
                var list = config.NugetPackages.ToList();
                list.Add(new ConfigurationNuGetPackage()
                {
                    Id         = package.Id,
                    Repository = repoName,
                    Version    = package.Version
                });
                config.NugetPackages = list.ToArray();
            }

            PackagesConfiguration.Set(config);
            PackagesConfiguration.Save();

            return(NuGetInstallResult.Success);
        }