private string Transform(string less, TestPath path)
        {
            var engine = new EngineFactory().GetEngine();
            engine.CurrentDirectory = path.Directory;

            return engine.TransformToCss(less, path.FileName);
        }
Beispiel #2
0
        public void Process(ToolContext context)
        {
            foreach (WorkfileContext workContext in context.Workfiles)
            {
                Directory.SetCurrentDirectory(workContext.OriginalSource.DirectoryName);

                if (Path.GetExtension(workContext.Workfile.Name).ToLower() == ".less")
                {
                    string compressed = null;
                    using (var reader = new StreamReader(workContext.Workfile.FullName, true))
                    {
                        var engine = new EngineFactory(new DotlessConfiguration()
                        {
                            CacheEnabled = false,
                            MinifyOutput = false,
                            Web = false,
                        }).GetEngine();

                        compressed = engine.TransformToCss(reader.ReadToEnd(), null);
                        IEnumerable<string> l = engine.GetImports();
                    }

                    string newFileName = Path.Combine(workContext.Workfile.DirectoryName, Path.GetFileNameWithoutExtension(workContext.Workfile.Name)) + ".css";
                    workContext.Workfile.MoveTo(newFileName);

                    using (var writer = new StreamWriter(newFileName, false))
                    {
                        writer.Write(compressed);
                    }
                }
            }
        }
        public void ProcessRequest(HttpContextBase context)
        {
            var physicalPath = context.Server.MapPath(context.Request.Path);
            var querystring = context.Request.QueryString.ToString();
            var response = context.Response;

            try
            {
                var source = fileWrapper.GetFileString(physicalPath);
                var engine = new EngineFactory(new DotlessConfiguration
                                                   {
                                                       CacheEnabled = false,
                                                       Logger = typeof (LessLogger),
                                                       Web = HttpContext.Current != null
                                                   }
                    ).GetEngine();
                var lessEngine = (LessEngine) ((ParameterDecorator) engine).Underlying;
                ((LessLogger)lessEngine.Logger).Response = response;
                var result = engine.TransformToCss(source, physicalPath + (string.IsNullOrWhiteSpace(querystring) ? string.Empty : "?" + querystring));
                response.ContentType = "text/css";
                if(!string.IsNullOrEmpty(result))
                    response.Write(result);
            }
            catch (System.IO.FileNotFoundException ex)
            {
                response.StatusCode = 404;
                response.Write("/* File Not Found while parsing: " + ex.Message + " */");
            }
            catch (System.Exception ex)
            {
                response.StatusCode = 500;
                response.Write("/* Error in less parsing: " + ex.Message + " */");
            }
        }
Beispiel #4
0
        public IEnumerable<IDocument> Execute(IReadOnlyList<IDocument> inputs, IExecutionContext context)
        {
            DotlessConfiguration config = DotlessConfiguration.GetDefault();
            config.RootPath = context.InputFolder;
            config.Logger = typeof (LessLogger);

            EngineFactory engineFactory = new EngineFactory(config);
            ILessEngine engine = engineFactory.GetEngine();

            // TODO: Get rid of RefelectionMagic and this ugly hack as soon as dotless gets better external DI support
            engine.AsDynamic().Underlying.Cache = new LessCache(context.ExecutionCache);

            return inputs.Select(x =>
            {
                string path = x.Get<string>("SourceFilePath", null);
                string fileName = null;
                if (path != null)
                {
                    engine.CurrentDirectory = Path.GetDirectoryName(path);
                    fileName = Path.GetFileName(path);
                }
                else
                {
                    engine.CurrentDirectory = context.InputFolder;
                    fileName = Path.GetRandomFileName();
                }
                using (context.Trace.WithIndent().Verbose("Processing Less for {0}", path ?? "inline content"))
                {
                    string content = engine.TransformToCss(x.Content, fileName);
                    return x.Clone(content);
                }
            });
        }
