public BundleConfigurer(Bundle bundle, IFileProvider sourceFileProvider, IServiceProvider appServices)
        {
            if (bundle == null)
            {
                throw new ArgumentNullException(nameof(bundle));
            }

            if (sourceFileProvider == null)
            {
                throw new ArgumentNullException(nameof(sourceFileProvider));
            }

            if (appServices == null)
            {
                throw new ArgumentNullException(nameof(appServices));
            }

            Bundle      = bundle;
            AppServices = appServices;

            _bundleSource = new Lazy <FileBundleSource>(() =>
            {
                var result = new FileBundleSource(sourceFileProvider, bundle);
                AddSource(result);
                return(result);
            }, isThreadSafe: false);
        }
        public FileBundleSourceModel(FileBundleSource bundleSource)
        {
            _fileProvider =
                bundleSource.FileProvider ??
                throw ErrorHelper.PropertyNotSpecifed(nameof(FileBundleSource), nameof(FileBundleSource.FileProvider));
            _caseSensitiveFilePaths = bundleSource.CaseSensitiveFilePaths;

            _fileFilters = bundleSource.FileFilters;

            ILookup <bool, FileBundleSourceItem> lookup = bundleSource.Items.ToLookup(it => it.Exclude);

            var excludePatterns = lookup[true].Select(bsi => bsi.Pattern).ToArray();

            _includes = lookup[false].Select(bsi => CreateInclude(bsi, excludePatterns)).ToArray();
        }
