Beispiel #1
0
        private void TranslateAndCreatePhpFiles(TranslationInfo translationInfo, string outDir)
        {
            if (_verboseToConsole)
            {
                Console.WriteLine("Translate C# -> Php");
            }

            translationInfo.CurrentAssembly = CompiledAssembly;
            var assemblyTi = translationInfo.GetOrMakeTranslationInfo(CompiledAssembly);
            var ecBaseDir  = Path.Combine(outDir, assemblyTi.RootPath.Replace("/", "\\"));

            Console.WriteLine("Output root {0}", ecBaseDir);

            if (!string.IsNullOrEmpty(assemblyTi.PhpPackageSourceUri))
            {
                DownloadAndUnzip(assemblyTi.PhpPackageSourceUri, ecBaseDir, assemblyTi.PhpPackagePathStrip);
                return; //??? czy return?
            }

            var translationState = new TranslationState(translationInfo);
            var translator       = new Translator.Translator(translationState);

            translator.Translate(Sandbox);

            var libName = assemblyTi.LibraryName;

            if (_verboseToConsole)
            {
                Console.WriteLine("Create Php output files");
            }

            #region Tworzenie plików php

            {
                // var emitContext = new EmitContext();
                var emitStyle = new PhpEmitStyle();

                translationInfo.CurrentAssembly = CompiledAssembly; // dla pewności
                foreach (var module in translator.Modules.Where(i => i.Name.Library == libName && !i.IsEmpty))
                {
                    var fileName = module.Name.MakeEmitPath(ecBaseDir, 1);
                    foreach (var modProcessor in translationInfo.ModuleProcessors)
                    {
                        modProcessor.BeforeEmit(module, translationInfo);
                    }
                    var emiter = new PhpSourceCodeEmiter();
                    module.Emit(emiter, emitStyle, fileName);
                }
            }

            #endregion
        }
Beispiel #2
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();
        }
