Monthly Archives: February 2016

Export all types implementing interface with Castle Windsor

Just an example!

In the registration part of your container, do this:

Classes.FromAssemblyContaining<IForcedZoneType>()
.BasedOn<IForcedZoneType>()
.WithService.FromInterface().LifestyleSingleton()
);

Isn’t that pure madness? That’s how fluent APIs work. Readable, flexible and… HIGH LEARNING CURVE!

Advertisements

NuGet package that copies files/folders to Output

You have the specific need: your NuGet package needs some native libs/files (not .NET assemblies) that need to place in the output folder.

For example, I have been using LEAD Tools to perform OCR to some images. The OCR engine needs a bunch of files to work (build and run, of course), so I created this structure inside my NuGet package:

snip_20160205133525

Basically, if consists of a build folder (special folder) inside the package, with the file structure that you want to be copied into your output folder (where your executable files and/or libraries will be built).

And most importantly, it also needs to have a .targets file that is a special MSBuild file that will be interpreted each type you build your project.

In the snapshot above it’s listed as LeadTools.Ocr.AdvantageEngine-x86.targets

This file has to contain this script:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <NativeLibs Include="$(MSBuildThisFileDirectory)**\*.*" />
    <Content Include="@(NativeLibs)">
      <Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
</Project>

I’m not 100% sure, but I think this file should have the same name as your NuGet package (the Id of the package).

With both things, the files/folders inside the build folder and the targets file, you’re done 🙂

Installing a package like this in a project will modify your .csproj to include a special Import section like this:

  <Import Project="..\packages\LeadTools.Ocr.AdvantageEngine-x86.19.0.1\build\LeadTools.Ocr.AdvantageEngine-x86.targets" Condition="Exists('..\packages\LeadTools.Ocr.AdvantageEngine-x86.19.0.1\build\LeadTools.Ocr.AdvantageEngine-x86.targets')" />
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('..\packages\LeadTools.Ocr.AdvantageEngine-x86.19.0.1\build\LeadTools.Ocr.AdvantageEngine-x86.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\LeadTools.Ocr.AdvantageEngine-x86.19.0.1\build\LeadTools.Ocr.AdvantageEngine-x86.targets'))" />
  </Target>