<commit_before>#
# FileHelpers.ps1
#

$TemplateRootDir = "C:\Dev\JSNLog\jsnlog.SimpleWorkingDemoGenerator\TemplateFiles"
$ProjectsRootDir = "C:\Dev\JSNLog\jsnlogSimpleWorkingDemos\jsnlogSimpleWorkingDemos"
$SolutionPath = "$ProjectsRootDir\jsnlogSimpleWorkingDemos.sln"
$EmptySolutionRootDir = "C:\Dev\JSNLog\jsnlog.SimpleWorkingDemoGenerator\EmptySolution\jsnlogSimpleWorkingDemos"

Function RemoveDirectory([string] $path)
{
	cmd /c "rd /s /q $path"
}

Function CopyDirectory([string] $fromPath, [string] $toPath)
{
	if (Test-Path -path $toPath) { RemoveDirectory $toPath }
	New-Item $toPath -itemtype directory -force
	copy-item "$fromPath\*" $toPath -force -recurse -verbose
}

Function CopyEmptySolution()
{
	CopyDirectory $EmptySolutionRootDir $ProjectsRootDir
}

# Returns the absolute path of a file within a project
Function SiteFilePath([string] $projectName, [string] $siteFileRelPath)
{
	$relPath = Join-Path $projectName $siteFileRelPath
	$path = Join-Path $ProjectsRootDir $relPath
	Return $path
}

# Returns the absolute path of a file within the templates
Function TemplateFilePath([string] $templateFileRelPath)
{
	$path = Join-Path $TemplateRootDir $templateFileRelPath
	Return $path
}

# Returns the absolute path of a feature directory within the templates directory
Function FeatureDirPath([string] $feature)
{
	$path = Join-Path $TemplateRootDir $feature
	Return $path
}

# Returns the absolute path of a project directory within the jsnlogSimpleWorkingDemos directory
Function ProjectDirPath([string] $projectName)
{
	$path = Join-Path $ProjectsRootDir $projectName
	Return $path
}

# Returns the absolute path to a file that lives within a project, given
# the project and the file's relative path within the project directory.
Function ProjectFilePath([string] $projectName, [string] $projectFileRelPath)
{
	$projectDirPath = ProjectDirPath $projectName
	$path = Join-Path $projectDirPath $projectFileRelPath
	Return $path
}

Function ToMarker([string] $markerName)
{
	Return "{{{$markerName}}}"
}

# Inserts $value into a project file, right after the marker with the given name
Function InsertValueInProjectFile([string] $value, [string] $markerName, [string] $projectName, [string] $projectFileRelPath)
{
	$projectFilePath = ProjectFilePath $projectName $projectFileRelPath
	$content = [IO.File]::ReadAllText($projectFilePath)

	$marker = ToMarker $markerName
	$content = $content.replace($marker, "$marker`n$value")

	$content | Out-File -encoding ascii $projectFilePath
}

# Reads a template file and returns its contents.
# Takes a path relative to the root of the template directory.
Function TemplateFileContents([string] $templateFileRelPath)
{
	$path = TemplateFilePath $templateFileRelPath
	$content = [IO.File]::ReadAllText($path)
	Return $content
}

Function EnsureDirectoryExists([string] $path)
{
	$directory = Split-Path -Path $path
	New-Item -ItemType Directory -Force -Path $directory
}

# Writes the given content to a file in a site (represented by a project).
# If the file exists, it is overwritten.
# $siteFileRelPath is the relative path of the file, relative to the root of the project.
Function UpdateSiteFile([string] $content, [string] $projectName, [string] $siteFileRelPath)
{
	$path = SiteFilePath $projectName $siteFileRelPath
	EnsureDirectoryExists $path
	$content | Out-File -encoding ascii $path
}