Beispiel #5
0
        public IEnumerable<IDocument> Execute(IReadOnlyList<IDocument> inputs, IExecutionContext context)
        {
            DotlessConfiguration config = DotlessConfiguration.GetDefault();
            config.Logger = typeof (LessLogger);
            EngineFactory engineFactory = new EngineFactory(config);
            FileSystemReader fileSystemReader = new FileSystemReader(context.FileSystem);
            
            return inputs.AsParallel().Select(input =>
            {
                Trace.Verbose("Processing Less for {0}", input.SourceString());
                ILessEngine engine = engineFactory.GetEngine();
                
                // TODO: Get rid of RefelectionMagic and this ugly hack as soon as dotless gets better external DI support
                engine.AsDynamic().Underlying.Cache = new LessCache(context.ExecutionCache);
                engine.AsDynamic().Underlying.Underlying.Parser.Importer.FileReader = fileSystemReader;

                FilePath path = input.FilePath(Keys.RelativeFilePath);
                string fileName = null;
                if (path != null)
                {
                    engine.CurrentDirectory = path.Directory.FullPath;
                    fileName = path.FileName.FullPath;
                }
                else
                {
                    engine.CurrentDirectory = string.Empty;
                    fileName = Path.GetRandomFileName();
                }
                string content = engine.TransformToCss(input.Content, fileName);
                return context.GetDocument(input, content);
            });
        }
 public string ProcessFile(string content, AssetType assetType, string physicalFilePath, string contentPath)
 {
     if (assetType == AssetType.CSS && physicalFilePath.ToUpperInvariant().EndsWith(".LESS"))
     {
         var factory = new EngineFactory();
         var engine = factory.GetEngine(DotlessConfiguration.Default);
         return engine.TransformToCss(new LessSourceObject() { Content = content });
     }
     return content;
 }
Beispiel #7
0
        public string ProcessCss(FileInfo file)
        {
            var content = fileSystem.File.ReadAllText(file.FullName);

            //todo resolve imports
            //_fileSystem.Directory.SetCurrentDirectory("");
            var engineFactory = new EngineFactory();
            engineFactory.Configuration.MinifyOutput = true;
            var engine = engineFactory.GetEngine();

            return engine.TransformToCss(content, file.FullName);
        }
Beispiel #8
0
 public LessHandler(IFileWrapper fileWrapper)
 {
     this.fileWrapper = fileWrapper;
     var config = new DotlessConfiguration
     {
         CacheEnabled = false,
         Logger = typeof(LessLogger),
         Web = HttpContext.Current != null,
     };
     var engineFactory = new EngineFactory(config);
     if (HttpContext.Current == null)
         engine = engineFactory.GetEngine();
     else
         engine = engineFactory.GetEngine(new AspNetContainerFactory());
 }
Beispiel #9
0
        private string GenerateCss(string import, BundleContext context)
        {
            var configuration = new WebConfigConfigurationLoader().GetConfiguration();
            configuration.DisableParameters = true;

            var logger = new InMemoryLogger(configuration.LogLevel);
            var engine = new EngineFactory(configuration).GetEngine(CreateContainer(context, logger));
            var cssOutput = engine.TransformToCss(import, context.BundleVirtualPath);
            if (!engine.LastTransformationSuccessful)
            {
                return logger.GetOutput();
            }

            return cssOutput;
        }
Beispiel #10
0
        public void Minify()
        {
            var bundled = fileSystem.BundleFiles(files);

            //todo resolve imports
            //_fileSystem.Directory.SetCurrentDirectory("");

            //minify
            var engineFactory = new EngineFactory();
            engineFactory.Configuration.MinifyOutput = true;

            var engine = engineFactory.GetEngine();
            var minified = engine.TransformToCss(bundled, files.First().FullName);

            //build file
            fileSystem.File.WriteAllText(outputPath, minified);
        }
Beispiel #11
0
 public IEnumerable<IDocument> Execute(IReadOnlyList<IDocument> inputs, IExecutionContext context)
 {
     DotlessConfiguration config = DotlessConfiguration.GetDefault();
     config.RootPath = context.InputFolder;
     config.Logger = typeof (LessLogger);
     EngineFactory engineFactory = new EngineFactory(config);
     return inputs.Select(x =>
     {
         ILessEngine engine = engineFactory.GetEngine();
         string path = x.Get<string>("SourceFilePath", null);
         string fileName = null;
         if (path != null)
         {
             engine.CurrentDirectory = Path.GetDirectoryName(path);
             fileName = Path.GetFileName(path);
         }
         string content = engine.TransformToCss(x.Content, fileName);
         return x.Clone(content);
     });
 }
Beispiel #12
0
        private static string LessToCss(string css, string fileName)
        {
            var current = Directory.GetCurrentDirectory();
            Directory.SetCurrentDirectory(new FileInfo(FilePath(fileName)).DirectoryName);

            var engine = new EngineFactory().GetEngine(new DotlessConfiguration
            {
                MinifyOutput = false
            });

            string cssText = engine.TransformToCss(new LessSourceObject
            {
                Content = css,
            });

            Directory.SetCurrentDirectory(current);
            return cssText;
        }
