Ejemplo n.º 1
0
 static void Main(string[] args)
 {
     var helper = new SpdxLicenseHelper(NullLogger.Instance);
     var data   = helper.GetLicencesAsync(false).Result;
 }
Ejemplo n.º 2
0
        private async Task GenerateReportAsync()
        {
            var    logtype     = __ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION;
            var    messageType = OLEMSGICON.OLEMSGICON_INFO;
            string message     = "";
            string title       = "Nuget License Report";
            var    log         = await GetActivityLoggerAsync();

            log.LogEntry(
                (UInt32)__ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION,
                this.ToString(),
                "GenerateReportAsync was invoked"
                );

            try
            {
                var settingsManager = await GetSettingsManagerAsync();

                var userSettingsStore = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);
                var logger            = NullLogger.Instance;
                var spdxHelper        = new SpdxLicenseHelper(logger);
                var env = await GetEnvAsync();

                var licenseCache = await GetLicenseCacheAsync(ProjectSettings.LicenseCacheFileName);

                var installerServices = await GetIVsPackageInstallerServicesAsync();

                ReportGeneratorOptions reportOptions;



                if (userSettingsStore.CollectionExists(ProjectSettings.CollectionName) &&
                    userSettingsStore.PropertyExists(ProjectSettings.CollectionName,
                                                     ProjectSettings.ReportGenerationOptionsDataKey))
                {
                    reportOptions = JsonConvert.DeserializeObject <ReportGeneratorOptions>(
                        userSettingsStore.GetString(
                            ProjectSettings.CollectionName,
                            ProjectSettings.ReportGenerationOptionsDataKey
                            )
                        );

                    reportOptions.Path = ProjectSettings.ReportFileName;
                }
                else
                {
                    reportOptions = new ReportGeneratorOptions
                    {
                        Path     = ProjectSettings.ReportFileName,
                        Columns  = typeof(LicenseRow).GetProperties().Select(p => p.Name).ToList(),
                        FileType = FileType.Csv,
                        IncludePackageDependencies = false
                    };
                }

                var reportGenerator = new ReportGenerator(reportOptions);

                //Remove old report
                reportGenerator.RemoveReport();

                //Get nuget packages
                var helper = new NugetHelper(logger);
                //Todo: add more file types support.
                var nugetPackages =
                    helper.GetNugetPackages(installerServices, env?.Solution, reportOptions.IncludePackageDependencies);

                //First check if any NuGet packages are installed.
                if (nugetPackages != null && nugetPackages.Any())
                {
                    //Get spdx licenses
                    SpdxLicenseData spdxLicenseData;

                    var fi = new FileInfo(ProjectSettings.SpdxCacheFileName);
                    if (fi.Exists)
                    {
                        var json = File.ReadAllText(ProjectSettings.SpdxCacheFileName);
                        var cachedSpdxLicenseData = JsonConvert.DeserializeObject <SpdxLicenseData>(json);
                        spdxLicenseData = await spdxHelper.GetLicencesAsync(false);

                        if (cachedSpdxLicenseData.Version < spdxLicenseData.Version)
                        {
                            spdxLicenseData = await spdxHelper.GetLicencesAsync(true);
                            await UpdateSpdxLicenseCacheAsync(spdxLicenseData, ProjectSettings.SpdxCacheFileName);
                        }
                    }
                    else
                    {
                        //Query data.
                        //Todo: this will take a while so do something to alert user.
                        spdxLicenseData = await spdxHelper.GetLicencesAsync(true);
                        await UpdateSpdxLicenseCacheAsync(spdxLicenseData, ProjectSettings.SpdxCacheFileName);
                    }

                    await reportGenerator.GenerateAsync(nugetPackages, licenseCache, spdxLicenseData);
                    await UpdateLicenseCacheAsync(licenseCache, ProjectSettings.LicenseCacheFileName);

                    message = "Nuget Package License Report Generated";
                }
                else
                {
                    message = "No installed Nuget packages were found report failed to generate.";
                }
            }
            catch (NuGet.Protocol.Core.Types.FatalProtocolException exception)
            {
                message     = exception.Message + " This may be caused by not doing a restore on your Nuget packages.";
                logtype     = __ACTIVITYLOG_ENTRYTYPE.ALE_ERROR;
                messageType = OLEMSGICON.OLEMSGICON_WARNING;

                log.LogEntry(
                    (UInt32)logtype,
                    this.ToString(),
                    string.Concat(exception.StackTrace, " ", message)
                    );
            }
            catch (Exception exception)
            {
                message     = exception.Message;
                logtype     = __ACTIVITYLOG_ENTRYTYPE.ALE_ERROR;
                messageType = OLEMSGICON.OLEMSGICON_WARNING;

                log.LogEntry(
                    (UInt32)logtype,
                    this.ToString(),
                    string.Concat(exception.StackTrace, " ", message)
                    );
            }

            ThreadHelper.ThrowIfNotOnUIThread();

            log.LogEntry(
                (UInt32)logtype,
                this.ToString(),
                message
                );

            Debug.WriteLine(message);

            // Show a message box to prove we were here
            VsShellUtilities.ShowMessageBox(
                this._package,
                message,
                title,
                messageType,
                OLEMSGBUTTON.OLEMSGBUTTON_OK,
                OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
        }