Beispiel #1
0
        private void InnerTranslate(IAsset asset, CompilationOptions sassOptions, bool enableNativeMinification)
        {
            string         assetTypeName = asset.AssetTypeCode == Constants.AssetTypeCode.Sass ? "Sass" : "SCSS";
            string         newContent;
            string         assetUrl = asset.Url;
            IList <string> dependencies;

            try
            {
                CompilationResult result = SassCompiler.Compile(asset.Content, assetUrl, options: sassOptions);
                newContent   = result.CompiledContent;
                dependencies = result.IncludedFilePaths;
            }
            catch (SassСompilationException e)
            {
                throw new AssetTranslationException(
                          string.Format(CoreStrings.Translators_TranslationSyntaxError,
                                        assetTypeName, OUTPUT_CODE_TYPE, assetUrl, SassErrorHelpers.Format(e)));
            }
            catch (Exception e)
            {
                throw new AssetTranslationException(
                          string.Format(CoreStrings.Translators_TranslationFailed,
                                        assetTypeName, OUTPUT_CODE_TYPE, assetUrl, e.Message), e);
            }

            asset.Content  = newContent;
            asset.Minified = enableNativeMinification;
            asset.RelativePathsResolved   = false;
            asset.VirtualPathDependencies = dependencies;
        }
Beispiel #2
0
        public void CompileContent(bool withFileManager)
        {
            SetFileManager(withFileManager);
            Document document = s_documents[DocumentName];

            string compiledContent = SassCompiler.Compile(document.Content, document.AbsolutePath).CompiledContent;
        }
        public virtual void CodeCompilationIsCorrect()
        {
            // Arrange
            string inputFilePath = Path.Combine(_resourcesDirectoryPath,
                string.Format("variables/{0}/style{1}", _subfolderName, _fileExtension));
            string outputFilePath = Path.Combine(_resourcesDirectoryPath, "variables/style.css");

            string inputCode = File.ReadAllText(inputFilePath);
            string targetOutputCode = File.ReadAllText(outputFilePath);

            var options = new CompilationOptions
            {
                IndentedSyntax = _indentedSyntax
            };

            // Act
            CompilationResult result;

            using (var compiler = new SassCompiler())
            {
                result = compiler.Compile(inputCode, options: options);
            }

            // Assert
            Assert.Equal(targetOutputCode, result.CompiledContent);
            Assert.True(string.IsNullOrEmpty(result.SourceMap));
            Assert.Empty(result.IncludedFilePaths);
        }
Beispiel #4
0
        public void Test()
        {
            var compiler = new SassCompiler(new SassSettings());
            var result   = compiler.Compile("../../../TestCases/Scss/error.scss");

            Assert.IsTrue(result.Errors != null && result.Errors.Any());
        }
Beispiel #5
0
        public void Can_compile_sass_file(string label, int expectedFiles, CompilerOptions options)
        {
            // Arrange
            var cwd = Path.Combine(AppContext.BaseDirectory, "generated", label);

            if (Directory.Exists(cwd))
            {
                Directory.Delete(cwd, recursive: true);
            }
            Directory.CreateDirectory(cwd);
            options.OutputDirectory = cwd;

            // Act
            var result     = SassCompiler.Compile(Sample.GetBasicSCSS().FullName, options);
            var totalFiles = Directory.GetFiles(cwd, "*").Length;

            var builder   = new StringBuilder();
            var separator = string.Concat(Enumerable.Repeat('=', 50));

            foreach (var item in result.GeneratedFiles)
            {
                builder.AppendLine($"== {Path.GetFileName(item)} ({label})")
                .AppendLine(separator)
                .AppendLine(File.ReadAllText(item))
                .AppendLine()
                .AppendLine();
            }

            // Assert
            result.Success.ShouldBeTrue();
            totalFiles.ShouldBe(expectedFiles);
            result.GeneratedFiles.Length.ShouldBe(expectedFiles);

            Diff.Approve(builder, ".txt", label);
        }
Beispiel #6
0
        private static void Compile()
        {
            var          compiler = new SassCompiler();
            const string input    = "$primary-color: #333; body {color: $primary-color;}";
            var          output   = compiler.Compile(input, OutputStyle.Compressed, false);

            Console.WriteLine(output);
        }
