コード例 #1
0
    }     //Main

    /// <summary>
    /// Invokes tar and builds the list of files in the archive which are not to be deleted.
    /// </summary>
    /// <param name="CurrentSettings"></param>
    public static void RunCommand(Settings CurrentSettings)
    {
        try
        {
            StringDictionary FileTable     = new StringDictionary();
            ArrayList        DeleteList    = new ArrayList();
            string           DeleteCommand = null;
            string           sep           = Path.DirectorySeparatorChar.ToString();

            StreamWriter OutFile = new StreamWriter(CurrentSettings.DeletionScriptPath +
                                                    Path.DirectorySeparatorChar +
                                                    m_DeleteScriptName);

            switch (CurrentSettings.OSType)
            {
            case OSTypeEnum.Windows:
            {
                DeleteCommand = "@del /f ";
                break;
            }     //case

            case OSTypeEnum.Unix:
            {
                OutFile.WriteLine("#!/bin/sh");
                DeleteCommand = "rm -f -v ";
                break;
            }     //case

            default:
            {
                throw new System.InvalidOperationException("Invalid OSTypeEnum value.");
            }     //case
            } //switch

            string fullCommand = m_CommandArgument + CurrentSettings.TarFileName;
            // Check to see that tar is in path.

            Console.WriteLine();
            // tar fails on the Mac.  Try gnutar first.
            if (ToolInstalled("gnutar"))
            {
                m_CommandName = "gnutar";
                Console.WriteLine("Found utility named: {0}", m_CommandName);
            } //if
            else
            {
                if (ToolInstalled("tar"))
                {
                    m_CommandName = "tar";
                    Console.WriteLine("Found utility named: {0}", m_CommandName);
                    Console.WriteLine("Tar utility may truncate file names on Mac OS X.");
                }    //if
                else //No tar installed.
                {
                    Console.WriteLine("No tar utility found. Exiting...");
                    System.InvalidOperationException ioe = new System.InvalidOperationException("No tar utility found.");
                    throw ioe;
                } //else
            }

            ConsoleProcess toolProc = new ConsoleProcess(m_CommandName, fullCommand);
            Console.WriteLine(m_nl + "Starting command {0} {1}", m_CommandName, fullCommand);
            toolProc.Start();

            // Wait for all IO to complete.
            toolProc.WaitForOutput();

            // Get standard output and error (if any).
            string toolStdOut   = toolProc.StandardOutputString;
            string toolStdError = toolProc.StandardErrorString;

            // If there is output to stdErr or a bad command exit code output warning.
            if (toolStdError != null || toolProc.BaseProcess.ExitCode != 0)
            {
                Console.WriteLine(m_nl +
                                  "*************************** Tool Error ***************************");
                Console.WriteLine(m_nl +
                                  "Exit code: {0}", toolProc.BaseProcess.ExitCode);
                Console.WriteLine(m_nl +
                                  "Error in tool operation: {0}", toolStdError);
                System.Environment.ExitCode = toolProc.BaseProcess.ExitCode;
                return;
            } //if

            if (toolStdOut == null || toolStdOut.Length < 1)
            {
                Console.WriteLine(m_nl + "No file list generated, exiting");
                System.Environment.ExitCode = 1;
                return;
            } //if

            Console.WriteLine(m_nl + "Finished {0} {1}, searching for files to delete ...",
                              m_CommandName,
                              m_CommandArgument);

            StringReader outputList = new StringReader(toolStdOut);
            string       fname      = null;
            string       line       = null;

            while (outputList.Peek() > -1)
            {
                line = outputList.ReadLine();

                // Tar always outputs using forward slashes as the separator char.
                if (CurrentSettings.OSType == OSTypeEnum.Windows)
                {
                    fname = CurrentSettings.SSCLIRootDirectory + sep + line.Replace("/", sep);
                } //if
                else
                {
                    fname = CurrentSettings.SSCLIRootDirectory + sep + line;
                } //else

                if (!Directory.Exists(fname)) // filter out directory names
                {
                    // There is a rare case where the table already contains the name.
                    if (!FileTable.ContainsKey(fname.ToLower()))
                    {
                        FileTable.Add(fname.ToLower(), fname.ToLower());
                    } //if
                }     //if
            }         //while

            CreateDeletionFile(new DirectoryInfo(CurrentSettings.SSCLIRootDirectory),
                               OutFile,
                               FileTable,
                               DeleteList,
                               DeleteCommand,
                               CurrentSettings);
            OutFile.Flush();
            OutFile.Close();

            // Make script executable on Unix
            if (CurrentSettings.OSType == OSTypeEnum.Unix)
            {
                System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo();
                si.FileName  = "chmod";
                si.Arguments = "+x " +
                               CurrentSettings.DeletionScriptPath +
                               Path.DirectorySeparatorChar +
                               m_DeleteScriptName;
                si.UseShellExecute = false;
                System.Diagnostics.Process chmodproc = System.Diagnostics.Process.Start(si);
                chmodproc.WaitForExit();
            } //if

            Console.WriteLine(m_nl +
                              "*********************************************************");
            Console.WriteLine("Deletion script file created at: {0}",
                              CurrentSettings.DeletionScriptPath +
                              Path.DirectorySeparatorChar +
                              m_DeleteScriptName);
        } //try
        catch (Exception e)
        {
            Console.WriteLine("Exception in GenerateFile: {0}", e.ToString());
        } //catch
    }     //GenerateFile()
