【问题标题】:Running Grunt Typescript from MSBuild从 MSBuild 运行 Grunt Typescript
【发布时间】:2014-01-29 23:35:57
【问题描述】:

我设置了一个 typescript 项目,该项目是使用 typescript 插件使用 GruntJS 构建的。我还有一个 Visual Studio 项目,我希望能够从中调用构建过程。

我的第一次尝试是在 Visual Studio 的 BeforeBuild 目标中添加一个 <Exec> 任务,<Exec> 任务配置如下:

<Exec Command="grunt --no-color typescript" />

这可以很好地运行构建,但是,当 Grunt 输出错误并且它们填充 VS 中的错误列表时,文件名被错误地列为 EXEC。

查看Exec Documentation 我看到CustomErrorRegularExpression 是命令的参数,但我不太明白如何使用它来解决我的问题。

我搞砸了一点,并设法将报告的文件名更改为我的 .jsproj 文件,这也是不正确的。看着this post,我尝试形成自己的正则表达式:

<Exec CustomErrorRegularExpression="\.ts\([0-9]+,[0-9]+\):(.*)" Command="grunt --no-color typescript" IgnoreExitCode="true" />

有没有人有使用这个参数的这个命令来实现这种事情的经验?我认为问题的一部分可能是 grunt 在两行中打印出错误?

【问题讨论】:

    标签: msbuild gruntjs msbuild-task


    【解决方案1】:

    关于只处理单行消息的 Exec 任务是正确的。此外,它还使用Regex.IsMatch 来评估错误/警告条件,而不使用模式的捕获组。

    我无法通过 MSBuild 找到解决此问题的方法,但可以轻松地直接在 grunt 任务中进行更改以纠正问题。

    我正在使用来自 https://www.npmjs.org/package/grunt-typescript 的 grunt-typescript 任务。

    要完成这项工作,需要进行 3 项微不足道的更改。

    1) 替换tasks/typescript.js顶部附近的输出实用方法:

    /* Remove the >> markers and extra spacing from the output */
    function writeError(str) {
      console.log(str.trim().red);
    }
    
    function writeInfo(str) {
      console.log(str.trim().cyan);
    }
    

    2)替换Compiler.prototype.addDiagnostic将文件和错误数据写在同一行:

    Compiler.prototype.addDiagnostic = function (diagnostic) {
      var diagnosticInfo = diagnostic.info();
      if (diagnosticInfo.category === 1)
        this.hasErrors = true;
    
      var message = "  ";
      if (diagnostic.fileName()) {
        message = diagnostic.fileName() + 
          "(" + (diagnostic.line() + 1) + "," + (diagnostic.character() + 1) + "): ";
      }
    
      this.ioHost.stderr.Write(message + diagnostic.message());
    };
    

    完成这些更改后,您不再需要在 Exec 任务上设置 CustomErrorRegularExpression,并且您的构建输出应显示错误文本,包括带有行和列信息的正确源文件。

    【讨论】:

    • 不错!我没想过修改 grunt 任务本身。
    猜你喜欢
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 2019-01-23
    • 1970-01-01
    • 2016-02-29
    相关资源
    最近更新 更多