示例#1
0
        public void BeforeEmit(PhpCodeModule module, TranslationInfo info)
        {
            //var a = info.CurrentAssembly.GetCustomAttributes(false);
            var assemblyTI = info.GetOrMakeTranslationInfo(info.CurrentAssembly);
            // var c = info.ClassTranslations.Values.Where(u => u.ModuleName != null && u.ModuleName.Library == assemblyTI.LibraryName).ToArray();
            var typesInThisModule  = info.ClassTranslations.Values.Where(u => u.ModuleName != null && u.ModuleName == module.Name).ToArray();
            var typesWithAttribute = from i in typesInThisModule
                                     let attribute = i.Type.GetCustomAttribute <MainPluginModuleAttribute>(false)
                                                     where attribute != null
                                                     select i;

            if (typesWithAttribute.Any())
            {
                var PluginName = "Please fill AssemblyTrademark attribute";
                {
                    var a = info.CurrentAssembly.GetCustomAttribute <AssemblyTrademarkAttribute>();
                    if (a != null && !string.IsNullOrEmpty(a.Trademark))
                    {
                        PluginName = a.Trademark;
                    }
                }
                var Author = "";
                {
                    var a = info.CurrentAssembly.GetCustomAttribute <AssemblyCompanyAttribute>();
                    if (a != null && !string.IsNullOrEmpty(a.Company))
                    {
                        Author = a.Company;
                    }
                }
                var Description = "";
                {
                    var a = info.CurrentAssembly.GetCustomAttribute <AssemblyDescriptionAttribute>();
                    if (a != null && !string.IsNullOrEmpty(a.Description))
                    {
                        Description = a.Description;
                    }
                }
                List <string> l = new List <string>();
                l.Add("Plugin Name: " + PluginName);
                l.Add("Author: " + Author);
                l.Add("Author URI: " + "???");
                l.Add("Description: " + Description);
                module.TopComments = string.Join("\r\n", l) + "\r\n" + module.TopComments;
            }
            // throw new NotImplementedException();
        }
示例#2
0
        private HttpResponse ProcessRequest(HttpRequest req, Type type)
        {
            var y = new HttpResponse();
            ClassTranslationInfo ci = _translationInfo.GetOrMakeTranslationInfo(type);

            if (ci.PageMethod == null)
            {
                throw new Exception(string.Format("Page method not found in type {0}", type.FullName));
            }
            {
                // prepare sandbox
                Response.RuntimeResponse = y;
                Request.RuntimeRequest   = req;
                Script.Get    = req.Get;
                Script.Post   = req.Post;
                Script.Server = req.Server;
            }

            Action phpMain = (Action)Delegate.CreateDelegate(typeof(Action), ci.PageMethod);

            phpMain(); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Wywołanie metody
            // y.Echo("Mock output");
            return(y);
        }
示例#3
0
文件: Base.cs 项目: rajeshwarn/cs2php
        // Internal Methods 

        internal static Translator PrepareTranslator()
        {
            if (_translator != null)
            {
                return(_translator);
            }
            var csProject = LangPhpTestCsProj;

            using (var comp = new Cs2PhpCompiler {
                VerboseToConsole = true, ThrowExceptions = true
            })
            {
                Console.WriteLine("Try to load " + csProject);

#if DEBUG
                comp.LoadProject(csProject, "DEBUG");
#else
                comp.LoadProject(csProject, "RELEASE");
#endif

                /*
                 * linked with C:\programs\_CS2PHP\PUBLIC\Lang.Php.Compiler\bin\Debug\Lang.Php.Compiler.dll
                 * linked with C:\programs\_CS2PHP\PUBLIC\Lang.Php.Framework\bin\Debug\Lang.Php.Framework.dll
                 * linked with C:\programs\_CS2PHP\PUBLIC\Lang.Php.Test\bin\Debug\Lang.Php.dll
                 */


                Console.WriteLine("Preparing before compilation");
                string[] removeL = "Lang.Php.Compiler,Lang.Php.Framework,Lang.Php".Split(',');

                #region Remove Lang.Php reference

                {
                    foreach (var r in removeL)
                    {
                        // ... will be replaced by reference to dll from compiler base dir
                        // I know - compilation libraries should be loaded into separate application domain
                        var remove =
                            comp.CSharpProject.MetadataReferences.FirstOrDefault(i => i.Display.EndsWith(r + ".dll"));
                        if (remove != null)
                        {
                            comp.RemoveMetadataReferences(remove);
                        }
                    }
                }

                #endregion

                string[] filenames;

                #region We have to remove and add again references - strange

                {
                    // in other cases some referenced libraries are ignored
                    var refToRemove = comp.CSharpProject.MetadataReferences.OfType <MetadataFileReference>().ToList();
                    foreach (var i in refToRemove)
                    {
                        comp.RemoveMetadataReferences(i);
                    }
                    var ref1 = refToRemove.Select(i => i.FilePath).ToList();
                    // foreach (var r in removeL)
                    //     ref1.Add(Path.Combine(Directory.GetCurrentDirectory(), r + ".dll"));
                    ref1.Add(typeof(DirectCallAttribute).Assembly.Location);
                    ref1.Add(typeof(EmitContext).Assembly.Location);
                    ref1.Add(typeof(Extension).Assembly.Location);
                    filenames = ref1.Distinct().ToArray();
                }

                #endregion

                #region Translation assemblies

                {
                    comp.TranslationAssemblies.Add(typeof(Extension).Assembly);
                    comp.TranslationAssemblies.Add(typeof(Translator).Assembly);
                }

                #endregion

                foreach (var fileName in filenames)
                {
                    var g = new MetadataFileReference(fileName, MetadataReferenceProperties.Assembly);
                    comp.AddMetadataReferences(g);
                    Console.WriteLine("  Add reference     {0}", g.Display);
                }

                //                using (var sandbox = new AssemblySandbox())
                //                {
                //                //
                //                Console.WriteLine("Start compile");
                //                var result = comp.CompileCSharpProject(sandbox, comp.DllFileName);
                //                if (!result.Success)
                //                {
                //                    foreach (var i in result.Diagnostics)
                //                        Console.WriteLine(i);
                //                }
                //                Assert.True(result.Success, "Compilation failed");
                //                }
                TranslationInfo translationInfo = comp.ParseCsSource();


                translationInfo.CurrentAssembly = comp.CompiledAssembly;
                var assemblyTi = translationInfo.GetOrMakeTranslationInfo(comp.CompiledAssembly);
                var ecBaseDir  = Path.Combine(Directory.GetCurrentDirectory(), assemblyTi.RootPath.Replace("/", "\\"));
                Console.WriteLine("Output root {0}", ecBaseDir);

                var translationState = new TranslationState(translationInfo);
                _translator = new Translator(translationState);

                _translator.Translate(comp.Sandbox);
                return(_translator);
            }
        }