Beispiel #1
0
        public void Importing_less_file_not_found_throws_useful_exception()
        {
            var root = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()));

            try
            {
                var bundleA = root.CreateSubdirectory("bundle-a");
                file.SetupGet(f => f.Directory)
                .Returns(new FileSystemDirectory(bundleA.FullName));
                root.CreateSubdirectory("bundle-b");

                var compiler  = new LessCompiler();
                var exception = Assert.Throws <FileNotFoundException>(delegate
                {
                    compiler.Compile(
                        "@import \"../bundle-b/_MISSING.less\";\nbody{ color: @color }",
                        file.Object
                        );
                });
                exception.Message.ShouldContain("_MISSING.less");
                exception.Message.ShouldContain("test.less");
            }
            finally
            {
                root.Delete(true);
            }
        }
        private void InnerTranslate(IAsset asset, LessCompiler lessCompiler, bool enableNativeMinification)
        {
            string         newContent;
            string         assetUrl = asset.Url;
            IList <string> dependencies;

            try
            {
                CompilationResult result = lessCompiler.Compile(asset.Content, assetUrl);
                newContent   = result.CompiledContent;
                dependencies = result.IncludedFilePaths;
            }
            catch (FileNotFoundException)
            {
                throw;
            }
            catch (LessCompilationException e)
            {
                throw new AssetTranslationException(
                          string.Format(CoreStrings.Translators_TranslationSyntaxError,
                                        INPUT_CODE_TYPE, OUTPUT_CODE_TYPE, assetUrl, e.Message));
            }
            catch (Exception e)
            {
                throw new AssetTranslationException(
                          string.Format(CoreStrings.Translators_TranslationFailed,
                                        INPUT_CODE_TYPE, OUTPUT_CODE_TYPE, assetUrl, e.Message), e);
            }

            asset.Content  = newContent;
            asset.Minified = enableNativeMinification;
            asset.RelativePathsResolved   = false;
            asset.VirtualPathDependencies = dependencies;
        }
Beispiel #3
0
        public void Compile_converts_LESS_into_CSS()
        {
            var compiler = new LessCompiler();
            var css      = compiler.Compile("@color: #4d926f; #header { color: @color; }", file.Object);

            css.ShouldEqual("#header {\n  color: #4d926f;\n}\n");
        }
Beispiel #4
0
        public void Can_Compile_LESS_with_two_levels_of_import()
        {
            // Mocking out IFileSystem here would be lots of work, given the directory navigations
            // that are required. So it's easier to use a temp directory and a real FileSystemDirectory object.
            var root    = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()));
            var bundleA = root.CreateSubdirectory("bundle-a");
            var bundleB = root.CreateSubdirectory("bundle-b");

            file.SetupGet(f => f.Directory)
            .Returns(new FileSystemDirectory(bundleA.FullName));

            try
            {
                File.WriteAllText(
                    Path.Combine(root.FullName, "_base.less"),
                    "@size: 100px;"
                    );
                File.WriteAllText(
                    Path.Combine(bundleB.FullName, "_lib.less"),
                    "@import \"../_base.less\";\n@color: #ffffff; p { height: @size; }"
                    );

                var compiler = new LessCompiler();
                var css      = compiler.Compile(
                    "@import \"../bundle-b/_lib.less\";\nbody{ color: @color }",
                    file.Object
                    );
                css.ShouldEqual("p {\n  height: 100px;\n}\nbody {\n  color: #ffffff;\n}\n");
            }
            finally
            {
                root.Delete(true);
            }
        }
Beispiel #5
0
        public void Compile_LESS_that_fails_parsing_throws_LessCompileException()
        {
            var compiler  = new LessCompiler();
            var exception = Assert.Throws <LessCompileException>(delegate
            {
                compiler.Compile("#fail { - }", file.Object);
            });

            exception.Message.ShouldEqual("Less compile error in test.less:\r\nSyntax Error on line 1");
        }
Beispiel #6
0
        public void Compile_invalid_LESS_throws_exception()
        {
            var compiler  = new LessCompiler();
            var exception = Assert.Throws <LessCompileException>(delegate
            {
                compiler.Compile("#unclosed_rule {", file.Object);
            });

            exception.Message.ShouldEqual("Less compile error in test.less:\r\nMissing closing `}`");
        }
Beispiel #7
0
        public void Compile_LESS_with_unknown_mixin_throws_exception()
        {
            var less      = "form { \nmargin-bottom: @baseline; }";
            var compiler  = new LessCompiler();
            var exception = Assert.Throws <LessCompileException>(delegate
            {
                compiler.Compile(less, file.Object);
            });

            exception.Message.ShouldEqual("Less compile error in test.less:\r\nvariable @baseline is undefined");
        }
        public CompilingMainTwitterBootstrapLessFile()
        {
            var source  = File.ReadAllText("bootstrap\\bootstrap.less");
            var context = new CompileContext
            {
                RootDirectory  = new FileSystemDirectory(Path.GetFullPath(".")),
                SourceFilePath = "bootstrap/bootstrap.less"
            };

            var lessCompiler = new LessCompiler();
            var result       = lessCompiler.Compile(source, context);

            importedFilePaths = result.ImportedFilePaths;
        }
