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 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; }
private 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 AssemblyTranslationInfo GetOrMakeTranslationInfo(Assembly assembly) { if (assembly == null) { throw new ArgumentNullException(nameof(assembly)); } if (AssemblyTranslations.TryGetValue(assembly, out var ati)) { return(ati); } ati = AssemblyTranslations[assembly] = AssemblyTranslationInfo.FromAssembly(assembly, this); if (OnTranslationInfoCreated != null) { OnTranslationInfoCreated(this, new TranslationInfoCreatedEventArgs { AssemblyTranslation = ati }); } return(ati); }
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 EmitResult Compile2PhpAndEmit(string outDir, string dllFilename, Dictionary <string, string> referencedPhpLibsLocations) { if (_verboseToConsole) { Console.WriteLine("Compilation"); } var emitResult = CompileCSharpProject(_sandbox, dllFilename); if (!emitResult.Success) { return(emitResult); } GreenOk(); if (_verboseToConsole) { Console.WriteLine("Analize C# source"); } var info = ParseCsSource(); { var realOutputDir = Path.Combine(outDir, AssemblyTranslationInfo.GetRootPath(CompiledAssembly)); var ggg = (from assembly in _referencedAssemblies let moduleIncludeConst = assembly.GetCustomAttribute <ModuleIncludeConstAttribute>() where moduleIncludeConst != null let assemblyName = assembly.GetName().Name select new { DefinedConstName = moduleIncludeConst.ConstOrVarName, AssemblyName = assemblyName, RootPath = AssemblyTranslationInfo.GetRootPath(assembly) }).ToArray(); foreach (var x in ggg) { var definedConstName = x.DefinedConstName; if (definedConstName.StartsWith("$")) { throw new NotSupportedException(); } if (!definedConstName.StartsWith("\\")) { definedConstName = "\\" + definedConstName; } string path; if (!referencedPhpLibsLocations.TryGetValue(x.AssemblyName, out path)) { continue; } path = Path.Combine(path, x.RootPath); var relativePath = PathUtil.MakeRelativePath(path, realOutputDir); info.KnownConstsValues[definedConstName] = new KnownConstInfo(definedConstName, relativePath, false); // referencedLibsPaths[definedConstName] = new KnownConstInfo(definedConstName, relativePath, false); } } GreenOk(); TranslateAndCreatePhpFiles(info, outDir); EmitContentFiles(outDir); return(emitResult); }