Example #1
0
        public void Preprocess(PreprocessorOptions options) {
            Stream outputStream = options.TargetFile.GetStream();
            StreamWriter outputWriter = null;

            PreprocessorTextReader preprocessor = null;
            try {
                if (outputStream != null) {
                    outputWriter = new StreamWriter(outputStream);
                    preprocessor = new PreprocessorTextReader(options.SourceFile,
                                                              options.PreprocessorVariables,
                                                              _includeResolver,
                                                              _scriptInfo);

                    TextReader contentReader = preprocessor;
                    if (options.Minimize) {
                        contentReader = new CondenserTextReader(preprocessor, options.StripCommentsOnly);
                    }

                    if (preprocessor.Initialize(outputWriter)) {
                        int ch;
                        while ((ch = contentReader.Read()) != -1) {
                            if (options.UseWindowsLineBreaks && (ch == '\n')) {
                                outputWriter.Write('\r');
                            }
                            outputWriter.Write((char)ch);
                        }
                    }
                }
            }
            catch (PreprocessorException pe) {
                if (_errorHandler != null) {
                    _errorHandler.ReportError(pe.Message, pe.SourceFile + " (" + pe.Line + ", 1)");
                }

                if (pe.InnerException != null) {
                    throw pe.InnerException;
                }
            }
            finally {
                if (preprocessor != null) {
                    preprocessor.Close();
                }
                if (outputWriter != null) {
                    outputWriter.Flush();
                }
                if (outputStream != null) {
                    options.TargetFile.CloseStream(outputStream);
                }
            }
        }
Example #2
0
        private void GenerateScript()
        {
            if (_options.TemplateFile != null)
            {
                PreprocessorOptions preprocessorOptions = new PreprocessorOptions();
                preprocessorOptions.SourceFile  = _options.TemplateFile;
                preprocessorOptions.TargetFile  = _options.ScriptFile;
                preprocessorOptions.DebugFlavor = _options.DebugFlavor;
                preprocessorOptions.Minimize    = _options.Minimize;
                // preprocessorOptions.StripCommentsOnly = _options.StripCommentsOnly;
                preprocessorOptions.UseWindowsLineBreaks  = !_options.Minimize;
                preprocessorOptions.PreprocessorVariables = _options.Defines;

                ScriptPreprocessor preprocessor = new ScriptPreprocessor(this, this, this);
                preprocessor.Preprocess(preprocessorOptions);
            }
            else
            {
                Stream     outputStream = null;
                TextWriter outputWriter = null;

                try {
                    outputStream = _options.ScriptFile.GetStream();
                    if (outputStream == null)
                    {
                        ((IErrorHandler)this).ReportError("Unable to write to file " + _options.ScriptFile.FullName,
                                                          _options.ScriptFile.FullName);
                        return;
                    }

                    outputWriter = new StreamWriter(outputStream);

#if DEBUG
                    if (_options.InternalTestMode)
                    {
                        if (_testOutput != null)
                        {
                            outputWriter.Write(_testOutput);
                            outputWriter.WriteLine("Script");
                            outputWriter.WriteLine("================================================================");
                            outputWriter.WriteLine();
                            outputWriter.WriteLine();
                        }
                    }
#endif // DEBUG

                    GenerateScriptCore(outputWriter);
                }
                catch (Exception e) {
                    Debug.Fail(e.ToString());
                }
                finally {
                    if (outputWriter != null)
                    {
                        outputWriter.Flush();
                    }
                    if (outputStream != null)
                    {
                        _options.ScriptFile.CloseStream(outputStream);
                    }
                }
            }
        }
