public
Last active

Format all CS files in a Visual Studio solution (UGLY VERSION)

  • Download Gist
FormatAllFiles.ps1
PowerShell
1 2 3 4 5
// Open up the NuGet Package Manager in Visual Studio and paste the following line to format every file in the solution.
// I'll write a not-so-ugly version later. ;)
// BUG! This doesn't recursively grab all files from the project. :(
 
$dte.Solution.Projects | ForEach-Object {$_.ProjectItems | ForEach-Object { if ($_.Name.EndsWith('.cs')) {$window = $_.Open('{7651A701-06E5-11D1-8EBD-00A0C90F26EA}'); if ($window){Write-Host $_.Name;[System.Threading.Thread]::Sleep(100);$window.Activate();$_.Document.DTE.ExecuteCommand('Edit.FormatDocument');$_.Document.DTE.ExecuteCommand('Edit.RemoveAndSort');$window.Close(1);}} }}

http://nuget.codeplex.com/workitem/1055 does it recursively. Well, not the same action, but takes an action.

Looks like you have been after a solution to this problem for awhile now - back in 2009 I blogged about a Tweet you made and provided a macro solution to achieve this:
http://www.brianschmitt.com/2009/09/quickly-reformat-your-project-files.html

I know its not nuget, but it is a solution, and it works recursively. ;)

Your Macro solution looks great. It doesn't remove and sort usings though,
but that's an easy one line change.

DTE.ExecuteCommand("Edit.RemoveAndSort")

My co-worker and I came up with a nice solution using NuGet over the weekend
before I saw your email. But I made sure to link to your Macro solution in
my blog post. :)

http://haacked.com/archive/2011/05/22/an-obsessive-compulsive-guide-to-source-code-formatting.aspx

http://haacked.com/archive/2011/05/22/an-obsessive-compulsive-guide-to-source-code-formatting.aspx
Phil

On Sat, May 21, 2011 at 4:17 PM, brianschmitt <
reply@reply.github.com>wrote:

Looks like you have been after a solution to this problem for awhile now -
back in 2009 I blogged about a Tweet you made and provided a macro solution
to achieve this:

http://www.brianschmitt.com/2009/09/quickly-reformat-your-project-files.html

I know its not nuget, but it is a solution, and it works recursively. ;)

Reply to this email directly or view it on GitHub:
https://gist.github.com/984353

It looks like you're trying to write PowerShell. Would you like me to launch the PowerShell for Dummies Clippy Lives Forever Edition for you to do this?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.