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 void Compile() { using (new AppConfigManipulator()) { CompilerEngine.ExecuteInSeparateAppDomain( ce => { #if DEBUG ce.Configuration = "DEBUG"; #else ce.Configuration = "RELEASE"; #endif ce.CsProject = LangPhpTestCsProj; ce.OutDir = ce.BinaryOutputDir = Path.GetTempPath(); ce.Referenced.Clear(); var types = new[] { typeof (Func<,>), typeof (EmitContext), typeof (ThisExpression), typeof (CompilerEngine) }; { var tmp = Assembly.Load( "System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"); ce.Referenced.Add(tmp.GetCodeLocation().FullName); } ce.Referenced.AddRange(types.Select(type => type.Assembly.GetCodeLocation().FullName)); ce.TranlationHelpers.Clear(); ce.ReferencedPhpLibsLocations.Clear(); ce.Check(); using (var comp = ce.PreparePhpCompiler()) { Console.WriteLine("Compilation"); var emitResult = comp.CompileCSharpProject(comp.Sandbox, ce.DllFilename); if (!emitResult.Success) foreach (var i in emitResult.Diagnostics.Where(a => a.Severity == DiagnosticSeverity.Error)) throw new Exception("Compilation error: " + i.GetMessage()); var 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); var translator = new Translator(translationState); translator.Translate(comp.Sandbox); // ============= var m = string.Join(", ", translator.Modules.Select(i => i.Name.Name).OrderBy(i => i)).ToArray(); //Assert.True(m == "Lang_Php_Test_Code_MyCode, Lang_Php_Test_Code_SampleEmptyClass", m); MethodTranslation(ModuleMycode, ClassMycode, "BasicMath1", translator); MethodTranslation(ModuleMycode, ClassMycode, "Collections", translator); MethodTranslation(ModuleMycode, ClassMycode, "CostantsAndVariables", translator); MethodTranslation(ModuleMycode, ClassMycode, "Filters", translator); MethodTranslation(ModuleMycode, ClassMycode, "StringConcats", translator); MethodTranslation(ModuleMycode, ClassMycode, "PregTest", translator); // ModuleTranslation("Lang_Php_Test_Code_SampleEmptyClass", translator); // ModuleTranslation("Lang_Php_Test_Code_BusinessClass", translator); // ModuleTranslation("Lang_Php_Test_Code_BusinessClassDefinedConst", translator); foreach (var moduleName in translator.Modules.Select(i => i.Name.Name)) ModuleTranslation(moduleName, translator); } ; }, AppDomain.CurrentDomain); } }
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 }
// 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; } }
protected void ModuleTranslation(string module, Translator translator) { if (module == null) throw new ArgumentNullException("module"); if (translator == null) throw new ArgumentNullException("translator"); string expectedCode, translatedCode, shortFilename; { // translator = PrepareTranslator(); Console.WriteLine("We have module " + translator.Modules.First().Name.Name); PhpCodeModule mod = translator.Modules.First(i => i.Name.Name == module); // var cl = mod.Classes[0]; // var method = cl.Methods.First(i => i.Name == methodName); translatedCode = GetCode(mod); } { shortFilename = string.Format("module-{0}.txt", module); expectedCode = GetExpectedCode(shortFilename, translatedCode); } if (expectedCode != translatedCode) Save(translatedCode, shortFilename); Assert.True(expectedCode == translatedCode, "Invalid translation"); }
// Protected Methods protected void MethodTranslation(string module, string xClass, string methodName, Translator translator) { if (methodName == null) throw new ArgumentNullException("methodName"); string expectedCode, translatedCode, shortFilename; { // translator = PrepareTranslator(); Console.WriteLine("We have module " + translator.Modules.First().Name.Name); var mod = translator.Modules.First(i => i.Name.Name == module); var cl = mod.Classes[0]; Assert.True(cl.Name.FullName == xClass, "Invalid class name translation - attribute ScriptName"); PhpClassMethodDefinition method = cl.Methods.First(i => i.Name == methodName); translatedCode = GetCode(method); } { shortFilename = string.Format("{0}_{1}.txt", xClass.Replace("\\", ""), methodName); expectedCode = GetExpectedCode(shortFilename, translatedCode); } if (expectedCode != translatedCode) Save(translatedCode, shortFilename); Assert.True(expectedCode == translatedCode, "Invalid translation"); }