Skip to content

Commit

Permalink
Merge branch 'master' of github.com:VoidRayLLC/loom
Browse files Browse the repository at this point in the history
  • Loading branch information
tarwich committed Oct 29, 2013
2 parents 1f36bb3 + 51cede7 commit bb0b280
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions Loom/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Program
{
// Holds the options for this application
static Options options = null;
//Create global counter
public static int numScanned = 0;

/// <summary>
/// Main entry point for a C# application, and for ours
Expand Down Expand Up @@ -136,12 +138,12 @@ static int Main(string[] argv)

// Prepare the arguments
List<String> arguments = new List<string>();
// Add an empty target
arguments.Add("");
// Add the only argument (later we're going to support a list)
if (options["args"]) arguments.Add(options["args"]);
// Add the tail parameters if present
arguments.AddRange(options.Parameters);
// Add an empty target
arguments.Add("");
// Will we be doing a dry run?
Boolean dryRun = options["dry-run"];

Expand Down Expand Up @@ -179,9 +181,9 @@ static int Main(string[] argv)
task = (o) =>
{
// Set the target
arguments[arguments.Count - 1] = target;
arguments[0] = target;
// Run the command
String result = RunCommand(dryRun, options["script"], arguments.ToArray());
String result = RunCommand(dryRun, options["script"], target, arguments.ToArray());
// Aquire a lock for the file to prevent simultaneous edit
lock (file)
Expand Down Expand Up @@ -226,13 +228,18 @@ static int Main(string[] argv)
/// <param name="command">The command to run</param>
/// <param name="arguments">The arguments to pass to the command</param>
/// <returns>The captured STDOUT</returns>
protected static String RunCommand(Boolean dryRun, String command, params String[] arguments)
protected static String RunCommand(Boolean dryRun, String command, String target, params String[] arguments)
{
// Prepare the arguments. For now just join them, but we'll have to consider escaping in the future
String preparedArguments = String.Join(" ", arguments);
// Dry run, just echo the command
if(options["verbose"])
System.Console.WriteLine("{0} {1}", command, preparedArguments);
// Increment global counter to track progress
Interlocked.Increment(ref numScanned);

// show target being scanned. Verbose echos the full command
if (options["verbose"] || dryRun)
System.Console.WriteLine("{0}: Scanning {1} with => {2} {3}", numScanned, target, command, preparedArguments);
else
System.Console.WriteLine("{0}: Scanning {1}", numScanned, target);

// Don't actually spawn the process if this is a dry run
if (!dryRun)
Expand All @@ -253,7 +260,7 @@ protected static String RunCommand(Boolean dryRun, String command, params String
});

// Buffer to hold the text from STDOUT
String result = "";
String result = target + ",";
// Start capturing STDOUT
process.BeginOutputReadLine();
// Start capturing STDERR
Expand All @@ -270,14 +277,15 @@ protected static String RunCommand(Boolean dryRun, String command, params String
process.OutputDataReceived += (sender, eventData) =>
{
// Add this to the result
result += eventData.Data + "\n";
result += eventData.Data;
// Trim up the result to kill trailing whitespace and add CR+LF for windows
result = result.Trim() + "\r\n";
};

// Wait for the process to cleanly exit
process.WaitForExit();

// Trim up the result to kill trailing whitespace
return result.Trim();
return result;
}

return "";
Expand Down

0 comments on commit bb0b280

Please sign in to comment.