private string WriteAssemblyInfo(AssemblyDefinition assembly, out bool createdNewFile)
        {
            string fileContent;
            IAssemblyAttributeWriter writer = null;

            using (StringWriter stringWriter = new StringWriter())
            {
                IWriterSettings settings = new WriterSettings(writeExceptionsAsComments: true);
                writer = language.GetAssemblyAttributeWriter(new PlainTextFormatter(stringWriter), this.exceptionFormater, settings);
                IWriterContextService writerContextService = this.GetWriterContextService();
                writer.ExceptionThrown += OnExceptionThrown;
                writerContextService.ExceptionThrown += OnExceptionThrown;

                // "Duplicate 'TargetFramework' attribute" when having it written in AssemblyInfo
                writer.WriteAssemblyInfo(assembly, writerContextService, true,
                                         new string[1] {
                    "System.Runtime.Versioning.TargetFrameworkAttribute"
                }, new string[1] {
                    "System.Security.UnverifiableCodeAttribute"
                });

                fileContent = stringWriter.ToString();

                writer.ExceptionThrown -= OnExceptionThrown;
                writerContextService.ExceptionThrown -= OnExceptionThrown;
            }

            string parentDirectory = Path.GetDirectoryName(this.TargetPath);
            string relativePath    = filePathsService.GetAssemblyInfoRelativePath();
            string fullPath        = Path.Combine(parentDirectory, relativePath);

            string dirPath = Path.GetDirectoryName(fullPath);

            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }

            if (File.Exists(fullPath) && assembly.MainModule.Types.FirstOrDefault(x => x.FullName == "Properties.AssemblyInfo") != null)
            {
                createdNewFile = false;
                PushAssemblyAttributes(fullPath, fileContent, writer);
            }
            else
            {
                createdNewFile = true;
                using (StreamWriter fileWriter = new StreamWriter(fullPath, false, Encoding.UTF8))
                {
                    fileWriter.Write(fileContent);
                }
            }

            return(relativePath);
        }
Exemple #2
0
        private IDecompilationResults GetAssemblyAttributesDecompilationResults(string assemblyAttributesFilePath)
        {
            AvalonEditCodeFormatter formatter = new AvalonEditCodeFormatter(new StringWriter());

            IWriterSettings          settings             = new WriterSettings(writeExceptionsAsComments: true);
            IAssemblyAttributeWriter writer               = language.GetAssemblyAttributeWriter(formatter, this.exceptionFormater, settings);
            IWriterContextService    writerContextService = new TypeCollisionWriterContextService(new ProjectGenerationDecompilationCacheService(), decompilationPreferences.RenameInvalidMembers);

            string fileContent;

            try
            {
                writer.WriteAssemblyInfo(assembly, writerContextService, true,
                                         new string[1] {
                    "System.Runtime.Versioning.TargetFrameworkAttribute"
                }, new string[1] {
                    "System.Security.UnverifiableCodeAttribute"
                });

                fileContent = formatter.GetSourceCode().GetSourceCode();
            }
            catch (Exception e)
            {
                string[] exceptionMessageLines = exceptionFormater.Format(e, null, assemblyAttributesFilePath);
                fileContent = string.Join(Environment.NewLine, exceptionMessageLines);
            }

            using (StreamWriter outfile = new StreamWriter(assemblyAttributesFilePath))
            {
                outfile.Write(fileContent);
            }

            JustDecompile.EngineInfrastructure.ICodeViewerResults originalcodeViewerResults = formatter.GetSourceCode();

            if (!(originalcodeViewerResults is JustDecompile.EngineInfrastructure.DecompiledSourceCode))
            {
                throw new Exception("Unexpected code viewer results type.");
            }

            JustDecompile.EngineInfrastructure.DecompiledSourceCode decompiledSourceCode = originalcodeViewerResults as JustDecompile.EngineInfrastructure.DecompiledSourceCode;

            ICodeViewerResults codeViewerResults = new CodeViewerResults(decompiledSourceCode);

            return(new DecompilationResults(assemblyAttributesFilePath, codeViewerResults,
                                            new Dictionary <uint, IOffsetSpan>(), new Dictionary <uint, IOffsetSpan>(), new Dictionary <uint, IOffsetSpan>(), new Dictionary <uint, IOffsetSpan>(), new HashSet <uint>()));
        }
        private void PushAssemblyAttributes(string fileName, string fileContent, IAssemblyAttributeWriter writer)
        {
            string           usingsText, content;
            HashSet <string> usings = new HashSet <string>();

            using (TextReader reader = new StreamReader(fileName))
            {
                StringBuilder usingsBuilder = new StringBuilder();
                while (true)
                {
                    string   line  = reader.ReadLine();
                    char[]   chars = { ' ', ';' };
                    string[] words = line.Split(chars, StringSplitOptions.RemoveEmptyEntries);
                    if (words.Length == 2)
                    {
                        if (words[0] == "using")// make this language independant!
                        {
                            if (!(writer as BaseAssemblyAttributeWriter).AssemblyInfoNamespacesUsed.Contains(words[1]))
                            {
                                usingsBuilder.AppendLine(line);
                            }
                            usings.Add(words[1]);
                        }
                        else
                        {
                            content = line + Environment.NewLine;
                            break;
                        }
                    }
                    else
                    {
                        usingsBuilder.Append(line);
                    }
                }

                usingsText = usingsBuilder.ToString();
                content   += reader.ReadToEnd();
            }

            File.WriteAllText(fileName, usingsText + fileContent + content);
        }
        private void PushAssemblyAttributes(string fileName, string fileContent, IAssemblyAttributeWriter writer)
        {
            string usingsText, content;
            HashSet<string> usings = new HashSet<string>();
            using (TextReader reader = new StreamReader(fileName))
            {
                StringBuilder usingsBuilder = new StringBuilder();
                while (true)
                {
                    string line = reader.ReadLine();
                    char[] chars = { ' ', ';' };
                    string[] words = line.Split(chars, StringSplitOptions.RemoveEmptyEntries);
                    if (words.Length == 2)
                    {
                        if (words[0] == "using")// make this language independant!
                        {
                            if (!(writer as BaseAssemblyAttributeWriter).AssemblyInfoNamespacesUsed.Contains(words[1]))
                            {
                                usingsBuilder.AppendLine(line);
                            }
                            usings.Add(words[1]);
                        }
                        else
                        {
                            content = line + Environment.NewLine;
                            break;
                        }
                    }
                    else
                    {
                        usingsBuilder.Append(line);
                    }
                }

                usingsText = usingsBuilder.ToString();
                content += reader.ReadToEnd();
            }

            File.WriteAllText(fileName, usingsText + fileContent + content);

        }