Skip to content
This repository has been archived by the owner on Aug 31, 2022. It is now read-only.

Commit

Permalink
Fixup all async code
Browse files Browse the repository at this point in the history
  • Loading branch information
ajorkowski committed May 20, 2016
1 parent 1809fd5 commit b799fd0
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 113 deletions.
17 changes: 7 additions & 10 deletions NodeAssets.Compilers.CoffeeSharp/CoffeeSharpCompiler.cs
Expand Up @@ -17,17 +17,14 @@ public CoffeeSharpCompiler()

public Task<string> Compile(string initial, FileInfo originalFile)
{
return Task.Factory.StartNew(() =>
try
{
try
{
return _compiler.Compile(initial);
}
catch (JavaScriptException e)
{
throw new COMException(e.Message);
}
});
return Task.FromResult(_compiler.Compile(initial));
}
catch (JavaScriptException e)
{
throw new COMException(e.Message);
}
}
}
}
2 changes: 1 addition & 1 deletion NodeAssets.Compilers.Minify/CssMinifyCompiler.cs
Expand Up @@ -15,7 +15,7 @@ public CssMinifyCompiler()

public Task<string> Compile(string initial, FileInfo originalFile)
{
return Task.Factory.StartNew(() => _compiler.Compress(initial));
return Task.FromResult(_compiler.Compress(initial));
}
}
}
17 changes: 7 additions & 10 deletions NodeAssets.Compilers.Minify/JsMinifyCompiler.cs
Expand Up @@ -17,17 +17,14 @@ public JsMinifyCompiler()

public Task<string> Compile(string initial, FileInfo originalFile)
{
return Task.Factory.StartNew(() =>
try
{
try
{
return _compiler.Compress(initial);
}
catch(Exception e)
{
throw new COMException(e.Message);
}
});
return Task.FromResult(_compiler.Compress(initial));
}
catch(Exception e)
{
throw new COMException(e.Message);
}
}
}
}
4 changes: 1 addition & 3 deletions NodeAssets.Compilers.Node/CoffeeCompiler.cs
Expand Up @@ -16,10 +16,8 @@ public CoffeeCompiler(INodeExecutor executor)
public Task<string> Compile(string initial, FileInfo originalFile)
{
// Do not use the original file

var command = _executor.ExecuteCoffeeCommand("-c -e \"" + initial.Replace("\"", "\\\"") + "\"");

return Task.Factory.StartNew(() => _executor.RunCommand(command));
return _executor.RunCommand(command);
}
}
}
2 changes: 1 addition & 1 deletion NodeAssets.Compilers.Node/Commands/INodeExecutor.cs
Expand Up @@ -20,7 +20,7 @@ public interface INodeExecutor
CommandResult ExecuteCoffeeFile(FileInfo file);

// For more advanced users
string RunCommand(CommandResult result);
Task<string> RunCommand(CommandResult result);
CommandResult ExecuteNodeCommand(string args);
CommandResult ExecuteCoffeeCommand(string args);
}
Expand Down
39 changes: 21 additions & 18 deletions NodeAssets.Compilers.Node/Commands/NodeExecutor.cs
Expand Up @@ -25,27 +25,27 @@ public NodeExecutor(string nodeWorkspace, string nodeExePath = null)

public Task<string> CoffeeScript(string coffee)
{
return Task.Factory.StartNew(() => RunCommand(ExecuteCoffeeScript(coffee)));
return RunCommand(ExecuteCoffeeScript(coffee));
}

public Task<string> JsScript(string javascript)
{
return Task.Factory.StartNew(() => RunCommand(ExecuteJsScript(javascript)));
return RunCommand(ExecuteJsScript(javascript));
}

public Task<string> File(FileInfo file)
{
return Task.Factory.StartNew(() => RunCommand(ExecuteFile(file)));
return RunCommand(ExecuteFile(file));
}

