<commit_before>﻿namespace SIM.Tool.Windows.MainWindowComponents
{
  using System;
  using System.IO;
  using System.Linq;
  using System.Windows;
  using System.Xml;
  using SIM.Instances;
  using SIM.Products;
  using SIM.Tool.Base;
  using SIM.Tool.Base.Plugins;
  using Sitecore.Diagnostics.Base;
  using Sitecore.Diagnostics.Base.Annotations;
  using Sitecore.Diagnostics.InformationService.Client;
  using Sitecore.Diagnostics.Logging;
  using Sitecore.NuGet.Core;
  using SIM.Tool.Base.Profiles;

  [UsedImplicitly]
  public class GenerateNuGetPackagesButton : IMainWindowButton
  {
    private const string Link = "http://bitbucket.org/sitecoresupport/sitecore-nuget-packages-generator";

    public bool IsEnabled([CanBeNull] Window mainWindow, Instance instance)
    {
      return true;
    }

    public void OnClick(Window mainWindow, Instance instance)
    {
      Assert.ArgumentNotNull(mainWindow, "mainWindow");

      var nugetFolderPath = Environment.ExpandEnvironmentVariables(WindowsSettings.AppNuGetDirectory.Value);

      Action longRunningTask = delegate
      {
        if (!Directory.Exists(nugetFolderPath))
        {
          Directory.CreateDirectory(nugetFolderPath);
        }

        UpdateSettings(nugetFolderPath);
        GeneratePackages(nugetFolderPath);
      };

      var content = string.Format("The SC.* NuGet packages are now being generated in the {0} directory for all Sitecore versions that exist in the local repository. Read more: {1}", nugetFolderPath, Link);

      WindowHelper.LongRunningTask(longRunningTask, "Generating NuGet Packages", mainWindow, null, content, true);
    }

    private static void GeneratePackages([NotNull] string nugetFolderPath)
    {
      Assert.ArgumentNotNull(nugetFolderPath, "nugetFolderPath");

      Log.Info("Generating NuGet packages from {0} to {1}", ProfileManager.Profile.LocalRepository, nugetFolderPath);
      ProcessFolder(ProfileManager.Profile.LocalRepository, nugetFolderPath);
    }

    private static void ProcessFolder(string directory, string outputFolderPath)
    {
      var pattern = "* rev.*.zip";
      var zipfiles = Directory.GetFiles(directory, pattern, SearchOption.AllDirectories);
      foreach (var file in zipfiles)
      {
        try
        {
          // Create nupkg file
          new PackageGenerator().Generate(file, outputFolderPath);
        }
        catch (Exception ex)
        {
          Log.Error("Error processing file " + file + ". " + ex.Message + Environment.NewLine + "Stack trace:" + Environment.NewLine + ex.StackTrace);
        }
      }
    }

    private static void UpdateSettings([NotNull] string nugetFolderPath)
    {
      Assert.ArgumentNotNull(nugetFolderPath, "nugetFolderPath");

      var nugetConfigPath = Environment.ExpandEnvironmentVariables(@"%appdata%\NuGet\nuget.config");
      var bakFilePath = nugetConfigPath + ".bak";
      if (!File.Exists(bakFilePath))
      {
        File.Copy(nugetConfigPath, bakFilePath);<commit_msg>Fix 3d3f331<commit_after>﻿namespace SIM.Tool.Windows.MainWindowComponents
{
  using System;
  using System.IO;
  using System.Linq;
  using System.Windows;
  using System.Xml;
  using SIM.Instances;
  using SIM.Products;
  using SIM.Tool.Base;
  using SIM.Tool.Base.Plugins;
  using Sitecore.Diagnostics.Base;
  using Sitecore.Diagnostics.Base.Annotations;
  using Sitecore.Diagnostics.InformationService.Client;
  using Sitecore.Diagnostics.Logging;
  using Sitecore.NuGet.Core;
  using SIM.Tool.Base.Profiles;

  [UsedImplicitly]
  public class GenerateNuGetPackagesButton : IMainWindowButton
  {
    private const string Link = "http://bitbucket.org/sitecoresupport/sitecore-nuget-packages-generator";

    public bool IsEnabled([CanBeNull] Window mainWindow, Instance instance)
    {
      return true;
    }

    public void OnClick(Window mainWindow, Instance instance)
    {
      Assert.ArgumentNotNull(mainWindow, "mainWindow");

      var nugetFolderPath = Environment.ExpandEnvironmentVariables(WindowsSettings.AppNuGetDirectory.Value);

      Action longRunningTask = delegate
      {
        if (!Directory.Exists(nugetFolderPath))
        {
          Directory.CreateDirectory(nugetFolderPath);
        }

        UpdateSettings(nugetFolderPath);
        GeneratePackages(nugetFolderPath);
      };

      var content = string.Format("The SC.* NuGet packages are now being generated in the {0} directory for all Sitecore versions that exist in the local repository. Read more: {1}", nugetFolderPath, Link);

      WindowHelper.LongRunningTask(longRunningTask, "Generating NuGet Packages", mainWindow, null, content, true);
    }

    private static void GeneratePackages([NotNull] string nugetFolderPath)
    {
      Assert.ArgumentNotNull(nugetFolderPath, "nugetFolderPath");

      Log.Info("Generating NuGet packages from {0} to {1}", ProfileManager.Profile.LocalRepository, nugetFolderPath);
      ProcessFolder(ProfileManager.Profile.LocalRepository, nugetFolderPath);
    }

    private static void ProcessFolder(string directory, string outputFolderPath)
    {
      var pattern = "* rev.*.zip";
      var zipfiles = Directory.GetFiles(directory, pattern, SearchOption.AllDirectories);
      foreach (var file in zipfiles)
      {
        try
        {
          // Create nupkg file
          new PackageGenerator().Generate(file, outputFolderPath, true, false);
        }
        catch (Exception ex)
        {
          Log.Error("Error processing file " + file + ". " + ex.Message + Environment.NewLine + "Stack trace:" + Environment.NewLine + ex.StackTrace);
        }
      }
    }

    private static void UpdateSettings([NotNull] string nugetFolderPath)
    {
      Assert.ArgumentNotNull(nugetFolderPath, "nugetFolderPath");

      var nugetConfigPath = Environment.ExpandEnvironmentVariables(@"%appdata%\NuGet\nuget.config");
      var bakFilePath = nugetConfigPath + ".bak";
      if (!File.Exists(bakFilePath))
      {
        File.Copy(nugetConfigPath, bakFilePath);
{'fraction_non_alphanumeric': 0.09450659542494573, 'fraction_numerical': 0.0021706461846718985, 'mean_word_length': 3.437037037037037, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7251522', 'n_tokens_mistral': 1677, 'n_tokens_neox': 1625, 'n_words': 372}