Beispiel #1
0
        public void InitializeModulesFromBundles(IEnumerable <Bundle> bundles, string requireJsScriptPath)
        {
            requireJsScriptPath = PathUtilities.AppRelative(requireJsScriptPath);
            modules.Clear();

            var scriptBundles = GetScriptBundles(bundles);

            foreach (var bundle in scriptBundles)
            {
                foreach (var asset in bundle.Assets)
                {
                    if (asset.Path.Equals(requireJsScriptPath))
                    {
                        MainBundlePath = bundle.Path;
                    }
                    else
                    {
                        modules[asset.Path] = GetModule(asset, bundle);
                    }
                }
            }

            if (MainBundlePath == null)
            {
                modules.Clear();
                throw new ArgumentException("Cannot find a bundle that contains " + requireJsScriptPath);
            }
        }
Beispiel #2
0
 protected Bundle(string applicationRelativePath)
 {
     if (applicationRelativePath == null)
     {
         throw new ArgumentNullException("applicationRelativePath");
     }
     path = PathUtilities.AppRelative(applicationRelativePath);
 }
 public IDirectory GetDirectory(string path)
 {
     path = PathUtilities.AppRelative(path);
     return(new FakeFileSystem(files.Where(f => f.Key.StartsWith(path)))
     {
         FullPath = path,
         root = root ?? this
     });
 }
        public static void AddUrlWithLocalAssets <T>(this BundleCollection bundleCollection, string url, LocalAssetSettings settings, Action <Bundle> customizeBundle = null)
            where T : Bundle
        {
            var existingBundle = bundleCollection.FirstOrDefault(b => b.ContainsPath(PathUtilities.AppRelative(settings.Path)));

            if (existingBundle != null)
            {
                bundleCollection.Remove(existingBundle);
            }

            var bundleFactory     = (IBundleFactory <T>)bundleCollection.Settings.BundleFactories[typeof(T)];
            var sourceDirectory   = bundleCollection.Settings.SourceDirectory;
            var defaultFileSearch = bundleCollection.Settings.DefaultFileSearches[typeof(T)];
            IEnumerable <IFile> files;
            BundleDescriptor    bundleDescriptor;

            if (sourceDirectory.DirectoryExists(settings.Path))
            {
                var fileSearch = settings.FileSearch ?? defaultFileSearch;
                var directory  = sourceDirectory.GetDirectory(settings.Path);
                files = fileSearch.FindFiles(directory);

                var descriptorFile = TryGetDescriptorFile(directory);
                bundleDescriptor = descriptorFile.Exists
                                       ? new BundleDescriptorReader(descriptorFile).Read()
                                       : new BundleDescriptor {
                    AssetFilenames = { "*" }
                };
            }
            else
            {
                var singleFile = sourceDirectory.GetFile(settings.Path);
                if (singleFile.Exists)
                {
                    files            = new[] { singleFile };
                    bundleDescriptor = new BundleDescriptor {
                        AssetFilenames = { "*" }
                    };
                }
                else
                {
                    throw new DirectoryNotFoundException(string.Format("File or directory not found: \"{0}\"", settings.Path));
                }
            }

            bundleDescriptor.FallbackCondition = settings.FallbackCondition;
            bundleDescriptor.ExternalUrl       = url;
            var bundle = bundleFactory.CreateBundle(settings.Path, files, bundleDescriptor);

            if (customizeBundle != null)
            {
                customizeBundle(bundle);
            }
            bundleCollection.Add(bundle);
        }
Beispiel #5
0
        public string FileUrl(string applicationRelativeFilePath)
        {
            applicationRelativeFilePath = PathUtilities.AppRelative(applicationRelativeFilePath);

            var file = settings.SourceDirectory.GetFile(applicationRelativeFilePath);

            ThrowIfFileNotFound(applicationRelativeFilePath, file);
            ThrowIfCannotRequestRawFile(applicationRelativeFilePath, file);

            return(urlGenerator.CreateRawFileUrl(applicationRelativeFilePath));
        }
