예제 #1
0
파일: Program.cs 프로젝트: PJayB/cs-run
        static void AddCompilerSwitch(string arg, CompileSession session)
        {
            // Split based on first :
            string value    = string.Empty;
            int    colonPos = arg.IndexOf(':');

            if (colonPos != -1)
            {
                value = arg.Substring(colonPos + 1).Trim();
                arg   = arg.Substring(0, colonPos).Trim();
            }

            arg = arg.ToLower();

            // Switch
            if (arg == "//ref")
            {
                try
                {
                    session.AddReference(value);
                }
                catch (CompileSession.MatchedPartialReferenceException ex)
                {
                    Console.WriteLine($"WARNING: Adding reference based on partial name '{ex.PartialName}': '{ex.ResolvedName}'");
                }
            }
            else if (arg == "//entrypoint")
            {
                SetEntryPoint(value, session);
            }
            else if (arg == "//partialmatchwarning")
            {
                session.WarnOnPartialMatch = true;
            }
            else if (arg == "//nowarningsaserrors")
            {
                session.WarningsAsErrors = true;
            }
            else if (arg == "//warninglevel")
            {
                if (!int.TryParse(value, out session.WarningLevel))
                {
                    throw new Exception("Warning level expects a valid integer.");
                }
            }
            else if (arg == "//c")
            {
                session.CompilerOptions = value + " ";
            }
        }
예제 #2
0
파일: Program.cs 프로젝트: PJayB/cs-run
        static void AddCompilerSwitch(string arg, CompileSession session)
        {
            // Split based on first :
            string value = string.Empty;
            int colonPos = arg.IndexOf(':');
            if (colonPos != -1)
            {
                value = arg.Substring(colonPos + 1).Trim();
                arg = arg.Substring(0, colonPos).Trim();
            }

            arg = arg.ToLower();

            // Switch
            if (arg == "//ref")
            {
                try
                {
                   session.AddReference(value);
                }
                catch (CompileSession.MatchedPartialReferenceException ex)
                {
                    Console.WriteLine($"WARNING: Adding reference based on partial name '{ex.PartialName}': '{ex.ResolvedName}'");
                }
            }
            else if (arg == "//entrypoint")
            {
                SetEntryPoint(value, session);
            }
            else if (arg == "//nopartialmatchwarning")
            {
                session.WarnOnPartialMatch = false;
            }
            else if (arg == "//nowarningsaserrors")
            {
                session.WarningsAsErrors = true;
            }
            else if (arg == "//warninglevel")
            {
                if (!int.TryParse(value, out session.WarningLevel))
                {
                    throw new Exception("Warning level expects a valid integer.");
                }
            }
            else if (arg == "//c")
            {
                session.CompilerOptions = value + " ";
            }
        }
예제 #3
0
파일: Program.cs 프로젝트: PJayB/cs-run
        static void MainProtected(string[] args)
        {
            // Validate the input arguments are sane
            if (args.Length < 1 || (args.Length == 1 && args[0] == "/?"))
            {
                throw new ShowHelpException();
            }

            CompileSession compileSession = new CompileSession();

            // Add the default references
            compileSession.AddReference("System");
            compileSession.AddReference("System.Core");
            compileSession.AddReference("Microsoft.CSharp");

            // Process the local arguments
            int argumentIndex = 0;

            while (argumentIndex < args.Length && args[argumentIndex].StartsWith("//"))
            {
                AddCompilerSwitch(args[argumentIndex++], compileSession);
            }

            // Get the filename
            if (argumentIndex == args.Length)
            {
                throw new ShowHelpException();
            }

            string filename = args[argumentIndex++];

            // Get the script arguments
            List <string> scriptArguments = new List <string>();

            for (int i = argumentIndex; i < args.Length; ++i)
            {
                scriptArguments.Add(args[i]);
            }

            // Link the session to to local assemblies
            compileSession.AddLocalReferences();

            // Open the input file (which is the script file)
            try
            {
                using (StreamReader reader = new StreamReader(filename))
                {
                    // Load the entire file into a string
                    compileSession.Script = reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error reading script: " + ex.Message);
            }

            // Compile the assembly
            CompilerResults results = compileSession.Compile();

            if (results.Errors.Count > 0)
            {
                foreach (var error in results.Errors)
                {
                    Console.WriteLine(error.ToString());
                }

                if (results.Errors.HasErrors)
                {
                    throw new Exception("Compilation failed with errors.");
                }
            }

            // Execute the assembly
            ExecuteScript(results.CompiledAssembly, compileSession.EntryClass, compileSession.EntryMethod, scriptArguments.ToArray());
        }