Esempio n. 1
0
        /// <summary>
        /// This returns the appropriate html string to include in the web page such that the requested bundle will be loaded.
        /// </summary>
        /// <param name="bundleName"></param>
        /// <param name="cssOrJs"></param>
        /// <param name="inDevelopment">This controls whether we supply individual files or development mode 
        /// or single minified files/CDNs in non-development mode</param>
        /// <param name="getContentUrl">method to get url of content</param>
        /// <returns></returns>
        public string CalculateHtmlIncludes(string bundleName, CssOrJs cssOrJs, bool inDevelopment, Func<string, string> getContentUrl)
        {
            var settingFilePath = Path.Combine(_jsonDataDir, config.BundlesFileName);
            var reader = new ReadBundleFile(settingFilePath);

            var fileTypeInfo = config.GetFileTypeData(cssOrJs);

            if (inDevelopment)
            {
                var sb = new StringBuilder();
                //we send the individual files as found in the bundle json file
                foreach (var relFilePath in _searcher.UnpackBundle(reader.GetBundleDebugFiles(bundleName)))
                {
                    sb.AppendLine(fileTypeInfo.DebugHtmlFormatString
                        .Replace(FileTypeConfigInfo.FileUrlParam, getContentUrl(relFilePath)));
                }
                return sb.ToString();
            }
            
            //We are in nonDebug, i.e. production mode
            var cdnLinks = reader.GetBundleCdnInfo(bundleName);

            return cdnLinks.Any() 
                ? FormCdnIncludes(cdnLinks, bundleName, cssOrJs, fileTypeInfo, getContentUrl) 
                : FormSingleMinifiedFileInclude(bundleName, cssOrJs, fileTypeInfo, getContentUrl);
        }
        public void TestCreateReadBundleFileCdnOk()
        {
            //SETUP

            //ATTEMPT
            var reader = new ReadBundleFile(TestFileHelpers.GetTestFileFilePath("BowerBundles02*.json"));

            //VERIFY
            CollectionAssert.AreEqual(new[] { "mainCss", "standardLibsJs", "appLibsJs", "jqueryval" }, reader.BundleNames);
        }
        public void TestCreateReadBundleFileNonCdnOk()
        {
            //SETUP

            //ATTEMPT
            var reader = new ReadBundleFile(TestFileHelpers.GetTestFileFilePath("BowerBundles01*.json"));

            //VERIFY
            CollectionAssert.AreEqual(new[] {"mainCss", "standardLibsJs", "appLibsJs", "jqueryval"}, reader.BundleNames);
        }
Esempio n. 4
0
 /// <summary>
 /// This requires the path to the directory of the project where all the files are stored
 /// It uses the data directory to find the BowerBundles.json and the bundlerForBower.json user config file
 /// </summary>
 /// <param name="mvcAppPath">The absolute path to the project you want to test.</param>
 /// <param name="appDataDir">optional: if App_Data directory is not at the top level then you need to supply
 /// AppDomain.CurrentDomain.GetData("DataDirectory").ToString() or an equivalent absolute directory path</param>
 /// <param name="checkForConcatFile">optional: by default it checks the concat file, 
 /// but if you go straight to the minified file then set this to false</param>
 public CheckBundles(string mvcAppPath, string appDataDir = null, bool checkForConcatFile = true)
 {
     _mvcAppPath = mvcAppPath;
     _checkForConcatFile = checkForConcatFile;
     var appDataPath = appDataDir ?? Path.Combine(_mvcAppPath, DefaultAppDataDirName);
     _config = ConfigInfo.ReadConfig(Path.Combine(appDataPath, BundlerForBower.B4BConfigFileName));
     _bundleFilePath = Path.Combine(appDataPath, _config.BundlesFileName);
     _reader = new ReadBundleFile(_bundleFilePath);
     _searcher = new RelPathSearcher(s => Path.Combine(_mvcAppPath, s));
 }
        public void TestGetBundleCdnInfoNonCdnOk()
        {
            //SETUP
            var reader = new ReadBundleFile(TestFileHelpers.GetTestFileFilePath("BowerBundles01*.json"));

            //ATTEMPT
            var cdns = reader.GetBundleCdnInfo("standardLibsJs");

            //VERIFY
            cdns.Count.ShouldEqual(0);
        }
