<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using NuPack.VisualStudio.Resources;

namespace NuPack.VisualStudio.Cmdlets {

    /// <summary>
    /// This command lists the available packages which are either from a package source or installed in the current solution.
    /// </summary>
    [Cmdlet(VerbsCommon.Get, "Package", DefaultParameterSetName = "Default")]
    public class GetPackageCmdlet : NuPackBaseCmdlet {
        private readonly IPackageRepositoryFactory _repositoryFactory;

        public GetPackageCmdlet()
            : this(PackageRepositoryFactory.Default) {
        }

        [Parameter(Position = 0)]
        public string Filter { get; set; }

        [Parameter(Position = 1, ParameterSetName = "Installed")]
        public SwitchParameter Installed { get; set; }

        [Parameter(Position = 2, ParameterSetName = "Updates")]
        public SwitchParameter Updates { get; set; }

        [Parameter(Position = 3, ParameterSetName = "Default")]
        [Parameter(ParameterSetName = "Updates")]
        public string Source { get; set; }


        public GetPackageCmdlet(IPackageRepositoryFactory repositoryFactory) {
            if (repositoryFactory == null) {
                throw new ArgumentNullException("repositoryFactory");
            }
            _repositoryFactory = repositoryFactory;
        }


        protected override void ProcessRecordCore() {
            if (!IsSolutionOpen && (Installed.IsPresent || Updates.IsPresent)) {
                WriteError(VsResources.Cmdlet_NoSolution);
                return;
            }

            IPackageRepository repository;
            if (Installed.IsPresent) {
                repository = PackageManager.LocalRepository;
            }
            else if (!String.IsNullOrEmpty(Source)) {
                repository = _repositoryFactory.CreateRepository(Source);
            }
            else if (IsSolutionOpen) {
                repository = PackageManager.SourceRepository;
            }
            else {
                repository = _repositoryFactory.CreateRepository(ActivePackageSource);
            }

            if (Updates.IsPresent) {
                ShowUpdatePackages(repository, Filter);
            }
            WritePackagesFromRepository(repository, Filter);
        }

        private void WritePackagesFromRepository(IPackageRepository repository, string filter) {
            WritePackages(repository.GetPackages(filter));
        }

        private void WritePackages(IEnumerable<IPackage> packages) {
            var query = from p in packages
                        select new {
                            Id = p.Id,
                            Version = p.Version,
                            Description = p.Description
                        };

            WriteObject(query, enumerateCollection: true);
        }

        private void ShowUpdatePackages(IPackageRepository repository, string filter) {
            IEnumerable<IPackage> updates = PackageManager.LocalRepository.GetUpdates(repository, filter);
            WritePackages(updates);
        }

        private static string ActivePackageSource {<commit_msg>Fix GetPackage when -Update is set<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using NuPack.VisualStudio.Resources;

namespace NuPack.VisualStudio.Cmdlets {

    /// <summary>
    /// This command lists the available packages which are either from a package source or installed in the current solution.
    /// </summary>
    [Cmdlet(VerbsCommon.Get, "Package", DefaultParameterSetName = "Default")]
    public class GetPackageCmdlet : NuPackBaseCmdlet {
        private readonly IPackageRepositoryFactory _repositoryFactory;

        public GetPackageCmdlet()
            : this(PackageRepositoryFactory.Default) {
        }

        [Parameter(Position = 0)]
        public string Filter { get; set; }

        [Parameter(Position = 1, ParameterSetName = "Installed")]
        public SwitchParameter Installed { get; set; }

        [Parameter(Position = 2, ParameterSetName = "Updates")]
        public SwitchParameter Updates { get; set; }

        [Parameter(Position = 3, ParameterSetName = "Default")]
        [Parameter(ParameterSetName = "Updates")]
        public string Source { get; set; }


        public GetPackageCmdlet(IPackageRepositoryFactory repositoryFactory) {
            if (repositoryFactory == null) {
                throw new ArgumentNullException("repositoryFactory");
            }
            _repositoryFactory = repositoryFactory;
        }


        protected override void ProcessRecordCore() {
            if (!IsSolutionOpen && (Installed.IsPresent || Updates.IsPresent)) {
                WriteError(VsResources.Cmdlet_NoSolution);
                return;
            }

            IPackageRepository repository;
            if (Installed.IsPresent) {
                repository = PackageManager.LocalRepository;
            }
            else if (!String.IsNullOrEmpty(Source)) {
                repository = _repositoryFactory.CreateRepository(Source);
            }
            else if (IsSolutionOpen) {
                repository = PackageManager.SourceRepository;
            }
            else {
                repository = _repositoryFactory.CreateRepository(ActivePackageSource);
            }

            if (Updates.IsPresent) {
                ShowUpdatePackages(repository, Filter);
            }
            else {
                WritePackagesFromRepository(repository, Filter);
            }
        }

        private void WritePackagesFromRepository(IPackageRepository repository, string filter) {
            WritePackages(repository.GetPackages(filter));
        }

        private void WritePackages(IEnumerable<IPackage> packages) {
            var query = from p in packages
                        select new {
                            Id = p.Id,
                            Version = p.Version,
                            Description = p.Description
                        };

            WriteObject(query, enumerateCollection: true);
        }

        private void ShowUpdatePackages(IPackageRepository repository, string filter) {
            IEnumerable<IPackage> updates = PackageManager.LocalRepository.GetUpdates(repository, filter);
            WritePackages(updates);
        }

        private static string ActivePackageSource {
{'fraction_non_alphanumeric': 0.07788018433179723, 'fraction_numerical': 0.001228878648233487, 'mean_word_length': 2.0916429249762585, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7651667', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1438, 'n_words': 352}