<commit_before>﻿// Copyright 2011 <NAME>.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Web.Hosting;

namespace MvcCodeRouting {

   sealed class EmbeddedViewsVirtualPathProvider : VirtualPathProvider {

      static readonly StringComparison VirtualPathComparison = StringComparison.OrdinalIgnoreCase;
      static readonly StringComparer VirtualPathComparer = StringComparer.OrdinalIgnoreCase;

      static readonly List<AssemblyData> AssemblyDataTable = new List<AssemblyData>();
      readonly ConcurrentDictionary<string, AssemblyDataCollection> virtualPathCache = new ConcurrentDictionary<string, AssemblyDataCollection>(VirtualPathComparer);
      static bool embeddedViewsEnabled;

      public static void RegisterAssembly(RegisterInfo registerInfo) {

         string basePath = String.Join("/", new[] { "Views", registerInfo.ViewsLocation }.Where(s => !String.IsNullOrEmpty(s)));
         var assemblyData = new AssemblyData(registerInfo, basePath);

         if (assemblyData.HasResources) 
            AssemblyDataTable.Add(assemblyData);
      }

      public static void RegisterIfNecessary() {

         if (AssemblyDataTable.Count > 0 && !embeddedViewsEnabled) {
            HostingEnvironment.RegisterVirtualPathProvider(new EmbeddedViewsVirtualPathProvider());
            embeddedViewsEnabled = true;
         }
      }

      public override bool DirectoryExists(string virtualDir) {

         bool prevExists = base.DirectoryExists(virtualDir);

         if (prevExists)
            return prevExists;

         return GetAssemblyData(virtualDir).ResourceExists(virtualDir, isFile: false);
      }

      public override bool FileExists(string virtualPath) {
         <commit_msg>Removed limitation that required ViewEngineCollection.EnableCodeRouting() to be called after RouteCollection.MapCodeRoutes(), when using embedded views. Now both methods can be called in any order.<commit_after>﻿// Copyright 2011 <NAME>.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Web.Hosting;

namespace MvcCodeRouting {

   sealed class EmbeddedViewsVirtualPathProvider : VirtualPathProvider {

      static readonly StringComparison VirtualPathComparison = StringComparison.OrdinalIgnoreCase;
      static readonly StringComparer VirtualPathComparer = StringComparer.OrdinalIgnoreCase;

      static readonly List<AssemblyData> AssemblyDataTable = new List<AssemblyData>();
      readonly ConcurrentDictionary<string, AssemblyDataCollection> virtualPathCache = new ConcurrentDictionary<string, AssemblyDataCollection>(VirtualPathComparer);
      static bool embeddedViewsEnabled, registered;

      public static void RegisterAssembly(RegisterInfo registerInfo) {

         string basePath = String.Join("/", new[] { "Views", registerInfo.ViewsLocation }.Where(s => !String.IsNullOrEmpty(s)));
         var assemblyData = new AssemblyData(registerInfo, basePath);

         if (assemblyData.HasResources) 
            AssemblyDataTable.Add(assemblyData);

         if (embeddedViewsEnabled && !registered)
            RegisterIfNecessary();
      }

      public static void RegisterIfNecessary() {

         embeddedViewsEnabled = true;

         if (AssemblyDataTable.Count > 0 && !registered) {
            HostingEnvironment.RegisterVirtualPathProvider(new EmbeddedViewsVirtualPathProvider());
            registered = true;
         }
      }

      public override bool DirectoryExists(string virtualDir) {

         bool prevExists = base.DirectoryExists(virtualDir);

         if (prevExists)
            return prevExists;

         return GetAssemblyData(virtualDir).ResourceExists(virtualDir, isFile: false);
      }

      public override bool FileExists(string virtualPath) {
         
{'fraction_non_alphanumeric': 0.07879359095193214, 'fraction_numerical': 0.0033930254476908575, 'mean_word_length': 3.788808664259928, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7314385', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1248, 'n_words': 413}