Example #1
0
        static int Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += (s, e) => DumpError(e.ExceptionObject as Exception, abort: true);

            try
            {
                var filePattern = "";
                var workingDirectory = Environment.CurrentDirectory;
                var recurse = false;
                var showHelp = false;
                var overwrite = false;
                var warnAsErrors = false;
                var maxDegreeParallelism = Math.Max(1, Environment.ProcessorCount - 1);
                var minify = false;
                var verbose = false;
                string spriteProg = null;
                string spriteArguments = null;
                var version = false;
                var autoCacheBreak = false;
                var autoPrefix = false;
                var watch = false;

                var options = new OptionSet()
                {
                    { "r", "Recursively search all folders for matching files to compile", r => recurse = r != null },
                    { "f|force", "Force overwriting of existing css files.", f => overwrite = f != null },
                    { "wd:", "Sets the working directory for compilation. ~ in paths will be resolved with the working directory where needed.", wd => workingDirectory = wd },
                    { "<>", "File pattern to compile.", d => filePattern = d },
                    { "wae|warnaserror", "Treat warnings as errors", w => warnAsErrors = w != null },
                    { "?|help", "show this message and exit", h => showHelp = h != null },
                    { "mt:|maxthreads:", "maximum number of threads to use during compilation", t => maxDegreeParallelism = Int32.Parse(t) },
                    { "m|minify", "minify output", m => minify = m != null },
                    { "v|verbose", "print info output", v => verbose = v != null },
                    { "sp:|spriteprocessor:", "program to run on generated sprites, the sprite will be passed after spritearguments argument", sc => spriteProg = sc },
                    { "sa:|spritearguments:", "arguments to pass to spriteprocessor before the sprite file", sa => spriteArguments = sa },
                    { "version", "print version string and exit", v => version = v != null },
                    { "cb|cachebreak", "Include automatically generated cache breakers on referenced resources", cb => autoCacheBreak = cb != null },
                    { "p|prefix", "Automatically generate vender prefixed versions of properties", p => autoPrefix = p != null },
                    { "w|watch", "Automatically recompile when files change", w => watch = w != null }
                };

                options.Parse(args);

                if (showHelp)
                {
                    options.WriteOptionDescriptions(Console.Out);
                    return (int)ExitCode.Success;
                }

                if (version)
                {
                    var v = Assembly.GetExecutingAssembly().GetName().Version;
                    Console.WriteLine(v.ToString());
                    return (int)ExitCode.Success;
                }

                if (!VerifyParameters(workingDirectory, maxDegreeParallelism, spriteProg, overwrite, watch))
                {
                    return (int)ExitCode.BadParameters;
                }

                var toCompile = FindFiles(workingDirectory, filePattern, recurse);

                if (toCompile.Count == 0)
                {
                    Console.WriteLine("No files found to compile");
                    return (int)ExitCode.BadParameters;
                }

                if (verbose)
                {
                    Console.WriteLine("Compiling (" + toCompile.Count + ") files...");
                }

                DependencyGraph dependenices;
                var success = MultiThreadedCompile(maxDegreeParallelism, workingDirectory, toCompile, overwrite, warnAsErrors, minify, verbose, spriteProg, spriteArguments, autoCacheBreak, autoPrefix, out dependenices);

                if (watch)
                {
                    using (var watcher = new DependencyWatcher(dependenices))
                    {
                        watcher.Changed +=
                            delegate(List<string> changed)
                            {
                                var recompile = dependenices.NeedRecompilation(changed, toCompile).ToList();

                                if (recompile.Count > 0)
                                {
                                    foreach (var f in changed)
                                    {
                                        FileCache.Remove(f);
                                    }

                                    Console.WriteLine(changed.Count + " file(s) changed, recompiling " + recompile.Count + " .more files:");
                                    Console.WriteLine(string.Join("\n", recompile.Select(s => "\t" + s)));

                                    DependencyGraph ignored;
                                    MultiThreadedCompile(maxDegreeParallelism, workingDirectory, recompile, overwrite, warnAsErrors, minify, verbose, spriteProg, spriteArguments, autoCacheBreak, autoPrefix, out ignored);
                                }
                            };

                        watcher.Init(workingDirectory);

                        if (verbose)
                        {
                            Console.WriteLine("Watching for file changes, press any key to exit");
                        }

                        Console.ReadKey();

                        return (int)ExitCode.Success;
                    }
                }
                else
                {
                    if (verbose)
                    {
                        Console.ReadKey();
                    }
                }

                return (int)(success ? ExitCode.Success : ExitCode.CompilationErrors);
            }
            catch (Exception e)
            {
                DumpError(e, abort: false);

                return (int)ExitCode.Crash;
            }
        }
Example #2
0
 public DependencyWatcherExampleViewModel()
 {
     DependencyWatcher.Register(this, NotifyPropertyChanged);
 }