Esempio n. 6
0
        /// <summary>
        /// This requires the path to the directory of the project where all the files are stored
        /// It uses the data directory to find the BowerBundles.json and the bundlerForBower.json user config file
        /// </summary>
        /// <param name="mvcAppPath">The absolute path to the project you want to test.</param>
        /// <param name="appDataDir">optional: if App_Data directory is not at the top level then you need to supply
        /// AppDomain.CurrentDomain.GetData("DataDirectory").ToString() or an equivalent absolute directory path</param>
        /// <param name="checkForConcatFile">optional: by default it checks the concat file,
        /// but if you go straight to the minified file then set this to false</param>
        public CheckBundles(string mvcAppPath, string appDataDir = null, bool checkForConcatFile = true)
        {
            _mvcAppPath         = mvcAppPath;
            _checkForConcatFile = checkForConcatFile;
            var appDataPath = appDataDir ?? Path.Combine(_mvcAppPath, DefaultAppDataDirName);

            _config         = ConfigInfo.ReadConfig(Path.Combine(appDataPath, BundlerForBower.B4BConfigFileName));
            _bundleFilePath = Path.Combine(appDataPath, _config.BundlesFileName);
            _reader         = new ReadBundleFile(_bundleFilePath);
            _searcher       = new RelPathSearcher(s => Path.Combine(_mvcAppPath, s));
        }
        public void TestGetBundleDebugFilesCndFileBundleListOk(string bundleName, params string[] expectedfiles)
        {
            //SETUP
            var reader = new ReadBundleFile(TestFileHelpers.GetTestFileFilePath("BowerBundles01*.json"));

            //ATTEMPT
            var files = reader.GetBundleDebugFiles(bundleName, "");

            //VERIFY
            CollectionAssert.AreEqual(expectedfiles, files);
        }
        public void TestGetBundleCdnInfoNonCdnOk()
        {
            //SETUP
            var reader = new ReadBundleFile(TestFileHelpers.GetTestFileFilePath("BowerBundles01*.json"));

            //ATTEMPT
            var cdns = reader.GetBundleCdnInfo("standardLibsJs");

            //VERIFY
            cdns.Count.ShouldEqual(0);
        }
        public void TestGetBundleDebugFilesBadCndFileBundleSingleOk()
        {
            //SETUP
            var reader = new ReadBundleFile(TestFileHelpers.GetTestFileFilePath("BowerBundles03*.json"));

            //ATTEMPT
            var ex = Assert.Throws <InvalidOperationException>(() => reader.GetBundleDebugFiles("missingDevelopmentJs"));

            //VERIFY
            ex.Message.ShouldEqual("The CDN bundle missingDevelopmentJs, array element 0, is missing a property called 'development'.");
        }
        public void TestGetBundleDebugFilesNonCndFileBundleSingleOk(string bundleName, string expectedfile)
        {
            //SETUP
            var reader = new ReadBundleFile(TestFileHelpers.GetTestFileFilePath("BowerBundles01*.json"));

            //ATTEMPT
            var files = reader.GetBundleDebugFiles(bundleName, "").ToList();

            //VERIFY
            files.Count().ShouldEqual(1);
            files[0].ShouldEqual(expectedfile);
        }
        public void TestGetBundleCdnInfoCdnOk()
        {
            //SETUP
            var reader = new ReadBundleFile(TestFileHelpers.GetTestFileFilePath("BowerBundles02*.json"));

            //ATTEMPT
            var cdns = reader.GetBundleCdnInfo("standardLibsJs").ToList();

            //VERIFY
            cdns.Count.ShouldEqual(2);
            var i = 0;
            cdns[i++].ToString().ShouldEqual("Development: jquery.js, Production: jquery.min.js, "+
                "cdnUrl: https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.4.min.js, cdnSuccessTest: window.jQuery");
            cdns[i++].ToString().ShouldEqual("Development: bootstrap.js, Production: bootstrap.min.js, "+
                "cdnUrl: https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.5/bootstrap.min.js, cdnSuccessTest: window.jQuery && window.jQuery.fn && window.jQuery.fn.modal");
            //foreach (var cdnInfo in cdns)
            //{
            //    Console.WriteLine("cdns[i++].ToString().ShouldEqual(\"{0}\");", cdnInfo);
            //}
        }
        public void TestGetBundleCdnInfoCdnOk()
        {
            //SETUP
            var reader = new ReadBundleFile(TestFileHelpers.GetTestFileFilePath("BowerBundles02*.json"));

            //ATTEMPT
            var cdns = reader.GetBundleCdnInfo("standardLibsJs").ToList();

            //VERIFY
            cdns.Count.ShouldEqual(2);
            var i = 0;

            cdns[i++].ToString().ShouldEqual("Development: jquery.js, Production: jquery.min.js, " +
                                             "cdnUrl: https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.4.min.js, cdnSuccessTest: window.jQuery");
            cdns[i++].ToString().ShouldEqual("Development: bootstrap.js, Production: bootstrap.min.js, " +
                                             "cdnUrl: https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.5/bootstrap.min.js, cdnSuccessTest: window.jQuery && window.jQuery.fn && window.jQuery.fn.modal");
            //foreach (var cdnInfo in cdns)
            //{
            //    Console.WriteLine("cdns[i++].ToString().ShouldEqual(\"{0}\");", cdnInfo);
            //}
        }
