Beispiel #1
0
        public Assembly compileScript(string code, List <string> errors)
        {
            Assembly ret = null;

            String md5             = PortUtil.getMD5FromString(code).Replace("_", "");
            String cached_asm_file = Path.Combine(Utility.getCachedAssemblyPath(), md5 + ".dll");
            bool   compiled        = false;

            if (File.Exists(cached_asm_file))
            {
                ret = Assembly.LoadFile(cached_asm_file);
                if (!isValidCachedAssembly(ret))
                {
                    ret = null;
                    usedAssemblyChache.Remove(cached_asm_file);
                }
                if (ret != null)
                {
                    if (!usedAssemblyChache.Contains(cached_asm_file))
                    {
                        usedAssemblyChache.Add(cached_asm_file);
                    }
                }
            }

            CompilerResults cr = null;

            if (ret == null)
            {
                CSharpCodeProvider provider = new CSharpCodeProvider();
                String             path     = System.Windows.Forms.Application.StartupPath;

                if (System.IO.Path.GetFileName(System.Windows.Forms.Application.ExecutablePath).ToLower().StartsWith("nunit"))
                {
                    // nunit の場合は、 StartupPath が nunit のものになってしまうため、
                    // CadenciiTest.dll がデプロイされているディレクトリを、アセンブリのロード起点とする。
                    foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
                    {
                        if (System.IO.Path.GetFileName(asm.Location).ToLower() == "cadenciitest.dll")
                        {
                            path = System.IO.Path.GetDirectoryName(asm.Location);
                            break;
                        }
                    }
                }

                CompilerParameters parameters = new CompilerParameters(new String[] {
                    Path.Combine(path, "cadencii.vsq.dll"),
                    Path.Combine(path, "Cadencii.exe"),
                    Path.Combine(path, "cadencii.media.dll"),
                    Path.Combine(path, "cadencii.apputil.dll"),
                    Path.Combine(path, "cadencii.windows.forms.dll"),
                    Path.Combine(path, "cadencii.core.dll")
                });
                parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
                parameters.ReferencedAssemblies.Add("System.dll");
                parameters.ReferencedAssemblies.Add("System.Drawing.dll");
                parameters.ReferencedAssemblies.Add("System.Xml.dll");
                parameters.GenerateInMemory        = false;
                parameters.GenerateExecutable      = false;
                parameters.IncludeDebugInformation = true;
                try {
                    cr       = provider.CompileAssemblyFromSource(parameters, code);
                    ret      = cr.CompiledAssembly;
                    compiled = true;
                } catch (Exception ex) {
                    serr.println("Utility#compileScript; ex=" + ex);
                    Logger.write(typeof(Utility) + ".compileScript; ex=" + ex + "\n");
                }
                if (!compiled)
                {
                    int c = cr.Errors.Count;
                    for (int i = 0; i < c; i++)
                    {
                        errors.Add(_("line") + ":" + cr.Errors[i].Line + " " + cr.Errors[i].ErrorText);
                    }
                }
            }

            if (compiled)
            {
                if (!usedAssemblyChache.Contains(cached_asm_file))
                {
                    usedAssemblyChache.Add(cached_asm_file);
                }
                if (File.Exists(cached_asm_file))
                {
                    try {
                        PortUtil.deleteFile(cached_asm_file);
                    } catch (Exception ex) {
                        serr.println("Utility#compileScript; ex=" + ex);
                        Logger.write(typeof(Utility) + ".compileScript; ex=" + ex + "\n");
                    }
                }
                try {
                    PortUtil.copyFile(cr.PathToAssembly, cached_asm_file);
                } catch (Exception ex) {
                    serr.println("Utility#compileScript; ex=" + ex);
                    Logger.write(typeof(Utility) + ".compileScript; ex=" + ex + "\n");
                }
            }

            return(ret);
        }