MSBuild,自定义任务运行自定义工具生成linq到sql模型的类?

时间:2010-07-06 15:24:46

标签: c# msbuild msbuild-task

我有以下情况。我们使用存储过程来访问数据库,我们使用LiNQ 2 SQL生成类,或者我们使用Unplugged LINQ to SQL Generator。它已经作为自定义工具运行,但是对生成的类进行区分是一个巨大的痛苦。我们想自动生成类但是将它从版本控制中排除,所以我开始创建一个msbuild任务。找到this postthis post,但我不能自己解决这个问题。我添加了一些代码,任务如下所示:

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

添加自定义目标
<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添加到项目文件中,如下所示:

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

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

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

这会导致出现错误信息

  

“GenerateDesignerDC”任务有一个   输出规格无效。该   “TaskParameter”属性是必需的,   以及“ItemName”或   “PropertyName”属性必须是   指定(但不是两者)。

我需要做些什么来完成这项工作?

1 个答案:

答案 0 :(得分:10)

您尚未在任务中声明输出属性。您必须使用Output属性上的OutputFiles属性。

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;
    }
}