Esempio n. 1
0
        /// <summary>
        /// Analyzes a single Project file for NuGet package references.
        /// </summary>
        /// <param name="projectFilePath"></param>
        /// <returns></returns>
        public async Task <HashSet <NugetPackage> > GetProjectNugetPackagesAsync(string projectFilePath, string baseIntermediateOutputPath, bool excludeTestProjects)
        {
            if (!_fileSystem.File.Exists(projectFilePath))
            {
                Console.Error.WriteLine($"Project file \"{projectFilePath}\" does not exist");
                return(new HashSet <NugetPackage>());
            }

            var isTestProject = IsTestProject(projectFilePath);
            var packages      = new HashSet <NugetPackage>();

            Console.WriteLine();
            Console.WriteLine($"» Analyzing: {projectFilePath}");

            if (excludeTestProjects && isTestProject)
            {
                Console.WriteLine($"Skipping: {projectFilePath}");
                return(new HashSet <NugetPackage>());
            }

            if (!DisablePackageRestore)
            {
                Console.WriteLine("  Attempting to restore packages");
                var restoreResult = _dotnetUtilsService.Restore(projectFilePath);

                if (restoreResult.Success)
                {
                    Console.WriteLine("  Packages restored");
                }
                else
                {
                    Console.WriteLine("Dotnet restore failed:");
                    Console.WriteLine(restoreResult.ErrorMessage);
                    throw new DotnetRestoreException($"Dotnet restore failed with message: {restoreResult.ErrorMessage}");
                }
            }

            var assetsFilename = _fileSystem.Path.Combine(GetProjectProperty(projectFilePath, baseIntermediateOutputPath), "project.assets.json");

            if (!File.Exists(assetsFilename))
            {
                Console.WriteLine($"File not found: \"{assetsFilename}\", \"{projectFilePath}\" ");
            }
            packages.UnionWith(_projectAssetsFileService.GetNugetPackages(assetsFilename, isTestProject));


            // if there are no project file package references look for a packages.config
            if (!packages.Any())
            {
                Console.WriteLine("  No packages found");
                var directoryPath = _fileSystem.Path.GetDirectoryName(projectFilePath);
                var packagesPath  = _fileSystem.Path.Combine(directoryPath, "packages.config");
                if (_fileSystem.File.Exists(packagesPath))
                {
                    Console.WriteLine("  Found packages.config. Will attempt to process");
                    packages = await _packagesFileService.GetNugetPackagesAsync(packagesPath).ConfigureAwait(false);
                }
            }
            return(packages);
        }
        /// <summary>
        /// Analyzes a single Project file for NuGet package references.
        /// </summary>
        /// <param name="projectFilePath"></param>
        /// <returns></returns>
        public async Task <HashSet <NugetPackage> > GetProjectNugetPackagesAsync(string projectFilePath)
        {
            if (!_fileSystem.File.Exists(projectFilePath))
            {
                Console.Error.WriteLine($"Project file \"{projectFilePath}\" does not exist");
                return(new HashSet <NugetPackage>());
            }

            var packages = new HashSet <NugetPackage>();

            Console.WriteLine();
            Console.WriteLine($"» Analyzing: {projectFilePath}");

            Console.WriteLine("  Attempting to restore packages");
            var restoreResult = _dotnetUtilsService.Restore(projectFilePath);

            if (restoreResult.Success)
            {
                var assetsFilename = _fileSystem.Path.Combine(
                    _fileSystem.Path.GetDirectoryName(projectFilePath),
                    "obj", "project.assets.json");

                packages.UnionWith(_projectAssetsFileService.GetNugetPackages(assetsFilename));
            }
            else
            {
                Console.WriteLine("Dotnet restore failed:");
                Console.WriteLine(restoreResult.ErrorMessage);
                throw new DotnetRestoreException($"Dotnet restore failed with message: {restoreResult.ErrorMessage}");
            }

            // if there are no project file package references look for a packages.config
            if (!packages.Any())
            {
                Console.WriteLine("  No packages found");
                var directoryPath = _fileSystem.Path.GetDirectoryName(projectFilePath);
                var packagesPath  = _fileSystem.Path.Combine(directoryPath, "packages.config");
                if (_fileSystem.File.Exists(packagesPath))
                {
                    Console.WriteLine("  Found packages.config. Will attempt to process");
                    packages = await _packagesFileService.GetNugetPackagesAsync(packagesPath).ConfigureAwait(false);
                }
            }
            return(packages);
        }