Example #3
0
        public bool Execute()
        {
            if (_commandLine.ShowHelp)
            {
                ShowHelp();
                return(true);
            }

            List <string> predefinedVariables = new List <string>();
            bool          debug                = false;
            bool          minimize             = true;
            bool          stripCommentsOnly    = false;
            bool          useWindowsLineBreaks = false;

            if (_commandLine.Options.Contains("debug"))
            {
                predefinedVariables.Add("DEBUG");
                debug = true;
            }

            string definesArgument = (string)_commandLine.Options["d"];

            if (definesArgument != null)
            {
                string[] defines = definesArgument.Split(',');
                predefinedVariables.AddRange(defines);
            }

            if (_commandLine.Options.Contains("noMin"))
            {
                minimize = false;
            }

            if (minimize)
            {
                if (_commandLine.Options.Contains("stripCommentsOnly"))
                {
                    stripCommentsOnly = true;
                }
            }

            if (_commandLine.Options.Contains("crlf"))
            {
                useWindowsLineBreaks = true;
            }

            string inputFile  = (string)_commandLine.Options["input"];
            string outputFile = (string)_commandLine.Options["output"];

            if (String.IsNullOrEmpty(inputFile))
            {
                ShowError("Input file was not specified.");
                return(false);
            }
            if (File.Exists(inputFile) == false)
            {
                ShowError("The specified input file does not exist.");
                return(false);
            }

            PreprocessorOptions options = new PreprocessorOptions();

            options.SourceFile            = new FileInputStreamSource(inputFile);
            options.TargetFile            = new FileOutputStreamSource(outputFile);
            options.DebugFlavor           = debug;
            options.Minimize              = minimize;
            options.StripCommentsOnly     = stripCommentsOnly;
            options.UseWindowsLineBreaks  = useWindowsLineBreaks;
            options.PreprocessorVariables = predefinedVariables;

            try {
                if (_commandLine.HideAbout == false)
                {
                    Console.WriteLine(GetAboutString());
                }

                ScriptPreprocessor preprocessor = new ScriptPreprocessor(this, this);
                preprocessor.Preprocess(options);

                return(true);
            }
            catch (Exception e) {
                ShowError("Unhandled Exception: " + Environment.NewLine + e.Message);
            }

            return(false);
        }
Example #4
0
        private void GenerateScript() {
            if (_options.TemplateFile != null) {
                PreprocessorOptions preprocessorOptions = new PreprocessorOptions();
                preprocessorOptions.SourceFile = _options.TemplateFile;
                preprocessorOptions.TargetFile = _options.ScriptFile;
                preprocessorOptions.DebugFlavor = _options.DebugFlavor;
                preprocessorOptions.Minimize = _options.Minimize;
                // preprocessorOptions.StripCommentsOnly = _options.StripCommentsOnly;
                preprocessorOptions.UseWindowsLineBreaks = !_options.Minimize;
                preprocessorOptions.PreprocessorVariables = _options.Defines;

                ScriptPreprocessor preprocessor = new ScriptPreprocessor(this, this, this);
                preprocessor.Preprocess(preprocessorOptions);
            }
            else {
                Stream outputStream = null;
                TextWriter outputWriter = null;

                try {
                    outputStream = _options.ScriptFile.GetStream();
                    if (outputStream == null) {
                        ((IErrorHandler)this).ReportError("Unable to write to file " + _options.ScriptFile.FullName,
                                                                  _options.ScriptFile.FullName);
                        return;
                    }

                    outputWriter = new StreamWriter(outputStream);

#if DEBUG
                    if (_options.InternalTestMode) {
                        if (_testOutput != null) {
                            outputWriter.Write(_testOutput);
                            outputWriter.WriteLine("Script");
                            outputWriter.WriteLine("================================================================");
                            outputWriter.WriteLine();
                            outputWriter.WriteLine();
                        }
                    }
#endif // DEBUG

                    GenerateScriptCore(outputWriter);
                }
                catch (Exception e) {
                    Debug.Fail(e.ToString());
                }
                finally {
                    if (outputWriter != null) {
                        outputWriter.Flush();
                    }
                    if (outputStream != null) {
                        _options.ScriptFile.CloseStream(outputStream);
                    }
                }
            }
        }
