Ejemplo n.º 1
0
        public void JSAnalysisTest()
        {
            var sourceDirectory  = Path.Combine(TestDeploymentPaths.TestDirectory, @"WebGrease.Tests\MinifyJSActivityTest");
            var minifyJSActivity = new MinifyJSActivity(new WebGreaseContext(new WebGreaseConfiguration()));

            minifyJSActivity.SourceFile      = Path.Combine(sourceDirectory, @"Input\Case2\test1.js");
            minifyJSActivity.DestinationFile = Path.Combine(sourceDirectory, @"Output\Case2\test1.js");
            minifyJSActivity.MinifyArgs      = "/global:jQuery";
            minifyJSActivity.AnalyzeArgs     = "-analyze -WARN:4";
            minifyJSActivity.ShouldMinify    = true;
            minifyJSActivity.ShouldAnalyze   = true;

            Exception exception = null;

            try
            {
                minifyJSActivity.Execute();
            }
            catch (WorkflowException workflowException)
            {
                exception = workflowException;
            }

            // Assertions
            Assert.IsNotNull(exception);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Executes the minification task
        /// </summary>
        /// <param name="context">context with settings to be used. Only used for the input/output settings.</param>
        private static void ExecuteMinification(IWebGreaseContext context)
        {
            var jsCruncher  = new MinifyJSActivity(context);
            var cssCruncher = new MinifyCssActivity(context);

            // only run the crunchers if the configuration has files for that particular file type
            foreach (var fileSet in context.Configuration.JSFileSets.Where(set => set.InputSpecs.Any()))
            {
                ProcessJsFileSet(jsCruncher, fileSet, context.Configuration.ConfigType, context.Configuration.DestinationDirectory);
            }

            // do the same thing for CSS files... nested loops are fun!
            foreach (var fileSet in context.Configuration.CssFileSets.Where(set => set.InputSpecs.Any()))
            {
                ProcessCssFileSet(cssCruncher, fileSet, context.Configuration.ConfigType, context.Configuration.DestinationDirectory);
            }
        }
Ejemplo n.º 3
0
        private static void ProcessJsFileSet(MinifyJSActivity jsCruncher, JSFileSet fileSet, string configType, string baseOutputPath)
        {
            var jsConfig = fileSet.Minification.GetNamedConfig(configType);

            if (jsConfig.ShouldMinify)
            {
                foreach (var file in fileSet.InputSpecs.SelectMany(inputSpec => GetFiles(inputSpec.Path, inputSpec.SearchPattern, inputSpec.SearchOption)))
                {
                    // execute minification on each file in the set.

                    // configure settings.
                    jsCruncher.ShouldMinify = jsConfig.ShouldMinify;

                    // if we specified some globals to ignore, format them on the command line with the
                    // other minification arguments
                    if (!string.IsNullOrWhiteSpace(jsConfig.GlobalsToIgnore))
                    {
                        jsCruncher.MinifyArgs = Strings.GlobalsToIgnoreArg + jsConfig.GlobalsToIgnore + ' ' + jsConfig.MinificationArugments;
                    }
                    else
                    {
                        jsCruncher.MinifyArgs = jsConfig.MinificationArugments;
                    }

                    jsCruncher.ShouldAnalyze = false; // we are minimizing, not validating

                    jsCruncher.SourceFile = file;
                    var outputPath = GetOutputFolder(fileSet.Output, baseOutputPath);
                    jsCruncher.DestinationFile = GetOutputFilename(file, outputPath, true);

                    try
                    {
                        // execute
                        jsCruncher.Execute();
                    }
                    catch (Exception ex)
                    {
                        HandleError(ex, file);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void JSMinificationTest()
        {
            var sourceDirectory  = Path.Combine(TestDeploymentPaths.TestDirectory, @"WebGrease.Tests\MinifyJSActivityTest");
            var minifyJSActivity = new MinifyJSActivity(new WebGreaseContext(new WebGreaseConfiguration()));

            minifyJSActivity.SourceFile = Path.Combine(sourceDirectory, @"Input\Case1\test1.js");
            var outputFilePath = Path.Combine(sourceDirectory, @"Output\Case1\test1.js");

            minifyJSActivity.DestinationFile = outputFilePath;
            minifyJSActivity.MinifyArgs      = "/global:jQuery";
            minifyJSActivity.ShouldMinify    = true;
            minifyJSActivity.Execute();

            // Assertions
            Assert.IsTrue(File.Exists(outputFilePath));
            var text = File.ReadAllText(outputFilePath);

            Assert.IsTrue(!string.IsNullOrWhiteSpace(text));
            Assert.IsTrue(text == "(function(n){document.write(n)})(jQuery);");
        }
Ejemplo n.º 5
0
        private static void ExecuteValidation(IWebGreaseContext context)
        {
            var jsValidator = new MinifyJSActivity(context);

            foreach (var fileSet in context.Configuration.JSFileSets.Where(set => set.InputSpecs.Any()))
            {
                var jsConfig = fileSet.Validation.GetNamedConfig(context.Configuration.ConfigType);

                if (jsConfig.ShouldAnalyze)
                {
                    foreach (var file in fileSet.InputSpecs.SelectMany(inputSpec => GetFiles(inputSpec.Path, inputSpec.SearchPattern, inputSpec.SearchOption)))
                    {
                        // execute analysis on each of the files in the set
                        jsValidator.ShouldMinify  = false; // hard set to false since we are only validating in this phase.
                        jsValidator.ShouldAnalyze = jsConfig.ShouldAnalyze;
                        jsValidator.AnalyzeArgs   = jsConfig.AnalyzeArguments;
                        jsValidator.SourceFile    = file;
                        var outputPath = GetOutputFolder(fileSet.Output, context.Configuration.DestinationDirectory);
                        jsValidator.DestinationFile = GetOutputFilename(file, outputPath);

                        try
                        {
                            jsValidator.Execute();
                        }
                        catch (Exception ex)
                        {
                            if (ex.InnerException is BuildWorkflowException)
                            {
                                HandleError(ex.InnerException, file);
                            }
                            else
                            {
                                HandleError(ex, file);
                            }
                        }
                    }
                }
            }
        }