Exemplo n.º 1
0
        public ConfigurationCollection ReadConfig()
        {
            string text;
            if (fileReader == null)
            {
                text = File.ReadAllText(Path);    
            }
            else
            {
                text = fileReader.ReadFile(path);
            }

            var collection = new ConfigurationCollection();
            var deserialized = (JObject)JsonConvert.DeserializeObject(text);
            collection.FilePath = Path;
            collection.Paths = GetPaths(deserialized);
            collection.Shim = GetShim(deserialized);
            collection.Map = GetMap(deserialized);

            if (options.ProcessBundles)
            {
                collection.Bundles = GetBundles(deserialized);
            }

            collection.AutoBundles = GetAutoBundles(deserialized);
            collection.Overrides = GetOverrides(deserialized);


            return collection;
        }
Exemplo n.º 2
0
        public void WriteConfig(ConfigurationCollection conf)
        {
            dynamic obj = new ExpandoObject();
            if (conf.Paths != null && conf.Paths.PathList != null && conf.Paths.PathList.Any())
            {
                obj.Paths = conf.Paths.PathList.ToDictionary(r => r.Key, r => r.Value);
            }

            if (conf.Overrides != null && conf.Overrides.Any())
            {
                obj.Overrides = conf.Overrides.ToDictionary(
                    r => r.BundleId,
                    r => new
                             {
                                 Paths = r.Paths.PathList.ToDictionary(x => x.Key, x => x.Value),
                                 BundledScripts = r.BundledScripts
                             });
            }

            File.WriteAllText(
                Path, 
                JsonConvert.SerializeObject(
                            obj,
                            new JsonSerializerSettings
                            {
                                Formatting = Formatting.Indented,
                                ContractResolver = new CamelCasePropertyNamesContractResolver()
                            }));
        }
Exemplo n.º 3
0
 public void WriteConfig(ConfigurationCollection conf)
 {
     var paths = this.GetPaths(conf.Paths);
     var overrides = this.GetOverrides(conf.Overrides);
     var document = new XDocument(new XElement("configuration", paths, overrides));
     File.WriteAllText(Path, document.ToString());
 }
Exemplo n.º 4
0
        public ConfigurationCollection ReadConfig()
        {
            if (collection == null)
            {
                collection = ProcessConfig();
            }

            return collection;
        }
Exemplo n.º 5
0
        public void Override(ConfigurationCollection collection, string entryPoint)
        {
            if (collection.Overrides == null || !collection.Overrides.Any())
            {
                return;
            }

            var relevantOverrides = this.GetRelevantOverrides(collection, entryPoint);
            var merged = this.MergeOverrides(relevantOverrides);
            this.ApplyOverride(collection, merged);
        }
Exemplo n.º 6
0
        public static string ExpandPaths(string name, ConfigurationCollection configuration)
        {
            var alias = configuration.Paths.PathList
                .Where(path => name.StartsWith(path.Key, StringComparison.OrdinalIgnoreCase))
                .Where(path => name.Length == path.Key.Length || name[path.Key.Length] == '/')
                .FirstOrDefault();

            if (alias == null)
                return name;

            return alias.Value + name.Substring(alias.Key.Length);
        }
