Esempio n. 1
0
        /// <summary>
        /// The function creates a ddl.bat file which calls the back-end specific
        /// command line tool.
        /// </summary>
        /// <param name="processCmd">OS Process Command Object</param>
        /// <param name="processBatchFile">The batch file that will call the command line tool</param>
        /// <param name="cmdFileName">The command file name to parse</param>
        /// <returns>True indicates success; false indicates an error</returns>
        bool ProcessFile(Process processCmd, string processBatchFile, string cmdFileName)
        {
            bool insideCommentBlock = false;

            using (StreamReader sr = new StreamReader(cmdFileName))
            {
                _results.Length = 0;
                string msg = string.Format("Parsing Command File: {0}{1}"
                                           , cmdFileName
                                           , Environment.NewLine);
                _results.Append(msg);
                FileMgr.WriteTextToFile(_outputFileName
                                        , msg
                                        , true
                                        , true);

                string line = sr.ReadLine();
                while (!_exit && line != null)
                {
                    while (_pause && !_exit) // have we been paused
                    {
                        Thread.Sleep(100);
                    }

                    line = line.Trim();
                    if (!insideCommentBlock &&
                        !line.StartsWith(Constants.CommentLineStart) &&
                        !line.StartsWith(Constants.CommentBlockEnd) &&
                        !line.StartsWith(Constants.CommentBlockStart) &&
                        line.Length > 0)
                    {
                        // check for the BreakWithMsg keyword
                        if (line.ToLower().StartsWith(Constants.BreakWithMsg.ToLower()))
                        {
                            // display the message and wait for user input
                            if (ContinueAfterPause(line))
                            {
                                line = sr.ReadLine();
                                continue;
                            }
                            else
                            {
                                _exit = true;
                                return(true);
                            }
                        }

                        if (line.Contains('{') && line.Contains('}'))
                        {
                            line = ReplaceMacros(line);
                        }

                        // check for the call keyword to process another command file
                        if (line.ToLower().StartsWith(Constants.RunCmdFile.ToLower()))
                        {
                            bool aborted = ProcessFile(processCmd, processBatchFile, _ddlSourceDir + "\\"
                                                       + line.ToLower().Replace(
                                                           Constants.RunCmdFile.ToLower(), "").Trim());
                            // check to see if we were aborted
                            if (aborted)
                            {
                                return(aborted);
                            }
                            line = sr.ReadLine();
                            continue;
                        }

                        bool serverOnly = false;
                        // check for the call keyword to process at server level only
                        if (line.ToLower().StartsWith(Constants.ServerOnly.ToLower()))
                        {
                            serverOnly = true;
                            line       = line.ToLower().Replace(Constants.ServerOnly.ToLower(), "").Trim();
                        }

                        string ddlObjectFile = _ddlSourceDir + "\\" + line;
                        if (!File.Exists(ddlObjectFile)) // line is script filename
                        {
                            FileMgr.WriteTextToFile(_outputFileName, string.Format("Error; File: {0} could not be found. {1}"
                                                                                   , ddlObjectFile, Environment.NewLine), true, true);
                        }
                        else
                        {
                            string cmdOutputFile = "cmdOutput.txt";
                            GenerateDDLBatch(ddlObjectFile, processBatchFile, serverOnly, cmdOutputFile);
                            ProcessDDLBatch(processCmd);
                            string results = null;
                            if (File.Exists(cmdOutputFile))
                            {
                                results = FileMgr.ReadTextFileIntoString(cmdOutputFile);
                                if (!string.IsNullOrEmpty(results))
                                {
                                    _results.Append(results);
                                    FileMgr.WriteTextToFile(_outputFileName, results, true, true);
                                    _results.Append(results);
                                }
                            }
                            else // there was some environment setup issue, because there was no cmdOutputFile found
                            {
                                results = string.Format("{0}Error; there was no output produced by the command.{0}"
                                                        + " Possible errors include that the command line utility could not be found.{0}"
                                                        + " Or that the listener for the database is not up or configured for tcpip.{0}"
                                                        + " Or the input credentials are not correct.{0}"
                                                        + " You can try executing the command listed from a dos prompt and viewing the results.{0}"
                                                        + " Please refer to the GettingStarted document.{0}"
                                                        , Environment.NewLine);
                                FileMgr.WriteTextToFile(_outputFileName, results, true, true);
                                _results.Append(results);
                                return(true); // abort the process
                            }
                        }
                    }
                    if (line.StartsWith(Constants.CommentBlockStart))
                    {
                        insideCommentBlock = true;
                    }
                    if (line.StartsWith(Constants.CommentBlockEnd))
                    {
                        insideCommentBlock = false;
                    }
                    line = sr.ReadLine();
                }
            }
            return(_exit ? true : false);
        }