Beispiel #7
0
        public static string TransformToCss(string fullFileName, string text, EnvDTE.ProjectItem projectItem)
        {
            string output = string.Empty;
            using (var fixture = new SassCompiler())
            {
                output = fixture.Compile(fullFileName);
            }

            return output;
        }
        public void ShouldNotCompileStringWithEchoOutputStyle()
        {
            // Arrange

            // Act
            Action compile = () => SassCompiler.Compile(_basicContent, OutputStyle.Echo);

            // Assert
            compile.ShouldThrow <ArgumentException>();
        }
        /* sass to css covert */
        public string sasscompiler(string sourcepath)
        {
            var options = new SassOptions
            {
                InputPath = sourcepath
            };
            var sass   = new SassCompiler(options);
            var result = sass.Compile();

            return(result.Output);
        }
Beispiel #10
0
 public static string Compile(string fileText)
 {
     try
     {
         return(SassCompiler.Compile(fileText, false, new List <string>()));
     }
     catch
     {
         return(string.Empty);
     }
 }
Beispiel #11
0
        public static string TransformToCss(string fullFileName, string text, EnvDTE.ProjectItem projectItem)
        {
            string output = string.Empty;

            using (var fixture = new SassCompiler())
            {
                output = fixture.Compile(fullFileName);
            }

            return(output);
        }
Beispiel #12
0
        public void ShouldCompileStringWithCompressedOutputStyle()
        {
            // Arrange
            const string expectedResult = "body .class {color:red;}";

            // Act
            string compiled = SassCompiler.Compile(_basicContent, OutputStyle.Compressed);

            // Assert
            compiled.Should().BeEquivalentTo(expectedResult);
        }
Beispiel #13
0
        public void ShouldCompileString()
        {
            // Arrange
            const string expectedResult = "/* line 3, source string */\nbody .class {\n  color: red; }\n";

            // Act
            string compiled = SassCompiler.Compile(_basicContent);

            // Assert
            compiled.Should().BeEquivalentTo(expectedResult);
        }
Beispiel #14
0
        public void ShouldCompileStringWithoutComments()
        {
            // Arrange
            const string expectedResult = "body .class {\n  color: red; }\n";

            // Act
            string compiled = SassCompiler.Compile(_basicContent, sourceComments: false);

            // Assert
            compiled.Should().BeEquivalentTo(expectedResult);
        }