Esempio n. 13
0
        /// <summary>
        /// This returns the appropriate html string to include in the web page such that the requested bundle will be loaded.
        /// </summary>
        /// <param name="bundleName"></param>
        /// <param name="cssOrJs"></param>
        /// <param name="inDevelopment">This controls whether we supply individual files or development mode 
        /// or single minified files/CDNs in non-development mode</param>
        /// <param name="getContentUrl">method to get url of content</param>
        /// <returns></returns>
        public string CalculateHtmlIncludes(string bundleName, CssOrJs cssOrJs, bool inDevelopment, Func<string, string> getContentUrl)
        {
            var settingFilePath = Path.Combine(_jsonDataDir, config.BundlesFileName);
            var reader = new ReadBundleFile(settingFilePath);

            var fileTypeInfo = config.GetFileTypeData(cssOrJs);

            if (inDevelopment)
            {
                var sb = new StringBuilder();
                //we send the individual files as found in the bundle json file
                foreach (var relFilePath in _searcher.UnpackBundle(reader.GetBundleDebugFiles(bundleName)))
                {
                    sb.AppendLine(fileTypeInfo.DebugHtmlFormatString
                        .Replace(FileTypeConfigInfo.FileUrlParam, getContentUrl(relFilePath)));
                }
                return sb.ToString();
            }

            //We are in nonDebug, i.e. production mode
            var cdnLinks = reader.GetBundleCdnInfo(bundleName);

            return cdnLinks.Any()
                ? FormCdnIncludes(cdnLinks, bundleName, cssOrJs, fileTypeInfo, getContentUrl)
                : FormSingleMinifiedFileInclude(bundleName, cssOrJs, fileTypeInfo, getContentUrl);
        }
        public void TestGetBundleDebugFilesBadCndFileBundleSingleOk()
        {
            //SETUP
            var reader = new ReadBundleFile(TestFileHelpers.GetTestFileFilePath("BowerBundles03*.json"));

            //ATTEMPT
            var ex = Assert.Throws<InvalidOperationException>( () => reader.GetBundleDebugFiles("missingDevelopmentJs"));

            //VERIFY
            ex.Message.ShouldEqual("The CDN bundle missingDevelopmentJs, array element 0, is missing a property called 'development'.");
        }
        public void TestGetBundleDebugFilesNonCndFileBundleSingleOk(string bundleName, string expectedfile)
        {
            //SETUP
            var reader = new ReadBundleFile(TestFileHelpers.GetTestFileFilePath("BowerBundles01*.json"));

            //ATTEMPT
            var files = reader.GetBundleDebugFiles(bundleName, "").ToList();

            //VERIFY
            files.Count().ShouldEqual(1);
            files[0].ShouldEqual(expectedfile);
        }
        public void TestGetBundleDebugFilesNonCndFileBundleListOk(string bundleName, params string [] expectedfiles)
        {
            //SETUP
            var reader = new ReadBundleFile(TestFileHelpers.GetTestFileFilePath("BowerBundles01*.json"));

            //ATTEMPT
            var files = reader.GetBundleDebugFiles(bundleName, "");

            //VERIFY
            CollectionAssert.AreEqual(expectedfiles, files);
        }