Esempio n. 1
0
        public override bool Execute()
        {
            var stopwatch = Stopwatch.StartNew();
            var root      = GitRepoDirectoryFinder.FindForDirectory(ProjectDirectory);

            var(fileConfig, configFilePath) = ConfigReader.Read(root);

            var configResult = ConfigDefaults.Convert(
                fileConfig,
                new ConfigInput
            {
                ReadOnly              = ReadOnly,
                ValidateContent       = ValidateContent,
                WriteHeader           = WriteHeader,
                Header                = Header,
                UrlPrefix             = UrlPrefix,
                LinkFormat            = LinkFormat,
                Convention            = Convention,
                Exclude               = Exclude,
                TocExcludes           = TocExcludes,
                TocLevel              = TocLevel,
                MaxWidth              = MaxWidth,
                UrlsAsSnippets        = UrlsAsSnippets,
                DocumentExtensions    = DocumentExtensions,
                TreatMissingAsWarning = TreatMissingAsWarning,
                HashSnippetAnchors    = HashSnippetAnchors,
            });

            var message = LogBuilder.BuildConfigLogMessage(root, configResult, configFilePath);

            Log.LogMessage(message);

            var processor = new DirectoryMarkdownProcessor(
                root,
                log: s => Log.LogMessage(s),
                readOnly: configResult.ReadOnly,
                directoryFilter: ExcludeToFilterBuilder.ExcludesToFilter(configResult.Exclude),
                writeHeader: configResult.WriteHeader,
                header: configResult.Header,
                urlPrefix: configResult.UrlPrefix,
                linkFormat: configResult.LinkFormat,
                convention: configResult.Convention,
                tocLevel: configResult.TocLevel,
                tocExcludes: configResult.TocExcludes,
                documentExtensions: configResult.DocumentExtensions,
                treatMissingAsWarning: configResult.TreatMissingAsWarning,
                maxWidth: configResult.MaxWidth,
                validateContent: configResult.ValidateContent,
                hashSnippetAnchors: configResult.HashSnippetAnchors);

            var snippets = new List <Snippet>();

            try
            {
                snippets.AppendUrlsAsSnippets(configResult.UrlsAsSnippets).GetAwaiter().GetResult();
                processor.AddSnippets(snippets);
                var snippetsInError = processor.Snippets.Where(x => x.IsInError).ToList();
                if (snippetsInError.Any())
                {
                    foreach (var snippet in snippetsInError)
                    {
                        Log.LogFileError($"Snippet error: {snippet.Error}. Key: {snippet.Key}", snippet.Path, snippet.StartLine, 0);
                    }

                    return(false);
                }

                processor.Run();
                return(true);
            }
            catch (MissingSnippetsException exception)
            {
                foreach (var missing in exception.Missing)
                {
                    if (configResult.TreatMissingAsWarning)
                    {
                        Log.LogWarning($"MarkdownSnippets: Missing snippet: {missing.Key}", missing.File, missing.LineNumber, 0);
                    }
                    else
                    {
                        Log.LogFileError($"MarkdownSnippets: Missing snippet: {missing.Key}", missing.File, missing.LineNumber, 0);
                    }
                }

                return(configResult.TreatMissingAsWarning);
            }
            catch (MissingIncludesException exception)
            {
                foreach (var missing in exception.Missing)
                {
                    if (configResult.TreatMissingAsWarning)
                    {
                        Log.LogWarning($"MarkdownSnippets: Missing include: {missing.Key}", missing.File, missing.LineNumber);
                    }
                    else
                    {
                        Log.LogFileError($"MarkdownSnippets: Missing include: {missing.Key}", missing.File, missing.LineNumber, 0);
                    }
                }

                return(configResult.TreatMissingAsWarning);
            }
            catch (ContentValidationException exception)
            {
                foreach (var error in exception.Errors)
                {
                    //TODO: add column
                    Log.LogFileError($"MarkdownSnippets: Content validation: {error.Error}", error.File, error.Line, error.Column);
                }

                return(configResult.TreatMissingAsWarning);
            }
            catch (MarkdownProcessingException exception)
            {
                Log.LogFileError($"MarkdownSnippets: {exception.Message}", exception.File, exception.LineNumber, 0);
                return(false);
            }
            catch (SnippetException exception)
            {
                Log.LogError($"MarkdownSnippets: {exception}");
                return(false);
            }
            finally
            {
                Log.LogMessageFromText($"Finished MarkdownSnippets {stopwatch.ElapsedMilliseconds}ms", MessageImportance.Normal);
            }
        }
Esempio n. 2
0
        /// <summary>
        ///   base "program" to start running the process <see cref="System.Console.WriteLine(System.String)"/> - <para>see <see cref="recursive_robocopy.classes.RobocopyTask.doRoboCopy()"/> for the algorithm and logic implementation</para>
        ///
        /// </summary>
        public void startRoboCopy()
        {
            System.Console.Out.WriteLine("START - Recursive robocopy");

            // validate folders
            bool hasErrors = false;

            //// basics - neither should be blank
            // source - exists
            if (!System.IO.Directory.Exists(myContext.Folders.SourceFolder))
            {
                hasErrors = true;
                System.Console.Out.WriteLine(String.Format("ERROR: Source directory does not exist ({0})", myContext.Folders.SourceFolder));
            }
            // target - can be created (mostly access tests)
            try
            {
                // create the target directory
                System.IO.Directory.CreateDirectory(myContext.Folders.TargetFolder);
            }
            catch (IOException ioe)
            {
                //directory cannot be created - record and log details to console
                hasErrors = true;
                System.Console.Out.WriteLine(String.Format("ERROR: Target directory could not be created - Detail: ({0}) Dir: {1}", ioe.Message, myContext.Folders.TargetFolder));
                consoleHelper.WriteUsage();
            }

            if (hasErrors) // stop process on errors
            {
                return;
            }

            // TODO report start & implement all logging

            // create and queue the top-level task
            var robocopytask = new RobocopyTask(myContext.Folders);

            // setup the countdownevent object and pass it in as state
            var cde            = new CountdownEvent(1);
            var cdeActiveTasks = new CountdownEvent(1);
            var semOutput      = new Semaphore(1, 1); // semaphore to control output

            var myState = new doRoboCopyState(cde, cdeActiveTasks, semOutput, true);

            System.Console.Out.WriteLine(string.Format("QUEUED - {0}", myContext.Folders.SourceFolder));

            // setup the thread pool
            int maxThreads, maxPortThreads;
            int localmaxThreads, localmaxPortThreads;
            int minThreads, minPortThreads;

            ThreadPool.GetMinThreads(out minThreads, out minPortThreads);
            ThreadPool.GetMaxThreads(out maxThreads, out maxPortThreads);

            // set max threads from the config or defaults if missing
            ThreadPool.SetMinThreads(1, minPortThreads);
            ThreadPool.SetMaxThreads((int)ConfigDefaults.getMaxThreads(), maxPortThreads);  // set thread pool limits - don't touch the local port max

            ThreadPool.GetMaxThreads(out localmaxThreads, out localmaxPortThreads);
            ThreadPool.GetMinThreads(out minThreads, out minPortThreads);

            System.Console.Out.WriteLine(string.Format("  Thread Pool - Min {0} max {1} local max {2}", minThreads, maxThreads, localmaxThreads));
            ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(robocopytask.doRoboCopy), myState);

            // Wait for everything to finish
            cde.Wait();

            // report end -
            System.Console.Out.WriteLine("END - Recursive robocopy");
        }