public void ReadAssembly() { var asm = typeof(object).GetTypeInfo().Assembly; using (var stream = File.OpenRead(asm.Location)) using (var reader = new AssemblyReader(stream)) { var result = reader.ReadAssembly(); //reader.BaseStream.Should().Be(expected, Output); } }
private static AssemblyDefinition CreateAssemblyDefinition(AssemblyReference assemblyRef) { var assemblyName = new AssemblyName(assemblyRef.ToString()); var assembly = Assembly.Load(assemblyName); using (var stream = File.OpenRead(assembly.Location)) using (var reader = new AssemblyReader(stream)) { return(reader.ReadAssembly()); } }
private async Task LoadLocalAssemblyAsync(string assemblyPath) { this.Dispatcher.Invoke(() => { _assemblyLoad = null; _listLocalAssembly.Clear(); _listMissingCrm.Clear(); txtBLoadedAssemblyPath.Text = string.Empty; txtBLoadedAssemblyName.Text = string.Empty; txtBLoadedAssemblyVersion.Text = string.Empty; txtBLoadedAssemblyCulture.Text = string.Empty; txtBLoadedAssemblyPublicKeyToken.Text = string.Empty; }); if (string.IsNullOrEmpty(assemblyPath) || !File.Exists(assemblyPath) ) { UpdateStatus(Properties.OutputStrings.FileNotExists); return; } if (!IsControlsEnabled) { return; } ToggleControls(false, Properties.OutputStrings.LoadingAssemblyFromPathFormat1, assemblyPath); AssemblyReaderResult assemblyCheck = null; using (var reader = new AssemblyReader()) { assemblyCheck = reader.ReadAssembly(assemblyPath); } if (assemblyCheck == null) { ToggleControls(true, Properties.OutputStrings.LoadingAssemblyFromPathFailedFormat1, assemblyPath); return; } assemblyCheck.Content = File.ReadAllBytes(assemblyPath); this._assemblyLoad = assemblyCheck; cmBAssemblyToLoad.Text = this._assemblyLoad.FilePath; txtBLoadedAssemblyPath.Text = this._assemblyLoad.FilePath; txtBLoadedAssemblyName.Text = this._assemblyLoad.Name; txtBLoadedAssemblyVersion.Text = this._assemblyLoad.Version; txtBLoadedAssemblyCulture.Text = this._assemblyLoad.Culture; txtBLoadedAssemblyPublicKeyToken.Text = this._assemblyLoad.PublicKeyToken; var fileInfo = new FileInfo(assemblyPath); txtBLoadedAssemblyDateModified.Text = fileInfo.LastWriteTime.ToString(EntityFileNameFormatter.dateFormatYearMonthDayHourMinuteSecond); if (PluginAssembly.Id == Guid.Empty) { txtBFileNameOnServer.Text = this._assemblyLoad.FileName; } txtBWorkflowActivityGroupName.Text = string.Format("{0} ({1})", this._assemblyLoad.Name, this._assemblyLoad.Version); var crmPlugins = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase); var crmWorkflows = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase); if (this.PluginAssembly != null && this.PluginAssembly.Id != Guid.Empty) { var repositoryType = new PluginTypeRepository(_service); var pluginTypes = await repositoryType.GetPluginTypesAsync(this.PluginAssembly.Id); foreach (var item in pluginTypes.Where(e => !e.IsWorkflowActivity.GetValueOrDefault()).Select(e => e.TypeName)) { crmPlugins.Add(item); } foreach (var item in pluginTypes.Where(e => e.IsWorkflowActivity.GetValueOrDefault()).Select(e => e.TypeName)) { crmWorkflows.Add(item); } } HashSet <string> assemblyPlugins = new HashSet <string>(_assemblyLoad.Plugins, StringComparer.InvariantCultureIgnoreCase); HashSet <string> assemblyWorkflows = new HashSet <string>(_assemblyLoad.Workflows, StringComparer.InvariantCultureIgnoreCase); var pluginsOnlyInCrm = crmPlugins.Except(assemblyPlugins, StringComparer.InvariantCultureIgnoreCase); var workflowOnlyInCrm = crmWorkflows.Except(assemblyWorkflows, StringComparer.InvariantCultureIgnoreCase); var pluginsOnlyInLocalAssembly = assemblyPlugins.Except(crmPlugins, StringComparer.InvariantCultureIgnoreCase); var workflowOnlyInLocalAssembly = assemblyWorkflows.Except(crmWorkflows, StringComparer.InvariantCultureIgnoreCase); List <PluginTreeViewItem> listLocalAssembly = new List <PluginTreeViewItem>(); List <PluginTreeViewItem> listMissingCrm = new List <PluginTreeViewItem>(); foreach (var pluginTypeName in pluginsOnlyInLocalAssembly.OrderBy(s => s)) { var nodeType = new PluginTreeViewItem(ComponentType.PluginType) { Name = pluginTypeName, Image = _imagePluginType, }; listLocalAssembly.Add(nodeType); } foreach (var pluginTypeName in workflowOnlyInLocalAssembly.OrderBy(s => s)) { var nodeType = new PluginTreeViewItem(ComponentType.PluginType) { Name = pluginTypeName, Image = _imageWorkflowActivity, IsWorkflowActivity = true, }; listLocalAssembly.Add(nodeType); } foreach (var pluginTypeName in pluginsOnlyInCrm.OrderBy(s => s)) { var nodeType = new PluginTreeViewItem(ComponentType.PluginType) { Name = pluginTypeName, Image = _imagePluginType, }; listMissingCrm.Add(nodeType); } foreach (var pluginTypeName in workflowOnlyInCrm.OrderBy(s => s)) { var nodeType = new PluginTreeViewItem(ComponentType.PluginType) { Name = pluginTypeName, Image = _imageWorkflowActivity, IsWorkflowActivity = true, }; listMissingCrm.Add(nodeType); } this.Dispatcher.Invoke(() => { foreach (var item in listLocalAssembly) { _listLocalAssembly.Add(item); } this.trVPluginTreeNew.UpdateLayout(); foreach (var item in listMissingCrm) { _listMissingCrm.Add(item); } this.trVPluginTreeMissing.UpdateLayout(); }); ToggleControls(true, Properties.OutputStrings.LoadingAssemblyFromPathCompletedFormat1, assemblyPath); }
public async Task <string> CreateFileWithAssemblyComparing(string folder, IOrganizationServiceExtented service, Guid idPluginAssembly, string assemblyName, string defaultOutputFilePath) { var repositoryType = new PluginTypeRepository(service); var task = repositoryType.GetPluginTypesAsync(idPluginAssembly); string assemblyPath = service.ConnectionData.GetLastAssemblyPath(assemblyName); List <string> lastPaths = service.ConnectionData.GetAssemblyPaths(assemblyName).ToList(); if (!string.IsNullOrEmpty(defaultOutputFilePath) && !lastPaths.Contains(defaultOutputFilePath, StringComparer.InvariantCultureIgnoreCase) ) { lastPaths.Insert(0, defaultOutputFilePath); } bool isSuccess = false; var t = new Thread(() => { try { var openFileDialog1 = new Microsoft.Win32.OpenFileDialog { Filter = "Plugin Assebmly (.dll)|*.dll", FilterIndex = 1, RestoreDirectory = true }; if (!string.IsNullOrEmpty(assemblyPath)) { openFileDialog1.InitialDirectory = Path.GetDirectoryName(assemblyPath); openFileDialog1.FileName = Path.GetFileName(assemblyPath); } else if (!string.IsNullOrEmpty(defaultOutputFilePath)) { openFileDialog1.InitialDirectory = Path.GetDirectoryName(defaultOutputFilePath); openFileDialog1.FileName = Path.GetFileName(defaultOutputFilePath); } if (lastPaths.Any()) { openFileDialog1.CustomPlaces = new List <Microsoft.Win32.FileDialogCustomPlace>(lastPaths.Select(p => new Microsoft.Win32.FileDialogCustomPlace(Path.GetDirectoryName(p)))); } if (openFileDialog1.ShowDialog() == true) { isSuccess = true; assemblyPath = openFileDialog1.FileName; } } catch (Exception ex) { DTEHelper.WriteExceptionToOutput(service.ConnectionData, ex); } }); t.SetApartmentState(ApartmentState.STA); t.Start(); t.Join(); if (!isSuccess) { return(string.Empty); } string filePath = string.Empty; service.ConnectionData.AddAssemblyMapping(assemblyName, assemblyPath); service.ConnectionData.Save(); AssemblyReaderResult assemblyCheck = null; using (var reader = new AssemblyReader()) { assemblyCheck = reader.ReadAssembly(assemblyPath); } if (assemblyCheck == null) { return(string.Empty); } var pluginTypes = await task; var crmPlugins = new HashSet <string>(pluginTypes.Where(e => !e.IsWorkflowActivity.GetValueOrDefault()).Select(e => e.TypeName), StringComparer.InvariantCultureIgnoreCase); var crmWorkflows = new HashSet <string>(pluginTypes.Where(e => e.IsWorkflowActivity.GetValueOrDefault()).Select(e => e.TypeName), StringComparer.InvariantCultureIgnoreCase); var content = new StringBuilder(); content.AppendLine(service.ConnectionData.GetConnectionInfo()).AppendLine(); content.AppendFormat("Description for PluginAssembly '{0}' at {1}", assemblyName, DateTime.Now.ToString("G", System.Globalization.CultureInfo.CurrentCulture)).AppendLine(); content.AppendFormat("Local Assembly '{0}'", assemblyPath).AppendLine(); HashSet <string> assemblyPlugins = new HashSet <string>(assemblyCheck.Plugins, StringComparer.InvariantCultureIgnoreCase); HashSet <string> assemblyWorkflows = new HashSet <string>(assemblyCheck.Workflows, StringComparer.InvariantCultureIgnoreCase); var pluginsOnlyInCrm = crmPlugins.Except(assemblyPlugins, StringComparer.InvariantCultureIgnoreCase); var workflowOnlyInCrm = crmWorkflows.Except(assemblyWorkflows, StringComparer.InvariantCultureIgnoreCase); var pluginsOnlyInLocalAssembly = assemblyPlugins.Except(crmPlugins, StringComparer.InvariantCultureIgnoreCase); var workflowOnlyInLocalAssembly = assemblyWorkflows.Except(crmWorkflows, StringComparer.InvariantCultureIgnoreCase); FillInformation(content, "Plugins ONLY in Crm Assembly {0}", pluginsOnlyInCrm); FillInformation(content, "Workflows ONLY in Crm Assembly {0}", workflowOnlyInCrm); FillInformation(content, "Plugins ONLY in Local Assembly {0}", pluginsOnlyInLocalAssembly); FillInformation(content, "Workflows ONLY in Local Assembly {0}", workflowOnlyInLocalAssembly); if (!pluginsOnlyInCrm.Any() && !workflowOnlyInCrm.Any() && !pluginsOnlyInLocalAssembly.Any() && !workflowOnlyInLocalAssembly.Any() ) { content.AppendLine().AppendLine().AppendLine(); content.AppendLine("No difference in Assemblies."); } FillInformation(content, "Plugins in Crm Assembly {0}", crmPlugins); FillInformation(content, "Workflows in Crm Assembly {0}", crmWorkflows); FillInformation(content, "Plugins in Local Assembly {0}", assemblyPlugins); FillInformation(content, "Workflows in Local Assembly {0}", assemblyWorkflows); string fileName = EntityFileNameFormatter.GetPluginAssemblyFileName(service.ConnectionData.Name, assemblyName, "Comparing", "txt"); filePath = Path.Combine(folder, FileOperations.RemoveWrongSymbols(fileName)); File.WriteAllText(filePath, content.ToString(), new UTF8Encoding(false)); this._iWriteToOutput.WriteToOutput(service.ConnectionData, "Assembly {0} Comparing exported to {1}", assemblyName, filePath); this._iWriteToOutput.WriteToOutputFilePathUri(service.ConnectionData, filePath); return(filePath); }
private async Task BuildProjectUpdatePluginAssembly( ConnectionData connectionData , CommonConfiguration commonConfig , IOrganizationServiceExtented service , PluginAssemblyRepository repositoryAssembly , PluginTypeRepository repositoryType , EnvDTE.Project project , bool registerPlugins ) { var assembly = await repositoryAssembly.FindAssemblyAsync(project.Name); if (assembly == null) { assembly = await repositoryAssembly.FindAssemblyByLikeNameAsync(project.Name); } if (assembly == null) { this._iWriteToOutput.WriteToOutput(service.ConnectionData, Properties.OutputStrings.PluginAssemblyNotFoundedByNameFormat1, project.Name); WindowHelper.OpenPluginAssemblyExplorer( this._iWriteToOutput , service , commonConfig , project.Name ); return; } this._iWriteToOutput.WriteToOutput(service.ConnectionData, Properties.OutputStrings.BuildingProjectFormat1, project.Name); var buildResult = await _iWriteToOutput.BuildProjectAsync(project); if (buildResult != 0) { this._iWriteToOutput.WriteToOutput(service.ConnectionData, Properties.OutputStrings.BuildingProjectFailedFormat1, project.Name); return; } this._iWriteToOutput.WriteToOutput(service.ConnectionData, Properties.OutputStrings.BuildingProjectCompletedFormat1, project.Name); string defaultOutputFilePath = PropertiesHelper.GetOutputFilePath(project); if (!File.Exists(defaultOutputFilePath)) { this._iWriteToOutput.WriteToOutput(connectionData, Properties.OutputStrings.FileNotExistsFormat1, defaultOutputFilePath); return; } this._iWriteToOutput.WriteToOutput(connectionData, Properties.OutputStrings.LoadingAssemblyFromPathFormat1, defaultOutputFilePath); AssemblyReaderResult assemblyLoad = null; using (var reader = new AssemblyReader()) { assemblyLoad = reader.ReadAssembly(defaultOutputFilePath); } if (assemblyLoad == null) { this._iWriteToOutput.WriteToOutput(connectionData, Properties.OutputStrings.LoadingAssemblyFromPathFailedFormat1, defaultOutputFilePath); return; } assemblyLoad.Content = File.ReadAllBytes(defaultOutputFilePath); var crmPlugins = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase); var crmWorkflows = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase); var pluginTypes = await repositoryType.GetPluginTypesAsync(assembly.Id); foreach (var item in pluginTypes.Where(e => !e.IsWorkflowActivity.GetValueOrDefault()).Select(e => e.TypeName)) { crmPlugins.Add(item); } foreach (var item in pluginTypes.Where(e => e.IsWorkflowActivity.GetValueOrDefault()).Select(e => e.TypeName)) { crmWorkflows.Add(item); } HashSet <string> assemblyPlugins = new HashSet <string>(assemblyLoad.Plugins, StringComparer.InvariantCultureIgnoreCase); HashSet <string> assemblyWorkflows = new HashSet <string>(assemblyLoad.Workflows, StringComparer.InvariantCultureIgnoreCase); var pluginsOnlyInCrm = crmPlugins.Except(assemblyPlugins, StringComparer.InvariantCultureIgnoreCase).ToList(); var workflowOnlyInCrm = crmWorkflows.Except(assemblyWorkflows, StringComparer.InvariantCultureIgnoreCase).ToList(); if (pluginsOnlyInCrm.Any() || workflowOnlyInCrm.Any()) { if (pluginsOnlyInCrm.Any()) { this._iWriteToOutput.WriteToOutput(connectionData, Properties.OutputStrings.PluginTypesExistsOnlyInCRMFormat1, pluginsOnlyInCrm.Count); foreach (var item in pluginsOnlyInCrm.OrderBy(s => s)) { this._iWriteToOutput.WriteToOutput(connectionData, _tabspacer + item); } } if (workflowOnlyInCrm.Any()) { this._iWriteToOutput.WriteToOutput(connectionData, Properties.OutputStrings.WorkflowTypesExistsOnlyInCRMFormat1, workflowOnlyInCrm.Count); foreach (var item in workflowOnlyInCrm.OrderBy(s => s)) { this._iWriteToOutput.WriteToOutput(connectionData, _tabspacer + item); } } this._iWriteToOutput.WriteToOutput(service.ConnectionData, Properties.OutputStrings.CannotUpdatePluginAssemblyFormat1, assembly.Name); return; } string workflowActivityGroupName = string.Format("{0} ({1})", assemblyLoad.Name, assemblyLoad.Version); service.ConnectionData.AddAssemblyMapping(assemblyLoad.Name, assemblyLoad.FilePath); service.ConnectionData.Save(); this._iWriteToOutput.WriteToOutput(connectionData, Properties.OutputStrings.UpdatingPluginAssemblyFormat1, service.ConnectionData.Name); assembly.Content = Convert.ToBase64String(assemblyLoad.Content); try { await service.UpdateAsync(assembly); if (registerPlugins) { var pluginsOnlyInLocalAssembly = assemblyPlugins.Except(crmPlugins, StringComparer.InvariantCultureIgnoreCase); var workflowOnlyInLocalAssembly = assemblyWorkflows.Except(crmWorkflows, StringComparer.InvariantCultureIgnoreCase); if (pluginsOnlyInLocalAssembly.Any() || workflowOnlyInLocalAssembly.Any()) { int totalCount = pluginsOnlyInLocalAssembly.Count() + workflowOnlyInLocalAssembly.Count(); var assemblyRef = assembly.ToEntityReference(); this._iWriteToOutput.WriteToOutput(connectionData, Properties.OutputStrings.RegisteringNewPluginTypesFormat2, service.ConnectionData.Name, totalCount); await RegisterNewPluginTypes(service, pluginsOnlyInLocalAssembly, assemblyRef, false, workflowActivityGroupName); await RegisterNewPluginTypes(service, workflowOnlyInLocalAssembly, assemblyRef, true, workflowActivityGroupName); this._iWriteToOutput.WriteToOutput(connectionData, Properties.OutputStrings.RegisteringNewPluginTypesCompletedFormat2, service.ConnectionData.Name, totalCount); } } } catch (Exception ex) { this._iWriteToOutput.WriteToOutput(connectionData, Properties.OutputStrings.UpdatingPluginAssemblyFailedFormat1, service.ConnectionData.Name); _iWriteToOutput.WriteErrorToOutput(service.ConnectionData, ex); _iWriteToOutput.ActivateOutputWindow(service.ConnectionData); } }