Beispiel #15
0
        public void ShouldCompileStringWithImport()
        {
            // Arrange
            string expectedResult = String.Format("/* line 1, {0} */\nbody {{\n  border-color: green; }}\n\n/* line 5, source string */\nbody .class {{\n  color: red; }}\n", ReplaceLastSlash(ImportPath));

            // Act
            string compiled = SassCompiler.Compile(_importingContent, includePaths: new[] { Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles") });

            // Assert
            compiled.Should().BeEquivalentTo(expectedResult);
        }
Beispiel #16
0
        public string Execute(RenderContext context, string input)
        {
            var sassOptions = new SassOptions
            {
                Data = input
            };
            var sass   = new SassCompiler(sassOptions);
            var result = sass.Compile();

            return(result.Output);
        }
        public void OnContentLoaded(CombinatorResource resource)
        {
            var extension = Path.GetExtension(resource.AbsoluteUrl.ToString()).ToLowerInvariant();

            if (extension != ".sass" && extension != ".scss")
            {
                return;
            }

            using (var compiler = new SassCompiler())
            {
                resource.Content = compiler.Compile(_virtualPathProvider.MapPath(resource.RelativeVirtualPath).Replace("\\", @"\"), false, new List <string>());
            }
        }
Beispiel #18
0
        private Task <CompilationResult> CompileSass()
        {
            string stylesDir = Path.Combine(hostingEnvironment.WebRootPath, "assets/css");
            string fullPath  = Path.Combine(stylesDir, "style.scss");

            return(cache.GetOrCreateAsync <CompilationResult>(fullPath, async entry =>
            {
                string src = await System.IO.File.ReadAllTextAsync(fullPath);
                return SassCompiler.Compile(src, new CompilationOptions
                {
                    IncludePaths = { stylesDir },
                    OutputStyle = OutputStyle.Compressed,
                });
            }));
        }
Beispiel #19
0
        public string Process(string input)
        {
            string output = input;

            try
            {
                var compiler = new SassCompiler();
                output = compiler.Compile(source: input, outputStyle: OutputStyle.Nested, sourceComments: true);
            }
            catch (Exception ex)
            {
                output += string.Format(@"/* Error Compiling Sass: {0} */", ex.ToString());
            }

            return(output);
        }
Beispiel #20
0
        public void ShouldCompileStringWithAdditionalImport()
        {
            // Arrange
            string expectedResult = String.Format("/* line 1, {0} */\nbody {{\n  border-color: green; }}\n\n/* line 3, {1} */\nbody > #id {{\n  font-size: 14px; }}\n\n/* line 6, source string */\nbody .class {{\n  color: red; }}\n",
                                                  ReplaceLastSlash(ImportPath),
                                                  ReplaceLastSlash(AdditionalImportPath));

            // Act
            string compiled = SassCompiler.Compile(_importingAdditionalContent, includePaths: new[]
            {
                TestFilesDirectory,
                AdditionalImportTestFilesDirectory
            });

            // Assert
            compiled.Should().BeEquivalentTo(expectedResult);
        }
Beispiel #21
0
        public void Process(BundleContext context, BundleResponse response)
        {
            response.ContentType = "text/css";
            response.Content     = string.Empty;

            foreach (var fileInfo in response.Files)
            {
                if (fileInfo.Extension.Equals(".sass", StringComparison.Ordinal) || fileInfo.Extension.Equals(".scss", StringComparison.Ordinal))
                {
                    response.Content += TransformCache.Get(fileInfo, () => _Engine.Compile(fileInfo.FullName, false, new List <string>()));
                }
                else if (fileInfo.Extension.Equals(".css", StringComparison.Ordinal))
                {
                    response.Content += TransformCache.Get(fileInfo, () => File.ReadAllText(fileInfo.FullName));
                }
            }
        }
        public static void CompileCss(string sassFilePath, string cssFilePath, DateTime? folderLastUpdatedUtc = null)
        {
            var sassCompiler = new SassCompiler(new SassOptions
            {
                InputPath = sassFilePath,
                OutputStyle = SassOutputStyle.Compact,
                IncludeSourceComments = false,
            });

            var result = sassCompiler.Compile();

            if (result.ErrorStatus != 0)
            {
                throw new InvalidOperationException("Compiling sass caused a scripting host error. " +
                    string.Format("Error status: {0}. File: {1}. Line: {2}. Column: {3}. Message: {4}", result.ErrorStatus, result.ErrorFile, result.ErrorLine, result.ErrorColumn, result.ErrorMessage));
            }

            C1File.WriteAllText(cssFilePath, result.Output);

            if (folderLastUpdatedUtc.HasValue)
            {
                File.SetLastWriteTimeUtc(cssFilePath, folderLastUpdatedUtc.Value);
            }
        }
        public virtual void CodeWithImportCompilationIsCorrect()
        {
            // Arrange
            string inputFilePath = Path.Combine(_resourcesDirectoryPath,
                string.Format("import/{0}/base{1}", _subfolderName, _fileExtension));
            string importedFilePath = Path.Combine(_resourcesDirectoryPath,
                string.Format("import/{0}/_reset{1}", _subfolderName, _fileExtension));
            string outputFilePath = Path.Combine(_resourcesDirectoryPath, "import/base.css");

            string inputCode = File.ReadAllText(inputFilePath);
            string targetOutputCode = File.ReadAllText(outputFilePath);

            var options = new CompilationOptions
            {
                IndentedSyntax = _indentedSyntax,
                SourceMap = true
            };

            // Act
            CompilationResult result;

            using (var compiler = new SassCompiler())
            {
                result = compiler.Compile(inputCode, inputFilePath, options: options);
            }

            // Assert
            Assert.Equal(targetOutputCode, result.CompiledContent);
            Assert.False(string.IsNullOrEmpty(result.SourceMap));

            var includedFilePaths = result.IncludedFilePaths;
            Assert.Equal(1, includedFilePaths.Count);
            Assert.Equal(GetCanonicalPath(importedFilePath), includedFilePaths[0]);
        }