Exec() public static method

public static Exec ( string filename, string args, TextReader stdIn = null, TextWriter stdOut = null, TextWriter stdErr = null, Encoding encoding = null, string workingDirectory = null ) : int
filename string
args string
stdIn TextReader
stdOut System.IO.TextWriter
stdErr System.IO.TextWriter
encoding System.Text.Encoding
workingDirectory string
return int
示例#1
0
        /// <summary>
        /// Returns an array of file names representing all of the dependencies of the specified *.less file (not including itself).
        /// </summary>
        public static string[] GetDependencies(string lessFilePath)
        {
            lessFilePath = Path.GetFullPath(lessFilePath);

            using (var output = new StringWriter())
                using (var errors = new StringWriter())
                {
                    // We would use 'lessc --depends' but it returns a space-delimited list of filenames
                    // which will be problematic to parse if there are spaces in your filenames...
                    var    dependsJS = @"
var sys = require('util'),
    fs = require('fs'),
    path = require('path'),
    lessfile = process.argv[1],
    data = fs.readFileSync(lessfile).toString(),
    less = require('./node_modules/less/lib/less');

process.chdir(path.dirname(lessfile));

var parser = new less.Parser();
parser.parse(data, function (err, tree) {
    if (err) {
        less.writeError(err);
        return 1;
    }
    else {
        for (var file in parser.imports.files) {
            sys.puts(path.resolve(file));
        }
        return 0;
    }
});";
                    string args      = "--eval \"" + dependsJS + "\" \"" + lessFilePath + "\"";
                    int    exitCode  = ProcessUtil.Exec(NodeExe,
                                                        args: args,
                                                        stdIn: null,
                                                        stdOut: output,
                                                        stdErr: errors,
                                                        workingDirectory: TempDirectory
                                                        );
                    if (exitCode != 0)
                    {
                        string errorText = errors.ToString().Trim();
                        if (string.IsNullOrEmpty(errorText))
                        {
                            errorText = output.ToString().Trim();
                        }
                        throw new ApplicationException(string.Format("Error {0} in '{1}': \r\n{2}",
                                                                     exitCode,
                                                                     lessFilePath,
                                                                     errorText));
                    }
                    return(output.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
                }
        }
示例#2
0
        static ScriptHandlerBase()
        {
            extractNodeJs();
            NodeExe = Path.Combine(TempDirectory, "node.exe");

            // Use installed version of node if found on PATH
            if (ProcessUtil.Exec("cmd.exe", "/c where node.exe") == 0)
            {
                NodeExe = "node.exe";
            }
        }
示例#3
0
        int executeJScript(string scriptPath, string args, TextReader stdin, TextWriter stdout, TextWriter stderr, Encoding encoding)
        {
            args = "//E:JScript //nologo \"" + scriptPath + "\" " + args;

            if (encoding == Encoding.Unicode)
            {
                args = "//U " + args;
            }

            int exitCode = ProcessUtil.Exec("cscript.exe", args, stdin, stdout, stderr, encoding);

            return(exitCode);
        }
示例#4
0
 void renderScript(string scriptFileName, TextWriter output)
 {
     using (var scriptFile = new StreamReader(scriptFileName))
         using (var stdErr = new StringWriter())
         {
             Debug.WriteLine("Compiling " + scriptFileName);
             int exitCode = ProcessUtil.Exec(NodeExe,
                                             "\"" + _coffee + "\" --compile --stdio",
                                             scriptFile, output, stdErr);
             if (exitCode != 0)
             {
                 output.WriteLine("throw \"Error in " + Path.GetFileName(scriptFileName).JsEncode() + ": "
                                  + stdErr.ToString().Trim().JsEncode() + "\";");
             }
         }
 }
示例#5
0
 void renderScript(string scriptFileName, TextWriter output)
 {
     /*
      * Usage: coffee [options] path/to/script.coffee -- [args]
      * If called without options, `coffee` will run your script.
      *
      * -b, --bare         compile without a top-level function wrapper
      * -c, --compile      compile to JavaScript and save as .js files
      * -e, --eval         pass a string from the command line as input
      * -h, --help         display this help message
      * -i, --interactive  run an interactive CoffeeScript REPL
      * -j, --join         concatenate the source CoffeeScript before compiling
      * -m, --map          generate source map and save as .map files
      * -n, --nodes        print out the parse tree that the parser produces
      *    --nodejs       pass options directly to the "node" binary
      * -o, --output       set the output directory for compiled JavaScript
      * -p, --print        print out the compiled JavaScript
      * -s, --stdio        listen for and compile scripts over stdio
      * -l, --literate     treat stdio as literate style coffee-script
      * -t, --tokens       print out the tokens that the lexer/rewriter produce
      * -v, --version      display the version number
      * -w, --watch        watch scripts for changes and rerun commands
      */
     using (var scriptFile = new StreamReader(scriptFileName))
         using (var stdErr = new StringWriter())
         {
             Debug.WriteLine("Compiling " + scriptFileName);
             int exitCode = ProcessUtil.Exec(NodeExe,
                                             "\"" + _coffee + "\" --compile --stdio",
                                             scriptFile, output, stdErr);
             if (exitCode != 0)
             {
                 output.WriteLine("throw \"Error in " + Path.GetFileName(scriptFileName).JsEncode() + ": "
                                  + stdErr.ToString().Trim().JsEncode() + "\";");
             }
         }
 }
示例#6
0
        public static void RenderCss(string lessFilePath, TextWriter output, bool compress = true, string lessPrologue = null, string lessPostscript = null, string lineNumbers = null)
        {
            TextReader lessStream;

            string lessSrc = File.ReadAllText(lessFilePath);

            lessStream = new StringReader(
                lessPrologue
                + lessSrc
                + lessPostscript
                );

            using (lessStream)
                using (var errors = new StringWriter())
                {
                    string dirname = Path.GetDirectoryName(lessFilePath);
                    if (dirname.Contains(' '))
                    {
                        throw new ApplicationException("lessc doesn't support file paths with spaces '" + dirname + "'");
                    }

                    /*
                     *  usage: lessc [option option=parameter ...] <source> [destination]
                     *
                     *  If source is set to `-' (dash or hyphen-minus), input is read from stdin.
                     *
                     *  options:
                     *      -h, --help              Print help (this message) and exit.
                     *      --include-path          Set include paths. Separated by `:'. Use `;' on Windows.
                     *      --no-color              Disable colorized output.
                     *      -s, --silent            Suppress output of error messages.
                     *      --strict-imports        Force evaluation of imports.
                     *      --verbose               Be verbose.
                     *      -v, --version           Print version number and exit.
                     *      -x, --compress          Compress output by removing some whitespaces.
                     *      --yui-compress          Compress output using ycssmin
                     *      -O0, -O1, -O2           Set the parser's optimization level. The lower
                     *                              the number, the less nodes it will create in the
                     *                              tree. This could matter for debugging, or if you
                     *                              want to access the individual nodes in the tree.
                     *      --line-numbers=TYPE     Outputs filename and line numbers.
                     *                              TYPE can be either 'comments', which will output
                     *                              the debug info within comments, 'mediaquery'
                     *                              that will output the information within a fake
                     *                              media query which is compatible with the SASS
                     *                              format, and 'all' which will do both.
                     *      -rp, --rootpath         Set rootpath for url rewriting in relative imports and urls.
                     *                              Works with or withour the relative-urls option.
                     *      -ru, --relative-urls    re-write relative urls to the base less file.
                     */

                    string args = "\"" + _lessc + "\""
                                  + " -" // read from stdin
                                  + (compress ? " --yui-compress" : "")
                                  + " --include-path=" + dirname
                                  + " --no-color"
                                  + (lineNumbers != null ? " --line-numbers=" + lineNumbers : "");
                    int exitCode = ProcessUtil.Exec(NodeExe,
                                                    args: args,
                                                    stdIn: lessStream,
                                                    stdOut: output,
                                                    stdErr: errors);
                    if (exitCode != 0)
                    {
                        throw new ApplicationException(string.Format("Error {0} in '{1}': \r\n{2}",
                                                                     exitCode,
                                                                     lessFilePath,
                                                                     errors.ToString().Trim()));
                    }
                }
        }
示例#7
0
        public static void RenderCss(string lessFilePath, TextWriter output, bool compress = true, string lessPrologue = null, string lessPostscript = null, string lineNumbers = null)
        {
            TextReader lessStream;

            string lessSrc = File.ReadAllText(lessFilePath);

            lessStream = new StringReader(
                lessPrologue
                + lessSrc
                + lessPostscript
                );

            using (lessStream)
                using (var errors = new StringWriter())
                {
                    /*
                     *  usage: lessc [option option=parameter ...] <source> [destination]
                     *
                     *  If source is set to `-' (dash or hyphen-minus), input is read from stdin.
                     *
                     *      options:
                     *      -h, --help              Print help (this message) and exit.
                     *      --include-path=PATHS    Set include paths. Separated by `:'. Use `;' on Windows.
                     *      -M, --depends           Output a makefile import dependency list to stdout
                     *      --no-color              Disable colorized output.
                     *      --no-ie-compat          Disable IE compatibility checks.
                     *      -l, --lint              Syntax check only (lint).
                     *      -s, --silent            Suppress output of error messages.
                     *      --strict-imports        Force evaluation of imports.
                     *      --verbose               Be verbose.
                     *      -v, --version           Print version number and exit.
                     *      -x, --compress          Compress output by removing some whitespaces.
                     *      --yui-compress          Compress output using ycssmin
                     *      --max-line-len=LINELEN  Max line length used by ycssmin
                     *      -O0, -O1, -O2           Set the parser's optimization level. The lower
                     *                              the number, the less nodes it will create in the
                     *                              tree. This could matter for debugging, or if you
                     *                              want to access the individual nodes in the tree.
                     *      --line-numbers=TYPE     Outputs filename and line numbers.
                     *                              TYPE can be either 'comments', which will output
                     *                              the debug info within comments, 'mediaquery'
                     *                              that will output the information within a fake
                     *                              media query which is compatible with the SASS
                     *                              format, and 'all' which will do both.
                     *      -rp, --rootpath=URL     Set rootpath for url rewriting in relative imports and urls.
                     *                              Works with or without the relative-urls option.
                     *      -ru, --relative-urls    re-write relative urls to the base less file.
                     *      -sm=on|off              Turn on or off strict math, where in strict mode, math
                     *      --strict-math=on|off    requires brackets. This option may default to on and then
                     *                              be removed in the future.
                     *      -su=on|off              Allow mixed units, e.g. 1px+1em or 1px*1px which have units
                     *  --strict-units=on|off   that cannot be represented.
                     */

                    string args = "\"" + _lessc + "\""
                                  + " -" // read from stdin
                                  + (compress ? " --yui-compress" : "")
                                  + " --no-color"
                                  + (lineNumbers != null ? " --line-numbers=" + lineNumbers : "");
                    int exitCode = ProcessUtil.Exec(NodeExe,
                                                    args: args,
                                                    stdIn: lessStream,
                                                    stdOut: output,
                                                    stdErr: errors,
                                                    workingDirectory: Path.GetDirectoryName(lessFilePath));
                    if (exitCode != 0)
                    {
                        throw new ApplicationException(string.Format("Error {0} in '{1}': \r\n{2}",
                                                                     exitCode,
                                                                     lessFilePath,
                                                                     errors.ToString().Trim()));
                    }
                }
        }
示例#8
0
        public static void RenderCss(string lessFilePath, TextWriter output, bool compress = true, string lessPrologue = null, string lessPostscript = null, string lineNumbers = null)
        {
            using (var errors = new StringWriter())
            {
                #region lessc usage

                /*
                 *  usage: lessc [option option=parameter ...] <source> [destination]
                 *
                 *  If source is set to `-' (dash or hyphen-minus), input is read from stdin.
                 *
                 *  options:
                 *    -h, --help               Print help (this message) and exit.
                 *    --include-path=PATHS     Set include paths. Separated by `:'. Use `;' on Windows.
                 *    -M, --depends            Output a makefile import dependency list to stdout
                 *    --no-color               Disable colorized output.
                 *    --no-ie-compat           Disable IE compatibility checks.
                 *    --no-js                  Disable JavaScript in less files
                 *    -l, --lint               Syntax check only (lint).
                 *    -s, --silent             Suppress output of error messages.
                 *    --strict-imports         Force evaluation of imports.
                 *    --insecure               Allow imports from insecure https hosts.
                 *    -v, --version            Print version number and exit.
                 *    -x, --compress           Compress output by removing some whitespaces.
                 *    --clean-css              Compress output using clean-css
                 *    --source-map[=FILENAME]  Outputs a v3 sourcemap to the filename (or output filename.map)
                 *    --source-map-rootpath=X  adds this path onto the sourcemap filename and less file paths
                 *    --source-map-basepath=X  Sets sourcemap base path, defaults to current working directory.
                 *    --source-map-less-inline puts the less files into the map instead of referencing them
                 *    --source-map-map-inline  puts the map (and any less files) into the output css file
                 *    -rp, --rootpath=URL      Set rootpath for url rewriting in relative imports and urls.
                 *                             Works with or without the relative-urls option.
                 *    -ru, --relative-urls     re-write relative urls to the base less file.
                 *    -sm=on|off               Turn on or off strict math, where in strict mode, math
                 *    --strict-math=on|off     requires brackets. This option may default to on and then
                 *                             be removed in the future.
                 *    -su=on|off               Allow mixed units, e.g. 1px+1em or 1px*1px which have units
                 *    --strict-units=on|off    that cannot be represented.
                 *
                 *  -------------------------- Deprecated ----------------
                 *    -O0, -O1, -O2            Set the parser's optimization level. The lower
                 *                             the number, the less nodes it will create in the
                 *                             tree. This could matter for debugging, or if you
                 *                             want to access the individual nodes in the tree.
                 *    --line-numbers=TYPE      Outputs filename and line numbers.
                 *                             TYPE can be either 'comments', which will output
                 *                             the debug info within comments, 'mediaquery'
                 *                             that will output the information within a fake
                 *                             media query which is compatible with the SASS
                 *                             format, and 'all' which will do both.
                 *    --verbose                Be verbose.
                 *
                 */
                #endregion

                string args = "\"" + _lessc + "\"";

                TextReader stdin = null;
                if (!string.IsNullOrEmpty(lessPrologue) || !string.IsNullOrEmpty(lessPostscript))
                {
                    stdin = new StringReader(
                        lessPrologue
                        + File.ReadAllText(lessFilePath)
                        + lessPostscript
                        );
                    args = " -" + args; // read from stdin  -- NOTE: THIS DOESN'T SEEM TO WORK ON AZURE?!?
                }
                else
                {
                    args += " \"" + lessFilePath + "\"";
                }

                args += (compress ? " --clean-css" : "")
                        + " --no-color"
                        + (lineNumbers != null ? " --line-numbers=" + lineNumbers : "");

                int exitCode = ProcessUtil.Exec(NodeExe,
                                                args: args,
                                                stdIn: stdin,
                                                stdOut: output,
                                                stdErr: errors,
                                                workingDirectory: Path.GetDirectoryName(lessFilePath));
                if (exitCode != 0)
                {
                    throw new ApplicationException(string.Format("Error {0} in '{1}': \r\n{2}",
                                                                 exitCode,
                                                                 lessFilePath,
                                                                 errors.ToString().Trim()));
                }
            }
        }