Example #1
0
        // Private Methods 

        /// <summary>
        /// Zamienia deklarowane referencje na te, które są w katalogu aplikacji
        /// </summary>
        /// <param name="comp"></param>
        private static void Swap(Cs2PhpCompiler comp)
        {
            // ReSharper disable once CSharpWarnings::CS1030
#warning 'Be careful in LINUX'
            var files = new DirectoryInfo(ExeDir).GetFiles("*.*").ToDictionary(a => a.Name.ToLower(), a => a.FullName);
            var metadataFileReferences = comp.CSharpProject.MetadataReferences.OfType <MetadataFileReference>()
                                         .Select(
                reference => new
            {
                FileShortName = new FileInfo(reference.FilePath).Name.ToLower(),
                Reference     = reference
            })
                                         .ToArray();



            foreach (var fileReference in metadataFileReferences)
            {
                string fileFullName;
                if (!files.TryGetValue(fileReference.FileShortName, out fileFullName))
                {
                    continue;
                }

                var remove = fileReference.Reference;
                var add    = new MetadataFileReference(fileFullName, MetadataReferenceProperties.Assembly);
                if (remove.Display == add.Display)
                {
                    continue;
                }
                comp.RemoveMetadataReferences(remove);
                comp.AddMetadataReferences(add);
                Console.WriteLine("Swap\r\n    {0}\r\n    {1}", remove.Display, add.Display);
            }
        }
Example #2
0
        /// <summary>
        /// Loads csproj, prepares references and loads assemblies
        /// </summary>
        /// <returns></returns>
        public Cs2PhpCompiler PreparePhpCompiler()
        {
            var comp = new Cs2PhpCompiler
            {
                VerboseToConsole = true,
                ThrowExceptions  = true
            };

            // Console.WriteLine("Try to load " + csProject);
            comp.LoadProject(_csProject, _configuration);

            Console.WriteLine("Preparing before compilation");

            #region Remove Lang.Php reference

            {
                // ... 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("Lang.Php.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).Union(_referenced).ToList();
                ref1.Add(typeof(PhpDummy).Assembly.GetCodeLocation().FullName);
                ref1.AddRange(Referenced);
                filenames = ref1.Distinct().ToArray();
            }

            #endregion

            foreach (var fileName in filenames)
            {
                comp.AddMetadataReferences(new MetadataFileReference(fileName, MetadataReferenceProperties.Assembly));
            }


            Swap(comp);
            comp.ReferencedAssemblies = comp.CSharpProject.MetadataReferences
                                        .Select(reference => comp.Sandbox.LoadByFullFilename(reference.Display).WrappedAssembly)
                                        .ToList();
            foreach (var fileName in _tranlationHelpers)
            {
                var assembly = comp.Sandbox.LoadByFullFilename(fileName).WrappedAssembly;
                // ReSharper disable once UnusedVariable
                var an = assembly.GetName();
                Console.WriteLine(" Add translation helper {0}", assembly.FullName);
                comp.TranslationAssemblies.Add(assembly);
            }

            comp.TranslationAssemblies.Add(typeof(Extension).Assembly);
            comp.TranslationAssemblies.Add(typeof(Translator).Assembly);

            return(comp);
        }
Example #3
0
        // 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);
            }
        }