【问题标题】:MSBuild, custom task to run custom tool to generate classes for linq to sql model?MSBuild,自定义任务运行自定义工具为 linq to sql 模型生成类?
【发布时间】:2010-07-06 15:24:45
【问题描述】:

我有以下情况。我们使用存储过程来访问数据库,我们使用 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”属性必须是 指定(但不能同时指定)。

我需要做什么才能完成这项工作?

【问题讨论】:

    标签: c# msbuild msbuild-task


    【解决方案1】:

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

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

    【讨论】:

    • 正确,我还需要添加
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    相关资源
    最近更新 更多