Beispiel #9
0
        /// <summary>
        /// Passes the LESS source (and any @import-ed files) to the LESS Compiler and
        /// transforms it to CSS.
        /// </summary>
        private string CompileLess(string source, IFile file)
        {
            try
            {
                var compiler = new LessCompiler(_lessLog);
                var css      = compiler.Compile(source, file);

                return(css);
            }
            catch (Exception ex)
            {
                _log.Error("An error occurred in ProcessLess.CompileLess: {0}".FormatWith(ex.Message), ex);
                return(string.Empty);
            }
        }
Beispiel #10
0
        public void Compile(bool compileParentFiles = true)
        {
            if (this.Enabled)
            {
                LessCompiler.Compile(this.FullPath, this.OutputPath, this.Minify);
            }
            if (compileParentFiles)
            {
                foreach (File parentFile in this.ParentFiles)
                {
                    parentFile.Compile();
                }
            }

            //Recheck the imports of the current file
            CheckForImports();
        }
Beispiel #11
0
        public void Can_Compile_LESS_that_imports_another_LESS_file_from_different_directory()
        {
            var otherFile = new Mock <IFile>();

            directory.Setup(d => d.GetFile("../bundle-b/lib.less"))
            .Returns(otherFile.Object);
            otherFile.Setup(f => f.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            .Returns(() => "@color: #ffffff;".AsStream());

            var compiler = new LessCompiler();
            var css      = compiler.Compile(
                "@import \"../bundle-b/lib.less\";\nbody{ color: @color }",
                file.Object
                );

            css.ShouldEqual("body {\n  color: #ffffff;\n}\n");
        }
Beispiel #12
0
        public void Import_less_file_that_uses_outer_variable()
        {
            var otherFile = new Mock <IFile>();

            directory.Setup(d => d.GetFile("Framework.less"))
            .Returns(otherFile.Object);
            otherFile.Setup(f => f.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            .Returns(() => ".object { padding: @objectpadding; }".AsStream());

            var compiler = new LessCompiler();
            var result   = compiler.Compile(
                "@objectpadding: 20px;\n@import \"Framework.less\";",
                file.Object
                );

            result.ShouldEqual(".object {\n  padding: 20px;\n}\n");
        }
Beispiel #13
0
        public void Using_mixin_from_imported_css_file_throws_exception()
        {
            var otherFile = new Mock <IFile>();

            directory.Setup(d => d.GetFile("lib.css"))
            .Returns(otherFile.Object);
            otherFile.Setup(f => f.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            .Returns(() => ".mixin { color: red; }".AsStream());

            var compiler = new LessCompiler();

            Assert.Throws <LessCompileException>(delegate
            {
                compiler.Compile(
                    "@import \"lib.css\";\nbody{ .mixin; }",
                    file.Object
                    );
            });
        }
		private void InnerTranslate(IAsset asset, LessCompiler lessCompiler, bool enableNativeMinification)
		{
			string newContent;
			string assetUrl = asset.Url;
			var dependencies = new DependencyCollection();

			try
			{
				LessStylesheet stylesheet = GetLessStylesheet(asset);
				FillDependencies(assetUrl, stylesheet, dependencies);

				newContent = lessCompiler.Compile(stylesheet.Content, stylesheet.Url, dependencies);
			}
			catch (FileNotFoundException)
			{
				throw;
			}
			catch (LessCompilingException e)
			{
				throw new AssetTranslationException(
					string.Format(CoreStrings.Translators_TranslationSyntaxError,
						INPUT_CODE_TYPE, OUTPUT_CODE_TYPE, assetUrl, e.Message));
			}
			catch (Exception e)
			{
				throw new AssetTranslationException(
					string.Format(CoreStrings.Translators_TranslationFailed,
						INPUT_CODE_TYPE, OUTPUT_CODE_TYPE, assetUrl, e.Message), e);
			}

			asset.Content = newContent;
			asset.Minified = enableNativeMinification;
			asset.RelativePathsResolved = true;
			asset.VirtualPathDependencies = dependencies
				.Where(d => d.IsObservable)
				.Select(d => d.Url)
				.Distinct()
				.ToList()
				;
		}
Beispiel #15
0
        public void Compile_converts_valid_LESS_to_CSS()
        {
            var css = _compiler.Compile("@color: #4d926f; #header { color: @color; }", _file);

            css.Should().Be("#header {\n  color: #4d926f;\n}\n");
        }