Executing grunt commands via .NET application - c#

I am using grunt (javascript Task runner) for my web development build process. Included are things like tests with jasmine, code checking with plato, documentation (YUIdoc) and so on...
To make things a bit more convenient (and also to include this build process into another build process later) I tried to create a simple .net (C#) application that is able to execute the grunt commands and then outputs the results into a textbox. As I can always execute the grunt commands easily via the cmd-Window I tried utilizing System.Diagnostics.Process with cmd.exe to do that. Here is what I tried (for generating the code documentation):
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();
}
First thing which is weired: I am getting this output:
>Loading "requirejs.js" tasks...
Yes, I do use require.js inside my project but this has nothing to do with documentation. This is the output I usually get and I would expect:
>Running "doc" task
>
>Running "yuidoc:compile" (yuidoc) task
>Start YUIDoc compile...
>Scanning: ../someFolder/
>Output: ../release/Documentation/
>YUIDoc compile completed in 0.968 seconds
And the documentation is not generated of course. But there is a data.json File which is generated in the correct folder but it is empty. So at least it appears that it started correctly but could not continue(?) I also tried with other tasks but none of them work using a Process in C#.

In case anyone has the same problem I answer my own question as I found a solution now that works for me.
I am simply doing a 'detour' now using PowerShell scripts. There is a nice article about how you can run PowerShell from C# here:
http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C
Included in the System.Management.Automation there is the class Runspace which can be used to call PowerShell scripts. Now in my case such a 'script' is not more than this:
grunt build
You get back some escape codes for coloring the output which you can remove by calling it like:
grunt --no-color build

Related

Can we run node application in net core 3.1

I am having a .Net Core 3.1 console application (which is work as a azure consumer for a message queue)
When executing the Consumer, based on a flag value of message property, Its need to run separate application which is written in node.js.
I decided use separate run.sh (shell script) file to call node application and run.sh file will call through c#.
Here is the code snippet I used.
var command = "bash";
var myBatchFile = #"C:\Temp\spiderease_v1\run.sh"; //Path to shell script file
var argss = #"C:\Temp\spiderease_v1\run.sh CA--STATCAN--REG--ADMIN-DATA {url} {OUTPUT_PATH} {SPIDER_EASE_HOME} {SPIDER_TEMPLATE_HOME} {TYPE}"; //this would become "/home/ubuntu/psa/PdfGeneration/ApacheFolder/ApacheFOP/transform.sh /home/ubuntu/psa/PdfGeneration/ApacheFolder/XMLFolder/test.xml /home/ubuntu/psa/PdfGeneration/ApacheFolder/XSLTFolder/Certificate.xsl /home/ubuntu/psa/PdfGeneration/ApacheFolder/PDFFolder/test.pdf"
var processInfo = new ProcessStartInfo();
processInfo.UseShellExecute = false;
processInfo.FileName = command; // 'sh' for bash
processInfo.Arguments = argss; // The Script name
processInfo.RedirectStandardOutput = true;
var process = Process.Start(processInfo); // Start that process.
var outPut = process.StandardOutput.ReadToEnd();
process.WaitForExit();
But This won't work as expected.
Update:
This code snipt didn't give any error, but I am unable to ensure that file getting executed or not. because when I run this bash file directly, it produced some files in given path. here it didn't give any output.
Can someone help me out whether do I using correct implementation for this task or do I have any alternatives?
If this is an good solution, what would be the wrong in code?
(I am willing push this into docker Linux environment as well)
Can someone help me out on this?

MVC executing a LibreOffice conversion

