Exemplo n.º 1
0
        public bool ProcessFile(string sourceFile, string targetFile = "")
        {
            bool result = false;

            if (string.IsNullOrEmpty(targetFile))
            {
                targetFile = sourceFile;
            }

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            try
            {
                using (var module = CustomResolver.LoadMainModule(sourceFile))
                {
                    if (WriteSymbols)
                    {
                        module.ReadSymbols();
                    }

                    Assembly = module.Assembly;

                    var resolver   = module.AssemblyResolver;
                    var trinityRef = module.AssemblyReferences.Where(x => x.Name == "Semiodesk.Trinity").FirstOrDefault();
                    if (trinityRef == null)
                    {
                        Log.LogMessage("Reference to Semiodesk.Trinity not found. Stopping ImplementRdfMapping...");
                        return(true);
                    }
                    var trinity = resolver.Resolve(trinityRef);

                    PropertyMapping = trinity.MainModule.Types.Where(b => b.FullName == "Semiodesk.Trinity.PropertyMapping`1").FirstOrDefault();


                    Log.LogMessage("------ Begin Task: ImplementRdfMapping [{0}]", Assembly.Name);

                    bool assemblyModified = false;

                    // Iterate over all types in the main assembly.
                    foreach (TypeDefinition type in Assembly.MainModule.Types)
                    {
                        // In the following we need to seperate between properties which have the following attribute combinations:
                        //  - PropertyAttribute with PropertyChangedAttribute
                        //  - PropertyAttribute without PropertyChangedAttribute
                        //  - PropertyChangedAttribute only
                        HashSet <PropertyDefinition> mapping   = type.GetPropertiesWithAttribute("RdfPropertyAttribute").ToHashSet();
                        HashSet <PropertyDefinition> notifying = type.GetPropertiesWithAttribute("NotifyPropertyChangedAttribute").ToHashSet();

                        // Implement the GetTypes()-method for the given type.
                        if (mapping.Any() || type.TryGetCustomAttribute("RdfClassAttribute").Any())
                        {
                            ImplementRdfClassTask implementClass = new ImplementRdfClassTask(this, type);
                            if (implementClass.CanExecute())
                            {
                                // RDF types _must_ be implemented for classes with mapped properties.
                                assemblyModified = implementClass.Execute();
                            }
                        }

                        // Properties which do not raise the PropertyChanged-event can be implemented using minimal IL code.
                        if (mapping.Any())
                        {
                            var implementProperty = new ImplementRdfPropertyTask(this, type);

                            foreach (PropertyDefinition p in mapping.Except(notifying).Where(implementProperty.CanExecute))
                            {
                                assemblyModified = implementProperty.Execute(p);
                            }
                        }

                        // Properties which raise the PropertyChanged-event may also have the RdfProperty attribute.
                        if (notifying.Any())
                        {
                            var implementPropertyChanged = new ImplementNotifyPropertyChangedTask(this, type);

                            foreach (PropertyDefinition p in notifying.Where(implementPropertyChanged.CanExecute))
                            {
                                implementPropertyChanged.IsMappedProperty = mapping.Contains(p);

                                assemblyModified = implementPropertyChanged.Execute(p);
                            }
                        }
                    }

                    if (assemblyModified)
                    {
                        var param = new WriterParameters {
                            WriteSymbols = WriteSymbols
                        };

                        FileInfo sourceDll = new FileInfo(sourceFile);
                        FileInfo targetDll = new FileInfo(targetFile);

                        if (sourceDll.FullName != targetDll.FullName)
                        {
                            Assembly.Write(targetDll.FullName, param);
                        }
                        else
                        {
                            Assembly.Write(param);
                        }
                    }

                    result = true;
                }
            }
            catch (Exception ex)
            {
                Log.LogError(ex.ToString());

                result = false;
            }

            stopwatch.Stop();

            Log.LogMessage("------ End Task: ImplementRdfMapping [Total time: {0}s]", stopwatch.Elapsed.TotalSeconds);

            return(result);
        }
