Last active
August 29, 2015 14:26
-
-
Save maftieu/609c6042d513805bbee0 to your computer and use it in GitHub Desktop.
Edit all C# files inside a project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this requires NuGet to be installed, so it will bring the PowerShell command line | |
# and the EnvDTE object. See: https://msdn.microsoft.com/en-us/library/EnvDTE.aspx | |
function DoItOnProjectItem($item) { | |
$doc = $item; | |
if ($doc.Name -match "\.cs$") { | |
write-host $doc.Name; | |
# open and display document | |
$foo = $doc.Open("{7651A701-06E5-11D1-8EBD-00A0C90F26EA}"); # vsViewKindCode -> to display code window | |
# it's possible to not activate the document (that won't display it), | |
# but commands like remove/sort using and format won't work | |
$doc.Document.Activate(); | |
$sel = $doc.Document.Selection; | |
$sel.StartOfDocument(); | |
# add some using | |
$sel.Insert("using X.Y.Z;");$sel.NewLine(1); | |
# ... | |
# and remove other ones | |
if ($sel.FindText("using Z.Y.X", 0)) { $sel.SelectLine(); $sel.Delete(); } | |
# make some text replacement | |
while ($sel.FindText("Sender.Send", 0)) { $sel.Insert("SysLogSender.Send"); } | |
# remove and sort usings | |
$dte.ExecuteCommand("EditorContextMenus.CodeWindow.OrganizeUsings.RemoveAndSort"); | |
# and cleanly format document ;) | |
$dte.ExecuteCommand("Edit.FormatDocument"); | |
$doc.Save(); | |
$doc.Document.Close(); | |
} | |
} | |
function ModifyProject($projName) { | |
$dte.Solution.Projects | foreach-object { | |
$proj = $_; | |
if ($proj.Name -eq $projName) { | |
Write-Host "Project '$projName' found." | |
$proj.ProjectItems | foreach-object { | |
DoItOnProjectItem($_); | |
# do the same on sub-items (useful for code-behind) | |
$_.ProjectItems | foreach-object { DoItOnProjectItem($_); } | |
} | |
} | |
} | |
} | |
ModifyProject("Project Name"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment