Beispiel #1
0
        public virtual bool Uninstall(string repoName, string packageName)
        {
            PackagesConfiguration config = Packages;
            var list = config.NugetPackages.ToList();

            list.RemoveAll(d => d.Id.Equals(packageName, StringComparison.OrdinalIgnoreCase) && d.Repository.Equals(repoName, StringComparison.OrdinalIgnoreCase));
            config.NugetPackages = list.ToArray();
            PackagesConfiguration.Set(config);
            PackagesConfiguration.Save();

            var repoDir = Path.Combine(RepositoriesDirectory, repoName);

            if (!Directory.Exists(repoDir))
            {
                return(true);
            }

            foreach (var directory in Directory.GetDirectories(repoDir))
            {
                var name = new DirectoryInfo(directory).Name;
                if (name.ToLowerInvariant().StartsWith(packageName.ToLowerInvariant()))
                {
                    Directory.Delete(directory, true);
                    return(true);
                }
            }

            return(false);
        }
Beispiel #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);
        }