Exemple #3
0
        public FileBundleSourceModel(FileBundleSource bundleSource, bool enableChangeDetection)
        {
            _fileProvider =
                bundleSource.FileProvider ??
                throw ErrorHelper.PropertyNotSpecifed(nameof(FileBundleSource), nameof(FileBundleSource.FileProvider));

            _pathComparisonType = bundleSource.PathComparisonType;

            _fileFilters = bundleSource.FileFilters;

            var lookup = bundleSource.Items.ToLookup(it => it.Exclude);

            var excludePatterns = lookup[true].Select(bsi => bsi.Pattern).ToArray();

            // unfortunately file provider doesn't support exclude patterns and supports only a single include pattern
            // TODO: better solution?
            _includes = lookup[false].Select(bsi => CreateInclude(bsi, excludePatterns, enableChangeDetection)).ToArray();
        }
        public void Load(BundleCollection bundles, TextReader reader, ConfigFilePathMapper pathMapper)
        {
            if (bundles == null)
            {
                throw new ArgumentNullException(nameof(bundles));
            }

            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            if (bundles.SourceFileProvider == null)
            {
                throw ErrorHelper.PropertyCannotBeNull(nameof(bundles), nameof(bundles.SourceFileProvider));
            }

            if (pathMapper == null)
            {
                pathMapper = DefaultMapPath;
            }

            var serializer = JsonSerializer.CreateDefault();
            var items      = SerializationHelper.Deserialize <BundleData[]>(reader);

            var n = items.Length;

            for (var i = 0; i < n; i++)
            {
                var item = items[i];

                var outputPath = pathMapper(UrlUtils.NormalizePath(item.OutputFileName), bundles.PathPrefix, output: true);
                var extension  = Path.GetExtension(outputPath);

                var outputConfig = _extensionMappers.Select(em => em.MapOutput(extension)).FirstOrDefault(cfg => cfg != null);
                if (outputConfig == null)
                {
                    throw ErrorHelper.ExtensionNotRecognized(extension);
                }

                var bundle       = new Bundle(outputPath, outputConfig);
                var bundleSource = new FileBundleSource(bundles.SourceFileProvider, bundle);

                bundle.Transforms = outputConfig.ConfigurationHelper.SetDefaultTransforms(bundle.Transforms);

                if (item.Minify.Any(kvp => "enabled".Equals(kvp.Key, StringComparison.OrdinalIgnoreCase) && kvp.Value is bool boolValue && boolValue))
                {
                    bundle.Transforms = outputConfig.ConfigurationHelper.EnableMinification(bundle.Transforms);
                }

                var m = item.InputFiles.Count;
                for (var j = 0; j < m; j++)
                {
                    var inputFile = item.InputFiles[j];

                    var inputPath = pathMapper(UrlUtils.NormalizePath(inputFile), PathString.Empty, output: false);
                    extension = Path.GetExtension(inputPath);

                    var inputConfig = _extensionMappers.Select(em => em.MapInput(extension)).FirstOrDefault(cfg => cfg != null);
                    if (inputConfig == null)
                    {
                        throw ErrorHelper.ExtensionNotRecognized(extension);
                    }

                    var bundleSourceItem = new FileBundleSourceItem(inputPath, bundleSource);

                    bundleSourceItem.ItemTransforms = inputConfig.ConfigurationHelper.SetDefaultItemTransforms(bundleSourceItem.ItemTransforms);

                    bundleSource.Items.Add(bundleSourceItem);
                }

                bundle.Sources.Add(bundleSource);
                bundles.Add(bundle);
            }
        }
        public void Load(BundleCollection bundles, TextReader reader, ConfigFilePathMapper pathMapper)
        {
            if (bundles == null)
            {
                throw new ArgumentNullException(nameof(bundles));
            }

            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            if (bundles.SourceFileProvider == null)
            {
                throw ErrorHelper.PropertyCannotBeNull(nameof(bundles), nameof(bundles.SourceFileProvider));
            }

            if (pathMapper == null)
            {
                pathMapper = DefaultMapPath;
            }

            BundleData[] items = SerializationHelper.Deserialize <BundleData[]>(reader);

            var n = items.Length;

            for (var i = 0; i < n; i++)
            {
                BundleData item = items[i];

                PathString outputPath = pathMapper(UrlUtils.NormalizePath(item.OutputFileName), bundles.PathPrefix, output: true);
                if (!outputPath.HasValue)
                {
                    throw ErrorHelper.PathMappingNotPossible(item.OutputFileName, nameof(pathMapper));
                }

                var extension = Path.GetExtension(outputPath);

                IBundleConfiguration outputConfig = _extensionMappers.Select(em => em.MapOutput(extension)).FirstOrDefault(cfg => cfg != null);
                if (outputConfig == null)
                {
                    throw ErrorHelper.ExtensionNotRecognized(extension);
                }

                var bundle       = new Bundle(outputPath, outputConfig);
                var bundleSource = new FileBundleSource(bundles.SourceFileProvider, bundles.CaseSensitiveSourceFilePaths, bundle);

                bundle.Transforms = outputConfig.ConfigurationHelper.SetDefaultTransforms(bundle.Transforms);

                if (item.Minify == null ||
                    !item.Minify.Any(kvp => "enabled".Equals(kvp.Key, StringComparison.OrdinalIgnoreCase) ||
                                     kvp.Value is bool boolValue && boolValue))
                {
                    bundle.Transforms = outputConfig.ConfigurationHelper.EnableMinification(bundle.Transforms);
                }

                if (item.InputFiles != null)
                {
                    for (int j = 0, m = item.InputFiles.Count; j < m; j++)
                    {
                        var inputFile = item.InputFiles[j];

                        bool exclude;
                        if (inputFile.StartsWith("!"))
                        {
                            inputFile = inputFile.Substring(1);
                            exclude   = true;
                        }
                        else
                        {
                            exclude = false;
                        }

                        PathString inputPath = pathMapper(UrlUtils.NormalizePath(inputFile), PathString.Empty, output: false);
                        extension = Path.GetExtension(inputPath);

                        IBundleConfiguration inputConfig = _extensionMappers.Select(em => em.MapInput(extension)).FirstOrDefault(cfg => cfg != null);
                        if (inputConfig == null)
                        {
                            throw ErrorHelper.ExtensionNotRecognized(extension);
                        }

                        var bundleSourceItem = new FileBundleSourceItem(inputPath, bundleSource)
                        {
                            Exclude = exclude
                        };

                        bundleSourceItem.ItemTransforms = inputConfig.ConfigurationHelper.SetDefaultItemTransforms(bundleSourceItem.ItemTransforms);

                        bundleSource.Items.Add(bundleSourceItem);
                    }
                }

                bundle.Sources.Add(bundleSource);
                bundles.Add(bundle);
            }
        }