Hi I'm trying to convert either a doc or docx to a pdf in a c# MVC application. I know I can do this using libreOffice. So I created a simple batch file to take 2 variables and then run them into the libreoffice 'soffice' headless to convert to pdf.
So that gave me this code.
echo on
SET var1=%2
IF "%var1:~-1%"=="\" SET var1=%var1:~0,-1%
cd "C:\Program Files\LibreOffice 5\program\"
soffice --headless --convert-to pdf %1 --outdir %var1%
Originally I thought the problem was within my MVC application and the way I called this batch script. But I commented (REM) the soffice and outputted out the command in the bash using the standard output.
var psi = new ProcessStartInfo("cmd.exe", "/k " + command);
//psi.CreateNoWindow = true;
psi.FileName = command;
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.Arguments = string.Format("{0} {1}", fullPath2, tempPath);
var process = Process.Start(psi);
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
Trace.WriteLine(output);
Trace.WriteLine(error);
process.WaitForExit();
When I commented the soffice line - it hit the WaitForExit and worked no problems (ok with no pdf conversions, but the script exited).
If I don't do that it seems to execute the cmd and even the soffice commands because I can see them in the task manager - but obvisouly nothing happens.
Additionally the code above works when I did a c# command line program (I've hard coded the file/command lines in both instances). The executable also works when I run as the user that is running the app pool in my MVC application.
The bash file also works file 'standalone' no matter if me or my appPool user run it.
So what gives - why won't this run.
This is the code that comes out of that trace - so what the bash script does.
c:\windows\system32\inetsrv>echo on
c:\windows\system32\inetsrv>SET var1=C:\inetpub\xxxxxxxxx\Temp\
c:\windows\system32\inetsrv>IF "\" == "\" SET var1=C:\inetpub\xxxxxxxxx\Temp
c:\windows\system32\inetsrv>cd "C:\Program Files\LibreOffice 5\program\"
C:\Program Files\LibreOffice 5\program>soffice --headless --convert-to pdf C:\inetpub\xxxxxxxxx\Temp\636295920370843147.doc --outdir C:\inetpub\xxxxxxxxx\Temp
I've got a feeling that this has something to do with the amount of characters or something because the soffice does fireup (can see it in the task manager).
FYI there are no spaces or special characters anywhere.
Any ideas?
Update
This looks to be an issue with the wait command. So any help with that helpful, I'm starting to think perhaps this is an issue with c# and libreoffice 5 - I've seen examples that supposedly work with libreoffice 4.
I guess my challenge continues....

Shell Script File(.sh) does not run from c# core on linux

I am trying to run ".sh" file from c# core application.But it doesn't seem to be running properly.Here is my scenario.
I am working on .Net core project which is hosted on Linux environment.We are trying to create "PDF" in our project for which we have used "Apache FOP".
Here i have created one "shell script" file "transform.sh" which internally calls "fop" with required parameters.Since developement is being done on windows machine we tested the same usinf "batch" file i.e. "transform.bat",but since we cannot use the "batch" file on linux enviornment we have created shell script file "transform.sh"
Following is the code from"transform.sh"
./fop -xml $1 -xsl $2 -pdf $3
Following is C# code from which i am calling the "shell script file
var process = new Process
{
StartInfo = new ProcessStartInfo
{
UseShellExecute = false,
RedirectStandardOutput = true,
Arguments = string.Format("{0} {1} {2}", XML_filename, XSL_filename, output)
}
};
process.StartInfo.FileName = "Path to shell script file";
process.Start();
process.WaitForExit();
Above code doesnot give any error but it also does not create the pdf file.If i directly run the shell script file from "Terminal" it works fine and create pdf file.
./transform.sh "/home/ubuntu/psa//PdfGeneration/ApacheFolder/XMLFolder/test.xml" "/home/ubuntu/psa/PdfGeneration/ApacheFolder/XSLTFolder/Certificate.xsl" "/home/ubuntu/psa/PdfGeneration/ApacheFolder/PDFFolder/t444t.pdf"
Please let me know if there is something wrong i am doing?How can i make the sheel script run on linux through C# core application.
Thanks.
I was able to solve the issue,just thought that i should put my solution here so that it may help others in future...
As mentioned in Question i was not able to generate the PDF file through shell script on linux machine.After debugging as suggested by "#JNevill" I came to understand that the shell script file was not getting called from .net process itself.
So my first task was to make the shell script file called through .Net Process.
After lots of searching through Net and trying out different solutions i got solution at How to perform command in terminal using C#(Mono).
So changed my code of calling the process as follow,
var command = "sh";
var myBatchFile = //Path to shell script file
var argss = $"{myBatchFile} {xmlPath} {xsltPath} {pdfPath}"; //this would become "/home/ubuntu/psa/PdfGeneration/ApacheFolder/ApacheFOP/transform.sh /home/ubuntu/psa/PdfGeneration/ApacheFolder/XMLFolder/test.xml /home/ubuntu/psa/PdfGeneration/ApacheFolder/XSLTFolder/Certificate.xsl /home/ubuntu/psa/PdfGeneration/ApacheFolder/PDFFolder/test.pdf"
var processInfo = new ProcessStartInfo();
processInfo.UseShellExecute = false;
processInfo.FileName = command; // 'sh' for bash
processInfo.Arguments = argss; // The Script name
process = Process.Start(processInfo); // Start that process.
var outPut = process.StandardOutput.ReadToEnd();
process.WaitForExit();
After changing the code ,the ".sh" file got executed and i was able to generate the PDF file.
Also script of the ".sh" file i.e. (transform.sh) which was calling Apache FOP file i.e. "FOP.sh" also needed to be changed.
Initially code was
./fop -xml $1 -xsl $2 -pdf $3
Which i changed as follow,(Change was to give full path of the FOP file)
/home/ubuntu/psa/PdfGeneration/ApacheFolder/ApacheFOP/fop -xml $1 -xsl $2 -pdf $3
Late answer, but for me, it worked just by setting the RedirectStandardOutput to true and changing the FileName property like this:
processInfo.FileName = #"C:\Program Files\Git\git-bash.exe";
processInfo.RedirectStandardOutput = true;

