Example #1
0
        protected override void GenerateStyleSheet(OutputGroup outputGroup, IList <InputGroup> inputGroups, SwitchParser switchParser, string outputPath, Encoding outputEncoding)
        {
            if (!FileWriteOperation(outputPath, switchParser.IfNotNull(p => p.Clobber), () =>
            {
                // create the output file, clobbering any existing content
                using (var writer = new StreamWriter(outputPath, false, outputEncoding))
                {
                    if (inputGroups != null && inputGroups.Count > 0)
                    {
                        // for each input file, copy to the output, separating them with a newline (don't need a semicolon like JavaScript does)
                        var addSeparator = false;
                        foreach (var inputGroup in inputGroups)
                        {
                            if (addSeparator)
                            {
                                writer.WriteLine();
                            }
                            else
                            {
                                addSeparator = true;
                            }

                            writer.Write(inputGroup.Source);
                        }
                    }
                }

                return(true);
            }))
            {
                // could not write file
                Log.LogError(Strings.CouldNotWriteOutputFile, outputPath);
            }
        }
Example #2
0
        protected override void Because()
        {
            parser = new SwitchParser(Output, args);

            IsValid = parser.IsValid();
        }
        /// <summary>
        /// Process an output group by deleting the output files if they exist.
        /// </summary>
        /// <param name="outputGroup">the OutputGroup being processed</param>
        /// <param name="outputFileInfo">FileInfo for the desired output file</param>
        /// <param name="symbolsFileInfo">FileInfo for the optional desired symbol file</param>
        /// <param name="defaultSettings">default settings for this output group</param>
        /// <param name="manifestModifiedTime">modified time for the manifest</param>
        protected override void ProcessOutputGroup(OutputGroup outputGroup, FileInfo outputFileInfo, FileInfo symbolsFileInfo, SwitchParser defaultSettings, DateTime manifestModifiedTime)
        {
            // get the settings to use -- take the configuration for this output group
            // and apply them over the default settings
            var settings = ParseConfigSettings(outputGroup.GetConfigArguments(this.Configuration), defaultSettings);

            // we really only care about the clobber setting -- if the file is read-only, don't bother deleting it
            // unless we have the clobber setting. If the current setting is Preserve, then we want to change it to
            // Auto because it makes no sense to not delete a non-readonly file during a "clean"
            var clobber = settings.Clobber == ExistingFileTreatment.Preserve
                ? ExistingFileTreatment.Auto
                : settings.Clobber;

            // we don't care about the inputs, we just want to delete the outputs and be done
            if (outputFileInfo != null)
            {
                if (!FileWriteOperation(outputFileInfo.FullName, clobber, () =>
                {
                    outputFileInfo.IfNotNull(fi =>
                    {
                        if (fi.Exists)
                        {
                            Log.LogMessage(MessageImportance.Normal, Strings.DeletingFile, fi.FullName);
                            fi.Delete();
                        }
                    });
                    return(true);
                }))
                {
                    // can't delete the file - not an error; just informational
                    Log.LogMessage(MessageImportance.Normal, Strings.CouldNotDeleteOutputFile, outputFileInfo.FullName);
                }
            }

            if (symbolsFileInfo != null)
            {
                if (!FileWriteOperation(symbolsFileInfo.FullName, clobber, () =>
                {
                    symbolsFileInfo.IfNotNull(fi =>
                    {
                        if (fi.Exists)
                        {
                            Log.LogMessage(MessageImportance.Normal, Strings.DeletingFile, fi.FullName);
                            fi.Delete();
                        }
                    });
                    return(true);
                }))
                {
                    // can't delete the file - not an error; just informational
                    Log.LogMessage(MessageImportance.Normal, Strings.CouldNotDeleteOutputFile, symbolsFileInfo.FullName);
                }
            }
        }
