Create a gist now

Instantly share code, notes, and snippets.

Add Source File Headers
#requires -version 2
<# #>
param(
[Parameter(Mandatory = $true)]
[string] $sourceRoot,
[Parameter(Mandatory = $true)]
[string] $versionNumber
)
function CorrectPathBackslash {
param([string] $path)
if (![String]::IsNullOrEmpty($path)) {
if (!$path.EndsWith("\")) {
$path += "\"
}
}
return $path
}
$sourceRoot = CorrectPathBackslash $sourceRoot
#Change any ..\.. etc to proper path for path length usage later
$sourceRoot = [IO.Path]::GetFullPath($sourceRoot)
$today = Get-Date -format g
$visitedFiles = @{}
$projects = Get-ChildItem $sourceRoot -Filter *.csproj -Recurse
foreach($project in $projects) {
Write-Verbose "Processing $($project.Name)..."
$projectXml = New-Object XML
$projectXml.Load($project.FullName)
if ($projectXml.Project.PropertyGroup.Length -lt 1) {
Write-Verbose "No default property group for $($project.FullName)"
continue
}
$assemblyName = $projectXml.Project.PropertyGroup[0].AssemblyName
$sourceFiles = Get-ChildItem $project.Directory -Filter *.cs -Recurse
foreach($sourceFile in $sourceFiles) {
if ($visitedFiles.Contains($sourceFile.FullName)) {
continue
}
$visitedFiles[$sourceFile.FullName]=1
$content = Get-Content $sourceFile.FullName
Write-Verbose "Processing source file: $sourceFile.FullName"
if($content[0].StartsWith("// YourLibrary SDK")) {
for($i = 0; $i -lt 8; $i++) {
$content[$i] = $null
}
}
$relativeFilename = $sourceFile.FullName.Remove(0, $sourceRoot.Length)
$preface = "// YourLibrary SDK`n" + `
"// $relativeFilename`n" + `
"// $assemblyName, Version $versionNumber, Published $today`n" + `
"// (c) Copyright YourCompany`n" + `
"// --------------------------------------------------------------------------------`n" + `
"`n" + `
"`n"
Set-Content $sourceFile.FullName -value $preface, $content
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment