0

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?

Community
  • 1
  • 1
awj
  • 7,482
  • 10
  • 66
  • 120
  • `(int) ([EnvDTE.vsCMPart]::vsFindOptionsFromStart)` Is that supposed to be a cast? `[int]([EnvDTE.vsCMPart]::vsFindOptionsFromStart)` is what that should be assuming `[EnvDTE.vsCMPart]::vsFindOptionsFromStart` on its own is not viable. Is this something similar? `ref $endPoint` should be `[ref]$endPoint` maybe? FYI I know nothing about NuGet – Matt Mar 15 '15 at 00:18
  • My apologies, @Matt - that `(int)` shouldn't have been posted in the question - it was lingering from a trial-and-error period. It shouldn't be there, and even if I add parentheses that still makes no difference. I'll edit the question to reflect how I believe it should be entered. [If included, Visual Studio highlights the cast as an exception.] – awj Mar 15 '15 at 07:45

0 Answers0