How to add an assembly reference to a package via a script

Jan 8, 2011 at 11:21 PM
param($installPath, $toolsPath, $package, $project)
$project.Object.References.Add("System.Web")


sometimes I forget just how bloddy easy it is to automate COM with powershell.... (yes, you have to know the precise call and so on, but no need to load assemblies, cast, etc.).

epic win :)

/kzu

--
Daniel Cazzulino | Developer Lead | MS MVP | Clarius Consulting | +1 425.329.3471
Developer
Jan 9, 2011 at 1:01 AM

Dynamic languages ftw?

Jan 9, 2011 at 2:04 AM

for some stuff, definitely!

Jan 9, 2011 at 2:08 AM
Edited Jan 9, 2011 at 2:08 AM

Added Package authoring tips landing page for a hopefully growing collection of "how tos" :)

Cheers!
Coordinator
Jan 9, 2011 at 4:14 PM
Could we simply put each of the tips on the Package Authoring Tips page rather than having it link to it? Also, did you add a link to this on our Documentation home page? http://nuget.codeplex.com/documentation

Phil
Jan 9, 2011 at 5:46 PM

Sure thing.

And yes, its on the documentation homepage inside "authoring"

/kzu from galaxy tab

On Jan 9, 2011 2:15 PM, "Haacked" <notifications@codeplex.com> wrote:
> From: Haacked
>
> Could we simply put each of the tips on the Package Authoring Tips page rather than having it link to it? Also, did you add a link to this on our Documentation home page?http://nuget.codeplex.com/documentation
> Phil
>
>
Jan 9, 2011 at 10:27 PM

Is there a way to troubleshoot when this doesn't work? On one machine, the latest EFCodeFirst.SqlServerCompact doesn't add the references included in init.ps1 using this exact syntax, but I don't see why, and there's no indication until I try to build.

Jan 10, 2011 at 2:30 AM
jongalloway wrote:

Is there a way to troubleshoot when this doesn't work? On one machine, the latest EFCodeFirst.SqlServerCompact doesn't add the references included in init.ps1 using this exact syntax, but I don't see why, and there's no indication until I try to build.

Jon, which build are you using?  Can you type '$Host.Version' from the console?

Jan 10, 2011 at 3:16 AM

Jongalloway, we have verified that this is a recent regression in the default branch.

Jan 23, 2011 at 2:29 PM

It seems to be stripping off anything after the last "." in the name of the assembly that is being referenced. For example, "System.Data" becomes "System". 

Here's a hack to work around the issue:

param($installPath, $toolsPath, $package, $project)
try {
  $project.Object.References.Add("System.Data");
  $project.Object.References.Add("System.Data.Entity");
}
catch {
  $project.Object.References.Add("System.Data.HACK");
  $project.Object.References.Add("System.Data.Entity.HACK");
}

--

Daniel Mohl
blog.danielmohl.com
@dmohl

Jan 23, 2011 at 5:09 PM

@Daniel: not seeing this with the EFCodeFirst.SqlServerCompact package.  Does that package work for you?  Note that those are standard DTE commands, not directly related to NuGet.

Jan 23, 2011 at 7:03 PM

Hi David, Thanks for your response. I tried the EFCodeFirst.SqlServerCompact package yesterday and saw the problem, but have not been able to reproduce it with that package today. I do see it with package FSRepository. I get the following error when the hack shown above is not in place:

Install-Package : Exception calling "Add" with "1" argument(s): "A reference to 'System' (with assembly name 'System') could not be added. A reference to the com
ponent 'System' with the same assembly name already exists in the project."
At line:1 char:16
+ Install-Package <<<<  FSRepository
    + CategoryInfo          : NotSpecified: (:) [Install-Package], MethodInvocationException
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.Cmdlets.InstallPackageCmdlet

Since these are standard DTE commands I'm not sure that it makes a difference, but here's a little background information on what I'm doing:

- The package that I'm authoring (https://github.com/dmohl/FSRepository) targets F# projects.

- Because of this I'm using Change Set 69bd1dc2503a.

I'll keep working on it to try and discover what I'm doing wrong.

Thanks,

Daniel

Jan 23, 2011 at 7:29 PM

Puzzling, especially if you saw this with the EF package, as it's a popular one and we haven't seen this reported (I just tested it with the latest 1.1 bits).  Maybe it only repros on F# projects?

To isolate further and keep your package out of the equation, just try running commands directly from the NuGet Powershell console.  e.g.

PM> $project = Get-Project
PM> $project.Object.References.Add("System.Data")

 

Jan 23, 2011 at 7:31 PM

Actually I just tried it and that's just it: seems to be an F# project issue. dfowler may know something about it.

Developer
Jan 23, 2011 at 7:52 PM

After reflectoring the F# project system it seems that Add(path) expects a full path. They call Path.GetFileNameWithoutExtension(path) that would explain why the last segment is left off. A workaround I see based on the code:

if you do:
project.Object.References.Add("*System.Data")

Then it seems to work fine. The * indicates that it's not a full path. (I'm not sure why this is but that is the case).

Hope that helps

Developer
Jan 23, 2011 at 7:57 PM

In 1.1 we added some properties to DTE's project (via powershell magic) to make project detection easier. You can change the code in your install.ps1 to look like this:

if($project.Type -eq 'F#') {
    $project.Object.References.Add("*System.Data");
    $project.Object.References.Add("*System.Data.Entity");
}

Jan 23, 2011 at 8:01 PM

Interesting, thanks guys!