Beispiel #13
0
        /// <summary>
        /// The main method of the task.
        /// </summary>
        /// <param name="args">The arguments.</param>
        /// <returns>The return code.</returns>
        private int Main(string[] args)
        {
            var arguments = new List<string>();

            arguments.AddRange(args);

            var configuration = GetConfigurationFromArguments(arguments);

            if (configuration.Help)
                return -1;

            if (arguments.Count == 0)
            {
                WriteHelp();
                return -1;
            }

            string inputDirectoryPath, inputFilePattern;

            if (Directory.Exists(arguments[0]))
            {
                inputDirectoryPath = arguments[0];
                inputFilePattern = "*.less";
            }
            else
            {
                inputDirectoryPath = Path.GetDirectoryName(arguments[0]);
                if (string.IsNullOrEmpty(inputDirectoryPath)) inputDirectoryPath = "." + Path.DirectorySeparatorChar;
                inputFilePattern = Path.GetFileName(arguments[0]);
                if (!Path.HasExtension(inputFilePattern)) inputFilePattern = Path.ChangeExtension(inputFilePattern, "less");
            }

            var outputDirectoryPath = string.Empty;
            var outputFilename = string.Empty;
            if (arguments.Count > 1)
            {
                if (Directory.Exists(arguments[1]))
                {
                    outputDirectoryPath = arguments[1];
                }
                else
                {
                    outputDirectoryPath = Path.GetDirectoryName(arguments[1]);
                    outputFilename = Path.GetFileName(arguments[1]);

                    if (!Path.HasExtension(outputFilename))
                        outputFilename = Path.ChangeExtension(outputFilename, "css");
                }
            }

            if (string.IsNullOrEmpty(outputDirectoryPath))
            {
                outputDirectoryPath = inputDirectoryPath;
            }
            else
            {
                Directory.CreateDirectory(outputDirectoryPath);
            }

            if (HasWildcards(inputFilePattern))
            {
                if (!string.IsNullOrEmpty(outputFilename))
                {
                    Log.LogError("Output filename patterns/filenames are not supported when using input wildcards. You may only specify an output directory (end the output in a directory seperator)");
                    return -1;
                }
                outputFilename = string.Empty;
            }

            var filenames = Directory.GetFiles(inputDirectoryPath, inputFilePattern);
            if (filenames.Count() == 0)
            {
                Log.LogError("No files found matching pattern '{0}'", inputFilePattern);
                return -1;
            }

            var engineFactory = new EngineFactory(configuration);
            var containerFactory = new dotlessCompilerContainerFactory(this);
            var engine = engineFactory.GetEngine(containerFactory);
            foreach (var filename in filenames)
            {
                var inputFile = new FileInfo(filename);

                var outputFile =
                    string.IsNullOrEmpty(outputFilename) ?
                        Path.Combine(outputDirectoryPath, Path.ChangeExtension(inputFile.Name, "css")) :
                        Path.Combine(outputDirectoryPath, outputFilename);

                var outputFilePath = Path.GetFullPath(outputFile);

                var returnCode = CompileImpl(engine, inputFile.FullName, outputFilePath);
                if (returnCode != 0)
                {
                    // IF not zero
                    // THEN return the failure code
                    return returnCode;
                }
            }

            return ReturnCode;
        }
 public LessCssHttpHandler()
 {
     Config = new WebConfigConfigurationLoader().GetConfiguration();
     Container = new ContainerFactory().GetContainer(Config);
     Factory = new EngineFactory(Config);
 }
Beispiel #15
0
 private string ProcessLess(string file)
 {
     lock (typeof(CssBundle))
     {
         try
         {
             currentDirectoryWrapper.SetCurrentDirectory(Path.GetDirectoryName(file));
             var content = ReadFile(file);
             var engineFactory = new EngineFactory();
             var engine = engineFactory.GetEngine();
             return engine.TransformToCss(content, file);
         }
         finally
         {
             currentDirectoryWrapper.Revert();
         }
     }
 }
Beispiel #16
0
        private string ProcessLess(string file)
        {
            var currentDirectory = Environment.CurrentDirectory;

            try
            {
                Environment.CurrentDirectory = Path.GetDirectoryName(file) ?? currentDirectory;
                var content = ReadFile(file);
                var engineFactory = new EngineFactory();
                var engine = engineFactory.GetEngine();
                return engine.TransformToCss(content, file);
            }
            finally
            {
                Environment.CurrentDirectory = currentDirectory;
            }
        }
 public override string Process(string filePath, string content)
 {
     var engineFactory = new EngineFactory();
     var engine = engineFactory.GetEngine();
     return engine.TransformToCss(content, filePath);
 }
Beispiel #18
0
		public string GetCss(string lessSource, Assembly contextAssembly) {
			var engine = new EngineFactory().GetEngine();
			return engine.TransformToCss((contextAssembly != null ? GetLessVariableDefinitions(contextAssembly) + Environment.NewLine : "") + lessSource, "Module.less");
		}