示例#1
0
        public async Task ExecuteCommandAsync(ListPackageArgs listPackageArgs)
        {
            if (!File.Exists(listPackageArgs.Path))
            {
                Console.Error.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                      Strings.ListPkg_ErrorFileNotFound,
                                                      listPackageArgs.Path));
                return;
            }
            //If the given file is a solution, get the list of projects
            //If not, then it's a project, which is put in a list
            var projectsPaths = Path.GetExtension(listPackageArgs.Path).Equals(".sln") ?
                                MSBuildAPIUtility.GetProjectsFromSolution(listPackageArgs.Path).Where(f => File.Exists(f)) :
                                new List <string>(new string[] { listPackageArgs.Path });

            var autoReferenceFound = false;
            var msBuild            = new MSBuildAPIUtility(listPackageArgs.Logger);

            //Print sources, but not for generic list (which is offline)
            if (listPackageArgs.ReportType != ReportType.Default)
            {
                Console.WriteLine();
                Console.WriteLine(Strings.ListPkg_SourcesUsedDescription);
                ProjectPackagesPrintUtility.PrintSources(listPackageArgs.PackageSources);
                Console.WriteLine();
            }

            foreach (var projectPath in projectsPaths)
            {
                //Open project to evaluate properties for the assets
                //file and the name of the project
                var project = MSBuildAPIUtility.GetProject(projectPath);

                if (!MSBuildAPIUtility.IsPackageReferenceProject(project))
                {
                    Console.Error.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                          Strings.Error_NotPRProject,
                                                          projectPath));
                    Console.WriteLine();
                    continue;
                }

                var projectName = project.GetPropertyValue(ProjectName);

                var assetsPath = project.GetPropertyValue(ProjectAssetsFile);

                // If the file was not found, print an error message and continue to next project
                if (!File.Exists(assetsPath))
                {
                    Console.Error.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                          Strings.Error_AssetsFileNotFound,
                                                          projectPath));
                    Console.WriteLine();
                }
                else
                {
                    var lockFileFormat = new LockFileFormat();
                    var assetsFile     = lockFileFormat.Read(assetsPath);

                    // Assets file validation
                    if (assetsFile.PackageSpec != null &&
                        assetsFile.Targets != null &&
                        assetsFile.Targets.Count != 0)
                    {
                        // Get all the packages that are referenced in a project
                        var packages = msBuild.GetResolvedVersions(project.FullPath, listPackageArgs.Frameworks, assetsFile, listPackageArgs.IncludeTransitive, includeProjects: listPackageArgs.ReportType == ReportType.Default);

                        // If packages equals null, it means something wrong happened
                        // with reading the packages and it was handled and message printed
                        // in MSBuildAPIUtility function, but we need to move to the next project
                        if (packages != null)
                        {
                            // No packages means that no package references at all were found in the current framework
                            if (!packages.Any())
                            {
                                Console.WriteLine(string.Format(Strings.ListPkg_NoPackagesFoundForFrameworks, projectName));
                            }
                            else
                            {
                                if (listPackageArgs.ReportType != ReportType.Default)  // generic list package is offline -- no server lookups
                                {
                                    PopulateSourceRepositoryCache(listPackageArgs);
                                    await GetRegistrationMetadataAsync(packages, listPackageArgs);
                                    await AddLatestVersionsAsync(packages, listPackageArgs);
                                }

                                bool printPackages = FilterPackages(packages, listPackageArgs);

                                // Filter packages for dedicated reports, inform user if none
                                if (listPackageArgs.ReportType != ReportType.Default && !printPackages)
                                {
                                    switch (listPackageArgs.ReportType)
                                    {
                                    case ReportType.Outdated:
                                        Console.WriteLine(string.Format(Strings.ListPkg_NoUpdatesForProject, projectName));
                                        break;

                                    case ReportType.Deprecated:
                                        Console.WriteLine(string.Format(Strings.ListPkg_NoDeprecatedPackagesForProject, projectName));
                                        break;

                                    case ReportType.Vulnerable:
                                        Console.WriteLine(string.Format(Strings.ListPkg_NoVulnerablePackagesForProject, projectName));
                                        break;
                                    }
                                }

                                printPackages = printPackages || ReportType.Default == listPackageArgs.ReportType;
                                if (printPackages)
                                {
                                    var hasAutoReference = false;
                                    ProjectPackagesPrintUtility.PrintPackages(packages, projectName, listPackageArgs, ref hasAutoReference);
                                    autoReferenceFound = autoReferenceFound || hasAutoReference;
                                }
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine(string.Format(Strings.ListPkg_ErrorReadingAssetsFile, assetsPath));
                    }

                    // Unload project
                    ProjectCollection.GlobalProjectCollection.UnloadProject(project);
                }
            }

            // Print a legend message for auto-reference markers used
            if (autoReferenceFound)
            {
                Console.WriteLine(Strings.ListPkg_AutoReferenceDescription);
            }
        }
        public async Task ExecuteCommandAsync(ListPackageArgs listPackageArgs)
        {
            if (!File.Exists(listPackageArgs.Path))
            {
                Console.Error.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                      Strings.ListPkg_ErrorFileNotFound,
                                                      listPackageArgs.Path));
                return;
            }
            //If the given file is a solution, get the list of projects
            //If not, then it's a project, which is put in a list
            var projectsPaths = Path.GetExtension(listPackageArgs.Path).Equals(".sln") ?
                                MSBuildAPIUtility.GetProjectsFromSolution(listPackageArgs.Path).Where(f => File.Exists(f)) :
                                new List <string>(new string[] { listPackageArgs.Path });

            var autoReferenceFound = false;
            var deprecatedFound    = false;

            var msBuild = new MSBuildAPIUtility(listPackageArgs.Logger);

            //Print sources
            if (listPackageArgs.IncludeOutdated || listPackageArgs.IncludeDeprecated)
            {
                Console.WriteLine();
                Console.WriteLine(Strings.ListPkg_SourcesUsedDescription);
                ProjectPackagesPrintUtility.PrintSources(listPackageArgs.PackageSources);
                Console.WriteLine();
            }

            foreach (var projectPath in projectsPaths)
            {
                //Open project to evaluate properties for the assets
                //file and the name of the project
                var project = MSBuildAPIUtility.GetProject(projectPath);

                if (!MSBuildAPIUtility.IsPackageReferenceProject(project))
                {
                    Console.Error.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                          Strings.Error_NotPRProject,
                                                          projectPath));
                    Console.WriteLine();
                    continue;
                }

                var projectName = project.GetPropertyValue(ProjectName);

                var assetsPath = project.GetPropertyValue(ProjectAssetsFile);

                // If the file was not found, print an error message and continue to next project
                if (!File.Exists(assetsPath))
                {
                    Console.Error.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                          Strings.Error_AssetsFileNotFound,
                                                          projectPath));
                    Console.WriteLine();
                }
                else
                {
                    var lockFileFormat = new LockFileFormat();
                    var assetsFile     = lockFileFormat.Read(assetsPath);

                    // Assets file validation
                    if (assetsFile.PackageSpec != null &&
                        assetsFile.Targets != null &&
                        assetsFile.Targets.Count != 0)
                    {
                        // Get all the packages that are referenced in a project
                        var packages = msBuild.GetResolvedVersions(project.FullPath, listPackageArgs.Frameworks, assetsFile, listPackageArgs.IncludeTransitive);

                        // If packages equals null, it means something wrong happened
                        // with reading the packages and it was handled and message printed
                        // in MSBuildAPIUtility function, but we need to move to the next project
                        if (packages != null)
                        {
                            // No packages means that no package references at all were found
                            if (!packages.Any())
                            {
                                Console.WriteLine(string.Format(Strings.ListPkg_NoPackagesFoundForFrameworks, projectName));
                            }
                            else
                            {
                                var printPackages = true;

                                // Handle --outdated
                                if (listPackageArgs.IncludeOutdated)
                                {
                                    await AddLatestVersionsAsync(packages, listPackageArgs);

                                    printPackages = await FilterOutdatedPackagesAsync(packages);

                                    // If after filtering, all packages were found up to date, inform the user
                                    // and do not print anything
                                    if (!printPackages)
                                    {
                                        Console.WriteLine(string.Format(Strings.ListPkg_NoUpdatesForProject, projectName));
                                    }
                                }
                                // Handle --deprecated
                                else if (listPackageArgs.IncludeDeprecated)
                                {
                                    await GetDeprecationInfoAsync(packages, listPackageArgs);

                                    printPackages = await FilterDeprecatedPackagesAsync(packages);

                                    // If after filtering, no packages were found to be deprecated, inform the user
                                    // and do not print anything
                                    if (!printPackages)
                                    {
                                        Console.WriteLine(string.Format(Strings.ListPkg_NoDeprecatedPackagesForProject, projectName));
                                    }
                                }

                                // Make sure print is still needed, which may be changed in case
                                // outdated filtered all packages out
                                if (printPackages)
                                {
                                    var printPackagesResult = await ProjectPackagesPrintUtility.PrintPackagesAsync(
                                        packages,
                                        projectName,
                                        listPackageArgs.IncludeTransitive,
                                        listPackageArgs.IncludeOutdated,
                                        listPackageArgs.IncludeDeprecated);

                                    autoReferenceFound = autoReferenceFound || printPackagesResult.AutoReferenceFound;
                                    deprecatedFound    = deprecatedFound || printPackagesResult.DeprecatedFound;
                                }
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine(string.Format(Strings.ListPkg_ErrorReadingAssetsFile, assetsPath));
                    }

                    // Unload project
                    ProjectCollection.GlobalProjectCollection.UnloadProject(project);
                }
            }

            // If any auto-references were found, a line is printed
            // explaining what (A) means
            if (autoReferenceFound)
            {
                Console.WriteLine(Strings.ListPkg_AutoReferenceDescription);
            }

            // If any deprecated packages were found as part of the --outdated command,
            // a line is printed explaining what (D) means.
            if (listPackageArgs.IncludeOutdated && deprecatedFound)
            {
                Console.WriteLine(Strings.ListPkg_DeprecatedPkgDescription);
            }
        }