Esempio n. 1
0
        public void test_packagename_and_version_filter_combined()
        {
            var fileName        = "sample-csproj.xml";
            var expectedPackage = "Microsoft.NET.Test.Sdk";
            var expectedVersion = "16.7.1";

            var nameFilter    = "*Test*";
            var versionFilter = "16.*.1";

            var xmlProjectFile = Path.Combine(SamplesDir, fileName);

            var filter = new SearchQueryFilter()
            {
                Name    = nameFilter,
                Version = versionFilter
            };

            var projectFile = new ProjectFile(xmlProjectFile, filter);

            projectFile.QueryPackages();
            WriteOutput(projectFile);

            var fndPackage = projectFile.LastQueriedPackages.First();

            Assert.Equal(expectedPackage, fndPackage.Name);
            Assert.Equal(expectedVersion, fndPackage.Version);
        }
Esempio n. 2
0
        public void RenderResults(string basePath, SearchQueryFilter filter, IEnumerable <ProjectFile> projFiles)
        {
            var outPutModel = projFiles.Select(x => new
            {
                ProjectFile       = x.Filename.Replace(basePath + @"\", ""),
                Version           = x.Version,
                OutputType        = x.OutputType,
                Sdk               = x.ProjectSdk,
                TargetFramework   = x.TargetFramework,
                ProjectReferences = x.GetProjectReferences()?.Select(p => p.Include),
                PackageReferences = x.LastQueriedPackages,
            });

            var total = outPutModel.Count();
            var curr  = 0;

            ConsoleRender.W("\n[\n");
            foreach (var projectFile in outPutModel)
            {
                curr++;
                var json = JsonSerializer.Serialize(projectFile, JsonSerializerOptions);
                ConsoleRender.W(json);
                if (curr < total)
                {
                    ConsoleRender.W(",");
                }
                ConsoleRender.W("\n");
            }
            ConsoleRender.W("]\n");
        }
        public void RenderResults(string basePath, SearchQueryFilter filter,
                                  IEnumerable <ProjectFile> projFiles)
        {
            var strPad       = new string(' ', StartTabPad);
            var maxNameWidth = GetMaxLength(projFiles.SelectMany(x => x.LastQueriedPackages));

            ConsoleRender.W($"{basePath}\n");
            ConsoleRender.W($"{DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()}\n");
            ConsoleRender.W(
                $"Filter: {JsonSerializer.Serialize(filter, new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, IgnoreNullValues = true })}\n\n");

            foreach (var projectFile in projFiles)
            {
                var packages = projectFile.LastQueriedPackages;

                if (packages != null)
                {
                    Render(projectFile);
                    Render(packages, StartTabPad, maxNameWidth);
                    Render(projectFile.GetProjectReferences(), StartTabPad, maxNameWidth);
                }
            }

            // render summary
            // RenderSummaryCounts(projFiles, strPad);
        }
Esempio n. 4
0
        public bool SetNugetPackageVersions(SearchQueryFilter filter, string setVersion,
                                            IEnumerable <ProjectFile> projFiles, string strPad, ProjectNugetVersionUpdater tools)
        {
            var nameFilter    = filter.Name;
            var versionFilter = filter.Version;

            var numProjectFiles = projFiles.Count();

            if (numProjectFiles < 1)
            {
                ConsoleRender.W("No file(s) matching spec:")
                .W($"\n{strPad}Name    : {nameFilter}")
                .W($"\n{strPad}Version : {versionFilter}\n");
                return(true);
            }

            ConsoleRender.W($"Are you sure you want to change versions for {numProjectFiles} project files to ")
            .W($"{setVersion}", ConsoleColor.DarkMagenta).W(" ? Y/N: ");

            var inp = Console.ReadLine();

            if (!inp.Equals("Y", StringComparison.InvariantCultureIgnoreCase))
            {
                return(true);
            }

            // update versions
            tools.UpdateVersionInProjects(projFiles, nameFilter, versionFilter, setVersion, true);

            ConsoleRender.W($"Updated {numProjectFiles} projects with packages to version {setVersion}");
            return(false);
        }
Esempio n. 5
0
 private SearchColumn GetSearchColumn(SearchQueryFilter filter)
 {
     return(new SearchColumn {
         ColumnName = filter.ColumnName,
         ReferenceSchemaName = _schemaName,
         SchemaName = filter.SchemaName,
         Type = filter.Type
     });
 }
Esempio n. 6
0
        public ProjectFile(string filename, SearchQueryFilter filter)
        {
            _filename      = filename;
            _mapper        = new PackageReferenceXmlMapper();
            _nameFilter    = filter.Name;
            _versionFilter = filter.Version;

            ReadProjectInfo();
        }
Esempio n. 7
0
        static void Execute(string basePath, SearchQueryFilter filter, string?setVersion, string outputFile,
                            string outputFileFormatStr)
        {
            var tools           = new ProjectNugetVersionUpdater(new DotNetPackageReferenceUpdater());
            var projFileService = new ProjectFileService();

            OutputFileFormat outputFormat = !string.IsNullOrEmpty(outputFileFormatStr) ?
                                            Enum.Parse <OutputFileFormat>(outputFileFormatStr, true)
                : OutputFileFormat.Default;

            IProjectFileResultsRenderer projectFileRenderer;

            basePath = Path.GetFullPath(basePath);
            var projFiles = projFileService.GetProjectFilesByFilter(basePath, filter);

            if (!string.IsNullOrEmpty(outputFile))
            {
                var outputFilePath = Path.GetFullPath(outputFile);

                Console.Write($"Writing output to {outputFilePath}");
                if (!string.IsNullOrEmpty(outputFileFormatStr))
                {
                    Console.Write($" format: {outputFileFormatStr}");
                }
                Console.WriteLine();

                // no format so just redirect
                FileOutput.RedirectConsoleToFile(outputFilePath);
            }

            switch (outputFormat)
            {
            case OutputFileFormat.Json:
                projectFileRenderer = new ProjectFileJsonRenderer();
                break;

            default:
                projectFileRenderer = new ProjectFileConsoleRenderer();
                break;
            }

            projectFileRenderer.RenderResults(basePath, filter, projFiles);

            if (!string.IsNullOrEmpty(setVersion))
            {
                var startTabPad = 10;
                var strPad      = new string(' ', startTabPad);
                if (projFileService.SetNugetPackageVersions(filter, setVersion, projFiles, strPad, tools))
                {
                    return;
                }
            }
        }
Esempio n. 8
0
        private MatchQuery BuildMatchQuery(SearchQueryFilter filter, string value)
        {
            var searchColumn = GetSearchColumn(filter);
            var columnName   = _searchColumnNameProvider.GetSearchColumnName(searchColumn);
            var matchQuery   = new MatchQuery {
                Query = value,
                MinimumShouldMatch = MinimumShouldMatch.Percentage(MinimumShouldMatchPercentage),
                Field = new Field(columnName)
            };

            _lastBuildedMatchQuery = matchQuery;
            return(matchQuery);
        }
Esempio n. 9
0
        public IEnumerable <ProjectFile> GetProjectFilesByFilter(string basePath, SearchQueryFilter filter, string projFilePattern = "*.csproj")
        {
            var projFiles = Directory.GetFiles(basePath, projFilePattern, SearchOption.AllDirectories)
                            .Select(x => new ProjectFile(x, filter));

            if (!string.IsNullOrEmpty(filter.TargetFramework))
            {
                projFiles = projFiles.Where(x => x.TargetFramework == filter.TargetFramework);
            }

            projFiles = projFiles.Where(x => x.QueryPackages().Any()).ToList();
            return(projFiles);
        }
Esempio n. 10
0
        private static CommandLineApplication PrepCommandLineApplication()
        {
            var app = new CommandLineApplication();

            app.HelpOption();

            var basePathOption          = app.Option("-b|--base <PATH>", "Base Path", CommandOptionType.SingleValue);
            var optionName              = app.Option("-n|--name <NAME>", "Package Name filter", CommandOptionType.SingleValue);
            var outputFile              = app.Option("-o|--output <FILE>", "Send output to file", CommandOptionType.SingleValue);
            var outputFileFormartOption =
                app.Option("-of|--output-format", "File output format [json]", CommandOptionType.SingleValue);

            var optionTargetFrameworkFilter = app.Option("-fw|--framework <TARGETFRAMEWORK>", "Target framework filter",
                                                         CommandOptionType.SingleValue);
            var optionVersionFilter = app.Option("-v|--version <VERSION>", "Version filter", CommandOptionType.SingleValue);
            var optionSetVersion    = app.Option("-sv|--set-version <VERSION>", "Update versions of query to new version",
                                                 CommandOptionType.SingleValue);

            app.OnExecute(() =>
            {
                var basePathValue       = basePathOption.HasValue() ? basePathOption.Value() : null;
                var fwFilter            = optionTargetFrameworkFilter.HasValue() ? optionTargetFrameworkFilter.Value() : null;
                var nameFilter          = optionName.HasValue() ? optionName.Value() : null;
                var versionFilter       = optionVersionFilter.HasValue() ? optionVersionFilter.Value() : null;
                var setVersion          = optionSetVersion.HasValue() ? optionSetVersion.Value() : null;
                var outputFileOption    = outputFile.HasValue() ? outputFile.Value() : null;
                var outputFileFormatVal = outputFileFormartOption.HasValue() ? outputFileFormartOption.Value(): null;

                var searchQuery = new SearchQueryFilter()
                {
                    TargetFramework = fwFilter,
                    Name            = nameFilter,
                    Version         = versionFilter
                };


                var remaining = app.RemainingArguments;
                Execute(basePathValue, searchQuery, setVersion, outputFileOption, outputFileFormatVal);
            });
            return(app);
        }
Esempio n. 11
0
        public void test_version_filter()
        {
            var fileName        = "sample-csproj.xml";
            var expectedVersion = "16.7.1";
            var versionFilter   = "16.7.*";
            var xmlProjectFile  = Path.Combine(SamplesDir, fileName);

            var filter = new SearchQueryFilter()
            {
                Name    = "",
                Version = versionFilter
            };

            var projectFile = new ProjectFile(xmlProjectFile, filter);

            projectFile.QueryPackages();
            WriteOutput(projectFile);

            Assert.All(projectFile.LastQueriedPackages,
                       x => Assert.True(x.Version.Contains("16.7."), "invalid version"));
        }
Esempio n. 12
0
        public void test_framework_filter()
        {
            var net5query = new SearchQueryFilter()
            {
                TargetFramework = "net5.0"
            };
            var net3query = new SearchQueryFilter()
            {
                TargetFramework = "netcoreapp3.1"
            };

            var expectedNet5ProjectCount = 1;
            var expectedNet3ProjectCount = 2;

            var net5queryResults = _projectFileService.GetProjectFilesByFilter(GetSamplesDir(), net5query, "*-csproj.xml");

            Assert.Equal(expectedNet5ProjectCount, net5queryResults.Count());

            var net3queryResults = _projectFileService.GetProjectFilesByFilter(GetSamplesDir(), net3query, "*-csproj.xml");

            Assert.Equal(expectedNet3ProjectCount, net3queryResults.Count());
        }
Esempio n. 13
0
        private QueryContainer BuildFilterQuery(SearchQueryFilter filter)
        {
            var valuesCollection = filter.Value;

            using (var valueEnumerator = valuesCollection.GetEnumerator()) {
                valueEnumerator.MoveNext();
                var currentValue = valueEnumerator.Current;
                var filterQuery  = new QueryContainer();
                if (currentValue != null)
                {
                    filterQuery = BuildMatchQuery(filter, currentValue);
                }
                while (valueEnumerator.MoveNext())
                {
                    currentValue = valueEnumerator.Current;
                    if (currentValue != null)
                    {
                        filterQuery = filterQuery || BuildMatchQuery(filter, currentValue);
                    }
                }
                return(filterQuery);
            }
        }