Ejemplo n.º 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(Cs2PyCompiler 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 portableExecutableReferences = comp.CSharpProject.MetadataReferences
                                               .OfType <PortableExecutableReference>()
                                               .Select(
                reference => new
            {
                FileShortName = new FileInfo(reference.FilePath).Name.ToLower(),
                Reference     = reference
            })
                                               .ToArray();

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

                var remove = fileReference.Reference;
                var add    = MetadataReference.CreateFromFile(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);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Loads csproj, prepares references and loads assemblies
        /// </summary>
        /// <returns></returns>
        public Cs2PyCompiler PreparePyCompiler()
        {
            var comp = new Cs2PyCompiler
            {
                VerboseToConsole = true,
                ThrowExceptions  = true
            };

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

            Console.WriteLine("Preparing before compilation");

            #region Remove Lang.Py 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.Py.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 <PortableExecutableReference>().ToList();
                foreach (var i in refToRemove)
                {
                    comp.RemoveMetadataReferences(i);
                }
                var ref1 = refToRemove.Select(i => i.FilePath).Union(Referenced).ToList();
                ref1.Add(typeof(Py).Assembly.GetCodeLocation().FullName);
                ref1.AddRange(Referenced);
                filenames = ref1.Distinct().ToArray();
            }

            #endregion

            foreach (var fileName in filenames)
            {
                comp.AddMetadataReferences(
                    MetadataReference.CreateFromFile(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);
        }