コード例 #2
0
ファイル: cleanbuild.cs プロジェクト: ArildF/masters
    } //Main

    /// <summary>
    /// Invokes tar and builds the list of files in the archive which are not to be deleted.
    /// </summary>
    /// <param name="CurrentSettings"></param>
    public static void RunCommand(Settings CurrentSettings)
    {
        try
        {
            StringDictionary FileTable = new StringDictionary();
            ArrayList DeleteList = new ArrayList();
            string DeleteCommand = null;
            string sep = Path.DirectorySeparatorChar.ToString();

            StreamWriter OutFile = new StreamWriter(CurrentSettings.DeletionScriptPath + 
                Path.DirectorySeparatorChar + 
                m_DeleteScriptName);

            switch (CurrentSettings.OSType)
            {
                case OSTypeEnum.Windows:
                {
                    DeleteCommand = "@del /f ";
                    break;
                } //case
                case OSTypeEnum.Unix:
                {
                    OutFile.WriteLine("#!/bin/sh");
                    DeleteCommand = "rm -f -v ";
                    break;
                } //case
                default:
                {
                    throw new System.InvalidOperationException("Invalid OSTypeEnum value.");
                } //case
            } //switch

            string fullCommand = m_CommandArgument + CurrentSettings.TarFileName;
            // Check to see that tar is in path.

            Console.WriteLine();
            // tar fails on the Mac.  Try gnutar first.
            if (ToolInstalled("gnutar"))
            {
                m_CommandName = "gnutar";
                Console.WriteLine("Found utility named: {0}", m_CommandName);
            } //if
            else
            {
                if (ToolInstalled("tar"))
                {
                    m_CommandName = "tar";
                    Console.WriteLine("Found utility named: {0}", m_CommandName);
                    Console.WriteLine("Tar utility may truncate file names on Mac OS X.");
                } //if
                else  //No tar installed.
                {
                    Console.WriteLine("No tar utility found. Exiting...");
                    System.InvalidOperationException ioe = new System.InvalidOperationException("No tar utility found.");
                    throw ioe;
                } //else
            }

            ConsoleProcess toolProc = new ConsoleProcess(m_CommandName, fullCommand);
            Console.WriteLine(m_nl + "Starting command {0} {1}", m_CommandName, fullCommand);
            toolProc.Start();

            // Wait for all IO to complete.
            toolProc.WaitForOutput();

            // Get standard output and error (if any).
            string toolStdOut = toolProc.StandardOutputString;
            string toolStdError = toolProc.StandardErrorString;
    
            // If there is output to stdErr or a bad command exit code output warning.
            if (toolStdError != null || toolProc.BaseProcess.ExitCode != 0)
            {
                Console.WriteLine(m_nl + 
                    "*************************** Tool Error ***************************");
                Console.WriteLine(m_nl +
                    "Exit code: {0}", toolProc.BaseProcess.ExitCode);
                Console.WriteLine(m_nl + 
                    "Error in tool operation: {0}", toolStdError);
                System.Environment.ExitCode = toolProc.BaseProcess.ExitCode;
                return;
            } //if
    
            if (toolStdOut == null || toolStdOut.Length < 1)
            {
                Console.WriteLine(m_nl + "No file list generated, exiting");
                System.Environment.ExitCode = 1;
                return;
            } //if

            Console.WriteLine(m_nl + "Finished {0} {1}, searching for files to delete ...", 
                m_CommandName, 
                m_CommandArgument);
		
            StringReader outputList = new StringReader(toolStdOut);
            string fname = null;
            string line = null;
      
            while (outputList.Peek() > -1)
            {
                line = outputList.ReadLine();

                // Tar always outputs using forward slashes as the separator char.
                if (CurrentSettings.OSType == OSTypeEnum.Windows)
                {
                    fname = CurrentSettings.SSCLIRootDirectory + sep + line.Replace("/", sep);
                } //if
                else
                {
                    fname = CurrentSettings.SSCLIRootDirectory + sep + line;
                } //else

                if (!Directory.Exists(fname)) // filter out directory names
                {
                    // There is a rare case where the table already contains the name.
                    if (!FileTable.ContainsKey(fname.ToLower()))
                    {
                        FileTable.Add(fname.ToLower(), fname.ToLower());
                    } //if
                } //if
            } //while
    
            CreateDeletionFile(new DirectoryInfo(CurrentSettings.SSCLIRootDirectory), 
                OutFile, 
                FileTable, 
                DeleteList, 
                DeleteCommand, 
                CurrentSettings);
            OutFile.Flush();
            OutFile.Close();

            // Make script executable on Unix
            if (CurrentSettings.OSType == OSTypeEnum.Unix)
            {
                System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo();
                si.FileName = "chmod";
                si.Arguments = "+x " + 
                    CurrentSettings.DeletionScriptPath + 
                    Path.DirectorySeparatorChar + 
                    m_DeleteScriptName;
                si.UseShellExecute = false;
                System.Diagnostics.Process chmodproc= System.Diagnostics.Process.Start(si);
                chmodproc.WaitForExit();
            } //if

            Console.WriteLine(m_nl +
                "*********************************************************");
            Console.WriteLine("Deletion script file created at: {0}", 
                CurrentSettings.DeletionScriptPath +
                Path.DirectorySeparatorChar +
                m_DeleteScriptName);
        } //try
        catch (Exception e)
        {
            Console.WriteLine("Exception in GenerateFile: {0}", e.ToString());
        } //catch
    } //GenerateFile()