Process.Start does not output the full output

I have a process P, which calls the PSEXEC executable, and passes it an argument. My issue is that the output from my C# program is not the same from when I call exactly the same command directly into a command prompt.
Here is the output I expect (when ran from CMD):
Server Name Server Load
-------------------- ------------
601CTXD04 0
601CTXD05 0
...
601CTXP03 0
And here is the actual output (when ran from my program):
Server Name Server Load
-------------------- ------------
601CTXD04 0
So when I run the command from my C# program, I only get 1 server. I call the process the following way :
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = cServer.psexec; //Path to the PSEXEC executable
p.StartInfo.Arguments = "-accepteula \\\\601ctxp01 qfarm /load /continue";
p.Start();
string output = p.StandardOutput.ReadToEnd();
string[] lines = Regex.Split(output, "\\r\\n");
I really do not understand why, the exact same command ran from CMD does not give the same output when ran from a C# process. Please let me know what I am doing wrong as I am currently oblivious to my mistake.
You need to call p.WaitForExit() after p.StandardOutput.ReadToEnd(), otherwise you won't get all of the output from the process. Don't put it directly after p.Start() - you may get a deadlock. See http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput%28v=vs.110%29.aspx
It seems the issue was with PsExec itself (a Sysinternal utility) as I have tried an alternative called PAExec and the same command works fine, so I am using this one in my code instead.
Be sure to have the.
p.WaitForExit()
method after the line where you read the output. Here is an explanation from MSDN: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput(v=vs.110).aspx

Perl Script Output Capture Problem using C#

I was following one of the thread to run perl scripts from my c# program.
My c# code is like this:
private void RunScript(ArrayList selectedScriptFileList)
{
foreach (var curScriptFileName in selectedScriptFileList)
{
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("perl.exe");
myProcessStartInfo.Arguments = (string)(curScriptFileName);
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcessStartInfo.CreateNoWindow = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
myProcess.WaitForExit();
string output = myProcess.StandardOutput.ReadToEnd();
this.ScriptTestResultTextBox.AppendText(output);
}
}
And my perl script requires XML parsing. I can read the print statement before the XML parsing, but not after the parsing starts. The script runs find on DoS shell.
Here is part of my script:
print("\n");
print("****************** test1.pl ***********************\n");
print("\n");
print("1");
print("2");
my $scriptName = 'test1.pl';
my $file = '../../ScriptParamLib.xml';
my $parser = XML::LibXML->new();
my $tree = $parser->parse_file($file);
my $root = $tree->getDocumentElement;
my #species = $root->getElementsByTagName('test_node');
print("Accessing XML Data Base...\n");
The c# testbox only shows the first three print statement but not the last one.
Does anybody knows why?
Thanks
You could add more debugging print statements (e.g. one between every other line of your code) to see how far the execution gets. However, I'm going to go on a hunch and suggest that adding these three lines to your script will either solve the problem outright or lead you closer to a solution:
use strict;
use warnings;
use XML::LibXML;
Please update your question indicating how far execution gets and what errors you see!
I figured I should roll my comments into an answer since they proved to be helpful:
Since using an absolute path for $file in the Perl script works, the issue most likely has something to do with the working directory of the process that gets spawned from the C# program. You can use the Cwd module in the Perl script to see what the working directory actually is. If it's not what you expect, try setting it via the WorkingDirectory property of ProcessStartInfo in your C# program. Relative paths should work just fine after that.

Categories