private static void ValidateCssFileSet(CssFileSet fileSet, string[] locales, string[] themes, CssMinificationConfig retailConfig, CssMinificationConfig debugConfig) { // if the debug config is null, there should only be the retail config Assert.IsTrue(fileSet.Minification.Count == (debugConfig == null ? 1 : 2)); ValidateListIsSame(fileSet.Locales, locales); ValidateListIsSame(fileSet.Themes, themes); var retail = fileSet.Minification["Retail"]; Assert.IsNotNull(retail); ValidateCssMinificationConfig(retail, retailConfig); CssMinificationConfig debug; if (!fileSet.Minification.TryGetValue("Debug", out debug)) { debug = null; } if (debugConfig != null) { Assert.IsNotNull(debug); ValidateCssMinificationConfig(debug, debugConfig); } else { Assert.IsNull(debug); } }
private static void ProcessCssFileSet(MinifyCssActivity cssCruncher, CssFileSet fileSet, string configType, string baseOutputPath) { var cssConfig = fileSet.Minification.GetNamedConfig(configType); if (cssConfig.ShouldMinify) { foreach (var file in fileSet.InputSpecs.SelectMany(inputSpec => GetFiles(inputSpec.Path, inputSpec.SearchPattern, inputSpec.SearchOption))) { cssCruncher.ShouldMinify = cssConfig.ShouldMinify; cssCruncher.ShouldOptimize = cssConfig.ShouldMinify; foreach (string hack in cssConfig.ForbiddenSelectors) { cssCruncher.HackSelectors.Add(hack); } cssCruncher.SourceFile = file; cssCruncher.ShouldExcludeProperties = true; // disable lower case validation (this is on by default). cssCruncher.ShouldValidateForLowerCase = cssConfig.ShouldValidateLowerCase; // we are just minifying. Image assembly is a different action. cssCruncher.ShouldAssembleBackgroundImages = false; var outputPath = GetOutputFolder(fileSet.Output, baseOutputPath); cssCruncher.DestinationFile = GetOutputFilename(file, outputPath, true); try { cssCruncher.Execute(); } catch (Exception ex) { AggregateException aggEx; if (ex.InnerException != null && (aggEx = ex.InnerException as AggregateException) != null) { // antlr can throw a blob of errors, so they need to be deduped to get the real set of errors IEnumerable <string> messages = ErrorHelper.DedupeCSSErrors(aggEx); DisplayErrors(messages, file); } else { // Catch, record and display error HandleError(ex, file); } } } } }
/// <summary>Creates the input spec objects based on input and output paths.</summary> /// <param name="wgConfig">Config object to use.</param> /// <param name="inputPath">Input path from the command parameters</param> /// <param name="outputPath">output path from the command parameters</param> private static void AddInputSpecs(WebGreaseConfiguration wgConfig, string inputPath, string outputPath) { if (inputPath.IsNullOrWhitespace() && outputPath.IsNullOrWhitespace()) { // no paths need to be overriden. return; } string outputPathExtension = Path.GetExtension(outputPath); string inputPathExtension = Path.GetExtension(inputPath); bool createCssInput = false; bool createJsInput = false; // Set the file filter to the extension of the output path (if it's a file) if (!outputPathExtension.IsNullOrWhitespace()) { // if the output path is a file we only process css OR js files into it. if (outputPathExtension.EndsWith(Strings.Css, StringComparison.OrdinalIgnoreCase)) { createCssInput = true; } else { createJsInput = true; } } else if (!inputPathExtension.IsNullOrWhitespace()) { // if the input path is not a folder, only set one of the file sets for processing if (inputPathExtension.EndsWith(Strings.Css, StringComparison.OrdinalIgnoreCase)) { createCssInput = true; } else { createJsInput = true; } } else { // if both the intput and output path are not a file, assume they are a folder process both JS and CSS files found within. createCssInput = true; createJsInput = true; } var cssFileSet = new CssFileSet(); var jsFileSet = new JSFileSet(); // set or update input specs if (createCssInput) { cssFileSet.InputSpecs.Add(GetInputSpec(inputPath, Strings.CssFilter)); } if (createJsInput) { if (jsFileSet.InputSpecs.Any()) { jsFileSet.InputSpecs.Clear(); } jsFileSet.InputSpecs.Add(GetInputSpec(inputPath, Strings.JsFilter)); } // set output spec jsFileSet.Output = outputPath; cssFileSet.Output = outputPath; wgConfig.JSFileSets.Add(jsFileSet); wgConfig.CssFileSets.Add(cssFileSet); }