<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Microsoft.TemplateEngine.Core.Contracts;

namespace Microsoft.TemplateEngine.Orchestrator.RunnableProjects
{
    [Flags]
    public enum FileDispositionStates
    {
        None = 0,
        Include = 0x01,
        CopyOnly = 0x02,
        Exclude = 0x04
    }

    public static class EnumExtensions
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static bool Has(this FileDispositionStates value, FileDispositionStates check)
        {
            return (value & check) == check;
        }
    }

    public class FileSourceHierarchicalPathMatcher
    {
        public FileSourceHierarchicalPathMatcher(FileSourceMatchInfo matchInfo)
        {
            List<FileSourceEvaluablePathMatcher> evaluators = new List<FileSourceEvaluablePathMatcher>();

            for (int i = matchInfo.Modifiers.Count - 1; i >= 0; i--)
            {
                evaluators.Add(new FileSourceEvaluablePathMatcher(matchInfo.Modifiers[i]));
            }

            evaluators.Add(new FileSourceEvaluablePathMatcher(matchInfo.TopLevelEvaluable));
            _evaluators = evaluators;

            _cachedEvaluatedPath = string.Empty;
            _cachedStatesForFile = FileDispositionStates.None;
        }

        private IReadOnlyList<FileSourceEvaluablePathMatcher> _evaluators;

        // Caching the most recent result.
        private string _cachedEvaluatedPath;
        private FileDispositionStates _cachedStatesForFile;

        public FileDispositionStates Evaluate(string path)
        {
            if (!string.Equals(path, _cachedEvaluatedPath, StringComparison.Ordinal))
            {
                _cachedStatesForFile = EvaluatePath(path);
                _cachedEvaluatedPath = path;
            }

            return _cachedStatesForFile;
        }

        private FileDispositionStates EvaluatePath(string path)
        {
            FileDispositionStates continuationReason = FileDispositionStates.None;

            foreach (FileSourceEvaluablePathMatcher evaluator in _evaluators)
            {
                FileDispositionStates state = evaluator.Evaluate(path);

                if (state.Has(FileDispositionStates.Exclude))
                {
                    return  FileDispositionStates.Exclude;
                }
                else if (state.Has(FileDispositionStates.CopyOnly))
                {
                    if (state.Has(FileDispositionStates.Include))
                    {
                        return FileDispositionStates.CopyOnly;
                    }

                    continuationReason = FileDispositionStates.CopyOnly;
                }
                else if (state.Has(FileDispositionStates.Include))
                {
                    if (continuationReason == FileDispositionStates.CopyOnly)
                    {
                        return FileDispositionStates.CopyOnly;
                    }

                    return FileDispositionStates.Include;
                }
            }<commit_msg>Fix for CopyOnly within sources<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Microsoft.TemplateEngine.Core.Contracts;

namespace Microsoft.TemplateEngine.Orchestrator.RunnableProjects
{
    [Flags]
    public enum FileDispositionStates
    {
        None = 0,
        Include = 0x01,
        CopyOnly = 0x02,
        Exclude = 0x04
    }

    public static class EnumExtensions
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static bool Has(this FileDispositionStates value, FileDispositionStates check)
        {
            return (value & check) == check;
        }
    }

    public class FileSourceHierarchicalPathMatcher
    {
        public FileSourceHierarchicalPathMatcher(FileSourceMatchInfo matchInfo)
        {
            List<FileSourceEvaluablePathMatcher> evaluators = new List<FileSourceEvaluablePathMatcher>();

            for (int i = matchInfo.Modifiers.Count - 1; i >= 0; i--)
            {
                evaluators.Add(new FileSourceEvaluablePathMatcher(matchInfo.Modifiers[i]));
            }

            evaluators.Add(new FileSourceEvaluablePathMatcher(matchInfo.TopLevelEvaluable));
            _evaluators = evaluators;

            _cachedEvaluatedPath = string.Empty;
            _cachedStatesForFile = FileDispositionStates.None;
        }

        private IReadOnlyList<FileSourceEvaluablePathMatcher> _evaluators;

        // Caching the most recent result.
        private string _cachedEvaluatedPath;
        private FileDispositionStates _cachedStatesForFile;

        public FileDispositionStates Evaluate(string path)
        {
            if (!string.Equals(path, _cachedEvaluatedPath, StringComparison.Ordinal))
            {
                _cachedStatesForFile = EvaluatePath(path);
                _cachedEvaluatedPath = path;
            }

            return _cachedStatesForFile;
        }

        private FileDispositionStates EvaluatePath(string path)
        {
            FileDispositionStates continuationReason = FileDispositionStates.None;

            foreach (FileSourceEvaluablePathMatcher evaluator in _evaluators)
            {
                FileDispositionStates state = evaluator.Evaluate(path);

                if (state.Has(FileDispositionStates.Exclude))
                {
                    return FileDispositionStates.Exclude;
                }
                else if (state.Has(FileDispositionStates.CopyOnly))
                {
                    if (state.Has(FileDispositionStates.Include))
                    {
                        return FileDispositionStates.CopyOnly | FileDispositionStates.Include;
                    }

                    continuationReason = FileDispositionStates.CopyOnly;
                }
                else if (state.Has(FileDispositionStates.Include))
                {
                    if (continuationReason == FileDispositionStates.CopyOnly)
                    {
                        return FileDispositionStates.CopyOnly | FileDispositionStates.Include;
                    }

                    return FileDispositionStates.Include;
                }
            }
{'fraction_non_alphanumeric': 0.062361856646668774, 'fraction_numerical': 0.003789074834227976, 'mean_word_length': 1.9220479704797049, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7492733', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1484, 'n_words': 276}