Пример #1
0
        static void Main(string[] args)
        {
            OctaviaOptions octaviaOptions = OctaviaOptions.GetOctaviaOptions(args);

            if (args.Length == 0 || args.Contains("-help"))
            {
                Console.WriteLine(OctaviaOptions.GetHelp());
                return;
            }

            if (octaviaOptions.FatalError.Count() > 0)
            {
                foreach (ArgumentError error in octaviaOptions.FatalError)
                {
                    Console.WriteLine(error);
                }
                Console.WriteLine(OctaviaOptions.GetHelp());
                return;
            }

            FileSystemWatcher watcher = new FileSystemWatcher
            {
                Path = octaviaOptions.WatchFolder,
                IncludeSubdirectories = true,
                NotifyFilter          = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName,
                Filter = $"*.{octaviaOptions.Extension}"
            };

            watcher.Created            += (sender, e) => OnWatchChange(sender, e, octaviaOptions);
            watcher.Changed            += (sender, e) => OnWatchChange(sender, e, octaviaOptions);
            watcher.Renamed            += (sender, e) => OnWatchChange(sender, e, octaviaOptions);
            watcher.Deleted            += (sender, e) => OnWatchChange(sender, e, octaviaOptions);
            watcher.EnableRaisingEvents = true;

            Compile(octaviaOptions);

            while (Console.ReadKey().Key != ConsoleKey.Q)
            {
                ;
            }
            Console.Clear();
        }
Пример #2
0
        private static void Compile(OctaviaOptions o)
        {
            Stopwatch stopwatch = new Stopwatch();

            Console.Clear();

            Console.WriteLine($"Watching directory {o.WatchFolder}\nPress [q] to stop.\n");

            stopwatch.Restart();

            StringBuilder compiled = new StringBuilder();

            bool next;
            int  fileOpenedCount;

            foreach (string file in o.Files)
            {
                Console.WriteLine($"[{DateTime.Now.ToString("HH:mm:ss")}] reading: {file}");
                fileOpenedCount = 0;
                next            = false;

                do
                {
                    try
                    {
                        using (StreamReader streamReader = new StreamReader(file))
                        {
                            if (!o.NoRegions)
                            {
                                compiled.Append($"/* #region {file} */\n\n");
                            }

                            compiled.Append(streamReader.ReadToEnd());

                            if (!o.NoRegions)
                            {
                                compiled.Append($"\n\n/* #endregion */\n\n");
                            }

                            next = true;
                        }
                    }
                    catch (IOException)
                    {
                        if (fileOpenedCount == 10)
                        {
                            Console.WriteLine($"    {file} is locked. It will be retried on the next compilation.");
                            next = true;
                        }
                        else
                        {
                            fileOpenedCount++;
                        }
                    }
                } while (!next);
            }

            Console.WriteLine($"\n[{DateTime.Now.ToString("HH:mm:ss")}] writing {o.DestinationFile}");
            using (StreamWriter streamWriter = new StreamWriter(o.DestinationFile))
            {
                streamWriter.Write(compiled);
            }

            stopwatch.Stop();
            Console.WriteLine($"\n    Done in: {stopwatch.ElapsedMilliseconds}ms");
        }
Пример #3
0
 private static void OnWatchChange(object sender, FileSystemEventArgs e, OctaviaOptions octaviaOptions)
 {
     Compile(octaviaOptions);
 }
Пример #4
0
        public static OctaviaOptions GetOctaviaOptions(string[] args)
        {
            OctaviaOptions o = new OctaviaOptions();

            #region Source directory checking (critical; contains, has value, directory exists).
            // Check if the arguments contain the source keyword.
            if (args.Contains(SourceArgument))
            {
                // Check if the arguments contain something after the source keyword (its value).
                if (Array.IndexOf(args, SourceArgument) + 1 < args.Length)
                {
                    // Check if the given source directory exists.
                    string sourceFolder = args[Array.IndexOf(args, SourceArgument) + 1];
                    if (Directory.Exists(sourceFolder))
                    {
                        o.WatchFolder = sourceFolder;
                    }
                    else
                    {
                        o.FatalError.Add(new ArgumentError(SourceArgument, $"The source folder \"{sourceFolder}\" does not exist."));
                    }
                }
                else
                {
                    o.FatalError.Add(new ArgumentError(SourceArgument, $"Missing required value."));
                }
            }
            else
            {
                o.FatalError.Add(new ArgumentError(SourceArgument, $"Missing required parameter."));
            }
            #endregion

            #region Destination file checking (critical; contains, has value, file exists).
            // Check if the arguments contain the destination keyword.
            if (args.Contains(DestinationArgument))
            {
                // Check if the arguments contain something after the destination keyword (its value).
                if (Array.IndexOf(args, DestinationArgument) + 1 < args.Length)
                {
                    // Check if the given destination file exists.
                    string destinationFile = args[Array.IndexOf(args, DestinationArgument) + 1];
                    if (File.Exists(destinationFile))
                    {
                        o.DestinationFile = destinationFile;
                    }
                    else
                    {
                        o.FatalError.Add(new ArgumentError(DestinationArgument, $"The destination file \"{destinationFile}\" does not exist."));
                    }
                }
                else
                {
                    o.FatalError.Add(new ArgumentError(DestinationArgument, $"Missing required value."));
                }
            }
            else
            {
                o.FatalError.Add(new ArgumentError(DestinationArgument, $"Missing required parameter."));
            }
            #endregion

            #region No regions checking (non-critical; contains).
            // Check if the arguments contain the no-regions keyword.
            if (args.Contains(NoRegionsArgument))
            {
                o.NoRegions = true;
            }
            #endregion

            #region File extension checking (non-critical; contains, has-value).
            // Check if the arguments contain the extension keyword.
            if (args.Contains(ExtensionArgument))
            {
                // Check if the argument contain something after the extension keyword (its value).
                if (Array.IndexOf(args, ExtensionArgument) + 1 < args.Length)
                {
                    o.Extension = args[Array.IndexOf(args, ExtensionArgument) + 1];
                }
                else
                {
                    o.FatalError.Add(new ArgumentError(ExtensionArgument, $"Argument exists but missing required value."));
                }
            }
            #endregion

            #region Configuration file checking (non-critical; contains, has value, file exists).
            // Check if the arguments contain the extension keyword.
            if (args.Contains(ConfigArgument))
            {
                // Check if the argument contain something after the extension keyword (its value).
                if (Array.IndexOf(args, ConfigArgument) + 1 < args.Length)
                {
                    // Check if the config file actually exists.
                    string configFile = args[Array.IndexOf(args, ConfigArgument) + 1];
                    if (File.Exists(configFile))
                    {
                        // Format the watch files so it is like: "src/file.css" and not just "file".
                        foreach (string configLine in File.ReadAllLines(configFile))
                        {
                            o.Files.Add($"{o.WatchFolder}/{configLine}.{o.Extension}");
                        }
                    }
                    else
                    {
                        o.FatalError.Add(new ArgumentError(ConfigArgument, $"The configuration file \"{configFile}\" does not exist."));
                    }
                }
                else
                {
                    o.FatalError.Add(new ArgumentError(ConfigArgument, $"Argument exists but missing required value."));
                }
            }
            else
            {
                // Without checking for FatalErrors, and running without a specified WatchFolder, the program crashes.
                if (o.FatalError.Count == 0)
                {
                    // Here we don't need to format the watch files as they are already in the format of "src/file.css".
                    o.Files = Directory.GetFiles(o.WatchFolder, $"*.{o.Extension}").ToList();
                }
            }
            #endregion

            return(o);
        }