【问题标题】:Executing grunt commands via .NET application通过 .NET 应用程序执行 grunt 命令
【发布时间】:2014-09-26 17:59:18
【问题描述】:

我正在使用 grunt(javascript 任务运行器)进行 Web 开发构建过程。包括 jasmine 测试、plato 代码检查、文档 (YUIdoc) 等...

为了让事情更方便一些(也为了稍后将此构建过程包含到另一个构建过程中),我尝试创建一个简单的 .net (C#) 应用程序,该应用程序能够执行 grunt 命令,然后将结果输出到一个文本框。因为我总是可以通过 cmd-Window 轻松执行 grunt 命令,所以我尝试使用 System.Diagnostics.Processcmd.exe 来执行此操作。这是我尝试过的(用于生成代码文档):

private void documentationToolStripMenuItem_Click(object sender, EventArgs e)
{
    Process p = new Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
    p.StartInfo.FileName = "cmd.exe";
    p.StartInfo.Arguments = "/C grunt doc";
    p.StartInfo.WorkingDirectory = "c:\\myProjectFolder\\";
    p.Start();

    p.WaitForExit();
    rtbOut.Text = p.StandardOutput.ReadToEnd();
}

奇怪的第一件事:我得到了这个输出:

>Loading "requirejs.js" tasks...

是的,我确实在我的项目中使用了 require.js,但这与文档无关。这是我通常得到的输出,我期望:

>Running "doc" task
>
>Running "yuidoc:compile" (yuidoc) task
>Start YUIDoc compile...
>Scanning: ../someFolder/
>Output: ../release/Documentation/
>YUIDoc compile completed in 0.968 seconds

当然不会生成文档。但是在正确的文件夹中生成了一个 data.json 文件,但它是空的。所以至少看起来它正确启动但无法继续(?)我也尝试了其他任务,但它们都没有使用 C# 中的进程。

【问题讨论】:

  • 不确定行为,但为什么你通过 cmd 而不是直接运行 grunt?毕竟是一个命令行工具(前提是你安装了 grunt-cli 包)。
  • 我尝试使用“grunt”作为文件名,但找不到它(系统找不到指定的文件),我认为实际上没有“grunt.exe”左右。它以某种方式通过 node.exe 运行。老实说,我不知道这在内部是如何运作的......

标签: c# .net gruntjs


【解决方案1】:

如果有人遇到同样的问题,我会回答我自己的问题,因为我现在找到了适合我的解决方案。

我现在只是在使用 PowerShell 脚本“绕道”。这里有一篇关于如何从 C# 运行 PowerShell 的好文章:

http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C

包含在 System.Management.Automation 中的 Runspace 类可用于调用 PowerShell 脚本。现在就我而言,这样的“脚本”不止于此:

grunt build

你会得到一些用于着色输出的转义码,你可以通过调用它来删除它:

grunt --no-color build

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多