Example #4
0
        private int ProcessCssFile(IList <InputGroup> inputGroups, SwitchParser switchParser, StringBuilder outputBuilder)
        {
            var retVal = 0;

            // blank line before
            WriteProgress();

            // we can share the same parser object
            var parser = new CssParser();

            parser.Settings   = switchParser.CssSettings;
            parser.JSSettings = switchParser.JSSettings;

            using (var writer = new StringWriter(outputBuilder, CultureInfo.InvariantCulture))
            {
                // if we are echoing the input, then set the settings echo writer to the output stream
                // otherwise make sure it's null
                if (this.m_echoInput)
                {
                    parser.EchoWriter = writer;
                }
                else
                {
                    parser.EchoWriter = null;
                }

                var ndx = 0;
                foreach (var inputGroup in inputGroups)
                {
                    // process input source...
                    parser.CssError += (sender, ea) =>
                    {
                        var error = ea.Error;
                        if (inputGroup.Origin == Configuration.SourceOrigin.Project || error.Severity == 0)
                        {
                            // ignore severity values greater than our severity level
                            if (error.Severity <= switchParser.WarningLevel)
                            {
                                // we found an error
                                m_errorsFound = true;

                                WriteError(error.ToString());
                            }
                        }
                    };

                    if (m_echoInput && ndx > 0)
                    {
                        writer.Write(switchParser.CssSettings.LineTerminator);
                    }

                    // if we want to time this, start a stopwatch now
                    Stopwatch stopwatch = null;
                    if (m_outputTimer)
                    {
                        stopwatch = new Stopwatch();
                        stopwatch.Start();
                    }

                    // crunch the source and output to the string builder we were passed
                    var crunchedStyles = parser.Parse(inputGroup.Source);

                    if (stopwatch != null)
                    {
                        var ticks = stopwatch.ElapsedTicks;
                        stopwatch.Stop();

                        // frequency is ticks per second, so if we divide by 1000.0, then we will have a
                        // double-precision value indicating the ticks per millisecond. Divide this into the
                        // number of ticks we measure, and we'll get the milliseconds in double-precision.
                        var frequency    = Stopwatch.Frequency / 1000.0;
                        var timerMessage = string.Format(CultureInfo.CurrentCulture, AjaxMin.TimerFormat, 0, ticks / frequency);

                        Debug.WriteLine(timerMessage);
                        Debug.WriteLine(string.Empty);
                        WriteProgress(timerMessage);
                        WriteProgress();
                    }

                    // if there is output, send it where it needs to go
                    if (!string.IsNullOrEmpty(crunchedStyles))
                    {
                        if (!m_echoInput)
                        {
                            if (ndx++ > 0)
                            {
                                // separate input group outputs with an appropriate newline
                                writer.Write(switchParser.CssSettings.LineTerminator);
                            }

                            writer.Write(crunchedStyles);
                        }
                    }
                }
            }

            return(retVal);
        }
            public void SetupContext()
            {
                Parser = new SwitchParser<TestConfigSettings>();

                Parser.AddSwitch("--enum", "-e").SetsEnumField(x => x.EnumTest);
            }
Example #6
0
 protected override void Because()
 {
     parser = new SwitchParser(Output, args);
 }
Example #7
0
            public void SetupContext()
            {
                Parser = new SwitchParser <TestConfigSettings>();

                Parser.AddSwitch("--bar", "-b").WithSingleParamAction((c, v) => { c.Bar = v; });
            }
            public void SetupContext()
            {
                Parser = new SwitchParser<TestConfigSettings>();
                Parser.AddSwitch("--foo", "-f")
                    .SetsField(x => x.Foo)
                    .WithDescription("FooDescription");

                Usage = Parser.GetUsage();
            }
Example #9
0
 public void SetupContext()
 {
     Parser = new SwitchParser <TestConfigSettings>();
     Parser.AddSwitch("--foo", "-f").WithAction(c => { c.Foo = true; });
 }
Example #10
0
 public void SetupContext()
 {
     Parser = new SwitchParser <TestConfigSettings>();
 }
Example #11
0
            public void SetupContext()
            {
                Parser = new SwitchParser <TestConfigSettings>();

                Parser.AddSwitch("--enum", "-e").SetsEnumField(x => x.EnumTest);
            }
Example #12
0
 protected override void SetupSwitches(SwitchParser <InPlaceSettings> parser)
 {
     parser.AddSwitch("--tags", "-g")
     .SetsField(x => x.Tags);
 }
Example #13
0
 protected override void SetupSwitches(SwitchParser <StubGenerationSettings> parser)
 {
     parser.AddSwitch("--clipboard", "-b").SetsField(x => x.WriteToClipboard).WithDescription("Writes the generated stubs to the clipboard");
     parser.AddSwitch("--destination", "-d").SetsField(x => x.Destination).WithDescription("Writes the generated stubs to a file");
 }
