- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图让我们的 T4 模板在构建时运行,而不添加对 Visual Studio 建模 SDK 的依赖项。我已经成功使用了 here 显示的批处理文件的变体,但我现在遇到的问题是我的 .tt 文件使用了 $(SolutionDir)
变量来引用其他项目(因此现在没有编译)。
处理这个问题的最佳方法是什么?其他人做了什么? (硬编码绝对路径不是一个选项)
编辑:
我看到有 -a
可以传递给 TextTransform.exe 的参数,是否可以使用它来定义 $(SolutionDir)
?
最佳答案
查看 TextTransformation.exe 的源代码(使用 ILSpy)我认为如果不修改模板这是不可能的(但我确实有一个解决方案)。
最终我们在这里关心的是模板解析过程中的步骤,其中 Microsoft.VisualStudio.TextTemplating.Engine.ResolveAssemblyReferences() 叫做。这代表 ITextTemplatingEngineHost.ResolveAssemblyReference() (虽然它确实首先扩展了环境变量)
当模板从命令行运行时,所使用的实现是由 提供的。命令行主机 , 它的实现只是查找引用路径和 GAC 中提供的文件。鉴于此时文件名仍然包含 $(SolutionPath) 位,它永远不会成功。
您可以实现自己的 TextTransform.exe 版本,但您必须从头开始(或使用反射),因为 CommandLineHost 是内部的 :-( 或者您可能会利用 Mono 端口 https://stackoverflow.com/a/1395377/26167
我不能说我对此感到高兴,因为我发现自己在同一条船上......
编辑:但是...由于最终您需要做的就是更改模板,因此我编写了一个 PowerShell 脚本以将模板复制到临时目录,在此过程中手动扩展 $(SolutionDir) 宏,然后从那里执行它们。这似乎工作得很好。
将其放入有问题的项目中(您可能想要更改文件扩展名),您应该很高兴:
<#
.Synopsis
Executes all the T4 templates within designated areas of the containing project
.Description
Unfortunately the Visual Studio 2010 'Transform All Templates' function doesn't appear
to work in SSDT projects, so have to resort to hackery like this to bulk-execute templates
#>
param(
)
$ErrorActionPreference = 'stop';
$scriptDir = Split-Path $MyInvocation.MyCommand.Path
$commonProgramFiles32 = $env:CommmonProgramFiles
if (Test-Path environment::"CommonProgramFiles(x86)") { $commonProgramFiles32 = (gi "Env:CommonProgramFiles(x86)").Value };
$t4 = Resolve-Path "$commonProgramFiles32\Microsoft Shared\TextTemplating\10.0\texttransform.exe";
$solutionDir = Resolve-Path "$scriptDir\..\"
$templates = @(dir "$scriptDir\Database Objects\load\*.tt")
# Cloning to temp dir originally caused issues, because I use the file name in the template (doh!)
# Now I copy to temp dir under the same name
pushd $scriptDir;
try{
foreach($template in $templates){
$templateTemp = Join-Path ([IO.Path]::GetTempPath()) $template.Name;
$targetfile = [IO.Path]::ChangeExtension($template.FullName, '.sql');
Write-Host "Running $($template.Name)"
Write-Host "...output to $targetFile";
# When run from outside VisualStudio you can't use $(SolutionDir)
# ...so have to modify the template to get this to work...
# ...do this by cloning to a temp file, and running this instead
Get-Content $template.FullName | % {
$_.Replace('$(SolutionDir)',"$solutionDir")
} | Out-File -FilePath:$templateTemp
try{
& $t4 $templateTemp -out $targetfile -I $template.DirectoryName;
}finally{
if(Test-Path $templateTemp){ Remove-Item $templateTemp; }
}
}
}finally{
popd;
}
关于.net - 通过 TextTransform.exe 运行模板时使用 $(SolutionDir),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13805563/
在属性表中:对于一个用户宏,名称为 OSGVE_DIR,值为:$(SolutionDir)\..\..\..\.. $(SolutionDir) 的路径是什么?\..\..\..\.. 斜线表示转到子
我有 2 个包含解决方案的文件夹: MyNETProject(网络项目) MyWiXInstaller (WiXProject) 我的安装程序需要从 MyNETProject\bin\Release
我的 .csproj 文件包含此声明: Debug ... ..\ 我的期望是当您直接从项目目录构建项目并且不指定 /p:SolutionDir= 参数时需要发现
我正在尝试构建一个包含两个项目的解决方案: COM 动态链接库 使用此 DLL 的应用程序 在 COM dll 中,我试图将 idl 生成的 tlb 嵌入到项目中。我有通常的配置 Debug-Rele
我试图让我们的 T4 模板在构建时运行,而不添加对 Visual Studio 建模 SDK 的依赖项。我已经成功使用了 here 显示的批处理文件的变体,但我现在遇到的问题是我的 .tt 文件使用了
我有一个项目,它有一个构建后事件,可以将 DLL 复制到某个目录: xcopy "$(TargetDir)$(TargetName).dll" "$(SolutionDir)..\UdpLocatio
当我使用Windows CMD运行SandcaSTLe帮助文件生成器项目文件(例如myproject.shfbproj)时,我遇到一个令人讨厌的问题:$(SolutionDir)与$(ProjectD
我正在尝试为 C# 项目编写构建后事件。我正在使用自定义控制台应用程序 (myTool.exe) 来执行此操作。 例如构建后事件是 "$(SolutionDir)tools\myTool.exe" "
我正在构建一个为 Excel 添加的 C#。为了对其进行调试,我需要使用包含插件的调试或发布路径的命令行参数来启动 Excel.exe。 例如: 启动外部程序:C:\Program Files\Mic
如何从 C++ 代码访问 $(SolutionDir) 宏。 只是我想将解决方案目录路径作为字符串获取。这样我就可以在任何计算机上设置我的项目并相应地获取 sln 目录路径。 最佳答案 前段时间我遇到
我在使用 MSDeploy 和 Visual Studio 的发布配置文件时遇到了一个烦人的问题。在我的项目中,我有一个属性组,我设置了一个项目供以后引用。 $(SolutionDir)WebSite
我一直在尝试在我的 Xamarin android 项目中使用 Realm,它的依赖之一是 Fody,每次我尝试运行我的应用程序时都会显示以下错误:错误 MSB4044:未为“Fody.Weaving
我在使用 MSDeploy 和 Visual Studio 的发布配置文件时遇到了一个烦人的问题。在我的项目中,我有一个属性组,我设置了一个项目供以后引用。 $(SolutionDir)WebSite
我有 10 个子项目,在这 10 个项目中,我正在用我的 FAKE 脚本构建 3 个项目,但我收到一个错误,即“导入的项目” "D:\MyProject\Project1\BuildTools\Sty
我有一个 .sln 解决方案文件,它引用了一个 .csproj 项目文件,该文件具有类似以下的构建后任务: xcopy $(SolutionDir)\dir1\Somefl
我有一个解决方案,其中包含一些在多个其他项目中使用的类库。当我从 VS 执行 Install-Package 时,它将采用 $(SolutionDir)\packages\ 并将其转换为相对路径,例如
现在我正在考虑在预构建事件中写入文件的路径,然后在代码中从该文件中读取,但我想知道是否有更简单、更优雅的方法。 编辑:这是对 Help with one step build all projects
我有一个最初在 Visual Studio 2010 中构建的 .net 4 MVC Web Api 应用程序。作为此解决方案的一部分,我有一套单元测试。 我用以下内容装饰了单元测试方法 [AspN
我相信该问题已记录在案 here moved here看起来这可能是 Visual Studio 中的一个错误,但我想知道是否有人知道解决方法。 基本上,在预构建事件中,我有以下两行(除其他外)一前一
是否有可能获得 $(ProjectDir) 的值?或 $(SolutionDir)在 Visual Studio 之外以编程方式?我的情况是我需要将 vcproj 文件作为文本文件读取并获取其属性,如
我是一名优秀的程序员,十分优秀!