Beispiel #3
0
        public static FieldTranslationInfo FromFieldInfo(FieldInfo fieldInfo, TranslationInfo info)
        {
            if (fieldInfo == null)
            {
                return(null);
            }

            var fieldInfoDeclaringType = fieldInfo.DeclaringType;

            if (fieldInfoDeclaringType == null)
            {
                throw new Exception("fieldInfo.DeclaringType is null"); // Resharper
            }
            var fti = new FieldTranslationInfo();

            if (fieldInfo.IsLiteral)
            {
                fti._destination = FieldTranslationDestionations.ClassConst;
            }
            {
                fti._scriptName = fieldInfo.Name;
                var scriptNameAttribute = fieldInfo.GetCustomAttribute <ScriptNameAttribute>();
                if (scriptNameAttribute != null)
                {
                    fti._scriptName             = scriptNameAttribute.Name;
                    fti._isScriptNamePhpEncoded = scriptNameAttribute.Kind == ScriptNameAttribute.Kinds.IntIndex;
                }
            }
            {
                var asDefinedConstAttribute = fieldInfo.GetCustomAttribute <AsDefinedConstAttribute>();
                if (asDefinedConstAttribute != null)
                {
                    fti._destination = FieldTranslationDestionations.DefinedConst;
                    if (!string.IsNullOrEmpty(asDefinedConstAttribute.DefinedConstName))
                    {
                        fti._scriptName = asDefinedConstAttribute.DefinedConstName;
                    }
                }
            }
            {
                var globalVariableAttribute = fieldInfo.GetCustomAttribute <GlobalVariableAttribute>();
                if (globalVariableAttribute != null)
                {
                    Check(fieldInfo, fti);
                    fti._destination = FieldTranslationDestionations.GlobalVariable;
                    if (!string.IsNullOrEmpty(globalVariableAttribute.GlobalVariableName))
                    {
                        fti._scriptName = globalVariableAttribute.GlobalVariableName;
                    }
                }
            }
            {
                var asValueAttribute = fieldInfo.GetCustomAttribute <AsValueAttribute>();
                if (asValueAttribute != null)
                {
                    Check(fieldInfo, fti);
                    fti._destination    = FieldTranslationDestionations.JustValue;
                    fti._usGlueForValue = asValueAttribute.Glue;
                }
            }
            var canBeNull = false;

            switch (fti._destination)
            {
            case FieldTranslationDestionations.JustValue:
            case FieldTranslationDestionations.GlobalVariable:
                canBeNull          = true;
                fti._includeModule = null;      // force null
                break;

            case FieldTranslationDestionations.DefinedConst:
            case FieldTranslationDestionations.ClassConst:
            case FieldTranslationDestionations.NormalField:
                var cti = info.GetOrMakeTranslationInfo(fieldInfoDeclaringType);
                fti._includeModule = cti.ModuleName;
                if (cti.BuildIn)
                {
                    fti._includeModule = null;
                    canBeNull          = true;
                }
                var isFieldOutsideClass = fti._destination == FieldTranslationDestionations.GlobalVariable ||
                                          fti._destination == FieldTranslationDestionations.DefinedConst;
                {
                    // can be in other module for GlobalVariable and DefinedConst
                    var moduleAttribute = fieldInfo.GetCustomAttribute <ModuleAttribute>();
                    if (moduleAttribute != null)
                    {
                        if (!isFieldOutsideClass)
                        {
                            throw new Exception(string.Format("Module attribute can only be defined for GlobalVariable or DefinedConst. Check {0}.", fieldInfo.ExcName()));
                        }
                        fti._includeModule = new PhpCodeModuleName(moduleAttribute.ModuleShortName,
                                                                   info.GetOrMakeTranslationInfo(fieldInfoDeclaringType.Assembly));
                    }
                }
                if (cti.IsPage)
                {
                    fti._isDefinedInNonincludableModule = true;
                }
                if (!isFieldOutsideClass)
                {
                    if (cti.IsArray || cti.Type.IsEnum || cti.BuildIn)
                    {
                        canBeNull          = true;
                        fti._includeModule = null;     // force null
                    }
                    else if (cti.DontIncludeModuleForClassMembers)
                    {
                        throw new Exception(
                                  string.Format("field {0} belongs to nonincludable class (array, enum or skipped)",
                                                fieldInfo.ExcName()));
                    }
                }
                break;
            }

            if (!fti._includeModule.IsEmpty())
            {
                return(fti);
            }
            if (canBeNull)
            {
                fti._includeModule = null; // can be not null but empty
                fti._isDefinedInNonincludableModule = false;
            }
            else
            {
                throw new Exception(string.Format("Include module is empty for field {0}.",
                                                  fieldInfo.ExcName()));
            }
            return(fti);
        }
        public static FieldTranslationInfo FromFieldInfo(FieldInfo fieldInfo, TranslationInfo info)
        {
            if (fieldInfo == null)
                return null;

            var fieldInfoDeclaringType = fieldInfo.DeclaringType;
            if (fieldInfoDeclaringType == null)
                throw new Exception("fieldInfo.DeclaringType is null"); // Resharper
            var fti = new FieldTranslationInfo();
            if (fieldInfo.IsLiteral)
                fti._destination = FieldTranslationDestionations.ClassConst;
            {
                fti._scriptName = fieldInfo.Name;
                var scriptNameAttribute = fieldInfo.GetCustomAttribute<ScriptNameAttribute>();
                if (scriptNameAttribute != null)
                {
                    fti._scriptName = scriptNameAttribute.Name;
                    fti._isScriptNamePhpEncoded = scriptNameAttribute.Kind == ScriptNameAttribute.Kinds.IntIndex;
                }
            }
            {
                var asDefinedConstAttribute = fieldInfo.GetCustomAttribute<AsDefinedConstAttribute>();
                if (asDefinedConstAttribute != null)
                {
                    fti._destination = FieldTranslationDestionations.DefinedConst;
                    if (!string.IsNullOrEmpty(asDefinedConstAttribute.DefinedConstName))
                        fti._scriptName = asDefinedConstAttribute.DefinedConstName;
                }
            }
            {
                var globalVariableAttribute = fieldInfo.GetCustomAttribute<GlobalVariableAttribute>();
                if (globalVariableAttribute != null)
                {
                    Check(fieldInfo, fti);
                    fti._destination = FieldTranslationDestionations.GlobalVariable;
                    if (!string.IsNullOrEmpty(globalVariableAttribute.GlobalVariableName))
                        fti._scriptName = globalVariableAttribute.GlobalVariableName;
                }
            }
            {
                var asValueAttribute = fieldInfo.GetCustomAttribute<AsValueAttribute>();
                if (asValueAttribute != null)
                {
                    Check(fieldInfo, fti);
                    fti._destination = FieldTranslationDestionations.JustValue;
                    fti._usGlueForValue = asValueAttribute.Glue;
                }
            }
            var canBeNull = false;
            switch (fti._destination)
            {
                case FieldTranslationDestionations.JustValue:
                case FieldTranslationDestionations.GlobalVariable:
                    canBeNull = true;
                     fti._includeModule = null; // force null
                    break;
                case FieldTranslationDestionations.DefinedConst:
                case FieldTranslationDestionations.ClassConst:
                case FieldTranslationDestionations.NormalField:
                    var cti = info.GetOrMakeTranslationInfo(fieldInfoDeclaringType);
                    fti._includeModule = cti.ModuleName;
                    if (cti.BuildIn)
                    {
                        fti._includeModule = null;
                        canBeNull = true;
                    }
                    var isFieldOutsideClass = fti._destination == FieldTranslationDestionations.GlobalVariable ||
                                  fti._destination == FieldTranslationDestionations.DefinedConst;
                    {
                        // can be in other module for GlobalVariable and DefinedConst
                        var moduleAttribute = fieldInfo.GetCustomAttribute<ModuleAttribute>();
                        if (moduleAttribute != null)
                        {
                            if (!isFieldOutsideClass)
                                throw new Exception(string.Format("Module attribute can only be defined for GlobalVariable or DefinedConst. Check {0}.", fieldInfo.ExcName()));
                            fti._includeModule = new PhpCodeModuleName(moduleAttribute.ModuleShortName,
                                info.GetOrMakeTranslationInfo(fieldInfoDeclaringType.Assembly));
                        }
                    }
                    if (cti.IsPage)
                        fti._isDefinedInNonincludableModule = true;
                    if (!isFieldOutsideClass)
                    {
                        if (cti.IsArray || cti.Type.IsEnum || cti.BuildIn)
                        {
                            canBeNull = true;
                            fti._includeModule = null; // force null
                        }
                        else if (cti.DontIncludeModuleForClassMembers)
                            throw new Exception(
                                string.Format("field {0} belongs to nonincludable class (array, enum or skipped)",
                                    fieldInfo.ExcName()));
                    }
                    break;
            }

            if (!fti._includeModule.IsEmpty())
                return fti;
            if (canBeNull)
            {
                fti._includeModule = null; // can be not null but empty
                fti._isDefinedInNonincludableModule = false;
            }
            else
                throw new Exception(string.Format("Include module is empty for field {0}.",
                    fieldInfo.ExcName()));
            return fti;
        }