Example #14
0
            public void SetupContext()
            {
                Parser = new SwitchParser<TestConfigSettings>();

                Parser.AddSwitch("--bar", "-b").WithSingleParamAction((c, v) => { c.Bar = v; });
            }
Example #15
0
 protected ContainerSwitchConfigurator(ContainerConfigurator <settingsT> configurator)
 {
     _configurator = configurator;
     SwitchParser  = new SwitchParser <settingsT>();
     SetupSwitches(SwitchParser);
 }
Example #16
0
            public void SetupContext()
            {
                Parser = new SwitchParser<TestConfigSettings>();

                Parser
                    .AddSwitch("--baz", "-z")
                    .WithMultiParamAction((c, v) => { c.Baz = v; });

                Result = TestWithParams("--baz", "baz1", "baz2", "baz3");
            }
Example #17
0
 protected abstract void SetupSwitches(SwitchParser <settingsT> parser);
Example #18
0
 public void SetupContext()
 {
     Parser = new SwitchParser<TestConfigSettings>();
 }
 protected override void GenerateStyleSheet(OutputGroup outputGroup, IList <InputGroup> inputGroups, SwitchParser switchParser, string outputPath, Encoding outputEncoding)
 {
     // shouldn't get called because we override the ProcessOutputGroup method
     throw new NotImplementedException();
 }
Example #20
0
 public void SetupContext()
 {
     Parser = new SwitchParser<TestConfigSettings>();
     Parser.AddSwitch("--foo", "-f").WithAction(c => { c.Foo = true; });
 }
Example #21
0
        public override IEnumerable <PvcCore.PvcStream> Execute(IEnumerable <PvcCore.PvcStream> inputStreams)
        {
            var          resultStreams = new List <PvcStream>();
            SwitchParser switchParser  = null;

            if (!String.IsNullOrEmpty(this.commandLineSwitches))
            {
                switchParser = new SwitchParser();
                switchParser.Parse(this.commandLineSwitches);
            }

            foreach (var inputStream in inputStreams)
            {
                var dirName      = Path.GetDirectoryName(inputStream.StreamName);
                var fileName     = Path.GetFileNameWithoutExtension(inputStream.StreamName) + ".min" + Path.GetExtension(inputStream.StreamName);
                var resultName   = Path.Combine(dirName, fileName);
                var fileContent  = new StreamReader(inputStream).ReadToEnd();
                var minifier     = new Minifier();
                var sourceStream = new MemoryStream();
                var outputWriter = new StreamWriter(sourceStream);

                if (inputStream.StreamName.EndsWith(".js"))
                {
                    // Currently AjaxMin only supports JS source maps
                    if (this.generateSourceMaps)
                    {
                        var resultMapName = resultName + ".map";
                        var utf8          = new UTF8Encoding(false);
                        var mapStream     = new MemoryStream();
                        var mapWriter     = new SourcemapStreamWriter(mapStream, utf8);
                        var sourceMap     = new V3SourceMap(mapWriter);

                        if (sourceMap != null)
                        {
                            if (switchParser == null)
                            {
                                switchParser = new SwitchParser();
                            }

                            switchParser.JSSettings.SymbolsMap     = sourceMap;
                            switchParser.JSSettings.TermSemicolons = true;

                            sourceMap.StartPackage(resultName, resultMapName);

                            outputWriter.Write(minifier.MinifyJavaScript(fileContent, switchParser.JSSettings));

                            sourceMap.EndPackage();
                            sourceMap.EndFile(outputWriter, "\r\n");

                            sourceMap.Dispose();
                            mapWriter.Flush();

                            resultStreams.Add(new PvcStream(() => mapStream).As(resultMapName));
                        }
                    }
                    else
                    {
                        CodeSettings settings = new CodeSettings();
                        if (switchParser != null)
                        {
                            settings = switchParser.JSSettings;
                        }
                        outputWriter.Write(minifier.MinifyJavaScript(fileContent, settings));
                    }
                }
                else
                {
                    CssSettings settings = new CssSettings();
                    if (switchParser != null)
                    {
                        settings = switchParser.CssSettings;
                    }
                    outputWriter.Write(minifier.MinifyStyleSheet(fileContent, settings));
                }

                foreach (var error in minifier.ErrorList)
                {
                    Console.Error.WriteLine(error.ToString());
                }

                outputWriter.Flush();
                resultStreams.Add(new PvcStream(() => sourceStream).As(resultName));
            }

            return(resultStreams);
        }