public Task<string> JsFile(FileInfo file)
{
return Task.Factory.StartNew(() => RunCommand(ExecuteJsFile(file)));
return RunCommand(ExecuteJsFile(file));
}

public Task<string> CoffeeFile(FileInfo file)
{
return Task.Factory.StartNew(() => RunCommand(ExecuteCoffeeFile(file)));
return RunCommand(ExecuteCoffeeFile(file));
}

public CommandResult ExecuteCoffeeScript(string coffee)
Expand Down Expand Up @@ -98,14 +98,15 @@ public CommandResult ExecuteCoffeeFile(FileInfo file)
return ExecuteCoffeeCommand(file.FullName);
}

public string RunCommand(CommandResult result)
public async Task<string> RunCommand(CommandResult result)
{
var stdOut = result.StdOut.ReadToEnd();
result.RunningTask.Wait();
var stdOut = await result.StdOut.ReadToEndAsync().ConfigureAwait(false);
var code = await result.RunningTask.ConfigureAwait(false);

if (result.RunningTask.Result != 0)
if (code != 0)
{
throw new COMException("The execution of the command failed: \r\n" + result.StdErr.ReadToEnd(), result.RunningTask.Result);
var error = await result.StdErr.ReadToEndAsync().ConfigureAwait(false);
throw new COMException("The execution of the command failed: \r\n" + error, code);
}

return stdOut;
Expand Down Expand Up @@ -144,19 +145,21 @@ public CommandResult ExecuteNodeCommand(string args)
// Now we create a process, assign its ProcessStartInfo and start it
var proc = new Process { StartInfo = procStartInfo };
proc.Start();

var task = Task.Factory.StartNew(() =>
{
proc.WaitForExit();
return proc.ExitCode;
});

return new CommandResult(task, proc.StandardOutput, proc.StandardError, proc.StandardInput);

return new CommandResult(WaitForExitAsync(proc), proc.StandardOutput, proc.StandardError, proc.StandardInput);
}

private string EscapeScript(string script)
{
return script.Replace("\\", "\\\\").Replace("\"", "\\\"").Replace("\\\\r", "\\r").Replace("\\\\n", "\\n");
}

public static Task<int> WaitForExitAsync(Process process)
{
var tcs = new TaskCompletionSource<int>();
process.EnableRaisingEvents = true;
process.Exited += (sender, args) => tcs.TrySetResult(process.ExitCode);
return tcs.Task;
}
}
}
2 changes: 1 addition & 1 deletion NodeAssets.Compilers.Node/CssoCompiler.cs
Expand Up @@ -26,7 +26,7 @@ public Task<string> Compile(string initial, FileInfo originalFile)
command.StdIn.Flush();
command.StdIn.Close();

return Task.Factory.StartNew(() => _executor.RunCommand(command));
return _executor.RunCommand(command);
}
}
}
2 changes: 1 addition & 1 deletion NodeAssets.Compilers.Node/LessCompiler.cs
Expand Up @@ -28,7 +28,7 @@ public Task<string> Compile(string initial, FileInfo originalFile)
command.StdIn.Flush();
command.StdIn.Close();

return Task.Factory.StartNew(() => _executor.RunCommand(command));
return _executor.RunCommand(command);
}
}
}
2 changes: 1 addition & 1 deletion NodeAssets.Compilers.Node/StylusCompiler.cs
Expand Up @@ -32,7 +32,7 @@ public Task<string> Compile(string initial, FileInfo originalFile)
command.StdIn.Flush();
command.StdIn.Close();

return Task.Factory.StartNew(() => _executor.RunCommand(command));
return _executor.RunCommand(command);
}
}
}
6 changes: 3 additions & 3 deletions NodeAssets.Compilers.Node/UglifyJSCompiler.cs
@@ -1,6 +1,6 @@
using System.IO;
using NodeAssets.Core.Commands;
using System.IO;
using System.Threading.Tasks;
using NodeAssets.Core.Commands;

