Exemple #1
0
 public void SetupWatchers(IEnumerable <string> files, CompilationDelegate del)
 {
     foreach (var path in files)
     {
         if (!CompilationDelegates.ContainsKey(path))
         {
             var fsWatcher = new FileSystemWatcher(Path.GetDirectoryName(path), Path.GetFileName(path));
             fsWatcher.Changed            += FileChangedHandler;
             fsWatcher.Deleted            += FileDeletedHandler;
             fsWatcher.EnableRaisingEvents = true;
             Console.WriteLine("Started watching '{0}' for changes", path);
             CompilationDelegates.Add(path, del);
             FileSystemWatchers.Add(fsWatcher);
         }
         else
         {
             lock (_eventLock)
             {
                 bool add = true;
                 foreach (var d in CompilationDelegates[path].GetInvocationList())
                 {
                     if (d.Target.Equals(del.Target))
                     {
                         add = false;
                     }
                 }
                 if (add)
                 {
                     CompilationDelegates[path] += del;
                 }
             }
         }
     }
 }
Exemple #2
0
 public void SetupWatchers(IEnumerable<string> files, CompilationDelegate del)
 {
     foreach (var path in files)
     {
         if (!CompilationDelegates.ContainsKey(path))
         {
             var fsWatcher = new FileSystemWatcher(Path.GetDirectoryName(path), Path.GetFileName(path));
             fsWatcher.Changed += FileChangedHandler;
             fsWatcher.Deleted += FileDeletedHandler;
             fsWatcher.EnableRaisingEvents = true;
             Console.WriteLine("Started watching '{0}' for changes", path);
             CompilationDelegates.Add(path, del);
             FileSystemWatchers.Add(fsWatcher);
         }
         else
         {
             lock (_eventLock)
             {
                 bool add = true;
                 foreach (var d in CompilationDelegates[path].GetInvocationList())
                 {
                     if (d.Target.Equals(del.Target)) add = false;
                 }
                 if (add) CompilationDelegates[path] += del;
             }
         }
     }
 }
Exemple #3
0
        public static int Main(string[] args)
        {
            var arguments = new List <string>();

            arguments.AddRange(args);

            var configuration = GetConfigurationFromArguments(arguments);

            if (configuration.Help)
            {
                return(-1);
            }

            if (arguments.Count == 0)
            {
                WriteHelp();
                return(-1);
            }

            var inputDirectoryPath = Path.GetDirectoryName(arguments[0]);

            if (string.IsNullOrEmpty(inputDirectoryPath))
            {
                inputDirectoryPath = "." + Path.DirectorySeparatorChar;
            }
            var inputFilePattern    = Path.GetFileName(arguments[0]);
            var outputDirectoryPath = string.Empty;
            var outputFilename      = string.Empty;

            if (string.IsNullOrEmpty(inputFilePattern))
            {
                inputFilePattern = "*.less";
            }
            if (!Path.HasExtension(inputFilePattern))
            {
                inputFilePattern = Path.ChangeExtension(inputFilePattern, "less");
            }

            if (arguments.Count > 1)
            {
                outputDirectoryPath = Path.GetDirectoryName(arguments[1]);
                outputFilename      = Path.GetFileName(arguments[1]);

                if (!Path.HasExtension(outputFilename))
                {
                    outputFilename = Path.ChangeExtension(outputFilename, "css");
                }
            }

            if (string.IsNullOrEmpty(outputDirectoryPath))
            {
                outputDirectoryPath = inputDirectoryPath;
            }
            else
            {
                Directory.CreateDirectory(outputDirectoryPath);
            }

            if (HasWildcards(inputFilePattern))
            {
                if (!string.IsNullOrEmpty(outputFilename))
                {
                    Console.WriteLine("Output filename patterns/filenames are not supported when using input wildcards. You may only specify an output directory (end the output in a directory seperator)");
                    return(-1);
                }
                outputFilename = string.Empty;
            }

            var filenames = Directory.GetFiles(inputDirectoryPath, inputFilePattern);
            var engine    = new EngineFactory(configuration).GetEngine();

            using (var watcher = new Watcher()
            {
                Watch = configuration.Watch
            })
            {
                if (watcher.Watch && HasWildcards(inputFilePattern))
                {
                    CompilationFactoryDelegate factoryDelegate = (input) => CreationImpl(engine, input, Path.GetFullPath(outputDirectoryPath));
                    watcher.SetupDirectoryWatcher(Path.GetFullPath(inputDirectoryPath), inputFilePattern, factoryDelegate);
                }

                foreach (var filename in filenames)
                {
                    var inputFile = new FileInfo(filename);

                    var outputFile =
                        string.IsNullOrEmpty(outputFilename) ?
                        Path.Combine(outputDirectoryPath, Path.ChangeExtension(inputFile.Name, "css")) :
                        Path.Combine(outputDirectoryPath, outputFilename);

                    var outputFilePath = Path.GetFullPath(outputFile);

                    CompilationDelegate compilationDelegate = () => CompileImpl(engine, inputFile.FullName, outputFilePath);

                    Console.WriteLine("[Compile]");

                    var files = compilationDelegate();

                    if (watcher.Watch)
                    {
                        watcher.SetupWatchers(files, compilationDelegate);
                    }
                }

                if (configuration.Watch)
                {
                    WriteAbortInstructions();
                }
                else if (filenames.Count() == 0)
                {
                    Console.WriteLine("No files found matching pattern '{0}'", inputFilePattern);
                    return(-1);
                }

                while (watcher.Watch && Console.ReadKey(true).Key != ConsoleKey.Enter)
                {
                    System.Threading.Thread.Sleep(200);
                }
            }
            return(returnCode);
        }
