Example #1
0
        private void RegisterBundle(HttpServerUtility server, string bundle,
            BundleType type,
            params string[] files)
        {
            if (string.IsNullOrWhiteSpace(bundle))
            throw new ArgumentNullException("bundle", "Cannot register an empty bundle");
              var compressor = GetCompressor(type);
              if (compressor.Cache.ContainsKey(bundle))
            throw new ArgumentException("Bundle already registered");
              if (files == null || files.Length == 0)
            throw new ArgumentNullException("files", "No files to register");

              var list = files.ToList();
              if (server != null)
            list = list.ConvertAll(file => server.MapPath(file));

              if (RaiseErrorIfFileDoesNotExists)
              {
            var notFound = list.ToList().FindAll(file => !File.Exists(file));
            if (notFound != null && notFound.Count > 0)
              throw new ArgumentException(string.Format("Not able to find following files: {0}{1}", Environment.NewLine, string.Join(Environment.NewLine, notFound)));
              }

              if (IsDevMode)
            compressor.Cache.Add(bundle, string.Join(Environment.NewLine, files.ToList().ConvertAll(path => string.Format(compressor.Tag, ResolvePath(path)))));
              else
              {
            StringBuilder sb = new StringBuilder();
            var yuiCompressor = compressor.GetCompressor();

            list.ToList().ForEach(file =>
            {
              if (RaiseErrorIfFileDoesNotExists || (!RaiseErrorIfFileDoesNotExists && File.Exists(file)))
            sb.Append(yuiCompressor.Compress(File.ReadAllText(file)));
            });

            if (imageCache.Count > 0)
              imageCache.Keys.ToList().ForEach(key => sb.Replace(key, imageCache[key]));

            var content = sb.ToString();
            content = yuiCompressor.Compress(content); // double compression

            var format = AddHash ? "{0}." + CreateHash(content) + ".{1}" : "{0}.{1}";
            var path = Path.Combine(server.MapPath(compressor.Folder), string.Format(format, bundle, compressor.Extension));
            WriteContent(path, content);

            path = ResolvePath(server.RelativePath(path));
            compressor.Cache.Add(bundle, string.Format(compressor.Tag, path));
              }
        }