/// <summary>
        /// Transforms the content of the given string.
        /// </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)
        {
            try
            {
                var options = new LibSass.Compiler.Options.SassOptions
                {
                    InputPath        = path,
                    OutputStyle      = LibSass.Compiler.Options.SassOutputStyle.Expanded,
                    Precision        = 5,
                    IsIndentedSyntax = System.IO.Path.GetExtension(path).Equals(".sass", StringComparison.OrdinalIgnoreCase)
                };
                var compiler = new LibSass.Compiler.SassCompiler(options);
                var result   = compiler.Compile();

                foreach (var file in result.IncludedFiles)
                {
                    cruncher.AddFileMonitor(file, "not empty");
                }

                return(result.Output);
            }
            catch (Exception ex)
            {
                throw new SassAndScssCompilingException(ex.Message, ex.InnerException);
            }
        }
        public Module Transform(Module module)
        {
            string fullModulePath = module.FullPath;
            string moduleId       = module.ModuleId;
            string content        = module.TransformedContent ?? _fileSystem.File.ReadAllText(fullModulePath);

            LibSass.Compiler.SassCompiler compiler = new LibSass.Compiler.SassCompiler(new LibSass.Compiler.Options.SassOptions
            {
                Data        = content,
                InputPath   = fullModulePath,
                OutputStyle = LibSass.Compiler.Options.SassOutputStyle.Compressed,
                Importers   = new LibSass.Compiler.Options.CustomImportDelegate[]
                {
                    new CustomImportDelegate((string currentImport, string parentImport, ISassOptions sassOptions) =>
                    {
                        var importPath = _fileSystem.Path.GetFullPath(_fileSystem.Path.Combine(_fileSystem.Path.GetDirectoryName(fullModulePath), currentImport));
                        return(new SassImport[]
                        {
                            new SassImport
                            {
                                Data = _fileSystem.File.ReadAllText(importPath),
                                Path = importPath
                            }
                        });
                    })
                }
            });

            CssToJsModule cssTransformer = new CssToJsModule();
            var           css            = compiler.Compile().Output;

            module.OriginalContent    = module.OriginalContent ?? content;
            module.TransformedContent = cssTransformer.Build(css, moduleId);
            return(module);
        }
Exemple #3
0
        static AssetProcessingResult DoCompile(string sass)
        {
            var sassOptions = new SassOptions
            {
                Data        = sass,
                OutputStyle = SassOutputStyle.Compressed,
            };
            var sassCompiler = new LibSass.Compiler.SassCompiler(sassOptions);

            try
            {
                var result = sassCompiler.Compile();
                if (result.ErrorStatus != 0)
                {
                    // TODO: enrich error dump
                    return(new AssetProcessingResult(AssetProcessingResult.CompilationStatus.Failure, null, result.ErrorMessage));
                }

                return(new AssetProcessingResult(AssetProcessingResult.CompilationStatus.Success, result.Output));
            }
            catch (Exception e)
            {
                log.Error(e, "Error compiling sass `{sass}`", sass);

                if (Debugger.IsAttached)
                {
                    Debugger.Break();
                }

                return(new AssetProcessingResult(AssetProcessingResult.CompilationStatus.Failure, null, e.Message));
            }
        }
Exemple #4
0
 public string Compile()
 {
     LibSass.Compiler.SassResult result = null;
     try
     {
         LibSass.Compiler.SassCompiler compiler = new LibSass.Compiler.SassCompiler(options);
         result = compiler.Compile();
     }
     catch
     {
     }
     return(result.Output);
 }
Exemple #5
0
        public string Execute(RenderContext context, string input)
        {
            if (Kooboo.Lib.Helper.RuntimeSystemHelper.IsWindow())
            {
                var sassOptions = new LibSass.Compiler.Options.SassOptions
                {
                    Data = input
                };
                var sass   = new LibSass.Compiler.SassCompiler(sassOptions);
                var result = sass.Compile();

                return(result.Output);
            }
            if (Kooboo.Lib.Helper.RuntimeSystemHelper.IsLinux())
            {
                return(SassHelper.Compile(input));
            }
            //mac template don't support
            return(string.Empty);
        }
Exemple #6
0
        public string CompileSass(string input, SassMode mode, string location, int precision = 5)
        {
            var compiler = new LibSass.Compiler.SassCompiler(new SassOptions
            {
                InputPath             = location,
                Data                  = input,
                OutputStyle           = SassOutputStyle.Nested,
                IncludeSourceComments = false,
                Precision             = precision,
                IsIndentedSyntax      = mode == SassMode.Sass
            });

            var result = compiler.Compile();

            if (!string.IsNullOrEmpty(result.ErrorMessage))
            {
                throw new InvalidOperationException(string.Format("Sass compilation failed ({0})", result.ErrorMessage));
            }

            return(result.Output);
        }
