的MSBuild,自定义任务运行自定义工具生成类LINQ to SQL的模式呢? [英] MSBuild, custom task to run custom tool to generate classes for linq to sql model?

查看:134
本文介绍了的MSBuild,自定义任务运行自定义工具生成类LINQ to SQL的模式呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情形。我们使用存储过程来访问数据库,我们使用LINQ 2 SQL生成的类或也就是我们用不插电的的LINQ to SQL生成这一点。它已运行为自定义工具,但版本比较生成的类是在脖子上一个很大的痛苦。我们想自动生成的类,但是从版本控制中排除它,所以我创建一个MSBuild任务的任务设置。发现这篇文章和<一个HREF =htt​​p://dasz.at/index.php/2010/02/building-a-simple-msbuild-task/相对=nofollow>这个帖子,但我解决不了这个我自己。我添加一些代码的任务如下所示:

I have the following scenario. We use stored procedures to access the database and we use LiNQ 2 SQL to generate the classes or namely we use Unplugged LINQ to SQL Generator for this. It has been run as a custom tool but diffing the generated classes is a big pain in the neck. We would like to auto generate the classes but exclude it from version control so I set on the task of creating an msbuild task. Found this post and this post but I can't solve this one by myself. I added some code the task looks like the following:

public class GenerateDesignerDC : Task
{
    public ITaskItem[] InputFiles { get; set; }
    public ITaskItem[] OutputFiles { get; set; }

    public override bool Execute()
    {
        var generatedFileNames = new List<string>();
        foreach (var task in InputFiles)
        {

            string inputFileName = task.ItemSpec;
            string outputFileName = Path.ChangeExtension(inputFileName, ".Designer.cs");
            string result;

            // Build code string
            var generator = new ULinqCodeGenerator("CSharp");
            string fileContent;
            using (FileStream fs = File.OpenRead(inputFileName))
            using (StreamReader rd = new StreamReader(fs))
            {
                fileContent = rd.ReadToEnd();
            }

            using (var destination = new FileStream(outputFileName, FileMode.Create))
            {
                byte[] bytes = Encoding.UTF8.GetBytes(generator.BuildCode(inputFileName, fileContent));
                destination.Write(bytes, 0, bytes.Length);
            }
            generatedFileNames.Add(outputFileName);
        }

        OutputFiles = generatedFileNames.Select(name => new TaskItem(name)).ToArray();

        return true;
    }
}

现在我尝试添加自定义目标,这被称为custom.target

Now I try to add a custom target for this called custom.target

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <CoreCompileDependsOn>$(CoreCompileDependsOn);GenerateToolOutput</CoreCompileDependsOn>
    </PropertyGroup>
    <UsingTask TaskName="BuildTasks.GenerateDesignerDC" AssemblyFile="BuildTasks.dll" />
    <Target Name="GenerateToolOutput" Inputs="@(dbml)" Outputs="@(dbml->'$(IntermediateOutputPath)%(FileName).designer.cs')">
        <GenerateDesignerDC InputFiles="@(dbml)" OutputFiles="@(dbml->'$(IntermediateOutputPath)%(FileName).designer.cs')">
            <Output ItemGroup="Compile" TaskParameter="OutputFiles" />
        </GenerateDesignerDC>
    </Target>
</Project>



我也添加必要的ItemGroups到项目文件类似如下:

I also add the necessary ItemGroups to the project file like follows:

<ItemGroup>
    <AvailableItemName Include="dbml" />
</ItemGroup>
<ItemGroup>
    <Compile Include="@(dbml)" />
</ItemGroup>



最后,我将文件添加到项目中的以下内容:

And finally I add the files to the project with the following:

<dbml Include="DAL\SettingsDC.dbml">
    <SubType>Designer</SubType>
    <Generator>ULinqToSQLGenerator</Generator>
    <LastGenOutput>SettingsDC.designer.cs</LastGenOutput>
</dbml>

这会导致一个错误消息说

This causes an error message saying

GenerateDesignerDC的任务有一个
无效的输出规范。在
TaskParameter是必需的属性,
和无论是ITEMNAME或
属性名属性必须是
规定(但不能同时)。

The "GenerateDesignerDC" task has an invalid output specification. The "TaskParameter" attribute is required, and either the "ItemName" or "PropertyName" attribute must be specified (but not both).

什么我需要做的,使这项工作?

What do I need to do to make this work?

推荐答案

您还没有宣布你的任务的输出特性。你必须使用对 OutputFiles 属性输出属性。

You haven't declared an output property in your task. You have to use the Output attribute on OutputFiles property.

public class GenerateDesignerDC : Task
{
    [Required]
    public ITaskItem[] InputFiles { get; set; }

    [Output]
    public ITaskItem[] OutputFiles { get; set; }

    public override bool Execute()
    {
        var generatedFileNames = new List<string>();
        foreach (var task in InputFiles)
        {

            string inputFileName = task.ItemSpec;
            string outputFileName = Path.ChangeExtension(inputFileName, ".Designer.cs");
            string result;

            // Build code string
            var generator = new ULinqCodeGenerator("CSharp");
            string fileContent;
            using (FileStream fs = File.OpenRead(inputFileName))
            using (StreamReader rd = new StreamReader(fs))
            {
                fileContent = rd.ReadToEnd();
            }

            using (var destination = new FileStream(outputFileName, FileMode.Create))
            {
                byte[] bytes = Encoding.UTF8.GetBytes(generator.BuildCode(inputFileName, fileContent));
                destination.Write(bytes, 0, bytes.Length);
            }
            generatedFileNames.Add(outputFileName);
        }

        OutputFiles = generatedFileNames.Select(name => new TaskItem(name)).ToArray();

        return true;
    }
}

这篇关于的MSBuild,自定义任务运行自定义工具生成类LINQ to SQL的模式呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