public static IPhpValue MakePathValueRelatedToFile(KnownConstInfo value, TranslationInfo info) { if (value.Value == null) { info.Log(MessageLevels.Error, string.Format("Const value {0} must be a string (currently is null)", value.Name)); return new PhpConstValue("???"); } if (value.Value is string) return MakePathValueRelatedToFile(value.Value as string); info.Log(MessageLevels.Warning, string.Format("Const value {0} must be a string", value.Name)); return new PhpConstValue("???"); }
// Public Methods public static AssemblyTranslationInfo FromAssembly(Assembly assembly, TranslationInfo translationInfo) { if (assembly == null) return null; var ati = new AssemblyTranslationInfo(); { ati._assembly = assembly; var moduleIncludeConst = assembly.GetCustomAttribute<ModuleIncludeConstAttribute>(); if (moduleIncludeConst != null) { ati._includePathConstOrVarName = (moduleIncludeConst.ConstOrVarName ?? "").Trim(); if (ati._includePathConstOrVarName.StartsWith("$")) { } else { ati._includePathConstOrVarName = "\\" + ati._includePathConstOrVarName.TrimStart('\\'); } } ati._rootPath = GetRootPath(assembly); var phpPackageSource = assembly.GetCustomAttribute<PhpPackageSourceAttribute>(); if (phpPackageSource != null) { ati._phpPackageSourceUri = phpPackageSource.SourceUri; ati._phpPackagePathStrip = phpPackageSource.StripArchivePath; } { var configModule = assembly.GetCustomAttribute<ConfigModuleAttribute>(); if (configModule != null) ati.ConfigModuleName = configModule.Name; } { var defaultTimezone = assembly.GetCustomAttribute<DefaultTimezoneAttribute>(); if (defaultTimezone != null) ati._defaultTimezone = defaultTimezone.Timezone; } } ati._libraryName = LibNameFromAssembly(assembly); ati._phpIncludePathExpression = GetDefaultIncludePath(ati, translationInfo); return ati; }
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(); }
static IPhpValue GetDefaultIncludePath(AssemblyTranslationInfo ati, TranslationInfo translationInfo) { var pathElements = new List<IPhpValue>(); #region Take include path variable or const if (!string.IsNullOrEmpty(ati.IncludePathConstOrVarName)) { if (ati.IncludePathConstOrVarName.StartsWith("$")) pathElements.Add(new PhpVariableExpression(ati.IncludePathConstOrVarName, PhpVariableKind.Global)); else { var tmp = ati.IncludePathConstOrVarName; if (!tmp.StartsWith("\\")) // defined const is in global namespace ALWAYS tmp = "\\" + tmp; KnownConstInfo info; if (translationInfo != null && translationInfo.KnownConstsValues.TryGetValue(tmp, out info) && info.UseFixedValue) pathElements.Add(new PhpConstValue(info.Value)); else pathElements.Add(new PhpDefinedConstExpression(tmp, PhpCodeModuleName.Cs2PhpConfigModuleName)); } } #endregion //#region RootPathAttribute //{ // if (!string.IsNullOrEmpty(ati.RootPath) && ati.RootPath != "/") // pathElements.Add(new PhpConstValue(ati.RootPath)); //} //#endregion var result = PhpBinaryOperatorExpression.ConcatStrings(pathElements.ToArray()); return result; }
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); }
/// <summary> /// Tworzy instancję obiektu /// <param name="type"></param> /// </summary> public ClassTranslationInfo(Type type, TranslationInfo info) { _type = type; _info = info; }
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; }
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 }
public TranslationInfo ParseCsSource() { // must be public var knownTypes = GetKnownTypes(); CheckRequiredTranslator(); var translationInfo = new TranslationInfo(_sandbox); translationInfo.TranslationAssemblies.AddRange(_translationAssemblies); translationInfo.Prepare(); // Console.WriteLine("======================"); foreach (var tree in _projectCompilation.SyntaxTrees) { var now = DateTime.Now; Console.WriteLine("parse file {0}", tree.FilePath); var root = (CompilationUnitSyntax)tree.GetRoot(); var state = new CompileState { Context = { RoslynCompilation = _projectCompilation, RoslynModel = _projectCompilation.GetSemanticModel(tree) } }; translationInfo.State = state; var langVisitor = new LangVisitor(state) { throwNotImplementedException = true }; state.Context.KnownTypes = knownTypes; var compilationUnit = langVisitor.Visit(root) as CompilationUnit; translationInfo.Compiled.Add(compilationUnit); Console.WriteLine(" {0:0.0} sek", DateTime.Now.Subtract(now).TotalSeconds); } Console.WriteLine("Parsowanie c# skończone, mamy drzewo definicji"); translationInfo.FillClassTranslations(knownTypes); return translationInfo; }