给定一个 .Net C# 项目,我想检查构建是否包含警告。运行命令时
dotnet build -consoleloggerparameters:"Summary;Verbosity=normal" -m -p:"WarnLevel=5;EnforceCodeStyleInBuild=true" -t:"clean,build"
Run Code Online (Sandbox Code Playgroud)
我得到了预期的输出
Build succeeded.
2 Warning(s)
0 Error(s)
Run Code Online (Sandbox Code Playgroud)
但退出代码为 0,那么我如何知道该构建是否包含警告?
我创建了一个 shell 脚本来玩一下
#!/bin/bash
dotnet build -consoleloggerparameters:"Summary;Verbosity=normal" -m -p:"WarnLevel=5;EnforceCodeStyleInBuild=true" -t:"clean,build"
if [ $? -eq 0 ]
then
echo ">>> Success"
else
echo ">>> Contains warnings or errors"
fi
read
Run Code Online (Sandbox Code Playgroud)
但如何扩展脚本来检查是否成功构建并带有警告?配置的 CI 管道应该会通过,但如果出现警告,我想将它们标记为不稳定。
如果可能的话,我想避免正则表达式魔法。
编辑:
我不想添加该标志-warnaserror
,因为那样的话 CI 管道将会失败,并且您将无法知道是否发生了构建错误或者代码风格是否不好。
What you can do is simply add /WarnAsError to your dotnet build.
Example code:
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
int x = 0;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Output of dotnet build:
dotnet build
Microsoft (R) Build Engine version 16.10.0+4242f381a for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
All projects are up-to-date for restore.
Program.cs(10,17): warning CS0219: The variable 'x' is assigned but its value is never used [ConsoleApp1.csproj]
ConsoleApp1 -> ConsoleApp1.dll
Build succeeded.
\Program.cs(10,17): warning CS0219: The variable 'x' is assigned but its value is never used [ConsoleApp1.csproj]
1 Warning(s)
0 Error(s)
Time Elapsed 00:00:00.79
Run Code Online (Sandbox Code Playgroud)
Output of dotnet build /WarnAsError:
dotnet build /WarnAsError
Microsoft (R) Build Engine version 16.10.0+4242f381a for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
Restored ConsoleApp1.csproj (in 60 ms).
Program.cs(10,17): error CS0219: The variable 'x' is assigned but its value is never used [ConsoleApp1.csproj]
ConsoleApp1 -> ConsoleApp1.dll
Build FAILED.
Program.cs(10,17): error CS0219: The variable 'x' is assigned but its value is never used [ConsoleApp1.csproj]
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:01.00
Run Code Online (Sandbox Code Playgroud)
EDIT: OP requested to not fail the build upon warning.
Something you can try, is to have the warnings be written to a file and afterwards count the number of lines in that file.
Exmample:
I took the code above and added several variables:
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
Run Code Online (Sandbox Code Playgroud)
Now when I compile, I get 5 warnings. Now I added this to my dotnet build:
$warningsFile = "SomeFile.log"
dotnet build -fl1 "/flp1:logFile=$warningsFile;warningsonly"
Run Code Online (Sandbox Code Playgroud)
Afterwards I can simply count the number of lines in that file. Using powershell:
gc $warningsFile | Measure-Object -Line
Run Code Online (Sandbox Code Playgroud)
Results in:
Lines Words Characters Property
----- ----- ---------- --------
5
Run Code Online (Sandbox Code Playgroud)
I'm not sure whether this will work in all cases, but at least it will tell you whether you had warnings or not. Hope this helps you
归档时间: |
|
查看次数: |
3722 次 |
最近记录: |