<commit_before>using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Bloom.Book;
using Bloom.Collection;
using Bloom.Properties;
using Bloom.Publish.Epub;
using Bloom.web;
using BloomTemp;
using CommandLine;
using L10NSharp;
using SIL.IO;

namespace Bloom.CLI
{
	class CreateArtifactsCommand
	{
		private static ProjectContext _projectContext;

		public static int Handle(CreateArtifactsParameters options)
		{
			Console.Out.WriteLine();

			Program.SetUpErrorHandling();
			try
			{
				using (var applicationContainer = new ApplicationContainer())
				{
					Bloom.Program.RunningNonApplicationMode = true;
					Program.SetUpLocalization(applicationContainer);
					Browser.SetUpXulRunner();
					Browser.XulRunnerShutdown += Program.OnXulRunnerShutdown;
					LocalizationManager.SetUILanguage(Settings.Default.UserInterfaceLanguage, false);   // Unclear if this line is needed or not.
					if (DesktopAnalytics.Analytics.AllowTracking)
					{
						throw new ApplicationException("Allow tracking is enabled but we don't want the Harvester to actually send analytics.");
					}

					Program.RunningHarvesterMode = true;
					string collectionFilePath = options.CollectionPath;
					using (_projectContext = applicationContainer.CreateProjectContext(collectionFilePath))
					{
						Bloom.Program.SetProjectContext(_projectContext);

						// Make the .bloomd and /bloomdigital outputs
						CreateArtifacts(options);
					}
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
				Console.WriteLine(ex.StackTrace);
				return 1;
			}

			return 0;
		}

		private static void CreateArtifacts(CreateArtifactsParameters parameters)
		{
			string zippedBloomDOutputPath = parameters.BloomDOutputPath;
			string unzippedBloomDigitalOutputPath = parameters.BloomDigitalOutputPath;

			bool isBloomDOrBloomDigitalRequested = !String.IsNullOrEmpty(zippedBloomDOutputPath) || !String.IsNullOrEmpty(unzippedBloomDigitalOutputPath);
			if (isBloomDOrBloomDigitalRequested)
			{
				CreateBloomDigitalArtifacts(parameters.BookPath, parameters.Creator, zippedBloomDOutputPath, unzippedBloomDigitalOutputPath);
			}

			Control control = new Control();
			control.CreateControl();

			using (var countdownEvent = new CountdownEvent(1))
			{
				// Create the ePub in the background. (Some of the ePub work needs to happen off the main thread)
				ThreadPool.QueueUserWorkItem(
					x =>
					{
						CreateEpubArtifact(parameters, control);
						countdownEvent.Signal();    // Decrement by one
					}
				);

				// Wait around until the worker thread is done.
				while (!countdownEvent.IsSet)	// Set = true if the count is down to 0.
				{
					Thread.Sleep(100);
					Application.DoEvents();
				}
			}
		}

		public static void CreateBloomDigitalArtifacts(string bookPath, string creator, string zippedBloomDOutputPath, string unzippedBloomDigitalOutputPath)
		{
#if DEBUG
			MessageBox.Show("Attach debugger now");
#endif

			using (var tempBloomD = TempFile.CreateAndGetPathButDontMakeTheFile())
			{
				if (String.IsNullOrEmpty(zippedBloomDOutputPath))
				{<commit_msg>Comment out debugger attachment pause for now<commit_after>using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Bloom.Book;
using Bloom.Collection;
using Bloom.Properties;
using Bloom.Publish.Epub;
using Bloom.web;
using BloomTemp;
using CommandLine;
using L10NSharp;
using SIL.IO;

namespace Bloom.CLI
{
	class CreateArtifactsCommand
	{
		private static ProjectContext _projectContext;

		public static int Handle(CreateArtifactsParameters options)
		{
			Console.Out.WriteLine();

			Program.SetUpErrorHandling();
			try
			{
				using (var applicationContainer = new ApplicationContainer())
				{
					Bloom.Program.RunningNonApplicationMode = true;
					Program.SetUpLocalization(applicationContainer);
					Browser.SetUpXulRunner();
					Browser.XulRunnerShutdown += Program.OnXulRunnerShutdown;
					LocalizationManager.SetUILanguage(Settings.Default.UserInterfaceLanguage, false);   // Unclear if this line is needed or not.
					if (DesktopAnalytics.Analytics.AllowTracking)
					{
						throw new ApplicationException("Allow tracking is enabled but we don't want the Harvester to actually send analytics.");
					}

					Program.RunningHarvesterMode = true;
					string collectionFilePath = options.CollectionPath;
					using (_projectContext = applicationContainer.CreateProjectContext(collectionFilePath))
					{
						Bloom.Program.SetProjectContext(_projectContext);

						// Make the .bloomd and /bloomdigital outputs
						CreateArtifacts(options);
					}
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
				Console.WriteLine(ex.StackTrace);
				return 1;
			}

			return 0;
		}

		private static void CreateArtifacts(CreateArtifactsParameters parameters)
		{
			string zippedBloomDOutputPath = parameters.BloomDOutputPath;
			string unzippedBloomDigitalOutputPath = parameters.BloomDigitalOutputPath;

			bool isBloomDOrBloomDigitalRequested = !String.IsNullOrEmpty(zippedBloomDOutputPath) || !String.IsNullOrEmpty(unzippedBloomDigitalOutputPath);
			if (isBloomDOrBloomDigitalRequested)
			{
				CreateBloomDigitalArtifacts(parameters.BookPath, parameters.Creator, zippedBloomDOutputPath, unzippedBloomDigitalOutputPath);
			}

			Control control = new Control();
			control.CreateControl();

			using (var countdownEvent = new CountdownEvent(1))
			{
				// Create the ePub in the background. (Some of the ePub work needs to happen off the main thread)
				ThreadPool.QueueUserWorkItem(
					x =>
					{
						CreateEpubArtifact(parameters, control);
						countdownEvent.Signal();    // Decrement by one
					}
				);

				// Wait around until the worker thread is done.
				while (!countdownEvent.IsSet)	// Set = true if the count is down to 0.
				{
					Thread.Sleep(100);
					Application.DoEvents();
				}
			}
		}

		public static void CreateBloomDigitalArtifacts(string bookPath, string creator, string zippedBloomDOutputPath, string unzippedBloomDigitalOutputPath)
		{
#if DEBUG
			// Useful for allowing debugging of Bloom while running the harvester
			//MessageBox.Show("Attach debugger now");
#endif

			using (var tempBloomD = TempFile.CreateAndGetPathButDontMakeTheFile())
			{
				if (String.IsNullOrEmpty(zippedBloomDOutputPath))
				{
{'fraction_non_alphanumeric': 0.08133030990173848, 'fraction_numerical': 0.0027210884353741495, 'mean_word_length': 4.085318985395849, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2878418', 'n_tokens_mistral': 2340, 'n_tokens_neox': 1869, 'n_words': 450}