I have a few NuGet packages that I've put together, with one of them being a common project referenced by all the others.
This common project inserts a configuration class into the App_Start
folder, and a method on this class is then invoked by WebActivator.
For one of the other packages I wish to add one more line of code to this method and I achieve this using install.ps1 (using code from this SO post).
To be a diligent NuGet package developer, I should probably remove this line of code if the NuGet package is uninstalled.
To do this removal I've tried using the FindPattern method of the FileCodeModel to find the line of code, but haven't had any luck in getting it to work.
This is what I have so far:
$app_start = $project.ProjectItems.Item("App_Start")
$item = $app_start.ProjectItems.Item("my-config-file.cs")
$namespace = $item.FileCodeModel.CodeElements | ? {$_.Kind -eq 5}
$class = $namespace.Members.Item("My-Config-Class")
$method = $class.Members.Item("My-Method-Name")
$startPoint = $method.GetStartPoint([EnvDTE.vsCMPart]::vsCMPartBody)
$startPoint = $startPoint.CreateEditPoint()
$endPoint = $method.GetEndpoint([EnvDTE.vsCMPart]::vsCMPartBody)
$endPoint = $endpoint.CreateEditPoint()
$startPoint.FindPattern("known-line-of-code", [EnvDTE.vsCMPart]::vsFindOptionsFromStart, ref $endPoint)
Specifically, it's that last line, FindPattern
, that I'm having trouble with. The docs list another optional parameter, and judging by Visual Studio's exception message...
Exception calling "FindPattern" with "3" argument(s): "Type mismatch.
...I guess I'm supposed to supply this parameter. But what do I supply it with?
And even if I could find the line of code, how do I then delete it?
Or is FindPattern
the wrong way to proceed? Is there another way to remove a known line of code?