Пример #1
0
        // ReSharper disable UnusedMember.Local
#pragma warning disable IDE0051 // Remove unused private members
        private async Task <int> OnExecuteAsync(CancellationToken cancellationToken)
#pragma warning restore IDE0051 // Remove unused private members
        // ReSharper restore UnusedMember.Local
        {
            if (OutputPath is null)
            {
                SkipDownloadOfLicenses = true;
            }

            IFileInfo?outputFile;

            if (OutputPath != null)
            {
                outputFile = _fileSystem.FileInfo.FromFileName(OutputPath);
                if (outputFile.Exists)
                {
                    _reporter.Error("The file to write the output to already exists. Specify another output path or delete the file");
                    return(1);
                }
            }
            else
            {
                outputFile = null;
            }

            var instances = MSBuildLocator.QueryVisualStudioInstances().ToList();

            MSBuildLocator.RegisterMSBuildPath(instances.First().MSBuildPath);

            var entryPoint = _entryPointLocator.GetEntryPoint(PathToProjectOrSolution);

            var dependencies = await _projectDependencyResolver.ResolveDependenciesAsync(entryPoint);

            _reporter.OutputInvariant($"Resolving dependencies of {entryPoint.File.FullName}");
            _reporter.OutputInvariant($"\tcount {dependencies.Count}");

            _reporter.Output("Extracting licensing information");
            var packageSpec = _packageLocator.Provide(dependencies);

            _reporter.Output("Correcting license locations");
            var existingLicenses = packageSpec.Values
                                   .Where(v => v.LicenseLocation != null)
                                   .Select(v => v.LicenseLocation !)
                                   .Distinct(EqualityComparer <Uri> .Default);
            var correctedLicenseLocations = _uriCorrector.Correct(existingLicenses);

            _reporter.OutputInvariant($"Downloading licenses (total {correctedLicenseLocations.Count})");
            IImmutableDictionary <Uri, string> licenses;

            if (SkipDownloadOfLicenses)
            {
                _reporter.Output("\tSkipping download");
                licenses = ImmutableDictionary <Uri, string> .Empty;
            }
            else
            {
                licenses = await _downloader.DownloadAsync(correctedLicenseLocations.Values.Select(v => v.corrected), cancellationToken);
            }

            var licenseDependencyInformation = new List <LicenseDependencyInformation>();

            foreach (var(package, (location, licenseExpression, authors)) in packageSpec)
            {
                Uri?   correctedUrl;
                string licenseContent;
                if (!(location is null))
                {
                    correctedUrl   = correctedLicenseLocations[location].corrected;
                    licenseContent = licenses.FirstOrDefault(l => l.Key == correctedUrl).Value ?? "";
                }
Пример #2
0
        // ReSharper disable UnusedMember.Local
#pragma warning disable IDE0051 // Remove unused private members
        private async Task <int> OnExecuteAsync()
#pragma warning restore IDE0051 // Remove unused private members
        // ReSharper restore UnusedMember.Local
        {
            if (OutputPath is null)
            {
                SkipDownloadOfLicenses = true;
            }

            IFileInfo?outputFile;

            if (OutputPath != null)
            {
                outputFile = _fileSystem.FileInfo.FromFileName(OutputPath);
                if (outputFile.Exists)
                {
                    _reporter.Error("The file to write the output to already exists. Specify another output path or delete the file");
                    return(1);
                }
            }
            else
            {
                outputFile = null;
            }

            var cancellationToken = CancellationToken.None;
            var instances         = MSBuildLocator.QueryVisualStudioInstances().ToList();

            MSBuildLocator.RegisterMSBuildPath(instances.First().MSBuildPath);

            _reporter.Output("Resolving dependencies");
            var dependencies = _projectDependencyResolver.ResolveDependencies(PathToProjectOrSolution);

            _reporter.OutputInvariant($"\tcount {dependencies.Count}");

            _reporter.Output("Extracting licensing information");
            var licenseSpecs = _licenseLocator.Provide(dependencies);

            _reporter.Output("Correcting license locations");
            var correctedLicenseLocations = _uriCorrector.Correct(licenseSpecs.Values.Select(v => v.Item1).Distinct(EqualityComparer <Uri> .Default));

            _reporter.OutputInvariant($"Downloading licenses (total {correctedLicenseLocations.Count})");
            IImmutableDictionary <Uri, string> licenses;

            if (SkipDownloadOfLicenses)
            {
                _reporter.Output("\tSkipping download");
                licenses = ImmutableDictionary <Uri, string> .Empty;
            }
            else
            {
                licenses = await _downloader.DownloadAsync(correctedLicenseLocations.Values.Select(v => v.corrected), cancellationToken);
            }

            var licenseDependencyInformation = new List <LicenseDependencyInformation>();

            foreach (var(package, (location, licenseExpression)) in licenseSpecs)
            {
                var correctedUrl          = correctedLicenseLocations[location].corrected;
                var content               = licenses.FirstOrDefault(l => l.Key == correctedUrl);
                var dependencyInformation = new LicenseDependencyInformation(package, content.Value ?? string.Empty, location, correctedUrl, licenseExpression);

                licenseDependencyInformation.Add(dependencyInformation);
            }

            if (outputFile != null)
            {
                var fileContent = JsonConvert.SerializeObject(licenseDependencyInformation, Formatting.Indented);

                await using var writer = outputFile.OpenWrite();
                var encoding = new UTF8Encoding(false, true);
                var bytes    = encoding.GetBytes(fileContent);
                await writer.WriteAsync(bytes, 0, bytes.Length, cancellationToken);

                await writer.FlushAsync(cancellationToken);
            }
            else
            {
                _reporter.OutputInvariant($"Licenses of {PathToProjectOrSolution}");
                foreach (var dependencyInformation in licenseDependencyInformation)
                {
                    _reporter.OutputInvariant($"dependency {dependencyInformation.PackageReference.Name} (version: {dependencyInformation.PackageReference.ResolvedVersion}, license expression: {dependencyInformation.LicenseExpression})");
                }
            }

            return(0);
        }