C# MSBuild,要运行自定义工具为linq to sql模型生成类的自定义任务?
我有下面的场景。我们使用存储过程访问数据库,并使用LiNQ 2 SQL生成类,也就是说,我们使用不插电的方法来生成类。它已经作为一个自定义工具运行,但是对生成的类进行区分是一个很大的难题。我们希望自动生成类,但将其从版本控制中排除,因此我开始创建msbuild任务。找到了,但我自己解决不了这个问题。我添加了一些代码,任务如下所示:C# MSBuild,要运行自定义工具为linq to sql模型生成类的自定义任务?,c#,msbuild,msbuild-task,C#,Msbuild,Msbuild Task,我有下面的场景。我们使用存储过程访问数据库,并使用LiNQ 2 SQL生成类,也就是说,我们使用不插电的方法来生成类。它已经作为一个自定义工具运行,但是对生成的类进行区分是一个很大的难题。我们希望自动生成类,但将其从版本控制中排除,因此我开始创建msbuild任务。找到了,但我自己解决不了这个问题。我添加了一些代码,任务如下所示: public class GenerateDesignerDC : Task { public ITaskItem[] InputFiles { get; s
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;
}
}
<dbml Include="DAL\SettingsDC.dbml">
<SubType>Designer</SubType>
<Generator>ULinqToSQLGenerator</Generator>
<LastGenOutput>SettingsDC.designer.cs</LastGenOutput>
</dbml>
public类GenerateDesignerDC:Task
{
公共ITaskItem[]输入文件{get;set;}
公共ITaskItem[]输出文件{get;set;}
公共重写bool Execute()
{
var generatedFileNames=新列表();
foreach(InputFiles中的var任务)
{
字符串inputFileName=task.ItemSpec;
字符串outputFileName=Path.ChangeExtension(inputFileName,“.Designer.cs”);
字符串结果;
//生成代码字符串
var生成器=新的ULinqCodeGenerator(“CSharp”);
字符串文件内容;
使用(FileStream fs=File.OpenRead(inputFileName))
使用(StreamReader rd=新StreamReader(fs))
{
fileContent=rd.ReadToEnd();
}
使用(var destination=newfilestream(outputFileName,FileMode.Create))
{
byte[]bytes=Encoding.UTF8.GetBytes(generator.BuildCode(inputFileName,fileContent));
destination.Write(字节、0、字节、长度);
}
generatedFileNames.Add(outputFileName);
}
OutputFiles=GeneratedFileName.Select(name=>new TaskItem(name)).ToArray();
返回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>
美元(CoreCompiledPendson);发电机输出
我还将必要的项目组添加到项目文件中,如下所示:
<ItemGroup>
<AvailableItemName Include="dbml" />
</ItemGroup>
<ItemGroup>
<Compile Include="@(dbml)" />
</ItemGroup>
最后,我使用以下内容将文件添加到项目中:
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;
}
}
<dbml Include="DAL\SettingsDC.dbml">
<SubType>Designer</SubType>
<Generator>ULinqToSQLGenerator</Generator>
<LastGenOutput>SettingsDC.designer.cs</LastGenOutput>
</dbml>
设计师
ULinqToSQLGenerator
设置sdc.designer.cs
这将导致一条错误消息,提示
“GeneratedDesignerDC”任务具有
无效的输出规范。这个
“TaskParameter”属性是必需的,
“项目名称”或
“PropertyName”属性必须为
指定(但不是两者都指定)
我需要做什么才能使这项工作正常?您尚未在任务中声明输出属性。您必须在
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;
}
}
public类GenerateDesignerDC:Task
{
[必需]
公共ITaskItem[]输入文件{get;set;}
[输出]
公共ITaskItem[]输出文件{get;set;}
公共重写bool Execute()
{
var generatedFileNames=新列表();
foreach(InputFiles中的var任务)
{
字符串inputFileName=task.ItemSpec;
字符串outputFileName=Path.ChangeExtension(inputFileName,“.Designer.cs”);
字符串结果;
//生成代码字符串
var生成器=新的ULinqCodeGenerator(“CSharp”);
字符串文件内容;
使用(FileStream fs=File.OpenRead(inputFileName))
使用(StreamReader rd=新StreamReader(fs))
{
fileContent=rd.ReadToEnd();
}
使用(var destination=newfilestream(outputFileName,FileMode.Create))
{
byte[]bytes=Encoding.UTF8.GetBytes(generator.BuildCode(inputFileName,fileContent));
destination.Write(字节、0、字节、长度);
}
generatedFileNames.Add(outputFileName);
}
OutputFiles=GeneratedFileName.Select(name=>new TaskItem(name)).ToArray();
返回true;
}
}
正确,我还需要添加