namespace NodeAssets.Compilers
{
Expand All @@ -26,7 +26,7 @@ public Task<string> Compile(string initial, FileInfo originalFile)
command.StdIn.Flush();
command.StdIn.Close();

return Task.Factory.StartNew(() => _executor.RunCommand(command));
return _executor.RunCommand(command);
}
}
}
18 changes: 8 additions & 10 deletions NodeAssets.Compilers.Sass/SassCompiler.cs
Expand Up @@ -16,17 +16,15 @@ public SassCompiler()

public Task<string> Compile(string initial, FileInfo originalFile)
{
return Task.Factory.StartNew(() =>
try
{
try
{
return _compiler.Compile(initial, NSass.OutputStyle.Nested, true, new [] { originalFile.DirectoryName });
}
catch (Exception e)
{
throw new COMException(e.Message, e.InnerException);
}
});
var compiled = _compiler.Compile(initial, NSass.OutputStyle.Nested, true, new [] { originalFile.DirectoryName });
return Task.FromResult(compiled);
}
catch (Exception e)
{
throw new COMException(e.Message, e.InnerException);
}
}
}
}
2 changes: 1 addition & 1 deletion NodeAssets.Core/PassthroughCompiler.cs
Expand Up @@ -7,7 +7,7 @@ public sealed class PassthroughCompiler : ICompiler
{
public Task<string> Compile(string initial, FileInfo originalFile)
{
return Task.Factory.StartNew(() => initial);
return Task.FromResult(initial);
}
}
}
55 changes: 26 additions & 29 deletions NodeAssets.Core/SourceManager/DefaultSourceCompiler.cs
Expand Up @@ -17,7 +17,7 @@ public DefaultSourceCompiler(bool minimise, string compileExtension)
_compileExtension = compileExtension;
}

public Task<string> CompileFile(FileInfo file, ICompilerConfiguration compilerConfig)
public async Task<string> CompileFile(FileInfo file, ICompilerConfiguration compilerConfig)
{
if (file == null) { throw new ArgumentNullException("file"); }
if (compilerConfig == null) { throw new ArgumentNullException("compilerConfig"); }
Expand All @@ -39,41 +39,38 @@ public Task<string> CompileFile(FileInfo file, ICompilerConfiguration compilerCo
}

// First step is grab the file contents, then continue
return Task.Factory.StartNew(() =>
// Do the initial compile
var fileData = file.Exists ? AttemptRead(file.FullName) : string.Empty;
var result = string.Empty;
bool hasErrored = false;
if (!string.IsNullOrEmpty(fileData))
{
// Do the initial compile
var fileData = file.Exists ? AttemptRead(file.FullName) : string.Empty;
var result = string.Empty;
bool hasErrored = false;
if (!string.IsNullOrEmpty(fileData))
try
{
try
{
result = compiler.Compile(fileData, file).Result;
}
catch (Exception e)
{
result = "An error occurred during initial compilation: \r\n" + e.GetBaseException().Message;
hasErrored = true;
}
result = await compiler.Compile(fileData, file).ConfigureAwait(false);
}
// Do the minimisation if it has been selected
if (!hasErrored && _minimise && !string.IsNullOrEmpty(result))
catch (Exception e)
{
try
{
result = minCompiler.Compile(result, null).Result;
}
catch (Exception e)
{
result = "An error occurred during minification: \r\n" + e.GetBaseException().Message;
}
result = "An error occurred during initial compilation: \r\n" + e.GetBaseException().Message;
hasErrored = true;
}
}

// Do the minimisation if it has been selected
if (!hasErrored && _minimise && !string.IsNullOrEmpty(result))
{
try
{
result = await minCompiler.Compile(result, null).ConfigureAwait(false);
}
catch (Exception e)
{
result = "An error occurred during minification: \r\n" + e.GetBaseException().Message;
}

return result;
});
}

return result;
}

private string AttemptRead(string path)
Expand Down

0 comments on commit b799fd0

Please sign in to comment.