Exemple #4
0
        public static void Main(string[] args)
        {
            var arguments = new List <string>();

            arguments.AddRange(args);

            var configuration = GetConfigurationFromArguments(arguments);

            if (configuration.Help)
            {
                return;
            }

            if (arguments.Count == 0)
            {
                WriteHelp();
                return;
            }

            var inputDirectoryPath = Path.GetDirectoryName(arguments[0]);

            if (string.IsNullOrEmpty(inputDirectoryPath))
            {
                inputDirectoryPath = ".\\";
            }
            var inputFilePattern    = Path.GetFileName(arguments[0]);
            var outputDirectoryPath = string.Empty;
            var outputFilename      = string.Empty;

            if (string.IsNullOrEmpty(inputFilePattern))
            {
                inputFilePattern = "*.less";
            }
            if (!Path.HasExtension(inputFilePattern))
            {
                inputFilePattern = Path.ChangeExtension(inputFilePattern, "less");
            }

            if (arguments.Count > 1)
            {
                outputDirectoryPath = Path.GetDirectoryName(arguments[1]);
                outputFilename      = Path.GetFileName(arguments[1]);
                outputFilename      = Path.ChangeExtension(outputFilename, "css");
            }
            else
            {
                outputDirectoryPath = inputDirectoryPath;
            }
            if (HasWildcards(inputFilePattern))
            {
                outputFilename = string.Empty;
            }

            var filenames = Directory.GetFiles(inputDirectoryPath, inputFilePattern);
            var engine    = new EngineFactory(configuration).GetEngine();

            using (var watcher = new Watcher()
            {
                Watch = configuration.Watch
            })
            {
                if (watcher.Watch && HasWildcards(inputFilePattern))
                {
                    CompilationFactoryDelegate factoryDelegate = (input) => CreationImpl(engine, input, Path.GetFullPath(outputDirectoryPath));
                    watcher.SetupDirectoryWatcher(Path.GetFullPath(inputDirectoryPath), inputFilePattern, factoryDelegate);
                }

                foreach (var filename in filenames)
                {
                    var inputFile   = new FileInfo(filename);
                    var pathbuilder = new System.Text.StringBuilder(outputDirectoryPath + "\\");
                    if (string.IsNullOrEmpty(outputFilename))
                    {
                        pathbuilder.Append(Path.ChangeExtension(inputFile.Name, "css"));
                    }
                    else
                    {
                        pathbuilder.Append(outputFilename);
                    }
                    var outputFilePath = Path.GetFullPath(pathbuilder.ToString());

                    CompilationDelegate compilationDelegate = () => CompileImpl(engine, inputFile.FullName, outputFilePath);
                    Console.WriteLine("[Compile]");
                    var files = compilationDelegate();
                    if (watcher.Watch)
                    {
                        watcher.SetupWatchers(files, compilationDelegate);
                    }
                }
                if (configuration.Watch)
                {
                    WriteAbortInstructions();
                }
                while (watcher.Watch && Console.ReadKey(true).Key != ConsoleKey.Enter)
                {
                    System.Threading.Thread.Sleep(200);
                }
            }
        }