Skip to content

Instantly share code, notes, and snippets.

@paveltimofeev
Last active March 7, 2022 22:39
Show Gist options
  • Save paveltimofeev/409ae6876a669894422e734a279c76a1 to your computer and use it in GitHub Desktop.
Save paveltimofeev/409ae6876a669894422e734a279c76a1 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
@paveltimofeev
Copy link
Author

powershell .\CS-merger.ps1 -source "C:\project\src\" -output "C:\project\assets\all-in-one.cs"

@paveltimofeev
Copy link
Author

set /p HEADER=<C:\project\src\copyrights.txt
powershell .\CS-merger.ps1 -source "C:\project\src\" -output "C:\project\assets\all-in-one.cs" -header %HEADER%

@paveltimofeev
Copy link
Author

paveltimofeev commented Jun 17, 2016

npm install -g grunt-cli

Gruntfile.js:

module.exports = function(grunt) {

  grunt.initConfig({
    watch: {
      scripts: {
        files: ['**/*.cs'],
        tasks: ['shell:ps'],
        options: {spawn: false},
      },
    },
    shell: {
      ps: {
        options: {stdout: true},
        command: 'powershell C:\\Temp\\csmerge\\CS-merger.ps1 -source C:\\Temp\\csmerge\\src\\ -output C:\\Temp\\csmerge\\compiled.cs -copyAsIs service,behaviour >> C:\\Temp\\csmerge\\CS-merger.log'
      }
    }
  });

  grunt.loadNpmTasks('grunt-shell');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.registerTask('default', ['watch']);
}

package.json

{
  "name": "csmerge",
  "version": "1.0.0",
  "description": "",
  "main": "Gruntfile.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "grunt": "~0.4.5",
    "grunt-contrib-watch": "1.0.0",
    "grunt-shell": "1.3.0"
  }
}

Run:

grunt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment