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);
        }
Ejemplo n.º 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>()));
        }