<commit_before>﻿using System;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.Build.Construction;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Zoltu.Collections.Generic.NotNull;
using Zoltu.Linq.NotNull;

namespace Zoltu.BuildTools.TypeScript
{
	public class FromReferencesTask : Task
	{
		[Required]
		public String ProjectFullPath
		{
			get { return _projectFullPath; }
			set { _projectFullPath = value; }
		}
		private String _projectFullPath;

		[Required]
		public String LibraryDirectoryFullPath
		{
			get { return _libraryDirectoryFullPath; }
			set { _libraryDirectoryFullPath = value; }
		}
		private String _libraryDirectoryFullPath;

		public override Boolean Execute()
		{
			try
			{
				Contract.Assume(!String.IsNullOrEmpty(ProjectFullPath));
				Contract.Assume(!String.IsNullOrEmpty(LibraryDirectoryFullPath));

				var project = ProjectRootElement.Open(ProjectFullPath);
				if (project == null)
					throw new NullReferenceException("project");

				var projectDirectoryPath = project.DirectoryPath;
				if (projectDirectoryPath == null)
					throw new NullReferenceException("projectDirectoryPath");

				Directory.CreateDirectory(LibraryDirectoryFullPath);

				var typeScriptFullPaths = GetReferencedProjects(project)
					.NotNullToNull()
					.SelectMany(x => GetTypeScriptItems(x).NotNullToNull(), GetTypeScriptFileFullPath)
					.Skip(1)
					.NotNull();

				foreach (var typeScriptFullPath in typeScriptFullPaths)
				{
					var sourceDirectoryPath = Path.GetDirectoryName(typeScriptFullPath);
					Contract.Assume(!String.IsNullOrEmpty(sourceDirectoryPath));
					var sourceFileName = Path.GetFileNameWithoutExtension(typeScriptFullPath);
					Contract.Assume(!String.IsNullOrEmpty(sourceFileName));

					CopyFile(sourceDirectoryPath, sourceFileName, LibraryDirectoryFullPath, ".d.ts", ".d.ts");
					CopyFile(sourceDirectoryPath, sourceFileName, LibraryDirectoryFullPath, ".js", ".js");
					var tsSourceFilePath = CopyFile(sourceDirectoryPath, sourceFileName, LibraryDirectoryFullPath, ".ts", ".ts.source");
					var jsMapFilePath = CopyFile(sourceDirectoryPath, sourceFileName, LibraryDirectoryFullPath, ".js.map", ".js.map");

					UpdateSourceMap(sourceFileName, jsMapFilePath, tsSourceFilePath);
				};

				return true;
			}
			catch (Exception exception)
			{
				Log.LogErrorFromException(exception);
				return false;
			}
		}

		private static INotNullEnumerable<ProjectRootElement> GetReferencedProjects(ProjectRootElement parentProject)
		{
			Contract.Requires(parentProject != null);
			Contract.Ensures(Contract.Result<INotNullEnumerable<ProjectRootElement>>() != null);
<commit_msg>Fixes a bug where "this" project wasn't being skipped.<commit_after>﻿using System;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.Build.Construction;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Zoltu.Collections.Generic.NotNull;
using Zoltu.Linq.NotNull;

namespace Zoltu.BuildTools.TypeScript
{
	public class FromReferencesTask : Task
	{
		[Required]
		public String ProjectFullPath
		{
			get { return _projectFullPath; }
			set { _projectFullPath = value; }
		}
		private String _projectFullPath;

		[Required]
		public String LibraryDirectoryFullPath
		{
			get { return _libraryDirectoryFullPath; }
			set { _libraryDirectoryFullPath = value; }
		}
		private String _libraryDirectoryFullPath;

		public override Boolean Execute()
		{
			try
			{
				Contract.Assume(!String.IsNullOrEmpty(ProjectFullPath));
				Contract.Assume(!String.IsNullOrEmpty(LibraryDirectoryFullPath));

				var project = ProjectRootElement.Open(ProjectFullPath);
				if (project == null)
					throw new NullReferenceException("project");

				var projectDirectoryPath = project.DirectoryPath;
				if (projectDirectoryPath == null)
					throw new NullReferenceException("projectDirectoryPath");

				Directory.CreateDirectory(LibraryDirectoryFullPath);

				var typeScriptFullPaths = GetReferencedProjects(project)
					.NotNullToNull()
					.Skip(1)
					.SelectMany(x => GetTypeScriptItems(x).NotNullToNull(), GetTypeScriptFileFullPath)
					.NotNull();

				foreach (var typeScriptFullPath in typeScriptFullPaths)
				{
					var sourceDirectoryPath = Path.GetDirectoryName(typeScriptFullPath);
					Contract.Assume(!String.IsNullOrEmpty(sourceDirectoryPath));
					var sourceFileName = Path.GetFileNameWithoutExtension(typeScriptFullPath);
					Contract.Assume(!String.IsNullOrEmpty(sourceFileName));

					CopyFile(sourceDirectoryPath, sourceFileName, LibraryDirectoryFullPath, ".d.ts", ".d.ts");
					CopyFile(sourceDirectoryPath, sourceFileName, LibraryDirectoryFullPath, ".js", ".js");
					var tsSourceFilePath = CopyFile(sourceDirectoryPath, sourceFileName, LibraryDirectoryFullPath, ".ts", ".ts.source");
					var jsMapFilePath = CopyFile(sourceDirectoryPath, sourceFileName, LibraryDirectoryFullPath, ".js.map", ".js.map");

					UpdateSourceMap(sourceFileName, jsMapFilePath, tsSourceFilePath);
				};

				return true;
			}
			catch (Exception exception)
			{
				Log.LogErrorFromException(exception);
				return false;
			}
		}

		private static INotNullEnumerable<ProjectRootElement> GetReferencedProjects(ProjectRootElement parentProject)
		{
			Contract.Requires(parentProject != null);
			Contract.Ensures(Contract.Result<INotNullEnumerable<ProjectRootElement>>() != null);

{'fraction_non_alphanumeric': 0.09682683590208523, 'fraction_numerical': 0.00036264732547597464, 'mean_word_length': 5.101769911504425, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '3189310', 'n_tokens_mistral': 1799, 'n_tokens_neox': 1492, 'n_words': 296}