/*
         *      public void UpdateBundle(string configuration, string name, string content)
         *      {
         *          var config = getConfiguration(configuration);
         *          var bundle = config.Bundles.FirstOrDefault(b=>b.Name.ToLower()==name.ToLower());
         *          if (bundle != null)
         *              bundle.Content = content;
         *          m_Persister.Save(config);
         *      }
         *
         */
        public void CreateOrUpdateBundle(string configuration, string name, string content)
        {
            var config = getConfiguration(configuration);
            var parts  = name.Split(new[] { '.' });

            BundleCollectionBase parentBundle = null;
            int i;

            for (i = parts.Length; i > 0 && parentBundle == null; i--)
            {
                var parent = string.Join(".", Enumerable.Range(0, i).Select(n => parts[n]));
                parentBundle = config.Bundles.FirstOrDefault(b => b.Name.ToLower() == parent.ToLower());
            }

            if (parentBundle == null)
            {
                parentBundle = config;
                for (int j = 0; j < parts.Length; j++)
                {
                    parentBundle = parentBundle.CreateBundle(parts[j]);
                }
            }
            else
            {
                for (int j = i + 1; j < parts.Length; j++)
                {
                    parentBundle = parentBundle.CreateBundle(parts[j]);
                }
            }

            (parentBundle as Bundle).Content = content;
            m_Persister.Save(config);
        }
        private static bool createBundles(BundleCollectionBase collection, IEnumerator enumerator, bool isRootLevel = false)
        {
            while (true)
            {
                var file = enumerator.Current.CastByExample(new { path = "", name = "" });
                if (!isRootLevel && !file.name.StartsWith(collection.Name + '.'))
                {
                    return(true);
                }


                var name = isRootLevel ? file.name : file.name.Substring(collection.Name.Length + 1);
                if (name.Contains('.'))
                {
                    //Implicitly defined bundle. e.g. bundl1.bundle2 file exists but bundle1 file does not. bundle1 should be created but with empty content
                    var intermidiate = collection.CreateBundle(name.Split('.').First());
                    if (!createBundles(intermidiate, enumerator))
                    {
                        return(false);
                    }
                }
                else
                {
                    Bundle bundle;
                    try
                    {
                        bundle = collection.CreateBundle(name, File.ReadAllText(file.path));
                    }
                    catch (Exception e)
                    {
                        throw new ConfigurationErrorsException(string.Format("Failed to parse bundle {0}.\r\n File path {1}", file.name, file.path), e);
                    }

                    if (!enumerator.MoveNext())
                    {
                        return(false);
                    }
                    if (!createBundles(bundle, enumerator))
                    {
                        return(false);
                    }
                }
            }
        }