<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
	<!-- Task to concat files injecting their names in between -->
	<UsingTask TaskName="ConcatFiles" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
		<ParameterGroup>
			<Inputs ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
			<BaseDir ParameterType="System.String" Required="true" />
			<Output ParameterType="System.String" Required="true" />
		</ParameterGroup>
		<Task>
			<Using Namespace="System"/>
			<Using Namespace="System.IO"/>
			<Code Type="Fragment" Language="cs"><![CDATA[
				using (StreamWriter output = new StreamWriter(Output)) {
					int bdl = BaseDir.Length;
					foreach (ITaskItem item in Inputs) {
						string inp = item.GetMetadata("FullPath");
						string rp = inp.StartsWith(BaseDir) ? inp.Substring(bdl) : inp; // a simple relative path
						output.WriteLine("/* Source: {0} */", rp);
						output.Write(File.ReadAllText(inp));
						output.WriteLine();
					}
				}
			]]></Code>
		</Task>
	</UsingTask>

	<!-- https://stackoverflow.com/questions/7837644/how-to-replace-string-in-file-using-msbuild -->
	<UsingTask TaskName="ReplaceFileText" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
	<ParameterGroup>
		<InputFilename ParameterType="System.String" Required="true" />
		<OutputFilename ParameterType="System.String" Required="true" />
		<MatchExpression ParameterType="System.String" Required="true" />
		<ReplacementText ParameterType="System.String" Required="true" />
	</ParameterGroup>
	<Task>
		<Using Namespace="System" />
		<Using Namespace="System.IO" />
		<Using Namespace="System.Text.RegularExpressions" />
		<Code Type="Fragment" Language="cs">
		<![CDATA[
				File.WriteAllText(
					OutputFilename,
					Regex.Replace(File.ReadAllText(InputFilename), MatchExpression, ReplacementText)
					);
			]]>
		</Code>
	</Task>
	</UsingTask>

	<!-- https://stackoverflow.com/questions/3524317/regex-to-strip-line-comments-from-c-sharp/3524689#3524689 -->
	<UsingTask TaskName="StripComments" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
	<ParameterGroup>
		<InputFilename ParameterType="System.String" Required="true" />
		<OutputFilename ParameterType="System.String" Required="true" />
	</ParameterGroup>
	<Task>
		<Using Namespace="System" />
		<Using Namespace="System.IO" />
		<Using Namespace="System.Text.RegularExpressions" />
		<Code Type="Fragment" Language="cs">
		<![CDATA[
				var blockComments = @"/\*(.*?)\*/";
				var lineComments = @"//(.*?)\r?\n";
				var strings = @"""((\\[^\n]|[^""\n])*)""";
				var verbatimStrings = @"@(""[^""]*"")+";
				File.WriteAllText(
					OutputFilename,
					Regex.Replace(File.ReadAllText(InputFilename),
						blockComments + "|" + lineComments + "|" + strings + "|" + verbatimStrings,
						me => {
							if (me.Value.StartsWith("/*") || me.Value.StartsWith("//"))
								return me.Value.StartsWith("//") ? "\n" : "";
							// Keep the literal strings
							return me.Value;
						},
						RegexOptions.Singleline
					)
				);
			]]>
		</Code>
	</Task>
	</UsingTask>

	<UsingTask TaskName="GetToolPath" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
		<ParameterGroup>
			<ToolName Required="true" />
			<LocalPath />
			<ToolPath Output="true" />
			<ToolFound ParameterType="System.Boolean" Output="true" />
		</ParameterGroup>
		<Task>
			<Reference Include="Microsoft.Win32.Registry" />
			<Code Type="Class" Language="cs" Source="$(MSBuildThisFileDirectory)FindToolPath.cs" />
		</Task>
	</UsingTask>


	<Target Name="FindGitWindows" Condition=" '$([MSBuild]::IsOsPlatform(Windows))' ">
		<GetToolPath ToolName="git">
			<Output TaskParameter="ToolPath" PropertyName="GitExe"/>
			<Output TaskParameter="ToolFound" PropertyName="GitExeFound"/>
		</GetToolPath>
	</Target>

	<Target Name="FindGitUnix" Condition=" '$([MSBuild]::IsOSUnixLike())' " >
		<PropertyGroup>
			<GitExe>git</GitExe>
		</PropertyGroup>
		<Exec Command="$(GitExe) help > /dev/null" IgnoreExitCode="True">
			<Output TaskParameter="ExitCode" PropertyName="GitExeExitCode" />
		</Exec>
		<PropertyGroup>
			<GitExeFound Condition=" '$(GitExeExitCode)' == '0' " >true</GitExeFound>
			<GitExeFound Condition=" '$(GitExeExitCode)' != '0' " >false</GitExeFound>
		</PropertyGroup>
	</Target>

	<Target Name="FindGit" DependsOnTargets="FindGitWindows;FindGitUnix" />
</Project>

devTools/FC.targets