I would like to allow my user to change some less variables so they can customize the styles.
When done I would like to generate the css file.
I m working in .net and have an azure worker role that can probably handle the task.
if it's not possible in .net may be with node.js?
Can someone suggest me a way to do it?
Thank you
I'm currently building my LESS files using lessc.wsf which you can download here: https://github.com/duncansmart/less.js-windows/tree/windows-script-host
Then in your Worker Role you can do something like this:
// File path.
var lessCompilerPath = "...\\lessc.wsf";
var lessPath = "site.less");
var cssPath = "site.css");
// Compile.
var process = new Process
{
StartInfo = new ProcessStartInfo("cscript")
{
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
Arguments = "//nologo \"" + lessCompilerPath + "\" \"" + lessPath + "\" \"" + cssPath + "\" -filenames",
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true
}
};
process.Start();
process.WaitForExit();
// Error.
if (process.ExitCode != 0)
{
throw new InvalidOperationException(process.StandardError.ReadToEnd());
}
You'll need to match the path to your files depending on where they are located in your solution.
Related
I am trying to run node and executing my javascript file using below code from c#. my c# application is running as windows service in windows server 2019 standard
string appjsloc = AppDomain.CurrentDomain.BaseDirectory + "\\appjs.js";
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
{
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
CreateNoWindow = true,
FileName = "cmd.exe",
Arguments = "/C node \"" + appjsloc + "\" \"" + inputfile + "\" \"" + outputfile + "\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
};
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
my input and output is changing and thus i am passing these two arguments dynamically whenever required . appjsloc is fixed.
using this code , node is started every time and then closed after executing my appjs file. this is time consuming and giving performance slowness, because in every execution , it start the node process first(which takes some time) and then execute my appjs file. Is there any way I can avoid starting node every time and reusing them for all my future execution of appjs.
I'm attempting to call Process.Start to invoke Babel and transpile a js file in my c# project.
I've installed babel into a directory "ES6" using the command:
npm install babel-preset-es2015 --save-dev
the directory C:\ES6\node_modules.bin now has a babel and babel.cmd file
Now I'm attempting to transpile a js file using Process.Start and redirecting the std out to capture the results I can use:
string babelFileName = #"C:\ES6\node_modules\.bin\babel";
var startInfo = new ProcessStartInfo {
FileName = babelFileName,
Arguments = " --presets es2015 " + ViewModel.EditorViewModel.JSFullFilePath,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
};
string js = ViewModel.EditorViewModel.Javascript;
using (var process = Process.Start(startInfo)) {
var standardOutput = new StringBuilder();
// read chunk-wise while process is running.
while (!process.HasExited) {
standardOutput.Append(process.StandardOutput.ReadToEnd());
}
int exitCode = process.ExitCode;
// make sure not to miss out on any remaindings.
standardOutput.Append(process.StandardOutput.ReadToEnd());
string stdout = standardOutput.ToString();
js = string.IsNullOrEmpty(stdout) ? js : stdout;
}
I've tried to invoke bable using "bable." to get around the missing ".exe" extension but no luck. I get the following exception:
The specified executable is not a valid application for this OS platform.
Hoping someone can point out how I can properly invoke babel to do this.
**UPDATE
I took a look at babel.cmd since this command line does transpile correctly:
C:\ES6\node_modules.bin>babel someES5.js --presets es2015
babel.cmd:
#IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\..\babel-cli\bin\babel.js" %*
) ELSE (
#SETLOCAL
#SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0\..\babel-cli\bin\babel.js" %*
)
And modified my C#:
string babeljs = #"C:\ES6\node_modules\babel-cli\bin\babel.js";
var startInfo = new ProcessStartInfo {
FileName = "node.exe",
Arguments = babeljs + " " + ViewModel.EditorViewModel.JSFullFilePath,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
This does essentially echo the un-transpiled js code verbatim. If I now add " --presets es2015 " to the arguments, the process completes successfully but with empty output.
What do I need to add here to get transpiled js from this node.exe process?
I wonder if someone can please help me understand why my Process is not working, nor generating an error.
The code below should loop through a directory, find all files with an sqb extension and for each file run a Process as a user account which has elevated privileges on the server.
The process should run an executable sqb2mtf.exe from the same folder as the files are located with an argument such as sqb2mtf.exe file.sqb file.bak for example purposes.
If I use Visual Studio 2013 and step through the code I can see each file being looped through and the Process appears to fire, but no files are converted, nor any errors presented to the variable readToEndError.
var directory = new DirectoryInfo(#"D:\inetpub\Import\");
foreach (var file in directory .EnumerateFiles("*.sqb"))
{
var convert = Path.GetFileNameWithoutExtension(file.ToString());
var process = new Process
{
StartInfo =
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
FileName = #"D:\inetpub\Import\sqb2mtf.exe",
UserName = "myUserName",
Domain = "myDomain",
Password = GetSecureString("myPassword"),
Arguments = #"D:\inetpub\Import\" + file + " " + #"D:\inetpub\Import\" + convert + ".bak"
}
};
process.Start();
string readToEndOutput = process.StandardOutput.ReadToEnd();
string readToEndError = process.StandardError.ReadToEnd();
process.WaitForExit();
}
I am going out of my mind, any advice to resolve this would be much appreciated :-)
Update
var directoryInfo = new DirectoryInfo(BackupDirectory);
foreach (var file in directoryInfo.EnumerateFiles("*.sqb"))
{
var convert = Path.GetFileNameWithoutExtension(file.ToString());
var fileName = BackupDirectory + "sqb2mtf.exe";
var arguments = "\"" + BackupDirectory + file + "\" \"" + BackupDirectory + convert + ".bak\"";
var process = new Process
{
StartInfo =
{
CreateNoWindow = true,
UseShellExecute = true,
RedirectStandardOutput = false,
RedirectStandardInput = false,
RedirectStandardError = false,
FileName = fileName,
Arguments = arguments
}
};
process.Start();
process.WaitForExit();
file.Delete();
}
One thing drawing on from the comments by InBetween is the need for quotes, in this case the quotes needed to surround the two separate files.
I can confirm this code does work on IISExpress, impersonating the different user, unfortunately just not IIS 7.5.
A workround was to move this code into a Console Application and install on the server in question, then use a Windows Schedule Task to run as a specific account.
With some legacy apps, I've discovered that I need to pass the arguments as quoted text, otherwise they simply wouldn't work.
Not sure if this is the issue but it's worth the try:
Arguments = "\"D:\\inetpub\\Import\\" + file + " D:\\inetpub\\Import\\" + convert + ".bak\"";
Still it seems strange that the process would simply die silently. I'd double check Domain, UserName and Password.
Tearing my hair out over something that should be ridiculously simple! I have cd'd to C:\Program Files\ImageMagick-6.9.0-Q16 on my shell and used the following the command string which works on my machine; it creates the expected output image, no problem:
convert "C:\Users\someguy\Debug\test_in.jpg" -resize 75x75 -colorspace
RGB "C:\Users\someguy\Debug\test_out.jpg"
I am trying to automate this with a simple test application in C#:
var proc = new Process
{
StartInfo = new ProcessStartInfo()
{
//WorkingDirectory = #"C:\Program Files\ImageMagick-6.9.0-Q16\",
Arguments = //_arguments,
"convert \"" + InputPath + "\" -resize 75x75 \"" + OutputPath + "\""
,
UseShellExecute = _useShellExecute,
RedirectStandardError = _redirectStandardError,
//RedirectStandardOutput = _redirectStandardOutput,
//CreateNoWindow = _createNoWindow,
//Verb = _verb,
FileName = #"C:\Program Files\ImageMagick-6.9.0-Q16\" + "convert.exe"
}
};
var test = proc.StartInfo.Arguments.ToString();
proc.Start();
string error = proc.StandardError.ReadToEnd();
proc.WaitForExit();
I have tried several permutations of this, using convert.exe, mogrify.exe, with verb as "runas", with the working directory set on, or off... (see commented out stuff - I've tried setting it) I have referred to how to use imageMagick with C# but I continue to get the same error:
mogrify.exe: unable to open image convert': No such file or directory
# error/blob.c/OpenBlob/2709. mogrify.exe: no decode delegate for this
image format' # error/constitute.c/ReadImage/501. mogrify.exe:
unable to open image `C:\Users\someguy\Debug\test_out.jpg': No such
file or directory # error/blob.c/OpenBlob/2709.
I feel like I'm just missing something really basic here, but I don't have a clue at this point. Could someone please offer a suggestion?
var proc = new Process
{
StartInfo = new ProcessStartInfo(_imageMagickFile)
{
//WorkingDirectory = #"C:\Program Files\ImageMagick-6.9.0-Q16\",
Arguments = //_arguments,
"" + InputPath + " -resize 75x75 " + OutputPath + ""
,
UseShellExecute = _useShellExecute,
RedirectStandardError = _redirectStandardError,
RedirectStandardOutput = _redirectStandardOutput,
CreateNoWindow = _createNoWindow,
Verb = _verb,
FileName = _imageMagickFile
}
};
var test = proc.StartInfo.Arguments.ToString();
proc.Start();
string error = proc.StandardError.ReadToEnd();
proc.WaitForExit();
I'm not actually sure why this works instead of the original question, but it does. Using convert.exe is indeed correct, not mogrify.exe. As an FYI, the other parameters are ShellExecute = false, Redirect std err/out are set to true, verb is runas.
Im trying to make C# application that uses hunpos tagger.
Runing hunpos-tag.exe requires three input arguments: model, inputFile, outputFile
In cmd it would look something like this:
hunpos-tag.exe model <inputFile >outputFile
If I just run it with model it writes something and waits for end command. When i tried using standard redirect I either get an exception (i solved this the code was off by a braceket i just get the or scenario now) or I get the results of running the tagger with just model argument. Here's the code:
string inputFilePath = path + "\\CopyFolder\\rr";
string pathToExe = path + "\\CopyFolder\\hunpos-tag.exe";
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = pathToExe,
UseShellExecute = false,
RedirectStandardInput = true,
WorkingDirectory = Directory.GetDirectoryRoot(pathToExe),
Arguments = path + "\\CopyFolder\\model.hunpos.mte5.defnpout",
};
try
{
Process _proc = new Process();
_proc.StartInfo.FileName = pathToExe;
_proc.StartInfo.UseShellExecute = false;
_proc.StartInfo.RedirectStandardInput = true;
_proc.StartInfo.Arguments = path + "\\CopyFolder\\model.hunpos.mte5.defnpout";
//Magic goes here
_proc.Start();
_proc.WaitForExit();
}
catch (Exception e)
{
Console.WriteLine(e);
}
Any ideas how can I redirect input before starting my process?
It is not only required to set RedirectStandardInput to true but also you need to use the input stream to write the text you want:
_proc.StandardInput.WriteLine("The text you want to write");
There's no need for that ProcessStartInfo if you're setting the info later on. Just get rid of that. And it seems you are already doing what you want. Just creating the process object doesn't start the process, Process.Start does. Just make a new StreamWriter and pass it Process.StandardInput (I think that's right, it may be something else)