示例#1
0
        public void Compile(FileInfo source, FileInfo output)
        {
            IEnumerable <string> includePaths = new[] { source.Directory.FullName };

            if (!string.IsNullOrWhiteSpace(Options.CompilationIncludePaths) && Directory.Exists(Options.CompilationIncludePaths))
            {
                includePaths = includePaths.Concat(Options.CompilationIncludePaths.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries));
            }

            var result = Compiler.CompileFile(source.FullName,
                                              sourceMapPath:          Options.GenerateSourceMaps ? output.FullName + ".map" : null,
                                              includeSourceComments:  Options.IncludeSourceComments,
                                              precision:              Options.Precision,
                                              additionalIncludePaths: includePaths,
                                              outputStyle:            Options.OutputStyle
                                              );

            var css       = result.CSS;
            var sourceMap = result.SourceMap;

            InteropHelper.CheckOut(output.FullName);

            if (Options.IncludeGeneratedCodeWarning)
            {
                css = new StringBuilder("/* AUTOGENERATED CSS: To make changes edit ").Append(source.Name).Append(" */").AppendLine().Append(css).ToString();
            }

            File.WriteAllText(output.FullName, css, UTF8_ENCODING);

            if (Options.GenerateSourceMaps && !string.IsNullOrEmpty(sourceMap))
            {
                InteropHelper.CheckOut(output.FullName + ".map");
                File.WriteAllText(output.FullName + ".map", sourceMap, UTF8_ENCODING);
            }
        }
示例#2
0
        private void Minify(FileInfo source, FileInfo file)
        {
            if (Options.IsDebugLoggingEnabled)
            {
                Logger.Log("Generating minified css file.");
            }

            try
            {
                var    css      = File.ReadAllText(source.FullName);
                string minified = "";
                if (!string.IsNullOrEmpty(css))
                {
                    var compressor = new CssCompressor {
                        RemoveComments = true
                    };
                    minified = compressor.Compress(css);
                }

                InteropHelper.CheckOut(file.FullName);
                File.WriteAllText(file.FullName, minified, UTF8_ENCODING);

                // nest
                if (Options.IncludeCssInProject)
                {
                    AddFileToProject(source, file, Options);
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex, "Failed to generate minified css file.");
                if (Options.ReplaceCssWithException)
                {
                    SaveExceptionToFile(ex, file);
                }
            }
        }