Beispiel #6
0
        public void AddUrlWithLocalAssets <T>(string url, LocalAssetSettings localAssetSettings, Action <Bundle> customizeBundle = null)
            where T : Bundle
        {
            var existingBundle = bundles.FirstOrDefault(
                b => b.ContainsPath(PathUtilities.AppRelative(localAssetSettings.Path))
                );

            if (existingBundle != null)
            {
                Remove(existingBundle);
            }

            var bundleFactory   = bundleFactoryProvider.GetBundleFactory <T>();
            var sourceDirectory = settings.SourceDirectory;
            IEnumerable <IFile> files;
            BundleDescriptor    bundleDescriptor;

            if (sourceDirectory.DirectoryExists(localAssetSettings.Path))
            {
                var fileSearch = localAssetSettings.FileSearch ?? fileSearchProvider.GetFileSearch(typeof(T));
                var directory  = sourceDirectory.GetDirectory(localAssetSettings.Path);
                files = fileSearch.FindFiles(directory);

                var descriptorFile = TryGetDescriptorFile <T>(directory);
                bundleDescriptor = ReadOrCreateBundleDescriptor(descriptorFile);
            }
            else
            {
                var singleFile = sourceDirectory.GetFile(localAssetSettings.Path);
                if (singleFile.Exists)
                {
                    files            = new[] { singleFile };
                    bundleDescriptor = new BundleDescriptor {
                        AssetFilenames = { "*" }
                    };
                }
                else
                {
                    throw new DirectoryNotFoundException(string.Format("File or directory not found: \"{0}\"", localAssetSettings.Path));
                }
            }

            bundleDescriptor.FallbackCondition = localAssetSettings.FallbackCondition;
            bundleDescriptor.ExternalUrl       = url;
            var bundle = bundleFactory.CreateBundle(localAssetSettings.Path, files, bundleDescriptor);

            if (customizeBundle != null)
            {
                customizeBundle(bundle);
            }
            Add(bundle);
        }
Beispiel #7
0
        void AddAllSubDirectoryAssetsToBundle(Bundle bundle, string path, HashedSet <IFile> remainingFiles)
        {
            path = PathUtilities.AppRelative(PathUtilities.NormalizePath(path));
            var filesInSubDirectory = remainingFiles
                                      .Where(file => file.FullPath.StartsWith(path, StringComparison.OrdinalIgnoreCase))
                                      .ToArray();

            foreach (var file in filesInSubDirectory)
            {
                remainingFiles.Remove(file);
                bundle.Assets.Add(new FileAsset(file, bundle));
            }
        }
Beispiel #8
0
        T Get <T>(string path, Func <Bundle[], T> getFromMatching)
        {
            path = PathUtilities.AppRelative(path);
            var matchingBundles = bundles.Where(b => b.ContainsPath(path)).ToArray();

            if (matchingBundles.Length == 0)
            {
                throw new ArgumentException(
                          string.Format("Bundle not found with path \"{0}\".", path)
                          );
            }

            return(getFromMatching(matchingBundles));
        }
Beispiel #9
0
        public IAmdModule this[string scriptPath]
        {
            get
            {
                scriptPath = PathUtilities.AppRelative(scriptPath);

                IAmdModule module;
                if (modules.TryGetValue(scriptPath, out module))
                {
                    return(module);
                }
                throw new ArgumentException("Module not found: " + scriptPath);
            }
        }
Beispiel #10
0
        public string FileUrl(string applicationRelativeFilePath)
        {
            applicationRelativeFilePath = PathUtilities.AppRelative(applicationRelativeFilePath);

            var file = settings.SourceDirectory.GetFile(applicationRelativeFilePath);

            ThrowIfFileNotFound(applicationRelativeFilePath, file);
            ThrowIfCannotRequestRawFile(applicationRelativeFilePath, file);

            using (var stream = file.OpenRead())
            {
                var hash = stream.ComputeSHA1Hash().ToHexString();
                return(urlGenerator.CreateRawFileUrl(applicationRelativeFilePath, hash));
            }
        }
