Esempio n. 1
0
        /*
        public static void Main(string[] args)
        {
            // arrg. ironruby leaks memory, ruining my whole project. dammit ironruby!
            ScriptRuntimeSetup runtimeSetup = new ScriptRuntimeSetup();
            LanguageSetup rubySetup = Ruby.CreateLanguageSetup();
            runtimeSetup.LanguageSetups.Add(rubySetup);
            var runtime = new ScriptRuntime(runtimeSetup);

            while (Console.ReadLine() != "q")
            {
                ScriptEngine engine = runtime.GetEngine("ruby");

                ScriptScope scriptScope = engine.CreateScope();
                scriptScope.SetVariable("leak", new LeakedObject());
                //scriptScope.SetVariable("console", Console.Out);

                //ScriptSource scriptSource = engine.CreateScriptSourceFromString("tag = {};\ntag['key'] = 'value';\nconsole.write_line(tag['key']);");
                ScriptSource scriptSource = engine.CreateScriptSourceFromString("leak.speak()");

                scriptSource.Execute(scriptScope);

                foreach (var name in scriptScope.GetVariableNames())
                    scriptScope.RemoveVariable(name);

                GC.Collect();
                Console.WriteLine("Memory usage is " + (GC.GetTotalMemory(true) / 1024.0) + "kb");
            }
        }

        public class LeakedObject
        {
            public void Speak()
            {
                Console.WriteLine("I'm a leaked object.");
            }
            ~LeakedObject()
            {
                Console.WriteLine("I got cleaned up!");
            }
        }
         * */
        public static void Main(string[] args)
        {
            FlatironEngine engine = new FlatironEngine(true, "../../");
            Console.WriteLine("going to evaluate.");
            string output = engine.Evaluate("basic.html");
            Console.WriteLine("output follows:");
            Console.WriteLine(output);
        }
        void ProcessDirectory(FlatironEngine flatiron, string srcDir, string destDir, bool prettyUrls)
        {
            Console.WriteLine("Entering " + srcDir);

            foreach (var filePath in Directory.GetFiles(srcDir))
            {
                var fileName = Path.GetFileName(filePath);

                if (fileName.Contains(".prnt.") || fileName.Contains(".incl.") ||
                    fileName.StartsWith(".") || (File.GetAttributes(filePath) & FileAttributes.Hidden) != 0)
                    continue;

                string destFile;
                if (fileName == "index.html" || !prettyUrls)
                    destFile = Path.Combine(destDir, fileName);
                else
                {
                    var dir = Path.Combine(destDir, fileName.Replace(".html", ""));
                    Directory.CreateDirectory(dir);
                    destFile = Path.Combine(dir, "index.html");
                }

                string destFileDir = Path.GetDirectoryName(destFile);

                if (!Directory.Exists(destFileDir))
                    Directory.CreateDirectory(destFileDir);

                if (File.Exists(destFile))
                    File.Delete(destFile);

                Console.WriteLine(filePath + " -> ");

                string result;

                try
                {
                    result = flatiron.Evaluate(Path.GetFullPath(filePath));
                }
                catch (Exception e)
                {
                    var fed = FlatironExceptionData.GetInstance(e);
                    if (fed == null) throw new Exception("Error evaluating " + filePath + " (no FlatironExceptionData available)", e);
                    throw new Exception("Error evaluating " + fed.Template + " (line " + fed.Line + "): " + e.Message, e);
                }

                Console.WriteLine("    " + destFile);

                using (var w = new StreamWriter(File.OpenWrite(destFile)))
                {
                    w.Write(result);
                    w.Flush();
                }
            }

            Console.WriteLine("Leaving " + srcDir);

            foreach (var directory in Directory.GetDirectories(srcDir))
                if (!directory.StartsWith("."))
                {
                    var sepSplit = directory.Split(Path.DirectorySeparatorChar);
                    var dirName = sepSplit[sepSplit.Length - 1];
                    var dest = Path.Combine(destDir, dirName);
                    ProcessDirectory(flatiron, directory, dest, prettyUrls);
                }
        }