Exemple #7
0
        /// <summary><![CDATA[
        /// Compile a SCSS file or files into CSS.
        /// ]]></summary>
        /// <param name="scssInputPath">Web path to the scss input file (e.g. "/scss/application.scss").</param>
        /// <param name="outputPath">Web path to the CSS output file (e.g. "/stylesheets/application.css").</param>
        /// <param name="debugMode">Set to true for expanded output with source maps, false for compressed production CSS only</param>
        /// <param name="force">Force a rebuild</param>
        /// <returns>True if successful, false if an error occurred</returns>
        public static void BuildScss(this string scssInputPath, string outputPath, bool debugMode = false, bool force = false)
        {
            var process = force;

            Debug.WriteLine("");
            Debug.WriteLine("LibSass result (" + (debugMode ? "debug" : "release") + " mode):");
            Debug.WriteLine("------------------------------");

            #region Determine if at least one file has been modified

            if (force)
            {
                Debug.WriteLine("FORCED REBUILD MODE");
            }

            else
            {
                if (!FileExists(outputPath))
                {
                    Debug.WriteLine("CSS file is missing, will recompile...");
                    process = true;
                }

                else
                {
                    if (debugMode && !FileExists(outputPath + ".map"))
                    {
                        Debug.WriteLine("Debug mode and CSS Map file is missing, will recompile...");
                        process = true;
                    }

                    else
                    {
                        if (HttpContext.Current.Application.KeyExists(ConvertFilePathToKey(scssInputPath)))
                        {
                            var files = (ArrayList)HttpContext.Current.Application[ConvertFilePathToKey(scssInputPath)];

                            if (files.Count > 0)
                            {
                                foreach (string file in files)
                                {
                                    var segments = file.Split('|');

                                    Debug.Write(GetFilename(segments[0]) + "... ");

                                    if (segments.Length == 2)
                                    {
                                        FileInfo fileInfo     = new FileInfo(MapPath(segments[0]));
                                        DateTime lastModified = fileInfo.LastWriteTime;

                                        if (segments[1] != lastModified.DateFormat(DateFormats.Utc))
                                        {
                                            process = true;
                                            Debug.WriteLine(" modified, will recompile...");
                                            break;
                                        }

                                        else
                                        {
                                            Debug.WriteLine(" unchanged");
                                        }
                                    }

                                    else
                                    {
                                        process = true;
                                        Debug.WriteLine(" has no previous timestamp, will recompile...");
                                        break;
                                    }
                                }
                            }

                            else
                            {
                                process = true;
                                Debug.WriteLine("No files list is present, will recompile...");
                            }
                        }

                        else
                        {
                            process = true;
                            Debug.WriteLine("Cannot determine prior build timestamps, will recompile...");
                        }
                    }
                }
            }

            #endregion

            #region No state or file(s) modified, process...

            if (process == true)
            {
                var debugOptions = new LibSass.Compiler.Options.SassOptions
                {
                    OutputStyle              = LibSass.Compiler.Options.SassOutputStyle.Compact,
                    IncludeSourceComments    = false,
                    IncludeSourceMapContents = true,
                    OutputPath    = MapPath(outputPath),
                    SourceMapFile = outputPath + ".map",
                    InputPath     = MapPath(scssInputPath)
                };

                var releaseOptions = new LibSass.Compiler.Options.SassOptions
                {
                    OutputStyle              = LibSass.Compiler.Options.SassOutputStyle.Compact,
                    IncludeSourceComments    = false,
                    IncludeSourceMapContents = false,
                    OutputPath = MapPath(outputPath),
                    InputPath  = MapPath(scssInputPath)
                };

                try
                {
                    var cssResult = new LibSass.Compiler.SassResult();
                    var css       = "";
                    var map       = "";

                    if (debugMode == false)
                    {
                        var sassCompiler = new LibSass.Compiler.SassCompiler(releaseOptions);
                        var cssc         = new Yahoo.Yui.Compressor.CssCompressor();

                        cssResult = sassCompiler.Compile();
                        css       = cssc.Compress(cssResult.Output);
                    }

                    else
                    {
                        var sassCompiler = new LibSass.Compiler.SassCompiler(debugOptions);

                        cssResult = sassCompiler.Compile();
                        css       = cssResult.Output;
                        map       = cssResult.SourceMap;
                    }

                    if (outputPath.FileExists())
                    {
                        DeleteFiles(outputPath);
                        Debug.WriteLine("Deleted " + outputPath);
                    }

                    if ((outputPath + ".map").FileExists())
                    {
                        DeleteFiles(outputPath + ".map");
                        Debug.WriteLine("Deleted " + outputPath + ".map");
                    }

                    WriteFile(outputPath, css);
                    Debug.WriteLine("Generated " + outputPath);

                    if (debugMode == true)
                    {
                        WriteFile(outputPath + ".map", map);
                        Debug.WriteLine("Generated " + outputPath + ".map");
                    }

                    if (cssResult.IncludedFiles.Length > 0)
                    {
                        ArrayList fileList = new ArrayList();

                        foreach (var file in cssResult.IncludedFiles)
                        {
                            FileInfo fileInfo     = new FileInfo(MapPath(file));
                            DateTime lastModified = fileInfo.LastWriteTime;
                            var      item         = file + "|" + lastModified.DateFormat(DateFormats.Utc);

                            fileList.Add(item);
                            Debug.WriteLine(item);
                        }

                        HttpContext.Current.Application[ConvertFilePathToKey(scssInputPath)] = fileList;
                        Debug.WriteLine("Saved state for " + fileList.Count + " files");
                        Debug.WriteLine("");
                    }

                    else
                    {
                        Debug.WriteLine("No files to process, aborting");
                        Debug.WriteLine("");
                    }
                }

                catch (Exception e)
                {
                    Debug.WriteLine("LibSass error: " + e.Message);
                    Debug.WriteLine("");
                }
            }

            else
            {
                Debug.WriteLine("File(s) not modified, aborting");
                Debug.WriteLine("");
            }

            #endregion
        }