Beispiel #11
0
        Bundle GetBundle(string path, Func <Bundle> createExternalBundle)
        {
            path = PathUtilities.AppRelative(path);

            var bundle = bundleContainer.FindBundleContainingPath <Bundle>(path);

            if (bundle == null && path.IsUrl())
            {
                bundle = createExternalBundle();
                bundle.Process(settings);
            }

            if (bundle == null)
            {
                throw new ArgumentException("Cannot find an asset bundle containing the path \"" + path + "\".");
            }

            return(bundle);
        }
        IEnumerable<Bundle> GetBundles(string path, Func<Bundle> createExternalBundle)
        {
            path = PathUtilities.AppRelative(path);

            var bundles = bundleContainer.FindBundlesContainingPath(path).ToArray();
            if (bundles.Length == 0 && path.IsUrl())
            {
                var bundle = createExternalBundle();
                bundle.Process(settings);
                bundles = new[] { bundle };
            }

            if (bundles.Length == 0)
            {
                throw new ArgumentException("Cannot find an asset bundle containing the path \"" + path + "\".");
            }

            return bundles;
        }
        /// <summary>
        /// Adds a bundle of type <typeparamref name="T"/> using asset files found in the given path.
        /// </summary>
        /// <typeparam name="T">The type of bundle to create.</typeparam>
        /// <param name="bundleCollection">The bundle collection to add to.</param>
        /// <param name="applicationRelativePath">The application relative path to the bundle's asset files.</param>
        /// <param name="fileSearch">The file search used to find asset files to include in the bundle.</param>
        /// <param name="customizeBundle">The delegate used to customize the created bundle before adding it to the collection.</param>
        public static void Add <T>(this BundleCollection bundleCollection, string applicationRelativePath, IFileSearch fileSearch, Action <T> customizeBundle)
            where T : Bundle
        {
            applicationRelativePath = PathUtilities.AppRelative(applicationRelativePath);
            Trace.Source.TraceInformation(string.Format("Creating {0} for {1}", typeof(T).Name, applicationRelativePath));

            T   bundle;
            var bundleFactory = (IBundleFactory <T>)bundleCollection.Settings.BundleFactories[typeof(T)];

            var source = bundleCollection.Settings.SourceDirectory;

            if (source.DirectoryExists(applicationRelativePath))
            {
                fileSearch = fileSearch ?? bundleCollection.Settings.DefaultFileSearches[typeof(T)];
                var directory = source.GetDirectory(applicationRelativePath);
                var allFiles  = fileSearch.FindFiles(directory);
                bundle = CreateDirectoryBundle(applicationRelativePath, bundleFactory, allFiles, directory);
            }
            else
            {
                var file = source.GetFile(applicationRelativePath);
                if (file.Exists)
                {
                    bundle = CreateSingleFileBundle(applicationRelativePath, file, bundleFactory);
                }
                else
                {
                    throw new DirectoryNotFoundException(string.Format("Bundle path not found: {0}", applicationRelativePath));
                }
            }

            if (customizeBundle != null)
            {
                customizeBundle(bundle);
            }

            TraceAssetFilePaths(bundle);

            bundleCollection.Add(bundle);
        }
Beispiel #14
0
 public IEnumerable <Bundle> FindBundlesContainingPath(string path)
 {
     path = PathUtilities.AppRelative(path);
     return(bundles.Where(bundle => bundle.ContainsPath(path)));
 }
 public bool DirectoryExists(string path)
 {
     path = PathUtilities.AppRelative(path);
     return(files.Any(f => f.Key.StartsWith(path) && f.Key != path));
 }