All pastes #793148 Raw Edit

Something

public text v1 · immutable
·published 2007-11-21 23:53 UTC
rendered paste body
//Name   $ C# Directory Compiler
//Author $ Dylan Johnston
//Date   $ November 21, 2007
//Notes  $ :D|-<

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace WindowsApplication3
{
    public partial class Form1 : Form
    {
        public Process p;
        public TextReader t;
        public int Last;
        public Form1()
        {
            InitializeComponent();
            if ( !Directory.Exists( @"out" ) ) {
                Directory.CreateDirectory( @"out" );
            }
            Last = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Clear it every time we click the button :D
            txtMain.Text = "";
            int n = 0;
            string CSType = "";
            string[] library = { "", "", "", "", "", "" }; //Has to be a better way!

            //Make sure we've got a directory!
            if (folderBrowserDialog1.SelectedPath == "") {
                txtMain.Text += "Please select a folder!";
                return;
            }

            //Make sure we've got an output file! (and check if it's a dll or exe)
            if (txtOut.Text != "" && txtOut.Text.ToLower().EndsWith(".dll") || txtOut.Text.ToLower().EndsWith(".exe")) {
                txtMain.Text += "Building ";
                if ( txtOut.Text.ToLower().EndsWith( ".dll" ) )
                {
                    txtMain.Text += "library file";
                    CSType = "dll";
                }
                if ( txtOut.Text.ToLower().EndsWith( ".exe" ) ) {
                    txtMain.Text += "executable file";
                    CSType = "exe";
                }
                txtMain.Text += " ( " + txtOut.Text + " ) \r\n";
            }
            else {
                txtMain.Text += "Error:\r\nInvalid output filename :(";
                return;
            }
            //Make sure we've only got 1 period!
            string[] splits = txtOut.Text.Split('.');
            if (splits.Length > 2) {
                txtMain.Text += "Error:\r\nPlease only use 1 period in the filename!\r\n";
                return;
            }

            String FLocation = folderBrowserDialog1.SelectedPath;

            //Make sure settings.cfg exists!
            try {
                t = new StreamReader( "settings.cfg" );
            }
            catch ( FileNotFoundException ) {
                txtMain.Text = "Error:\r\nSettings.cfg not found\r\n";
                return;
            }

            String Settings = t.ReadLine();
            String CSLocation = "";

            //Create loop to find all the information in settings.cfg
            while ( Settings != null ) {
                //Close if it's null
                if ( Settings == null ) {
                    txtMain.Text = "Error:\r\nInvalid Config\r\n";
                    return;
                }

                //Check for csc.exe (MSVC C# compiler)
                if ( Settings.StartsWith( "Location: " ) && Settings.EndsWith( "\\csc.exe" ) ) {
                    CSLocation = Settings.Remove( 0, 10 );
                    txtMain.Text += "Loading Directory:\r\n" + FLocation + "\r\n\r\n";
                }

                //Check for Librarys
                else if ( Settings.StartsWith( "Library: " ) && Settings.EndsWith( ".dll" ) ) {
                    library[n] = Settings.Remove( 0, 9 );
                    txtMain.Text += "Load Library :\r\n" + library[n] + "\r\n";
                    n++;
                }

                //Should happen if there is a blank or wierd settings.cfg
                else {
                    txtMain.Text += "Error:\r\nInvalid Config\r\n";
                    return;
                }

                //Move the line
                Settings = t.ReadLine();
            }

            DirectoryInfo DIR = new DirectoryInfo( FLocation );
            FileInfo[] files = DIR.GetFiles("*.cs");
            //Make sure we got files!
            if (files.Length == 0) {
                txtMain.Text += "Error:\r\nCouldn't locate any .cs files :(\r\n";
                return;
            }
            txtMain.Text += "Located Files:\r\n";

            //List files for giggles & shits.
            foreach(FileInfo fi in files) {
                txtMain.Text += fi.Name + "\r\n";
            }

            //Arrrrrrr guments.
            string Arguments = "";

            //Now we create the argument to pass to csc.exe
            if (cbDebug.CheckState == CheckState.Checked) //Are we debugging?
                Arguments = Arguments + "/define:DEBUG ";
            if (cbOpt.CheckState == CheckState.Checked) //Are we optimizing?
                Arguments = Arguments + "/optimize ";
            if (CSType == "dll")
                Arguments = Arguments + "/target:library ";

            else if ( CSType == "exe" && comboBox1.SelectedIndex.Equals( 0 ) ) //app
                Arguments = Arguments + "/target:winexe ";

            else if ( CSType == "exe" && comboBox1.SelectedIndex.Equals( 1 ) ) //console
                Arguments = Arguments + "/target:exe ";

            //Add library refrences to arguments
            for ( int a = 0; a < library.Length; a++ ) {
                if ( library[a].Length > 1 )
                    Arguments = Arguments + "/r:\"" + library[a] + "\" ";
            }

            //Add output file to arguments
            Arguments = Arguments + "/out:\"" + System.Environment.CurrentDirectory + "/out/" + txtOut.Text + "\" /nologo \"" + FLocation + "\\*.cs\"";
            txtMain.Text += "\r\nARGUMENTS:\r\n" + Arguments + "\r\n\r\n";
            try {
                p = new Process();
                p.StartInfo.FileName = CSLocation;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.Arguments = Arguments; //LETS MAKE DAT SHIT
                //Create a StreamReader Here And output error to txtMain
                p.Start();
                p.WaitForExit();
            }
            catch (FileNotFoundException) {
                //Lame :(
                txtMain.Text += "Error:\r\ncsc.exe not found!\r\n";
            }

            //Check if compiled.
            if ( File.Exists( System.Environment.CurrentDirectory + "\\out\\" + txtOut.Text ) )
                txtMain.Text += "Success!\r\nCompiled to \\out\\" + txtOut.Text + "\r\n";
            else
                txtMain.Text += "Error\r\nSomething catastrophic!\r\n";

        }

        //For an ease of use lets make this crap go away when we hover over it.
        private void txtOut_MouseHover(object sender, EventArgs e) {
            if ( txtOut.Text == "Output File Name" )
                txtOut.Text = "";
        }

        private void txtOut_MouseLeave(object sender, EventArgs e) {
            if ( txtOut.Text == "" )
                txtOut.Text = "Output File Name";
        }

        //Load Button
        private void button2_Click(object sender, EventArgs e) {
            folderBrowserDialog1.ShowDialog();
        }
        //Trys to restore last used position on the dropdownlist
        private void OutChange( object sender, EventArgs e ) {
            if ( txtOut.Text.EndsWith( ".exe" ) ) {
                comboBox1.Enabled = true;
                comboBox1.Items.Clear();
                comboBox1.Items.AddRange( new object[] {
                    "Application",
                    "Console"
                } );
                comboBox1.SelectedIndex = Last;
            }
            else if ( txtOut.Text.EndsWith( ".dll" ) ) {
                comboBox1.Enabled = true;
                comboBox1.Items.Clear();
                comboBox1.Items.Add( "Library" );
                comboBox1.SelectedIndex = 0;
            }
            else {
                comboBox1.Items.Clear();
                comboBox1.Items.Add( "<-- :)" );
                comboBox1.SelectedIndex = 0;
                comboBox1.Enabled = false;
                
            }
        }

        //Save last used position!
        private void comboBox1_SelectionChangeCommitted( object sender, EventArgs e ) {
            Last = comboBox1.SelectedIndex;
        }
    }
}