Esempio n. 1
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);
            }
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
0
        public void TestCssOutPut()
        {
            string lessFile = Environment.CurrentDirectory + @"\test.less";
            string output   = @"body {
  background-color: Green;
}
";

            LessCompiler compiler = null;

            try
            {
                compiler = new LessCompiler();
            }
            catch (Exception)
            {
                Assert.Fail("Compiler was not created successfully!");
            }

            string css = string.Empty;

            if (compiler != null)
            {
                css = compiler.CompileFile(lessFile, true);
            }

            Assert.AreEqual(output, css);
        }
        public void TestCssOutPut()
        {
            string lessFile = Environment.CurrentDirectory + @"\test.less";
            string output = @"body {
              background-color: Green;
            }
            ";

            LessCompiler compiler = null;
            try
            {
                compiler = new LessCompiler();
            }
            catch (Exception)
            {
                Assert.Fail("Compiler was not created successfully!");
            }

            string css = string.Empty;
            if (compiler != null)
            {
                css = compiler.CompileFile(lessFile);
            }

            Assert.AreEqual(output, css);
        }
        /// <summary>
        /// Translates a code of assets written on LESS to CSS code
        /// </summary>
        /// <param name="assets">Set of assets with code written on LESS</param>
        /// <returns>Set of assets with translated code</returns>
        public override IList <IAsset> Translate(IList <IAsset> assets)
        {
            if (assets == null)
            {
                throw new ArgumentException(CoreStrings.Common_ValueIsEmpty, "assets");
            }

            if (assets.Count == 0)
            {
                return(assets);
            }

            var assetsToProcessing = assets.Where(a => a.AssetTypeCode == Constants.AssetTypeCode.Less).ToList();

            if (assetsToProcessing.Count == 0)
            {
                return(assets);
            }

            bool enableNativeMinification = NativeMinificationEnabled;
            CompilationOptions options    = CreateCompilationOptions(enableNativeMinification);

            using (var lessCompiler = new LessCompiler(_createJsEngineInstance, _virtualFileManager, options))
            {
                foreach (var asset in assetsToProcessing)
                {
                    InnerTranslate(asset, lessCompiler, enableNativeMinification);
                }
            }

            return(assets);
        }
Esempio n. 6
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");
        }
        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;
        }
Esempio n. 8
0
        public void Setup()
        {
            var dir = new VirtualDirectory();

            _file = new VirtualFile("test.less")
            {
                Directory = dir
            };
            _compiler = new LessCompiler(new TestLogger());
        }
Esempio n. 9
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");
        }
Esempio n. 10
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 `}`");
        }
Esempio n. 11
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;
        }
Esempio n. 13
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);
            }
        }
        /// <summary>
        /// Translates a code of asset written on LESS to CSS code
        /// </summary>
        /// <param name="asset">Asset with code written on LESS</param>
        /// <returns>Asset with translated code</returns>
        public override IAsset Translate(IAsset asset)
        {
            if (asset == null)
            {
                throw new ArgumentException(CoreStrings.Common_ValueIsEmpty, "asset");
            }

            bool enableNativeMinification = NativeMinificationEnabled;
            CompilationOptions options    = CreateCompilationOptions(enableNativeMinification);

            using (var lessCompiler = new LessCompiler(_createJsEngineInstance, _virtualFileManager, options))
            {
                InnerTranslate(asset, lessCompiler, enableNativeMinification);
            }

            return(asset);
        }
Esempio n. 15
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();
        }
Esempio n. 16
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");
        }
Esempio n. 17
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");
        }
Esempio n. 18
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
                    );
            });
        }
		/// <summary>
		/// Translates a code of asset written on LESS to CSS-code
		/// </summary>
		/// <param name="asset">Asset with code written on LESS</param>
		/// <returns>Asset with translated code</returns>
		public override IAsset Translate(IAsset asset)
		{
			if (asset == null)
			{
				throw new ArgumentException(CoreStrings.Common_ValueIsEmpty, "asset");
			}

			lock (_translationSynchronizer)
			{
				bool enableNativeMinification = NativeMinificationEnabled;
				CompilationOptions options = CreateCompilationOptions(enableNativeMinification);
				var lessCompiler = new LessCompiler(_createJsEngineInstance, options);

				ClearLessStylesheetCache();

				try
				{
					InnerTranslate(asset, lessCompiler, enableNativeMinification);
				}
				finally
				{
					lessCompiler.Dispose();
					ClearLessStylesheetCache();
				}
			}

			return asset;
		}
		/// <summary>
		/// Translates a code of assets written on LESS to CSS-code
		/// </summary>
		/// <param name="assets">Set of assets with code written on LESS</param>
		/// <returns>Set of assets with translated code</returns>
		public override IList<IAsset> Translate(IList<IAsset> assets)
		{
			if (assets == null)
			{
				throw new ArgumentException(CoreStrings.Common_ValueIsEmpty, "assets");
			}

			if (assets.Count == 0)
			{
				return assets;
			}

			var assetsToProcessing = assets.Where(a => a.AssetTypeCode == Constants.AssetTypeCode.Less).ToList();
			if (assetsToProcessing.Count == 0)
			{
				return assets;
			}

			lock (_translationSynchronizer)
			{
				bool enableNativeMinification = NativeMinificationEnabled;
				CompilationOptions options = CreateCompilationOptions(enableNativeMinification);
				var lessCompiler = new LessCompiler(_createJsEngineInstance, options);

				ClearLessStylesheetCache();

				try
				{
					foreach (var asset in assetsToProcessing)
					{
						InnerTranslate(asset, lessCompiler, enableNativeMinification);
					}
				}
				finally
				{
					lessCompiler.Dispose();
					ClearLessStylesheetCache();
				}
			}

			return assets;
		}
Esempio n. 21
0
 public SitefinityLessCompiler()
 {
     compiler = new LessCompiler();
 }
		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()
				;
		}