Ejemplo n.º 1
0
        /// <summary>
        /// Compiles CS Extension into Assembly given a resource file
        /// TODO: compiler options
        /// </summary>
        /// <param name="resource"></param>
        internal static string ModeCSCompile(String resource)
        {
            String csxCode = "";

            Console.WriteLine("Compiling Extension fron File resource {0}", resource);
            String assemblyPath = String.Empty;

            // Local FS load
            if (File.Exists(resource))
            {
                try
                {
                    csxCode = File.ReadAllText(resource);
                    String compoptions = "/optimize";

                    assemblyPath = DynCSharpRunner.CompileSource(csxCode, false, false, compoptions);
                }
                catch (Exception fex)
                {
                    GeneralUtil.Usage(fex.Message);
                }
            }
            else
            {
                GeneralUtil.Usage("Check file location: " + resource);
            }
            return(assemblyPath);
        }
Ejemplo n.º 2
0
        private void replDCS()
        {
            bool replRunning = true;

            while (replRunning)
            {
                String dirLine    = String.Empty;
                String codeLine   = String.Empty;
                String directives = String.Empty;
                String code       = String.Empty;

                LookAndFeel.ResetColorConsole(true);
                LookAndFeel.SetColorConsole(ConsoleColor.Yellow, ConsoleColor.Black);

                while (replRunning)
                {
                    Console.Write("directive> ");
                    dirLine = Console.ReadLine();

                    // Imeddiately exit REPL
                    if (dirLine.StartsWith("QUIT"))
                    {
                        replRunning = false;
                        break;
                    }

                    if (dirLine.StartsWith("END"))
                    {
                        break;
                    }

                    directives += dirLine;
                }


                while (replRunning)
                {
                    Console.Write("code> ");
                    codeLine = Console.ReadLine();

                    // Imeddiately exit REPL
                    if (codeLine.StartsWith("QUIT"))
                    {
                        replRunning = false;
                        break;
                    }

                    if (codeLine.StartsWith("END"))
                    {
                        break;
                    }

                    code += codeLine;
                }

                if (replRunning)
                {
                    if (ConfigUtil.DEBUG)
                    {
                        Console.WriteLine("Directives: \n{0}, \nCode: \n{1}", directives, code);
                    }
                    LookAndFeel.SetColorConsole(ConsoleColor.Red, ConsoleColor.Black);
                    Console.WriteLine("Perforing dynamic compile and execution.");
                    if (!DynCSharpRunner.CompileRunSnippet(directives, code))
                    {
                        Console.WriteLine("Errors in compilation...");
                    }
                    LookAndFeel.ResetColorConsole(true);
                }
                else
                {
                    return;
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Invoke CS extension script
        /// </summary>
        internal static void ModeCSExec(String resource, String method, String klass)
        {
            //CompileRunSource
            Console.WriteLine(resource);

            String csxCode = "";

            switch (method.ToLower())
            {
            case "afile":
                Console.WriteLine("Execution of Extension from Assembly. Load and execute.");
                // Local FS load
                if (File.Exists(resource))
                {
                    try
                    {
                        Console.WriteLine("Before LoadAndRun");
                        DynCSharpRunner.LoadAndRunType(resource, klass);
                    }
                    catch (Exception fex)
                    {
                        GeneralUtil.Usage(fex.Message);
                    }
                }
                else
                {
                    GeneralUtil.Usage("Check file location: " + resource);
                }

                break;

            case "sfile":
                Console.WriteLine("Execution of Extension Source File. Compile and execute on the fly");

                if (klass == String.Empty)
                {
                    GeneralUtil.Usage("Need a Namespace.Class to run ");
                    break;
                }
                // Local FS load
                if (File.Exists(resource))
                {
                    try
                    {
                        csxCode = File.ReadAllText(resource);
                        DynCSharpRunner.CompileRunXSource(csxCode, klass);
                    }
                    catch (Exception fex)
                    {
                        GeneralUtil.Usage(fex.Message);
                    }
                }
                else
                {
                    GeneralUtil.Usage("Check file location: " + resource);
                }

                break;

            default:
                GeneralUtil.Usage("Unknown -method: " + method);
                break;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Run Cs extension ora Py Script
        /// </summary>
        /// <param name="mmLocation"></param>
        public static void RunMMRecord(String mmLocation)
        {
            Dictionary <String, MMRecord> mmfRepo = MMemoryLoader.GetMMFRepo();

            Console.WriteLine("In RunMMRecord");

            if (mmfRepo.ContainsKey(mmLocation))
            {
                MMRecord mmr;
                if (mmfRepo.TryGetValue(mmLocation, out mmr))
                {
                    Console.WriteLine("After TryGetValue");
                    Console.WriteLine("Found {0}, {1}, {2}", mmr.ResourceSize, mmr.ResourceType, mmr.ResourceMMFile);

                    switch (mmr.ResourceType.ToLower())
                    {
                    case "py":
                        try
                        {
                            dynamic pengine       = IPythonUtil.GetPyEngine();
                            dynamic pscope        = IPythonUtil.GetNewScope();
                            String  pyCodeContent = MMemoryLoader.MMRecordContentString(mmLocation);
                            if (ConfigUtil.DEBUG)
                            {
                                Console.WriteLine("Python Code: \n\n__BEGIN__\n\n{0}\n\n__END__\n\n", pyCodeContent);
                            }

                            dynamic pythonScript = IPythonUtil.GetPyEngine().CreateScriptSourceFromString(pyCodeContent);
                            Console.WriteLine("Execute Python script ({0}) contents from memory", mmLocation);
                            pythonScript.Execute(pscope);
                        }
                        catch (Exception ae)
                        {
                            Console.WriteLine("Iron Python Scope/Execution not created: {0}", ae.Message);
                        }
                        break;

                    case "cs":

                        Console.WriteLine("ResourceType {0}", mmr.ResourceType);
                        String csCodeContent = MMemoryLoader.MMRecordContentString(mmLocation);

                        Console.WriteLine("csCodeContent {0}", csCodeContent);

                        // Load CS Source Code of the extension, MMRecord's name is the name of the TypeToRun param
                        // when MMemoryLoader loads resource it might contain extension on the file, strip it when passing to TypeToRun
                        // Name of the MMRecord is the name of the TypeToRun minus extension. Name your extension file accordingly
                        // Example: If Memory File is WmiQuery.cs then TypeToRun is `WmiQuery`
                        DynCSharpRunner.CompileRunXSource(csCodeContent,
                                                          String.Join(".", new String[2] {
                            "Typhoon.Extensions",
                            mmLocation.Replace(
                                String.Concat(new String[] { ".", mmr.ResourceType }),
                                String.Empty)
                        }));
                        break;

                    default:
                        Console.WriteLine("Unknown ResourceType {0}", mmr.ResourceType);
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("Unable to get {0}. Valid value?", mmLocation);
                }
            }
            else
            {
                Console.WriteLine("Unable to find {0}. Valid value?", mmLocation);
            }
        }