# Adds a file to the .csproj of a project
# $siteFileRelPath is the relative path of the file, relative to the root of the project.
Function AddFileToCSProj([string] $projectName, [string] $siteFileRelPath)
{
	$marker = '<Content Include="Web.config" />'

	$csProjPath = SiteFilePath $projectName "$projectName.csproj"
	$csProjContent = [IO.File]::ReadAllText($csProjPath)

	$cleanedPath = $siteFileRelPath.Trim("\"," ")

	$newEntry = "<Content Include=""$cleanedPath"" />"
	if ($cleanedPath.EndsWith(".cs"))
	{
		$newEntry = "<Compile Include=""$cleanedPath"" />"
	}

	$csProjNewContent = $csProjContent.replace($marker, "$marker$newEntry")

	$csProjNewContent | Out-File -encoding ascii $csProjPath
}

# Writes the given content to a file in a site (represented by a project).
# If the file exists, it is overwritten.
# $siteFileRelPath is the relative path of the file, relative to the root of the project.
#
# If the file didn't already exist, it is added to the .csproj file as well.
Function AddSiteFile([string] $content, [string] $projectName, [string] $siteFileRelPath)
{
	$path = SiteFilePath $projectName $siteFileRelPath
	if (-not (Test-Path $path))
	{
		AddFileToCSProj $projectName $siteFileRelPath
	}

	UpdateSiteFile $content $projectName $siteFileRelPath
}

# Copies a file to a project within the jsnlogSimpleWorkingDemos site
# $projectFileRelPath is relative, within the project dir.
Function CopyFileToProject([string] $absoluteSourcePath, [string] $projectName, [string] $projectFileRelPath)
{
	$absoluteProjectFilePath = ProjectFilePath $projectName $projectFileRelPath

	if (-not (Test-Path $absoluteProjectFilePath))
	{
		AddFileToCSProj $projectName $projectFileRelPath
	}

	Copy-Item $absoluteSourcePath $absoluteProjectFilePath
}

# In $content, replaces a place holder by the contents of a template file.
# If that template file does not exist, the place holder is simply removed.
# Returns the new content.<commit_msg>create dir of destination file if doesn't exist<commit_after>#
# FileHelpers.ps1
#

$TemplateRootDir = "C:\Dev\JSNLog\jsnlog.SimpleWorkingDemoGenerator\TemplateFiles"
$ProjectsRootDir = "C:\Dev\JSNLog\jsnlogSimpleWorkingDemos\jsnlogSimpleWorkingDemos"
$SolutionPath = "$ProjectsRootDir\jsnlogSimpleWorkingDemos.sln"
$EmptySolutionRootDir = "C:\Dev\JSNLog\jsnlog.SimpleWorkingDemoGenerator\EmptySolution\jsnlogSimpleWorkingDemos"

Function RemoveDirectory([string] $path)
{
	cmd /c "rd /s /q $path"
}

Function CopyDirectory([string] $fromPath, [string] $toPath)
{
	if (Test-Path -path $toPath) { RemoveDirectory $toPath }
	New-Item $toPath -itemtype directory -force
	copy-item "$fromPath\*" $toPath -force -recurse -verbose
}

Function CopyEmptySolution()
{
	CopyDirectory $EmptySolutionRootDir $ProjectsRootDir
}

# Returns the absolute path of a file within a project
Function SiteFilePath([string] $projectName, [string] $siteFileRelPath)
{
	$relPath = Join-Path $projectName $siteFileRelPath
	$path = Join-Path $ProjectsRootDir $relPath
	Return $path
}

# Returns the absolute path of a file within the templates
Function TemplateFilePath([string] $templateFileRelPath)
{
	$path = Join-Path $TemplateRootDir $templateFileRelPath
	Return $path
}

# Returns the absolute path of a feature directory within the templates directory
Function FeatureDirPath([string] $feature)
{
	$path = Join-Path $TemplateRootDir $feature
	Return $path
}

# Returns the absolute path of a project directory within the jsnlogSimpleWorkingDemos directory
Function ProjectDirPath([string] $projectName)
{
	$path = Join-Path $ProjectsRootDir $projectName
	Return $path
}

# Returns the absolute path to a file that lives within a project, given
# the project and the file's relative path within the project directory.
Function ProjectFilePath([string] $projectName, [string] $projectFileRelPath)
{
	$projectDirPath = ProjectDirPath $projectName
	$path = Join-Path $projectDirPath $projectFileRelPath
	Return $path
}

Function ToMarker([string] $markerName)
{
	Return "{{{$markerName}}}"
}

# Inserts $value into a project file, right after the marker with the given name
Function InsertValueInProjectFile([string] $value, [string] $markerName, [string] $projectName, [string] $projectFileRelPath)
{
	$projectFilePath = ProjectFilePath $projectName $projectFileRelPath
	$content = [IO.File]::ReadAllText($projectFilePath)

	$marker = ToMarker $markerName
	$content = $content.replace($marker, "$marker`n$value")

	$content | Out-File -encoding ascii $projectFilePath
}

# Reads a template file and returns its contents.
# Takes a path relative to the root of the template directory.
Function TemplateFileContents([string] $templateFileRelPath)
{
	$path = TemplateFilePath $templateFileRelPath
	$content = [IO.File]::ReadAllText($path)
	Return $content
}

Function EnsureDirectoryExists([string] $path)
{
	$directory = Split-Path -Path $path
	New-Item -ItemType Directory -Force -Path $directory
}

# Writes the given content to a file in a site (represented by a project).
# If the file exists, it is overwritten.
# $siteFileRelPath is the relative path of the file, relative to the root of the project.
Function UpdateSiteFile([string] $content, [string] $projectName, [string] $siteFileRelPath)
{
	$path = SiteFilePath $projectName $siteFileRelPath
	EnsureDirectoryExists $path
	$content | Out-File -encoding ascii $path
}

# Adds a file to the .csproj of a project
# $siteFileRelPath is the relative path of the file, relative to the root of the project.
Function AddFileToCSProj([string] $projectName, [string] $siteFileRelPath)
{
	$marker = '<Content Include="Web.config" />'

	$csProjPath = SiteFilePath $projectName "$projectName.csproj"
	$csProjContent = [IO.File]::ReadAllText($csProjPath)

	$cleanedPath = $siteFileRelPath.Trim("\"," ")

	$newEntry = "<Content Include=""$cleanedPath"" />"
	if ($cleanedPath.EndsWith(".cs"))
	{
		$newEntry = "<Compile Include=""$cleanedPath"" />"
	}

	$csProjNewContent = $csProjContent.replace($marker, "$marker$newEntry")

	$csProjNewContent | Out-File -encoding ascii $csProjPath
}

# Writes the given content to a file in a site (represented by a project).
# If the file exists, it is overwritten.
# $siteFileRelPath is the relative path of the file, relative to the root of the project.
#
# If the file didn't already exist, it is added to the .csproj file as well.
Function AddSiteFile([string] $content, [string] $projectName, [string] $siteFileRelPath)
{
	$path = SiteFilePath $projectName $siteFileRelPath
	if (-not (Test-Path $path))
	{
		AddFileToCSProj $projectName $siteFileRelPath
	}

	UpdateSiteFile $content $projectName $siteFileRelPath
}

# Copies a file to a project within the jsnlogSimpleWorkingDemos site
# $projectFileRelPath is relative, within the project dir.
Function CopyFileToProject([string] $absoluteSourcePath, [string] $projectName, [string] $projectFileRelPath)
{
	$absoluteProjectFilePath = ProjectFilePath $projectName $projectFileRelPath

	if (-not (Test-Path $absoluteProjectFilePath))
	{
		AddFileToCSProj $projectName $projectFileRelPath
	}

	# Create destination directory in case it doesn't exist
	mkdir -force (Split-Path $absoluteProjectFilePath) | Out-Null

	Copy-Item $absoluteSourcePath $absoluteProjectFilePath
}

# In $content, replaces a place holder by the contents of a template file.
# If that template file does not exist, the place holder is simply removed.
# Returns the new content.
{'fraction_non_alphanumeric': 0.10214504596527069, 'fraction_numerical': 0.00018571826539140124, 'mean_word_length': 5.794952681388013, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2342387', 'n_tokens_mistral': 3328, 'n_tokens_neox': 3235, 'n_words': 1035}