public LicenseResolver(SpdxLicenseData spdxLicenseData, string githubUsername, string githubPassword)
        {
            _spdxLicenseData = spdxLicenseData;
            var byteArray = Encoding.ASCII.GetBytes(githubUsername + ":" + githubPassword);

            _authHeader = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
        }
        /// <summary>
        /// Download licenses from spdx.org
        /// </summary>
        /// <param name="getIndividualLicenseText"></param>
        /// <returns>SpdxLicenseData</returns>
        public async Task <SpdxLicenseData> GetLicencesAsync(bool getIndividualLicenseText)
        {
            var licenses = new SpdxLicenseData();

            try
            {
                var json = await GetHttpAsync(_url);

                if (string.IsNullOrEmpty(json))
                {
                    throw new Exception($"No data returned from {_url}.");
                }

                licenses = await LoadLicensesAsync(json, getIndividualLicenseText);
            }
            catch (Exception ex)
            {
                _log.LogError($"Error occurred when downloading licenses from spdx.org. ({ex.Message})");
            }

            //Adding Amazon Web services licensing.
            licenses.Licenses.Add(new SpdxLicense
            {
                Id             = "Apache License Version 2.0, January 2004",
                Name           = "Apache License Version 2.0, January 2004",
                KnownAliasUrls = new List <string>
                {
                    "http://aws.amazon.com/apache2.0/"
                },
                StandardLicenseTemplate = "",
                Text                  = "",
                SpdxDetailsUrl        = "",
                IsDeprecatedLicenseId = false,
                IsOsiApproved         = false,
                ReferenceNumber       = "-1"
            });

            //Add Microsoft licensing terms see list here for Visual Studio. https://visualstudio.microsoft.com/license-terms/

            //MICROSOFT SOFTWARE LICENSE TERMS
            //MICROSOFT VISUAL STUDIO 2017 TOOLS, ADD - ONs and EXTENSIONS
            licenses.Licenses.Add(new SpdxLicense
            {
                Id             = "MICROSOFT VISUAL STUDIO 2017 TOOLS, ADD - ONs and EXTENSIONS",
                Name           = "MICROSOFT VISUAL STUDIO 2017 TOOLS, ADD - ONs and EXTENSIONS",
                KnownAliasUrls = new List <string>
                {
                    "https://aka.ms/pexunj",
                    "https://visualstudio.microsoft.com/license-terms/mlt552233/"
                },
                StandardLicenseTemplate = "",
                Text                  = "",
                SpdxDetailsUrl        = "",
                IsDeprecatedLicenseId = false,
                IsOsiApproved         = false,
                ReferenceNumber       = "-1"
            });

            //Web API
            //MICROSOFT SOFTWARE LICENSE TERMS
            //MICROSOFT.NET LIBRARY
            licenses.Licenses.Add(new SpdxLicense
            {
                Id             = "MICROSOFT.NET LIBRARY",
                Name           = "MICROSOFT.NET LIBRARY",
                KnownAliasUrls = new List <string>
                {
                    "http://www.microsoft.com/web/webpi/eula/net_library_eula_enu.htm",
                    "http://go.microsoft.com/fwlink/?LinkId=529443",
                    "http://go.microsoft.com/fwlink/?LinkId=329770",
                    "https://www.microsoft.com/net/dotnet_library_license.htm"
                },
                StandardLicenseTemplate = "",
                Text                  = "",
                SpdxDetailsUrl        = "",
                IsDeprecatedLicenseId = false,
                IsOsiApproved         = false,
                ReferenceNumber       = "-1"
            });

            //MICROSOFT VISUAL STUDIO 2015 SOFTWARE DEVELOPMENT KIT
            licenses.Licenses.Add(new SpdxLicense
            {
                Id             = "MICROSOFT VISUAL STUDIO 2015 SOFTWARE DEVELOPMENT KIT",
                Name           = "MICROSOFT VISUAL STUDIO 2015 SOFTWARE DEVELOPMENT KIT",
                KnownAliasUrls = new List <string>
                {
                    "http://go.microsoft.com/fwlink/?LinkID=614949",
                    "https://visualstudio.microsoft.com/license-terms/mt171586/"
                },
                StandardLicenseTemplate = "",
                Text                  = "",
                SpdxDetailsUrl        = "",
                IsDeprecatedLicenseId = false,
                IsOsiApproved         = false,
                ReferenceNumber       = "-1"
            });

            //MICROSOFT PRE-RELEASE SOFTWARE LICENSE TERMS
            //MICROSOFT VISUAL STUDIO 2017 FAMILY PRE-RELEASE SOFTWARE
            licenses.Licenses.Add(new SpdxLicense
            {
                Id             = "MICROSOFT VISUAL STUDIO 2017 FAMILY PRE-RELEASE SOFTWARE",
                Name           = "MICROSOFT VISUAL STUDIO 2017 FAMILY PRE-RELEASE SOFTWARE",
                KnownAliasUrls = new List <string>
                {
                    "https://go.microsoft.com/fwlink/?LinkID=746386",
                    "https://visualstudio.microsoft.com/license-terms/mt591984/"
                },
                StandardLicenseTemplate = "",
                Text                  = "",
                SpdxDetailsUrl        = "",
                IsDeprecatedLicenseId = false,
                IsOsiApproved         = false,
                ReferenceNumber       = "-1"
            });

            //Copyright (c) 2015-2016 Xamarin, Inc.
            //Copyright(c) 2017 - 2018 Microsoft Corporation.
            //https://go.microsoft.com/fwlink/?linkid=868514
            licenses.Licenses.Add(new SpdxLicense
            {
                Id             = "Xamarin, Inc.  Microsoft Corporation",
                Name           = "Xamarin, Inc.  Microsoft Corporation",
                KnownAliasUrls = new List <string>
                {
                    "https://go.microsoft.com/fwlink/?linkid=868514"
                },
                StandardLicenseTemplate = "",
                Text                  = "",
                SpdxDetailsUrl        = "",
                IsDeprecatedLicenseId = false,
                IsOsiApproved         = false,
                ReferenceNumber       = "-1"
            });

            return(licenses);
        }
        public async Task GenerateAsync(IEnumerable <PackageInfo> packageList, Dictionary <string, LicenseRow> cachedLicenses, SpdxLicenseData spdxLicenseData)
        {
            var licenses = (await ToLicenseRowsAsync(packageList, cachedLicenses, spdxLicenseData)).OrderBy(p => p.Value.Id);

            switch (_options.FileType)
            {
            case FileType.Csv:
                using (var write = new StreamWriter(_options.Path))
                {
                    using (var helper = new CsvHelper.CsvWriter(write))
                    {
                        helper.Configuration.SanitizeForInjection = true;
                        helper.Configuration.QuoteAllFields       = true;

                        //Write headers
                        foreach (var property in _options.Columns)
                        {
                            helper.WriteField(property, true);
                        }
                        helper.NextRecord();

                        var properties = typeof(LicenseRow).GetProperties().ToList();

                        //Write values
                        foreach (var license in licenses)
                        {
                            //Get rid of new lines as they are bad in CSV and other report layouts.
                            license.Value.LicenseText = license.Value?.LicenseText.Replace("\r", "").Replace("\n", "").Replace(",", " ");

                            //
                            if (license.Value?.LicenseText?.Length >= 35000)
                            {
                                license.Value.LicenseText = license.Value.LicenseText.Substring(0, 35000);
                            }

                            foreach (var propertyName in _options.Columns)
                            {
                                var property = properties.FirstOrDefault(p => p.Name == propertyName);
                                var value    = property?.GetValue(license.Value, null);
                                helper.WriteField(value?.ToString());
                            }
                            helper.NextRecord();
                        }
                    }
                }
                break;

            case FileType.Html:
                throw new NotImplementedException();

            case FileType.Pdf:
                throw new NotImplementedException();

            case FileType.Word:
                throw new NotImplementedException();

            default:
                throw new ArgumentOutOfRangeException();
            }

            var proc  = new Process();
            var finfo = new FileInfo(_options.Path);

            if (finfo.Exists)
            {
                proc.StartInfo.FileName = finfo.FullName;
                proc.Start();
            }
        }
        private async Task <Dictionary <string, LicenseRow> > ToLicenseRowsAsync(IEnumerable <PackageInfo> packageList, Dictionary <string, LicenseRow> cachedLicenses, SpdxLicenseData spdxLicenseData)
        {
            var licenseResolver = new LicenseResolver(spdxLicenseData, "", "");

            var rows = new Dictionary <string, LicenseRow>();

            foreach (var package in packageList.OrderBy(p => p.LocalPackageInfo.Nuspec.GetTitle()))
            {
                var nugetId    = package.LocalPackageInfo.Nuspec.GetId() + " : " + package.LocalPackageInfo.Nuspec.GetVersion().Version;
                var licenseRow = new LicenseRow
                {
                    Id                = nugetId,
                    Component         = !string.IsNullOrEmpty(package.LocalPackageInfo.Nuspec.GetTitle()) ? package.LocalPackageInfo.Nuspec.GetTitle() : package.LocalPackageInfo.Nuspec.GetId(),
                    Project           = package.ProjectList.Aggregate("", (accumulator, piece) => (accumulator.Length > 0 ? "," : "") + piece.Name),//string.Join(",", package.ProjectList),
                    Author            = package.LocalPackageInfo.Nuspec.GetAuthors(),
                    LicenseUrl        = !string.IsNullOrEmpty(package.LocalPackageInfo.Nuspec.GetLicenseUrl()) ? package.LocalPackageInfo.Nuspec.GetLicenseUrl() : package.LocalPackageInfo.Nuspec.GetProjectUrl(),
                    License           = "Unknown",
                    Version           = package.LocalPackageInfo.Nuspec.GetVersion().Version.ToString(),
                    LicenseText       = "",
                    SpdxLicenseId     = "",
                    AccuracyOfLicense = AccuracyOfLicense.NotFound,
                    RequireAcceptance = package.LocalPackageInfo.Nuspec.GetRequireLicenseAcceptance()
                };

                //Check cache first
                //TODO consider checking if url has changed?
                if (cachedLicenses.ContainsKey(nugetId) && cachedLicenses[nugetId].LicenseUrl == licenseRow.LicenseUrl)
                {
                    licenseRow = cachedLicenses[nugetId];
                }
                else
                {
                    //Go determine license.
                    if (!string.IsNullOrEmpty(licenseRow.LicenseUrl))
                    {
                        var licenseTuple = await licenseResolver.ResolveAsync(new Uri(licenseRow.LicenseUrl));

                        if (licenseTuple?.Item2 != null)
                        {
                            licenseRow.License           = licenseTuple.Item2.Name;
                            licenseRow.SpdxLicenseId     = licenseTuple.Item2.Id;
                            licenseRow.LicenseText       = licenseTuple.Item2.Text;
                            licenseRow.AccuracyOfLicense = licenseTuple.Item1;

                            if (cachedLicenses.ContainsKey(nugetId))
                            {
                                cachedLicenses[nugetId] = licenseRow;
                            }
                            else if (!licenseRow.License.Equals("Unknown"))
                            {
                                cachedLicenses.Add(nugetId, licenseRow);
                            }
                            else
                            {
                                Debug.WriteLine("Unable to determine license for {licenseRow.LicenseUrl}.");
                            }
                        }
                        else
                        {
                            Debug.WriteLine("Unable to determine license for {licenseRow.LicenseUrl}.");
                        }
                    }
                    else
                    {
                        Debug.WriteLine("Unable to determine license for {licenseRow.LicenseUrl}.");
                    }
                }

                if (rows.ContainsKey(nugetId))
                {
                    rows[nugetId] = licenseRow;
                }
                else
                {
                    rows.Add(nugetId, licenseRow);
                }
            }

            return(rows);
        }