如何从cake(build)启动html报告

时间:2017-01-18 14:50:10

标签: cakebuild

我在我的项目中使用cake来构建,运行单元测试,检查代码覆盖率,然后生成HTML报告(使用ReportGenerator)。这一切都正常,我可以在浏览器中打开生成的报告。

然而,当我以前使用dos批处理文件执行此操作时,它还会启动我的默认浏览器并在生成报告后加载报告,但我找不到使用cake执行此操作的方法。

以下是我一直在使用的批处理文件的内容:

@ECHO OFF

SET SearchDirectory=%~dp0Grapevine.Tests\bin\Debug
SET DllContainingTests=%~dp0Grapevine.Tests\bin\Debug\Grapevine.Tests.dll

for /R "%~dp0packages" %%a in (*) do if /I "%%~nxa"=="xunit.console.exe" SET TestRunnerExe=%%~dpnxa
for /R "%~dp0packages" %%a in (*) do if /I "%%~nxa"=="OpenCover.Console.exe" SET OpenCoverExe=%%~dpnxa
for /R "%~dp0packages" %%a in (*) do if /I "%%~nxa"=="ReportGenerator.exe" SET ReportGeneratorExe=%%~dpnxa

if not exist "%~dp0GeneratedReports" mkdir "%~dp0GeneratedReports"
call :RunOpenCoverUnitTestMetrics

if %errorlevel% equ 0 (
 call :RunReportGeneratorOutput
)

if %errorlevel% equ 0 (
 call :RunLaunchReport
)
exit /b %errorlevel%

:RunOpenCoverUnitTestMetrics
"%OpenCoverExe%" ^
 -target:"%TestRunnerExe%" ^
 -targetargs:"\"%DllContainingTests%\"" ^
 -filter:"+[*]* -[*.Tests*]* -[*]*.*Config -[xunit*]* -[*]Grapevine.Interfaces.*" ^
 -mergebyhash ^
 -skipautoprops ^
 -register:user ^
 -output:"%~dp0GeneratedReports\CoverageReport.xml"^
 -searchdirs:"%SearchDirectory%"
exit /b %errorlevel%

:RunReportGeneratorOutput
"%ReportGeneratorExe%" ^
 -reports:"%~dp0\GeneratedReports\CoverageReport.xml" ^
 -targetdir:"%~dp0\GeneratedReports\ReportGeneratorOutput"
exit /b %errorlevel%

:RunLaunchReport
start "report" "%~dp0\GeneratedReports\ReportGeneratorOutput\index.htm"
exit /b %errorlevel%

我尝试过使用以下内容:

StartProcess(new FilePath("./GeneratedReports/ReportGeneratorOutput/index.htm"));

我收到以下错误:

An error occured when executing task 'generate-report'.
Error: The specified executable is not a valid application for this OS platform.

我已经验证路径正确且文件存在,并且在命令行上复制/粘贴文件路径确实会在我的默认浏览器中打开文件。

3 个答案:

答案 0 :(得分:0)

您可以使用StartProcess别名示例执行此操作:

FilePath reportpath = File("./GeneratedReports/ReportGeneratorOutput/index.htm");
StartProcess(reportpath);

答案 1 :(得分:0)

最终对我有用的是:

if (IsRunningOnWindows())
{
    StartProcess("explorer.exe", reportPath);
}

显然,这不适用于非Windows环境,但这超出了我的需求范围。我尝试的其他所有内容都会产生错误,无论是找不到文件还是可执行文件对操作系统无效。

答案 2 :(得分:0)

仅靠Cake我就找不到解决办法,所以我求助于StartProcess来调用CMD:

if (IsRunningOnWindows()) {
    StartProcess("cmd", new ProcessSettings {
        Arguments = $"/C start \"\" {testCoverageReportPath}index.htm"
    });
}

这非常适合我的需求。

相关问题