Skip to content

Instantly share code, notes, and snippets.

@jacano
Forked from paveltimofeev/CS-merger.ps1
Created March 15, 2017 12:59
Show Gist options
  • Save jacano/7a7928c9c0652870012ac4268b87f85d to your computer and use it in GitHub Desktop.
Save jacano/7a7928c9c0652870012ac4268b87f85d to your computer and use it in GitHub Desktop.
Merge *.cs files into the one file
param(
[Parameter(Mandatory=$true)]
$source,
[Parameter(Mandatory=$true)]
$output,
$header = "/// © Pavel Timofeev",
$excludes = @("AssemblyInfo.cs", "*.Designer.cs", "TemporaryGeneratedFile_*" ),
$copyAsIs = @(), # list of filemasks that should be copy as is without merge
$addFileNames = $false, # add names of src files
$leftComments = $false, # remove line commented with //
$compactify = $true # remove empty lines
)
$usings = @()
$classes = @()
Get-ChildItem $source -Filter "*.cs" -Recurse -Exclude $excludes | % {
$name = $_.Name
"Processing $name`r`n"
if($addFileNames) { $classes += ("`r`n// {0}" -f $name) }
if(($copyAsIs | ? { $name -match $_ }).Count -eq 0)
{
gc -Path $_.FullName | % {
$str = $_.ToString().Trim()
if($str -match "using " -and $str -notmatch "using \(") {
$usings += $str
}
else {
if($leftComments -or !$str.StartsWith("//") )
{
if( !$compactify -or $str -ne "")
{
$classes += $_
}
}
}
}
}
else
{
copy-item $_.FullName -Destination (Split-Path $output -Parent)
}
}
sc $output '///' # cleanup output file
ac $output $header
ac $output "/// Changed at $((Get-Date).GetDateTimeFormats('s'))`r`n"
ac $output ($usings | Sort | Get-Unique)
ac $output $classes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment