public static bool TryGetActiveProject(IServiceProvider serviceProvider, out Project project) { Contract.Requires(serviceProvider != null); Contract.Ensures(Contract.ValueAtReturn(out project) != null || !Contract.Result <bool>()); project = null; try { var dte = VsServiceProviderHelper.GetService <DTE>(serviceProvider); if (dte == null) { return(false); } if (dte.Solution != null) { object[] projects = dte.ActiveSolutionProjects as object[]; if (projects != null && projects.Length == 1) { project = (Project)projects[0]; } } return(project != null); } catch (Exception ex) { ContractsVsPackage.SwallowedException(ex); } return(false); }
public static bool TryBuildProject(ContractsVsPackage package, Project project) { Contract.Requires(package != null); Contract.Requires(project != null); SolutionBuild solutionBuild; SolutionConfiguration2 activeConfiguration; if (!TryGetActiveSolutionConfiguration(package, out solutionBuild, out activeConfiguration)) { return(false); } string configurationName = activeConfiguration.Name; string platformName = activeConfiguration.PlatformName; string buildName = configurationName + '|' + platformName; try { solutionBuild.BuildProject( buildName, project.UniqueName, true); } catch (Exception ex) { ContractsVsPackage.SwallowedException(ex); return(false); } return (solutionBuild.BuildState == vsBuildState.vsBuildStateDone && solutionBuild.LastBuildInfo == 0); }
public static bool TryGetProperty( IServiceProvider serviceProvider, Project project, string propertyName, out string value) { Contract.Requires(serviceProvider != null); Contract.Requires(project != null); Contract.Requires(propertyName != null); try { var properties = project.Properties; if (properties != null) { var property = project.Properties.Item(propertyName); if (property != null) { value = (string)property.Value; return(true); } } } catch (Exception ex) { ContractsVsPackage.SwallowedException(ex); } // try msbuild properties return (TryGetBuildProperty( serviceProvider, project, null, propertyName, out value)); }
protected DynamicMenuCommand(ContractsVsPackage package, Guid commandSet, uint id) : base( new EventHandler(InvokeCallback), new EventHandler(ChangeCallback), new EventHandler(QueryStatusCallback), new CommandID(commandSet, (int)id) ) { Contract.Requires(package != null); this.id = (int)id; this.package = package; }
public static bool TryGetHierarchyForProject( IServiceProvider serviceProvider, Project project, out IVsHierarchy hierarchy) { Contract.Requires(serviceProvider != null); Contract.Requires(project != null); string uniqueName; try { uniqueName = project.UniqueName; } catch (Exception ex) { ContractsVsPackage.SwallowedException(ex); uniqueName = null; } if (uniqueName == null) { hierarchy = null; return(false); } else { try { hierarchy = HierarchyFromUniqueName( serviceProvider, project.UniqueName); if (hierarchy == null) { return(false); } return(true); } catch (Exception ex) { ContractsVsPackage.SwallowedException(ex); hierarchy = null; return(false); } } }
private static void InvokeCallbackUIThread(object sender) { UIThreadInvoker.AssertUIThread(); try { var command = sender as DynamicMenuCommand; if (command == null) { throw new InvalidOperationException("invalid command"); } command.ExecuteCommand(); } catch (Exception ex) { ContractsVsPackage.ReportException(ex); } }
/// <summary> /// Initialization of the package; this method is called right after the package is sited, so this is the place /// where you can put all the initilaization code that rely on services provided by VisualStudio. /// </summary> protected override void Initialize() { base.Initialize(); // little helper to invoke delegates in the UI thread UIThreadInvoker.Initialize(); try { var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService; Contract.Assume(mcs != null); mcs.AddCommand(new RunCCheckInContextMenuCommand(this)); } catch (Exception e) { ContractsVsPackage.ReportException(e); } }
private static void QueryStatusCallbackUIThread(object sender) { UIThreadInvoker.AssertUIThread(); try { DynamicMenuCommand command = sender as DynamicMenuCommand; if (command == null) { throw new InvalidOperationException(); } if (command.Package.Zombied) { return; } command.QueryStatus(); } catch (Exception ex) { ContractsVsPackage.ReportException(ex); } }
protected override void Execute() { if (VsShellUtilities.IsSolutionBuilding(this.Package)) { return; } try { UIThreadInvoker.Invoke( // Use it to force the execution in the UI thread delegate { var EnvVars = new Dictionary <string, string>(); Project project; string projectGuid; if (!ProjectHelper.TryGetActiveProject(this.Package, out project) || !ProjectHelper.TryGetProjectGuid(this.Package, project, out projectGuid)) { MessageBox.Show( "Code Contracts could not detect the current project", "Code Contracts: Could not detect the current project", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } EnvVars["CodeContractsTargetProjectGuid"] = projectGuid; string name; if (ContextHelper.TryGetSelectedMemberFullNameMangled(this.Package, out name)) { EnvVars["CodeContractsTargetMember"] = name; } else if (ContextHelper.TryGetSelectedTypeFullNameMangled(this.Package, out name)) { EnvVars["CodeContractsTargetType"] = name; } else if (ContextHelper.TryGetSelectedNamespaceFullNameMangled(this.Package, out name)) { EnvVars["CodeContractsTargetNamespace"] = name; } else { MessageBox.Show( "Code Contracts could not detect the selected type, member or namespace. " + "Please make sure that the editor caret is inside the member to test.", "Code Contracts: Could not detect the code context", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } ThreadPool.QueueUserWorkItem(o => { try { foreach (var p in EnvVars) { Environment.SetEnvironmentVariable(p.Key, p.Value); } ProjectHelper.TryBuildProject(this.Package, project); } catch (Exception e) { ContractsVsPackage.ReportException(e); } finally { foreach (var p in EnvVars) { Environment.SetEnvironmentVariable(p.Key, null); } } }); }); } catch (Exception ex) { ContractsVsPackage.ReportException(ex); } }
public RunCCheckInContextMenuCommand(ContractsVsPackage package) : base(package, IdTable.ContractsVsPackageCmdSet.RunCCheckInContextMenuCommand) { Contract.Requires(package != null); }
protected DynamicMenuCommand(ContractsVsPackage package, uint id) : this(package, IdTable.ContractsVsPackageCmdSet.ID, id) { Contract.Requires(package != null); }
public static bool TryBuildProject(ContractsVsPackage package, Project project) { Contract.Requires(package != null); Contract.Requires(project != null); SolutionBuild solutionBuild; SolutionConfiguration2 activeConfiguration; if (!TryGetActiveSolutionConfiguration(package, out solutionBuild, out activeConfiguration)) return false; string configurationName = activeConfiguration.Name; string platformName = activeConfiguration.PlatformName; string buildName = configurationName + '|' + platformName; try { solutionBuild.BuildProject( buildName, project.UniqueName, true); } catch (Exception ex) { ContractsVsPackage.SwallowedException(ex); return false; } return solutionBuild.BuildState == vsBuildState.vsBuildStateDone && solutionBuild.LastBuildInfo == 0; }
protected EditorMenuCommand(ContractsVsPackage package, uint id) : base(package, id) { Contract.Requires(package != null); }
/// <summary> /// Tries to get a build property value /// </summary> /// <param name="project"></param> /// <param name="configuration">may be null for 'all' configuration</param> /// <param name="name"></param> /// <param name="value"></param> /// <returns></returns> public static bool TryGetBuildProperty( IServiceProvider serviceProvider, Project project, string configuration, string name, out string value) { Contract.Requires(serviceProvider != null); Contract.Requires(project != null); Contract.Requires(name != null); if (configuration == null) { configuration = String.Empty; } IVsHierarchy hierachy; if (!TryGetHierarchyForProject(serviceProvider, project, out hierachy)) { value = null; return(false); } IVsBuildPropertyStorage properties = hierachy as IVsBuildPropertyStorage; if (properties == null) { value = null; return(false); } try { int hresult = properties.GetPropertyValue( name, configuration, (uint)_PersistStorageType.PST_USER_FILE, out value); if (Microsoft.VisualStudio.ErrorHandler.Succeeded(hresult)) { return(true); } } catch (Exception ex) { ContractsVsPackage.SwallowedException(ex); } try { int hresult = properties.GetPropertyValue( name, configuration, (uint)_PersistStorageType.PST_PROJECT_FILE, out value); return(Microsoft.VisualStudio.ErrorHandler.Succeeded(hresult)); } catch (Exception ex) { ContractsVsPackage.SwallowedException(ex); } value = null; return(false); }