private static IPyValue GetDefaultIncludePath(AssemblyTranslationInfo ati, TranslationInfo translationInfo) { return(new PyConstValue("---FAKE---")); #if PHP var pathElements = new List <IPyValue>(); #region Take include path variable or const if (!string.IsNullOrEmpty(ati.IncludePathConstOrVarName)) { if (ati.IncludePathConstOrVarName.StartsWith("$")) { pathElements.Add(new PyVariableExpression(ati.IncludePathConstOrVarName, PyVariableKind.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 PyConstValue(info.Value)); } else { pathElements.Add(new PyDefinedConstExpression(tmp, PyCodeModuleName.Cs2PyConfigModuleName)); } } } #endregion //#region RootPathAttribute //{ // if (!string.IsNullOrEmpty(ati.RootPath) && ati.RootPath != "/") // pathElements.Add(new PyConstValue(ati.RootPath)); //} //#endregion var result = PyBinaryOperatorExpression.ConcatStrings(pathElements.ToArray()); return(result); #else throw new NotImplementedException(); #endif }
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); }
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 PyPackageSource = assembly.GetCustomAttribute <PyPackageSourceAttribute>(); if (PyPackageSource != null) { ati.PyPackageSourceUri = PyPackageSource.SourceUri; ati.PyPackagePathStrip = PyPackageSource.StripArchivePath; } { var configModule = assembly.GetCustomAttribute <ConfigModuleAttribute>(); if (configModule != null) { ati.ConfigModuleName = configModule.Name; } } { var ats = assembly.GetCustomAttributes <ImportModuleAsAttribute>(); foreach (var at in ats) { if (ati.ModuleAliases.TryGetValue(at.ModuleName, out var existingAlias)) { if (existingAlias != at.Alias) { throw new Exception( $"Duplicate module alias for {at.ModuleName}: {existingAlias} or {at.Alias}?"); } continue; } ati.ModuleAliases[at.ModuleName] = at.Alias; } } } ati.LibraryName = LibNameFromAssembly(assembly); ati.PyIncludePathExpression = GetDefaultIncludePath(ati, translationInfo); return(ati); }
public EmitResult Compile2PyAndEmit(string outDir, string dllFilename, Dictionary <string, string> referencedPyLibsLocations) { 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; } if (!referencedPyLibsLocations.TryGetValue(x.AssemblyName, out var 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(); TranslateAndCreatePyFiles(info, outDir); EmitContentFiles(outDir); return(emitResult); }