示例#1
0
文件: Program.cs 项目: PJayB/cs-run
        static void SetEntryPoint(string arg, CompileSession session)
        {
            arg = arg.Trim();

            // Split based on last .
            int dot = arg.LastIndexOf('.');

            if (dot == -1 || dot == 0 || dot == arg.Length - 1)
            {
                throw new Exception("Entry point must take the form 'Class.Method'.");
            }

            string entryMethod = arg.Substring(dot + 1).Trim();
            string entryClass  = arg.Substring(0, dot).Trim();

            if (string.IsNullOrEmpty(entryMethod))
            {
                throw new Exception("Entry point must take the form 'Class.Method'.");
            }
            if (string.IsNullOrEmpty(entryClass))
            {
                throw new Exception("Entry point must take the form 'Class.Method'.");
            }

            session.EntryClass  = entryClass;
            session.EntryMethod = entryMethod;
        }
示例#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 == "//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 + " ";
            }
        }
示例#3
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 + " ";
            }
        }
示例#4
0
文件: Program.cs 项目: PJayB/cs-run
        static void SetEntryPoint(string arg, CompileSession session)
        {
            arg = arg.Trim();

            // Split based on last .
             int dot = arg.LastIndexOf('.');
            if (dot == -1 || dot == 0 || dot == arg.Length - 1)
            {
                throw new Exception("Entry point must take the form 'Class.Method'.");
            }

            string entryMethod = arg.Substring(dot + 1).Trim();
            string entryClass = arg.Substring(0, dot).Trim();

            if (string.IsNullOrEmpty(entryMethod))
                throw new Exception("Entry point must take the form 'Class.Method'.");
            if (string.IsNullOrEmpty(entryClass))
                throw new Exception("Entry point must take the form 'Class.Method'.");

            session.EntryClass = entryClass;
            session.EntryMethod = entryMethod;
        }
示例#5
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();

            // 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());
        }
示例#6
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());
        }