Beispiel #5
0
        private void TranslateAndCreatePhpFiles(TranslationInfo translationInfo, string outDir)
        {
            if (_verboseToConsole)
                Console.WriteLine("Translate C# -> Php");

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

            if (!string.IsNullOrEmpty(assemblyTi.PhpPackageSourceUri))
            {
                DownloadAndUnzip(assemblyTi.PhpPackageSourceUri, ecBaseDir, assemblyTi.PhpPackagePathStrip);
                return; //??? czy return?
            }
            var translationState = new TranslationState(translationInfo);
            var translator = new Translator.Translator(translationState);

            translator.Translate(_sandbox);

            var libName = assemblyTi.LibraryName;


            if (_verboseToConsole)
                Console.WriteLine("Create Php output files");
            #region Tworzenie plików php
            {
                // var emitContext = new EmitContext();
                var emitStyle = new PhpEmitStyle();

                translationInfo.CurrentAssembly = _compiledAssembly;// dla pewności
                foreach (var module in translator.Modules.Where(i => i.Name.Library == libName && !i.IsEmpty))
                {
                    var fileName = module.Name.MakeEmitPath(ecBaseDir, 1);
                    foreach (var modProcessor in translationInfo.ModuleProcessors)
                    {
                        modProcessor.BeforeEmit(module, translationInfo);
                    }
                    var emiter = new PhpSourceCodeEmiter();
                    module.Emit(emiter, emitStyle, fileName);

                }
            }

            #endregion
        }