示例#1
0
        /// <summary>
        ///     Gets the file dependencies (@imports) of the LESS file being parsed.
        /// </summary>
        /// <param name="lessEngine">The LESS engine.</param>
        /// <returns>An array of file references to the dependent file references.</returns>
        private static IEnumerable <BundleFile> GetFileDependencies(LessEngine lessEngine)
        {
            ImportedFilePathResolver pathResolver = lessEngine.Parser.GetPathResolver();
            VirtualPathProvider      vpp          = BundleTable.VirtualPathProvider;

            foreach (string resolvedVirtualPath in lessEngine.GetImports().Select(pathResolver.GetFullPath))
            {
                // the file was successfully imported, therefore no need to check before vpp.GetFile
                yield return(new BundleFile(resolvedVirtualPath, vpp.GetFile(resolvedVirtualPath)));
            }

            lessEngine.ResetImports();
        }
示例#2
0
        public ParseResult ParseFile(string appRelativePath)
        {
            var physicalPath = GetFullPath(appRelativePath);

            using (new CurrentDirectoryWrapper(Path.GetDirectoryName(physicalPath)))
            {
                var engine = new LessEngine();

                return
                    (new ParseResult
                {
                    // Note, the order here is important
                    Content = engine.TransformToCss(fileReader.GetFileContents(appRelativePath), appRelativePath),
                    FileDependencies = new[] { physicalPath }.Concat(engine.GetImports().Select(x => GetImportPath(appRelativePath, x)))
                });
            }
        }
示例#3
0
        /// <summary>
        /// Transforms the content of the given string from Less into CSS.
        /// </summary>
        /// <param name="input">The input string to transform.</param>
        /// <param name="path">The path to the given input string to transform.</param>
        /// <param name="cruncher">The cruncher that is running the transform.</param>
        /// <returns>The transformed string.</returns>
        public string Transform(string input, string path, CruncherBase cruncher)
        {
            // The standard engine returns a FileNotFoundExecption so I've rolled my own path resolver.
            Parser parser = new Parser();
            DotLessPathResolver dotLessPathResolver = new DotLessPathResolver(path);
            FileReader          fileReader          = new FileReader(dotLessPathResolver);

            parser.Importer = new Importer(fileReader);
            ILessEngine lessEngine = new LessEngine(parser);

            try
            {
                string result = lessEngine.TransformToCss(input, path);

                if (cruncher.Options.CacheFiles)
                {
                    // Add each import as a file dependency so that the cache will clean itself.
                    IEnumerable <string> imports    = lessEngine.GetImports();
                    IList <string>       enumerable = imports as IList <string> ?? imports.ToList();

                    if (enumerable.Any())
                    {
                        foreach (string import in enumerable)
                        {
                            if (!import.Contains("://"))
                            {
                                string filePath =
                                    HostingEnvironment.MapPath(
                                        VirtualPathUtility.Combine(dotLessPathResolver.CurrentFileDirectory, import));

                                cruncher.AddFileMonitor(filePath, "not empty");
                            }
                        }
                    }
                }

                return(result);
            }
            catch (Exception ex)
            {
                throw new LessCompilingException(ex.Message, ex.InnerException);
            }
        }