Exemplo n.º 2
0
        public bool ProcessFile(string sourceFile, string targetFile = "")
        {
            bool result = false;

            if (string.IsNullOrEmpty(targetFile))
            {
                targetFile = sourceFile;
            }

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            try
            {
                var resolver = new DefaultAssemblyResolver();
                resolver.AddSearchDirectory(GetAssemblyDirectoryFromType(typeof(Resource)));

                var parameters = new ReaderParameters {
                    AssemblyResolver = resolver
                };

                Assembly = AssemblyDefinition.ReadAssembly(sourceFile, parameters);

                Log.LogMessage("------ Begin Task: ImplementRdfMapping [{0}]", Assembly.Name);

                bool assemblyModified = false;

                // Iterate over all types in the main assembly.
                foreach (TypeDefinition type in Assembly.MainModule.Types)
                {
                    // In the following we need to seperate between properties which have the following attribute combinations:
                    //  - PropertyAttribute with PropertyChangedAttribute
                    //  - PropertyAttribute without PropertyChangedAttribute
                    //  - PropertyChangedAttribute only
                    HashSet <PropertyDefinition> mapping   = type.GetPropertiesWithAttribute <RdfPropertyAttribute>().ToHashSet();
                    HashSet <PropertyDefinition> notifying = type.GetPropertiesWithAttribute <NotifyPropertyChangedAttribute>().ToHashSet();

                    // Implement the GetTypes()-method for the given type.
                    if (mapping.Any() || type.TryGetCustomAttribute <RdfClassAttribute>().Any())
                    {
                        ImplementRdfClassTask implementClass = new ImplementRdfClassTask(this, type);

                        // RDF types _must_ be implemented for classes with mapped properties.
                        assemblyModified = implementClass.Execute();
                    }

                    // Properties which do not raise the PropertyChanged-event can be implemented using minimal IL code.
                    if (mapping.Any())
                    {
                        var implementProperty = new ImplementRdfPropertyTask(this, type);

                        foreach (PropertyDefinition p in mapping.Except(notifying).Where(implementProperty.CanExecute))
                        {
                            assemblyModified = implementProperty.Execute(p);
                        }
                    }

                    // Properties which raise the PropertyChanged-event may also have the RdfProperty attribute.
                    if (notifying.Any())
                    {
                        var implementPropertyChanged = new ImplementNotifyPropertyChangedTask(this, type);

                        foreach (PropertyDefinition p in notifying.Where(implementPropertyChanged.CanExecute))
                        {
                            implementPropertyChanged.IsMappedProperty = mapping.Contains(p);

                            assemblyModified = implementPropertyChanged.Execute(p);
                        }
                    }
                }

                if (assemblyModified)
                {
                    if (WriteSymbols)
                    {
                        // Use the correct debug symbol reader and writer on Mono and .NET
                        if (Type.GetType("Mono.Runtime") != null)
                        {
                            using (ISymbolReader symbolReader = new MdbReaderProvider().GetSymbolReader(Assembly.MainModule, sourceFile))
                            {
                                ISymbolWriterProvider symbolWriter = new MdbWriterProvider();

                                WriteSymbolsToAssembly(targetFile, symbolReader, symbolWriter);
                            }
                        }
                        else
                        {
                            using (ISymbolReader symbolReader = new PdbReaderProvider().GetSymbolReader(Assembly.MainModule, sourceFile))
                            {
                                ISymbolWriterProvider symbolWriter = new PdbWriterProvider();

                                WriteSymbolsToAssembly(targetFile, symbolReader, symbolWriter);
                            }
                        }
                    }
                    else
                    {
                        Assembly.Write(targetFile, new WriterParameters {
                            WriteSymbols = false
                        });
                    }
                }

                result = true;
            }
            catch (Exception ex)
            {
                Log.LogError(ex.ToString());

                result = false;
            }

            stopwatch.Stop();

            Log.LogMessage("------ End Task: ImplementRdfMapping [Total time: {0}s]", stopwatch.Elapsed.TotalSeconds);

            return(result);
        }