MSBUILD:ReadLinesFromFile 不读取重复行
我正在使用 MSBuild 读取我的数据库项目所依赖的所有 SQL 部署后文件,并将这些数据写入一个已加载的主脚本中。
我得到所有需要的文件:
<ReadLinesFromFile File="$(ScriptsList)" >
<Output TaskParameter="Lines" ItemName="IncludedFiles"/>
</ReadLinesFromFile>
然后我对它们进行批处理(逐行读取所有文件到 ListedData 中)
<ReadLinesFromFile File='$(ScriptDirectory)$([System.String]::Copy("%(IncludedFiles.Identity))' Condition="$([System.String]::Copy('%(IncludedFiles.Identity)').Substring(0,2))==':r'">
<Output TaskParameter="Lines" ItemName="ListedData"/>
</ReadLinesFromFile>
所有文件都没有问题地找到,然后我将其写入 output.sql。 但该文件缺少几行,这使得 sqlcmd 无法解析 output.sql。
源:
INSERT INTO [Characteristics] (
[CharacteristicID],
[CharName],
[RuleName],
[ActionRuleName],
[CriteriaSetID],
[ActionCriteriaSetID],
[ListCodeID],
[LocalID],
[BomCategory]
)
SELECT ...something,something... from Characteristics
INSERT INTO [CharacteristicDomain] (
[RuleSet],
[CharName],
[CharSlot],
[Description],
[Seq],
[ValueInteger],
[ValueFloat],
[ValueDate],
[ValueString]
)
SELECT ...something,something... from CharacteristicsDomain
如您所见,将有几行带有一个“)”括号符号,并且任务仅读取第一行,然后忽略所有重复项(因为它是一个项目组,而不是列表)。因此,实际上我得到的文件如下所示:
输出:
INSERT INTO [Characteristics] (
[CharacteristicID],
[CharName],
[RuleName],
[ActionRuleName],
[CriteriaSetID],
[ActionCriteriaSetID],
[ListCodeID],
[LocalID],
[BomCategory]
)
SELECT ...something,something... from Characteristics
INSERT INTO [CharacteristicDomain] (
[RuleSet],
[CharName],
[CharSlot],
[Description],
[Seq],
[ValueInteger],
[ValueFloat],
[ValueDate],
[ValueString]
SELECT ...something,something... from CharacteristicsDomain
有人知道如何使用 MSBuild 从文件中读取行,但不会丢失重复行吗?
我想也许有某种方法可以使用 Exec 任务?我当然不能编写自己的任务,而且我也不允许修改sql文件(我不能依赖用户,他们会按照我需要的方式格式化文件)。我需要使用 MSBuild 读取文件,因为我在将它们推送到 sqlcmd 之前修改了其中一些文件。
I'm using MSBuild to read all SQL post-deployment files on which my database project is dependent and I write this data to one main script which is loaded.
I get all needed files:
<ReadLinesFromFile File="$(ScriptsList)" >
<Output TaskParameter="Lines" ItemName="IncludedFiles"/>
</ReadLinesFromFile>
And then I batch them (reading all files, line by line, into ListedData)
<ReadLinesFromFile File='$(ScriptDirectory)$([System.String]::Copy("%(IncludedFiles.Identity))' Condition="$([System.String]::Copy('%(IncludedFiles.Identity)').Substring(0,2))==':r'">
<Output TaskParameter="Lines" ItemName="ListedData"/>
</ReadLinesFromFile>
All files are found without problem and then I write it to output.sql.
But the file is missing several lines, which makes output.sql impossible to parse by sqlcmd.
SOURCE:
INSERT INTO [Characteristics] (
[CharacteristicID],
[CharName],
[RuleName],
[ActionRuleName],
[CriteriaSetID],
[ActionCriteriaSetID],
[ListCodeID],
[LocalID],
[BomCategory]
)
SELECT ...something,something... from Characteristics
INSERT INTO [CharacteristicDomain] (
[RuleSet],
[CharName],
[CharSlot],
[Description],
[Seq],
[ValueInteger],
[ValueFloat],
[ValueDate],
[ValueString]
)
SELECT ...something,something... from CharacteristicsDomain
As you see, there will be several lines with a single ')' bracket sign and the task reads only the first line, and then ignores all the duplicates (because it's an item group, not a list). So in effect i get a file looking like this:
OUTPUT:
INSERT INTO [Characteristics] (
[CharacteristicID],
[CharName],
[RuleName],
[ActionRuleName],
[CriteriaSetID],
[ActionCriteriaSetID],
[ListCodeID],
[LocalID],
[BomCategory]
)
SELECT ...something,something... from Characteristics
INSERT INTO [CharacteristicDomain] (
[RuleSet],
[CharName],
[CharSlot],
[Description],
[Seq],
[ValueInteger],
[ValueFloat],
[ValueDate],
[ValueString]
SELECT ...something,something... from CharacteristicsDomain
Does someone know a way to read lines from files using MSBuild, but not losing duplicate lines?
I thought maybe there some way to use Exec task? I surely can't write own tasks, and I'm also not allowed to modify sql files (I can't rely on users, that they will format the files the way i need it). I need to read files with MSBuild, because I modify some of them before I push them to sqlcmd.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你如何写入output.sql?如果您在 %(ListedData.Identity) 上进行批处理,那么这只会为您提供唯一的行。将其用作 @(ListedData) 应该没问题。
How are you writing to output.sql? If you are batching on %(ListedData.Identity), then that will give you only unique lines. Use it as @(ListedData) and it should be fine.
您的第二个 ReadLinesFromFile(创建 @(ListedData) 的那个)有问题。它使用 %(InincludedFiles.Identity) 进行任务批处理,因此带有“)”的两行将被放入一个批处理中。
Your second ReadLinesFromFile, the one that creates @(ListedData) is at fault. It is using task batching with %(IncludedFiles.Identity), so both lines with the ")" will be placed into a single batch.