From 1547b4c4ec88e2585384c9336714c69737558079 Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Tue, 12 Feb 2013 23:45:49 -0800 Subject: [PATCH] fixed scss option bug and Queue thread bug --- OrangeBits/Compilers/OrangeCompiler.cs | 3 +- OrangeBits/WebMatrixExtension.cs | 4 +- OrangeBits/Worker.cs | 308 +++++++++++++------------ 3 files changed, 159 insertions(+), 156 deletions(-) diff --git a/OrangeBits/Compilers/OrangeCompiler.cs b/OrangeBits/Compilers/OrangeCompiler.cs index 617db34..c094ed0 100644 --- a/OrangeBits/Compilers/OrangeCompiler.cs +++ b/OrangeBits/Compilers/OrangeCompiler.cs @@ -178,8 +178,7 @@ public static CompileResults Process(OrangeJob job) Success = true, InputPath = job.Path, OutputPath = outPath, - IsNewFile = !exists, - Message = "Compiled" + IsNewFile = !exists }; } return results; diff --git a/OrangeBits/WebMatrixExtension.cs b/OrangeBits/WebMatrixExtension.cs index aa73d92..84fa1f9 100644 --- a/OrangeBits/WebMatrixExtension.cs +++ b/OrangeBits/WebMatrixExtension.cs @@ -139,9 +139,7 @@ protected void SourceFileChanged(object source, FileSystemEventArgs e) case ".less": doIt = prefUtility.GetPref(e.FullPath, "AutoCompileLess", true) as bool?; break; - case ".scss": - doIt = prefUtility.GetPref(e.FullPath, "AutoCompileScss", true) as bool?; - break; + case ".scss": case ".sass": doIt = prefUtility.GetPref(e.FullPath, "AutoCompileSass", true) as bool?; break; diff --git a/OrangeBits/Worker.cs b/OrangeBits/Worker.cs index f5f107c..9decd6f 100644 --- a/OrangeBits/Worker.cs +++ b/OrangeBits/Worker.cs @@ -6,159 +6,165 @@ using OrangeBits.Compilers; using System.Linq; using System.Windows.Threading; - using Microsoft.WebMatrix.Extensibility; +using System.Collections.Concurrent; namespace OrangeBits { - /// - /// class that monitors the queue of file change events, and invokes the - /// compiler when neccessary - /// - public class Worker - { - //-------------------------------------------------------------------------- - // - // Properties - // - //-------------------------------------------------------------------------- - - #region Properties - - /// - /// queue that holds all of the files that need to be processed - /// - protected Queue queue = new Queue(); - - /// - /// timer for our background thread, this polls the queue - /// - protected Timer timer = new Timer(); - - /// - /// reference to the host that contains the worker - /// - protected IWebMatrixHost host; - - /// - /// - /// - protected Dispatcher mainDispatcher = Dispatcher.CurrentDispatcher; - - #endregion - - - //-------------------------------------------------------------------------- - // - // Constructors - // - //-------------------------------------------------------------------------- - - #region Constructor - /// - /// create a new timer, and check the queue periodically for new requests - /// - public Worker(IWebMatrixHost host) - { - this.host = host; - timer.Interval = 500; - timer.Elapsed += new ElapsedEventHandler(t_Elapsed); - timer.AutoReset = false; - timer.Start(); - } - #endregion - - - //-------------------------------------------------------------------------- - // - // Event Handlers - // - //-------------------------------------------------------------------------- - - #region t_Elapsed - /// - /// if there are any items in the queue which are at least 100 ms old, process them - /// - /// - /// - protected void t_Elapsed(object sender, ElapsedEventArgs e) - { - while ((queue.Count > 0) && (DateTime.Now > queue.Peek().Time.AddMilliseconds(100))) - { - var item = queue.Dequeue(); - ProcessItem(item); - } - timer.Start(); - } - #endregion - - - //-------------------------------------------------------------------------- - // - // Methods - // - //-------------------------------------------------------------------------- - - #region AddItem - /// - /// add a new file to process into the queue - /// - /// object that contains the path and times - public void AddItem(OrangeJob job) - { - // only add to the queue if the file isn't already in the queue - var exists = queue.Where(x => x.Path.ToLower() == job.Path.ToLower()).Count() > 0; - if (!exists) - this.queue.Enqueue(job); - } - #endregion - - #region ProcessItem - /// - /// process a file, generating the compiled output - /// - /// - protected void ProcessItem(OrangeJob job) - { - - var threadedOpenCmd = new Action(() => - { - mainDispatcher.Invoke(new Action(() => - { - var openCommand = host.HostCommands.OpenFileInEditor; - if (openCommand.CanExecute(job.OutputPath)) - openCommand.Execute(job.OutputPath); - })); - }); - - try - { - // do the actual compilation work - - - - CompileResults results = OrangeCompiler.Process(job); - - // show the notification bar to notify the user it happened - //host.ShowNotification(results.Message, "Open File", threadedOpenCmd); - - // refresh the tree so the new file (if created) shows up - if (results.IsNewFile) - { - mainDispatcher.Invoke(new Action(() => - { - var refreshCommand = host.HostCommands.GetCommand(CommonCommandIds.GroupId, (int)CommonCommandIds.Ids.Refresh); - if (refreshCommand.CanExecute(null)) - refreshCommand.Execute(null); - })); - } - } - catch (Exception ex) - { - Console.WriteLine(ex); - host.ShowNotification("There was an error processing " + job.Path, "Open File", threadedOpenCmd); - } - } - #endregion - } + /// + /// class that monitors the queue of file change events, and invokes the + /// compiler when neccessary + /// + public class Worker + { + //-------------------------------------------------------------------------- + // + // Properties + // + //-------------------------------------------------------------------------- + + #region Properties + + /// + /// queue that holds all of the files that need to be processed + /// + protected ConcurrentQueue queue = new ConcurrentQueue(); + + /// + /// timer for our background thread, this polls the queue + /// + protected Timer timer = new Timer(); + + /// + /// reference to the host that contains the worker + /// + protected IWebMatrixHost host; + + /// + /// + /// + protected Dispatcher mainDispatcher = Dispatcher.CurrentDispatcher; + + #endregion + + + //-------------------------------------------------------------------------- + // + // Constructors + // + //-------------------------------------------------------------------------- + + #region Constructor + /// + /// create a new timer, and check the queue periodically for new requests + /// + public Worker(IWebMatrixHost host) + { + this.host = host; + timer.Interval = 500; + timer.Elapsed += new ElapsedEventHandler(t_Elapsed); + timer.AutoReset = false; + timer.Start(); + } + #endregion + + + //-------------------------------------------------------------------------- + // + // Event Handlers + // + //-------------------------------------------------------------------------- + + #region t_Elapsed + /// + /// if there are any items in the queue which are at least 100 ms old, process them + /// + /// + /// + protected void t_Elapsed(object sender, ElapsedEventArgs e) + { + OrangeJob job; + while (queue.TryPeek(out job) && (DateTime.Now > job.Time.AddMilliseconds(100))) + { + if (queue.TryDequeue(out job)) + { + ProcessItem(job); + } + } + timer.Start(); + } + #endregion + + + //-------------------------------------------------------------------------- + // + // Methods + // + //-------------------------------------------------------------------------- + + #region AddItem + /// + /// add a new file to process into the queue + /// + /// object that contains the path and times + public void AddItem(OrangeJob job) + { + // only add to the queue if the file isn't already in the queue + lock (queue) + { + var exists = queue.Where(x => x.Path.ToLower() == job.Path.ToLower()).Count() > 0; + if (!exists) + this.queue.Enqueue(job); + } + } + #endregion + + #region ProcessItem + /// + /// process a file, generating the compiled output + /// + /// + protected void ProcessItem(OrangeJob job) + { + + var threadedOpenCmd = new Action(() => + { + mainDispatcher.Invoke(new Action(() => + { + var openCommand = host.HostCommands.OpenFileInEditor; + if (openCommand.CanExecute(job.OutputPath)) + openCommand.Execute(job.OutputPath); + })); + }); + + try + { + // do the actual compilation work + CompileResults results = OrangeCompiler.Process(job); + + // show the notification bar to notify the user it happened + if (!String.IsNullOrEmpty(results.Message)) + { + host.ShowNotification(results.Message, "Open File", threadedOpenCmd); + } + + // refresh the tree so the new file (if created) shows up + if (results.IsNewFile) + { + mainDispatcher.Invoke(new Action(() => + { + var refreshCommand = host.HostCommands.GetCommand(CommonCommandIds.GroupId, (int)CommonCommandIds.Ids.Refresh); + if (refreshCommand.CanExecute(null)) + refreshCommand.Execute(null); + })); + } + } + catch (Exception ex) + { + Console.WriteLine(ex); + host.ShowNotification("There was an error processing " + job.Path, "Open File", threadedOpenCmd); + } + } + #endregion + } } \ No newline at end of file