Example #1
0
        /// <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);
            }
        }
Example #2
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);
        }
Example #3
0
 private Compiler(string path)
 {
     options           = new LibSass.Compiler.Options.SassOptions();
     options.InputPath = path;
 }
Example #4
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
        }