예제 #1
0
        public bool WriteToStream(BaseCompositeFileProcessingProvider provider, StreamWriter sw, FileInfo fi, ClientDependencyType type, string origUrl, HttpContextBase http)
        {
            try
            {
                //if it is a file based dependency then read it
                var fileContents = File.ReadAllText(fi.FullName, Encoding.UTF8); //read as utf 8

                //for our custom file reader to work we just need to put the origUrl into the httpcontext items so
                //we can retreive it in the reader to then figure out the 'real' requested path.
                http.Items["Cdf_LessWriter_origUrl"] = origUrl;
                //get the default less config
                var config = DotlessConfiguration.GetDefaultWeb();
                //set the file reader to our custom file reader
                config.LessSource = typeof(CdfFileReader);
                //disable cache for this engine since we are already caching our own, plus enabling this will cause errors because
                // the import paths aren't resolved properly.
                config.CacheEnabled = false;
                //get the engine based on the custom config with our custom file reader
                var lessEngine = LessWeb.GetEngine(config);

                var output = lessEngine.TransformToCss(fileContents, origUrl);

                DefaultFileWriter.WriteContentToStream(provider, sw, output, type, http, origUrl);

                return(true);
            }
            catch (Exception ex)
            {
                ClientDependencySettings.Instance.Logger.Error(string.Format("Could not write file {0} contents to stream. EXCEPTION: {1}", fi.FullName, ex.Message), ex);
                return(false);
            }
        }
예제 #2
0
        internal static string Process(ref IEnumerable <BundleFile> files)
        {
            DotlessConfiguration lessConfig = new WebConfigConfigurationLoader().GetConfiguration();

            if (!lessConfig.LessSource.GetInterfaces().Contains(typeof(IFileReaderWithResolver)))
            {
                lessConfig.LessSource = typeof(LessVirtualFileReader);
            }

            // system.Web.Optimization cache is used instead
            lessConfig.CacheEnabled = false;

            var content = new StringBuilder();

            var targetFiles = new List <BundleFile>();

            foreach (BundleFile bundleFile in files)
            {
                // initialize the less engine once for each file.
                // this is to prevent leaking state between files
                ILessEngine lessEngine           = LessWeb.GetEngine(lessConfig);
                LessEngine  underlyingLessEngine = lessEngine.ResolveLessEngine();
                Parser      lessParser           = underlyingLessEngine.Parser;

                targetFiles.Add(bundleFile);
                string filePath = bundleFile.IncludedVirtualPath;
                filePath = filePath.Replace('\\', '/');
                filePath = VirtualPathUtility.ToAppRelative(filePath);

                lessParser.SetCurrentFilePath(filePath);
                string source    = bundleFile.ApplyTransforms();
                string extension = VirtualPathUtility.GetExtension(filePath);

                // if plain CSS file, do not transform LESS
                if (lessConfig.ImportAllFilesAsLess ||
                    ".less".Equals(extension, StringComparison.InvariantCultureIgnoreCase) ||
                    ".less.css".Equals(extension, StringComparison.InvariantCultureIgnoreCase))
                {
                    string lessOutput = lessEngine.TransformToCss(source, filePath);

                    // pass the transformation result if successful
                    if (lessEngine.LastTransformationSuccessful)
                    {
                        source = lessOutput;
                    }
                    else
                    {
                        // otherwise write out error message.
                        // the transformation error is logged in LessEngine.TransformToCss
                        if (lessConfig.Debug)
                        {
                            content.AppendLine(string.Format(
                                                   "/* Error occurred in LESS transformation of the file: {0}. Please see details in the dotless log */",
                                                   bundleFile.IncludedVirtualPath));
                        }
                        continue;
                    }
                }

                source = ConvertUrlsToAbsolute(bundleFile.IncludedVirtualPath, source);

                content.AppendLine(source);

                BundleFile[] fileDependencies = GetFileDependencies(underlyingLessEngine).ToArray();
                targetFiles.AddRange(fileDependencies);

                DependencyCache.SaveFileDependencies(filePath, fileDependencies.Select(file => file.IncludedVirtualPath).ToArray());
            }

            // include imports in bundle files to register cache dependencies
            files = BundleTable.EnableOptimizations ? targetFiles.Distinct() : targetFiles;

            return(content.ToString());
        }