Example #5
0
        public bool Execute()
        {
            if (_commandLine.ShowHelp) {
                ShowHelp();
                return true;
            }

            List<string> predefinedVariables = new List<string>();
            bool debug = false;
            bool minimize = true;
            bool stripCommentsOnly = false;
            bool useWindowsLineBreaks = false;

            if (_commandLine.Options.Contains("debug")) {
                predefinedVariables.Add("DEBUG");
                debug = true;
            }

            string definesArgument = (string)_commandLine.Options["d"];
            if (definesArgument != null) {
                string[] defines = definesArgument.Split(',');
                predefinedVariables.AddRange(defines);
            }

            if (_commandLine.Options.Contains("noMin")) {
                minimize = false;
            }

            if (minimize) {
                if (_commandLine.Options.Contains("stripCommentsOnly")) {
                    stripCommentsOnly = true;
                }
            }

            if (_commandLine.Options.Contains("crlf")) {
                useWindowsLineBreaks = true;
            }

            string inputFile = (string)_commandLine.Options["input"];
            string outputFile = (string)_commandLine.Options["output"];

            if (String.IsNullOrEmpty(inputFile)) {
                ShowError("Input file was not specified.");
                return false;
            }
            if (File.Exists(inputFile) == false) {
                ShowError("The specified input file does not exist.");
                return false;
            }

            PreprocessorOptions options = new PreprocessorOptions();
            options.SourceFile = new FileInputStreamSource(inputFile);
            options.TargetFile = new FileOutputStreamSource(outputFile);
            options.DebugFlavor = debug;
            options.Minimize = minimize;
            options.StripCommentsOnly = stripCommentsOnly;
            options.UseWindowsLineBreaks = useWindowsLineBreaks;
            options.PreprocessorVariables = predefinedVariables;

            try {
                if (_commandLine.HideAbout == false) {
                    Console.WriteLine(GetAboutString());
                }

                ScriptPreprocessor preprocessor = new ScriptPreprocessor(this, this);
                preprocessor.Preprocess(options);

                return true;
            }
            catch (Exception e) {
                ShowError("Unhandled Exception: " + Environment.NewLine + e.Message);
            }

            return false;
        }
Example #6
0
        public void Preprocess(PreprocessorOptions options)
        {
            Stream       outputStream = options.TargetFile.GetStream();
            StreamWriter outputWriter = null;

            PreprocessorTextReader preprocessor = null;

            try {
                if (outputStream != null)
                {
                    outputWriter = new StreamWriter(outputStream);
                    preprocessor = new PreprocessorTextReader(options.SourceFile,
                                                              options.PreprocessorVariables,
                                                              _includeResolver,
                                                              _scriptInfo);

                    TextReader contentReader = preprocessor;
                    if (options.Minimize)
                    {
                        contentReader = new CondenserTextReader(preprocessor, options.StripCommentsOnly);
                    }

                    if (preprocessor.Initialize(outputWriter))
                    {
                        int ch;
                        while ((ch = contentReader.Read()) != -1)
                        {
                            if (options.UseWindowsLineBreaks && (ch == '\n'))
                            {
                                outputWriter.Write('\r');
                            }
                            outputWriter.Write((char)ch);
                        }
                    }
                }
            }
            catch (PreprocessorException pe) {
                if (_errorHandler != null)
                {
                    _errorHandler.ReportError(pe.Message, pe.SourceFile + " (" + pe.Line + ", 1)");
                }

                if (pe.InnerException != null)
                {
                    throw pe.InnerException;
                }
            }
            finally {
                if (preprocessor != null)
                {
                    preprocessor.Close();
                }
                if (outputWriter != null)
                {
                    outputWriter.Flush();
                }
                if (outputStream != null)
                {
                    options.TargetFile.CloseStream(outputStream);
                }
            }
        }