views:

231

answers:

1

I'm trying to write an MSBuild task to build a database using FluentNhibernate mappings.

The code for the task currently looks like this...

public class CreateDatabase : Task
{
    [Required]
    public string ConfigFile
    { get; set; }

    [Required]
    public string MappingAssemblyName
    { get; set; }

    public override bool Execute()
    {
        var mappingAssembly = Assembly.Load(MappingAssemblyName);

        var config = new Configuration();
        config.Configure(ConfigFile);

        var fluentConfig = Fluently.Configure(config)
            .Mappings(m => m.FluentMappings.AddFromAssembly(mappingAssembly));

        var sessionSource = new SessionSource(fluentConfig);

        sessionSource.BuildSchema();

        return true;
    }
}

and the MSBuild usage looks like this...

  <ItemGroup>
    <Content Include="hibernate.cgf.xml" />
  </ItemGroup>
  <UsingTask AssemblyFile="..\lib\MyUtilities.MSBuild.dll" TaskName="CreateDatabase" />
  <Target Name="Build" >
    <CreateDatabase ConfigFile="@(Content)" MappingAssemblyName="MyMappingAssemlyName" />
  </Target>

But now I'm stuck

Not surprisingly, Assembly.Load fails because the assembly containing my Fluent mappings ('MyMappingAssemly') isn't present.

Assuming that the Fluent mappings are defined within another project within my solution, what is the best way to tell my MSBuild task about the mapping assembly? I think I might be going down the wrong path using the 'MappingAssemblyName' property.

+1  A: 

I'm assuming that you want to get the path to the output of a project, which defines your 'fluent mappings'. You can use the 'GetTargetPath' target like this:

<MSBuild Projects="..\path\to\projectfile" Targets="GetTargetPath">
    <Output TaskParameter="TargetOutputs" ItemName="ProjectPath"/>
</MSBuild>

You might want to set the configuration/platform for the target platform like this:

Properties="Configuration=$(Configuration); Platform=$(Platform)"

But this will work fine only if the config/platform for the referenced project match the current project. If you have different values set by the solution file, then you'll have to use the AssignProjectConfiguration task.

Since, I'm not sure whether this exactly what you want, i'll stop here and add more info later, if needed.

Ankit
Thanks Ankit, that's been a lot of help. Though it seemed I had to use the "PropertyName" attribute, rather than "ItemName".I now have a separate problem. My custom task has dependencies on other, third-party assemblies. How do I ensure that those assemblies are present when MSBuild executes my task? Added a reference to the project calling the task doesn't seem to be doing the trick.
sandy
Added this question http://stackoverflow.com/questions/1636521/custom-msbuild-with-other-dependencies
sandy