Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
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));
        }
Esempio n. 4
0
        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);
                }
            }
        }
Esempio n. 5
0
        /// <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);
        }