Exemplo n.º 7
0
 public ConfigurationCollection ReadConfig()
 {
     using (var stream = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
     {
         var doc = XDocument.Load(stream);
         var collection = new ConfigurationCollection();
         collection.FilePath = Path;
         collection.Paths = GetPaths(doc.Root);
         collection.Shim = GetShim(doc.Root);
         collection.Map = GetMap(doc.Root);
         return collection;
     }
 }
Exemplo n.º 8
0
 private void MergePaths(ConfigurationCollection collection)
 {
     var finalPaths = finalCollection.Paths.PathList;
     foreach (var path in collection.Paths.PathList)
     {
         var existing = finalPaths.Where(r => r.Key == path.Key).FirstOrDefault();
         if (existing != null)
         {
             existing.Value = path.Value;
         }
         else
         {
             finalPaths.Add(path);
         }
     }
 }
Exemplo n.º 9
0
        public void WriteConfig(ConfigurationCollection conf)
        {
            dynamic obj = new ExpandoObject();
            if (conf.Paths != null && conf.Paths.PathList != null && conf.Paths.PathList.Any())
            {
                obj.Paths = GetPaths(conf.Paths.PathList);
            }

            if (conf.Overrides != null && conf.Overrides.Any())
            {
                obj.Overrides = GetOverrides(conf.Overrides);
            }

            if (conf.Shim != null && conf.Shim.ShimEntries != null && conf.Shim.ShimEntries.Any())
            {
                obj.Shim = GetShim(conf.Shim.ShimEntries);
            }

            if (conf.Map != null && conf.Map.MapElements != null && conf.Map.MapElements.Any())
            {
                obj.Map = GetMap(conf.Map.MapElements);
            }

            if (conf.Bundles != null && conf.Bundles.BundleEntries != null && conf.Bundles.BundleEntries.Any())
            {
                obj.Bundles = GetBundles(conf.Bundles.BundleEntries);
            }

            if (conf.AutoBundles != null && conf.AutoBundles.Bundles != null && conf.AutoBundles.Bundles.Any())
            {
                obj.AutoBundles = GetAutoBundles(conf.AutoBundles.Bundles);
            }

            File.WriteAllText(
                Path, 
                JsonConvert.SerializeObject(
                            obj,
                            new JsonSerializerSettings
                            {
                                Formatting = Formatting.Indented,
                                ContractResolver = new CamelCasePropertyNamesContractResolver()
                            }));
        }
Exemplo n.º 10
0
        private List<CollectionOverride> GetRelevantOverrides(ConfigurationCollection collection, string entryPoint)
        {
            var result = collection.Overrides.ToList();

            // if there's a single bundle, just return it
            if (result.Count == 1)
            {
                return result;
            }
            
            // otherwise put the bundle containing the entrypoint last, if such a bundle exists, so that it gets priority
            var entryBundle = result.Where(r => r.BundledScripts.Where(x => x.ToLower() == entryPoint.ToLower()).Any()).FirstOrDefault();
            if (entryBundle != null)
            {
                result.Remove(entryBundle);
                result.Add(entryBundle);    
            }

            return result;
        }
Exemplo n.º 11
0
 private void MergeMaps(ConfigurationCollection collection)
 {
     var finalMaps = finalCollection.Map.MapElements;
     foreach (var map in collection.Map.MapElements)
     {
         var existingKey = finalMaps.Where(r => r.For == map.For).FirstOrDefault();
         if (existingKey != null)
         {
             existingKey.Replacements.AddRange(map.Replacements);
             existingKey.Replacements = existingKey.Replacements
                                                 .GroupBy(r => r.OldKey)
                                                 .Select(r => r.LastOrDefault())
                                                 .ToList();
         }
         else
         {
             finalMaps.Add(map);
         }
     }
 }
Exemplo n.º 12
0
        public static ConfigurationCollection CreateEmptyCollection()
        {
            var collection = new ConfigurationCollection();
            collection.Paths = new RequirePaths();
            collection.Paths.PathList = new List<RequirePath>();

            collection.AutoBundles = new AutoBundles();
            collection.AutoBundles.Bundles = new List<AutoBundle>();

            collection.Bundles = new RequireBundles();
            collection.Bundles.BundleEntries = new List<RequireBundle>();

            collection.Map = new RequireMap();
            collection.Map.MapElements = new List<RequireMapElement>();

            collection.Overrides = new List<CollectionOverride>();
            collection.Shim = new RequireShim();
            collection.Shim.ShimEntries = new List<ShimEntry>();

            return collection;
        }
Exemplo n.º 13
0
        public ConfigurationCollection ReadConfig()
        {
            using (var stream = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                var doc = XDocument.Load(stream);
                var collection = new ConfigurationCollection();
                collection.FilePath = Path;
                collection.Paths = GetPaths(doc.Root);
                collection.Shim = GetShim(doc.Root);
                collection.Map = GetMap(doc.Root);

                if (options.ProcessBundles)
                {
                    collection.Bundles = this.GetBundles(doc.Root);
                }

                collection.AutoBundles = this.GetAutoBundles(doc.Root);

                collection.Overrides = this.GetOverrides(doc.Root);

                return collection;
            }
        }
Exemplo n.º 14
0
 private void MergeShims(ConfigurationCollection collection)
 {
     var finalShims = finalCollection.Shim.ShimEntries;
     foreach (var shim in collection.Shim.ShimEntries)
     {
         var existingKey = finalShims.Where(r => r.For == shim.For).FirstOrDefault();
         if (existingKey != null)
         {
             existingKey.Exports = shim.Exports;
             existingKey.Dependencies.AddRange(shim.Dependencies);
             // distinct by Dependency
             existingKey.Dependencies = existingKey.Dependencies
                                                     .GroupBy(r => r.Dependency)
                                                     .Select(r => r.LastOrDefault())
                                                     .ToList();
         }
         else
         {
             finalShims.Add(shim);
         }
     }
 }
        private ConfigurationCollection ComposeCollection(List<Bundle> bundles)
        {
            var conf = new ConfigurationCollection();
            conf.Overrides = new List<CollectionOverride>();
            foreach (var bundle in bundles)
            {
                var scripts = bundle.Files.Select(r => PathHelpers.GetRequireRelativePath(EntryPoint, r.FileName)).ToList();
                var paths = new RequirePaths
                                {
                                    PathList = new List<RequirePath>()
                                };
                foreach (var script in scripts)
                {
                    paths.PathList.Add(new RequirePath
                                           {
                                               Key = script,
                                               Value = PathHelpers.GetRequireRelativePath(EntryPoint, bundle.Output)
                                           });
                }

                var over = new CollectionOverride
                               {
                                   BundleId = bundle.BundleId,
                                   BundledScripts = scripts,
                                   Paths = paths
                               };
                conf.Overrides.Add(over);
            }

            return conf;
        }
Exemplo n.º 16
0
 public ExpandPathsShould()
 {
     configuration = ReadJson(new TestFileReader());
 }
Exemplo n.º 17
0
 private void ApplyOverride(ConfigurationCollection collection, CollectionOverride collOverride)
 {
     this.ApplyMapOverride(collection.Map, collOverride.Map);
     this.ApplyPathsOverride(collection.Paths, collOverride.Paths);
     this.ApplyShimOverride(collection.Shim, collOverride.Shim);
 }
Exemplo n.º 18
0
 private void MergeOverrides(ConfigurationCollection collection)
 {
     var finalOverrides = finalCollection.Overrides;
     finalOverrides.AddRange(collection.Overrides);
 }
Exemplo n.º 19
0
        private void MergeAutoBundles(ConfigurationCollection collection)
        {
            var finalAutoBundles = finalCollection.AutoBundles.Bundles;
            foreach (var autoBundle in collection.AutoBundles.Bundles)
            {
                var existing = finalAutoBundles.Where(r => r.Id == autoBundle.Id).FirstOrDefault();
                if (existing != null)
                {
                    if (!string.IsNullOrEmpty(autoBundle.OutputPath))
                    {
                        existing.OutputPath = autoBundle.OutputPath;
                    }

                    if (!string.IsNullOrEmpty(autoBundle.CompressionType))
                    {
                        existing.CompressionType = autoBundle.CompressionType;
                    }

                    foreach (var include in autoBundle.Includes)
                    {
                        existing.Includes.Add(include);
                    }

                    foreach (var exclude in autoBundle.Excludes)
                    {
                        existing.Excludes.Add(exclude);
                    }
                }
                else
                {
                    finalAutoBundles.Add(autoBundle);
                }
            }
        }
Exemplo n.º 20
0
 public ConfigValidator(ConfigurationCollection collection)
 {
     this.collection = collection;
 }
Exemplo n.º 21
0
 public ScriptProcessor(string relativeFileName, string scriptText, ConfigurationCollection configuration)
 {
     RelativeFileName = relativeFileName;
     OriginalString = scriptText;
     this.configuration = configuration;
 }
Exemplo n.º 22
0
 private void ValidateCollection(ConfigurationCollection collection, string path)
 {
     var validator = new ConfigValidator(collection);
     validator.GetErrors().ForEach(x => logger.LogError(x.Message, path));
 }