public T WithProject <T>(IVsProject project, Func <MSProject, T> func)
        {
            var msproject = project.GetMSBuildProject();

            msproject.ReevaluateIfNecessary();
            return(func(msproject));
        }
Exemple #2
0
        public static string GetEvaluatedProperty(IVsProject project, string name, string defaultValue)
        {
            var    msproject = project.GetMSBuildProject(true);
            string value     = msproject.GetEvaluatedProperty(name);

            return(string.IsNullOrEmpty(value) ? defaultValue : value);
        }
Exemple #3
0
        public static void SetupGenerated(IVsProject project, IceBuilderProjectType type)
        {
            if (type == IceBuilderProjectType.CppProjectType)
            {
                //
                // This will ensure that property reads don't use a cached project.
                //
                project.GetMSBuildProject(false);

                var generated = GetCppGeneratedFiles(project);
                Package.Instance.FileTracker.Reap(GetProjectFullPath(project), GetGeneratedFiles(generated));
                Package.Instance.VCUtil.AddGeneratedFiles(project, generated);
            }
            else // C# project
            {
                var generated           = GetCsharpGeneratedFiles(project);
                var dteproject          = project.GetDTEProject();
                var activeConfiguration = dteproject.ConfigurationManager.ActiveConfiguration;
                Package.Instance.FileTracker.Reap(GetProjectFullPath(project), GetGeneratedFiles(generated));
                string configName = string.Format("{0}|{1}", activeConfiguration.ConfigurationName, activeConfiguration.PlatformName);
                foreach (var fileset in generated)
                {
                    if (fileset.sources.Count > 1)
                    {
                        const string message =
                            "The OutputDir SliceCompile item metadata must evaluate to the same value with all project configurations.";

                        UIUtil.ShowErrorDialog("Ice Builder", message);
                        break;
                    }

                    var file = fileset.sources.First().Key;
                    if (!Directory.Exists(Path.GetDirectoryName(file)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(file));
                    }

                    EnvDTE.ProjectItem item = FindProjectItem(file);
                    if (item == null)
                    {
                        if (!File.Exists(file))
                        {
                            File.Create(file).Dispose();
                        }
                        dteproject.ProjectItems.AddFromFile(file);
                        try
                        {
                            //
                            // Remove the file otherwise it will be considered up to date.
                            //
                            File.Delete(file);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }
        }
Exemple #4
0
        public static List <string> GetIceBuilderItems(IVsProject project)
        {
            var msproject = project.GetMSBuildProject();

            return(msproject.Items.Where(item => item.ItemType.Equals("SliceCompile"))
                   .Select(item => item.EvaluatedInclude)
                   .ToList());
        }
        public void UpdateProject(IVsProject project, Action <MSProject> action)
        {
            project.EnsureIsCheckout();
            var msproject = project.GetMSBuildProject();

            msproject.ReevaluateIfNecessary();
            action(msproject);
            project.GetDTEProject().Save();
        }
Exemple #6
0
        CheckGenerateFileIsValid(IVsProject project, IceBuilderProjectType projectType, string path)
        {
            var projectDir = project.GetProjectBaseDirectory();
            var msproject  = project.GetMSBuildProject();

            if (projectType == IceBuilderProjectType.CsharpProjectType)
            {
                string outputDir       = GetDefaultOutputDir(msproject, true);
                string generatedSource = Path.Combine(projectDir, outputDir, GetCSharpGeneratedItemPath(Path.GetFileName(path)));
                if (File.Exists(generatedSource))
                {
                    const string message =
                        "A file named '{0}' already exists.\nIf you want to add '{1}' first remove '{0}'.";

                    UIUtil.ShowErrorDialog("Ice Builder",
                                           string.Format(message,
                                                         GetPathRelativeToProject(project, generatedSource),
                                                         GetPathRelativeToProject(project, path)));
                    return(false);
                }
            }
            else if (projectType == IceBuilderProjectType.CppProjectType)
            {
                var dteproject      = project.GetDTEProject();
                var outputDir       = GetDefaultOutputDir(msproject, false);
                var headerOutputDir = GetDefaultHeaderOutputDir(msproject, false);
                var source          = GetCppGeneratedSourceItemPath(msproject, path);
                var header          = GetCppGeneratedHeaderItemPath(msproject, path);

                foreach (EnvDTE.Configuration config in dteproject.ConfigurationManager)
                {
                    var evaluatedOutputDir       = Package.Instance.VCUtil.Evaluate(config, outputDir);
                    var evaluatedHeaderOutputDir = headerOutputDir.Equals(outputDir) ? evaluatedOutputDir :
                                                   Package.Instance.VCUtil.Evaluate(config, headerOutputDir);

                    string generatedSource = Path.GetFullPath(Path.Combine(projectDir, evaluatedOutputDir, source));
                    string generatedHeader = Path.GetFullPath(Path.Combine(projectDir, evaluatedHeaderOutputDir, header));

                    if (File.Exists(generatedSource) || File.Exists(generatedHeader))
                    {
                        const string message =
                            "A file named '{0}' or '{1}' already exists. If you want to add '{2}' first remove '{0}' and '{1}'.";

                        UIUtil.ShowErrorDialog("Ice Builder",
                                               string.Format(message,
                                                             GetPathRelativeToProject(project, generatedSource),
                                                             GetPathRelativeToProject(project, generatedHeader),
                                                             GetPathRelativeToProject(project, path)));
                        return(false);
                    }
                }
            }
            return(true);
        }
 public static IceBuilderProjectType IsIceBuilderEnabled(IVsProject project)
 {
     if (project != null)
     {
         IceBuilderProjectType type = IsCppProject(project) ? IceBuilderProjectType.CppProjectType :
                                      IsCSharpProject(project) ? IceBuilderProjectType.CsharpProjectType : IceBuilderProjectType.None;
         if (type != IceBuilderProjectType.None)
         {
             if (MSBuildUtils.IsIceBuilderEnabled(project.GetMSBuildProject(true)))
             {
                 return(type);
             }
         }
     }
     return(IceBuilderProjectType.None);
 }
 public static IceBuilderProjectType IsIceBuilderNuGetInstalled(IVsProject project)
 {
     if (project != null)
     {
         IceBuilderProjectType type = IsCppProject(project) ? IceBuilderProjectType.CppProjectType :
                                      IsCSharpProject(project) ? IceBuilderProjectType.CsharpProjectType : IceBuilderProjectType.None;
         if (type != IceBuilderProjectType.None)
         {
             if (Package.Instance.NuGet.IsPackageInstalled(project.GetDTEProject(), Package.NuGetBuilderPackageId) ||
                 MSBuildUtils.HasIceBuilderPackageReference(project.GetMSBuildProject(true)))
             {
                 return(type);
             }
         }
     }
     return(IceBuilderProjectType.None);
 }
Exemple #9
0
        public void SaveProject(IVsProject project)
        {
            Microsoft.Build.Evaluation.Project msproject = project.GetMSBuildProject(true);
            IVsHierarchy hier        = project as IVsHierarchy;
            Guid         projectGUID = Guid.Empty;

            IVsSolution.GetGuidOfProject(hier, out projectGUID);
            IVsSolution4.UnloadProject(projectGUID, (uint)_VSProjectUnloadStatus.UNLOADSTATUS_UnloadedByUser);
            msproject.Save();
            try
            {
                ProjectCollection.GlobalProjectCollection.UnloadProject(msproject);
            }
            catch (Exception)
            {
                //expected if the project is not in the global project collection
            }
            IVsSolution4.ReloadProject(ref projectGUID);
        }
 public void UpdateProject(IVsProject project, Action <MSProject> action)
 {
     project.EnsureIsCheckout();
     ThreadHelper.JoinableTaskFactory.Run(async() =>
     {
         var unconfiguredProject = GetUnconfiguredProject(project);
         if (unconfiguredProject != null)
         {
             await UpdateProjectAsync(unconfiguredProject, action);
         }
         else
         {
             var msproject = project.GetMSBuildProject();
             msproject.ReevaluateIfNecessary();
             action(msproject);
         }
     });
     project.GetDTEProject().Save();
 }
Exemple #11
0
        GetCsharpGeneratedFiles(IVsProject project)
        {
            var generated       = new List <GeneratedFileSet>();
            var msproject       = project.GetMSBuildProject();
            var dteproject      = project.GetDTEProject();
            var items           = GetIceBuilderItems(project);
            var propertyStorage = project as IVsBuildPropertyStorage;
            var projectDir      = GetProjectBaseDirectory(project);

            foreach (var item in items)
            {
                var fileset = new GeneratedFileSet
                {
                    filename = item,
                    sources  = new Dictionary <string, List <EnvDTE.Configuration> >(),
                    headers  = new Dictionary <string, List <EnvDTE.Configuration> >()
                };

                var outputDir = msproject.GetItemMetadata(item, "OutputDir");
                foreach (EnvDTE.Configuration configuration in dteproject.ConfigurationManager)
                {
                    var configName         = string.Format("{0}|{1}", configuration.ConfigurationName, configuration.PlatformName);
                    var evaluatedOutputDir = Evaluate(propertyStorage, configName, outputDir);

                    var csFilename = string.Format("{0}.cs", Path.GetFileNameWithoutExtension(item));
                    csFilename = Path.GetFullPath(Path.Combine(projectDir, evaluatedOutputDir, csFilename));

                    if (fileset.sources.ContainsKey(csFilename))
                    {
                        fileset.sources[csFilename].Add(configuration);
                    }
                    else
                    {
                        var configurations = new List <EnvDTE.Configuration>();
                        configurations.Add(configuration);
                        fileset.sources[csFilename] = configurations;
                    }
                }
                generated.Add(fileset);
            }
            return(generated);
        }
        public T WithProject <T>(IVsProject project, Func <MSProject, T> func)
        {
            var data = default(T);

            ThreadHelper.JoinableTaskFactory.Run(async() =>
            {
                var unconfiguredProject = GetUnconfiguredProject(project);
                if (unconfiguredProject != null)
                {
                    data = await WithProjectAsync(unconfiguredProject, func);
                }
                else
                {
                    var msproject = project.GetMSBuildProject();
                    msproject.ReevaluateIfNecessary();
                    data = func(msproject);
                }
            });
            return(data);
        }
Exemple #13
0
        public void InitializeProject(IVsProject project)
        {
            IceBuilderProjectType projectType = DTEUtil.IsIceBuilderNuGetInstalled(project);

            if (projectType != IceBuilderProjectType.None)
            {
                var dteproject = project.GetDTEProject();
                if (projectType == IceBuilderProjectType.CppProjectType)
                {
                    VCUtil.SetupSliceFilter(dteproject);
                }
                else
                {
                    if (project is IVsAggregatableProject)
                    {
                        if (MSBuildUtils.AddProjectFlavorIfNotExists(project.GetMSBuildProject(true), IceBuilderNewFlavor))
                        {
                            dteproject.Save();
                        }
                    }
                }
                FileTracker.Add(project, projectType);
            }
        }
Exemple #14
0
        GetCppGeneratedFiles(IVsProject project)
        {
            var generated       = new List <GeneratedFileSet>();
            var msproject       = project.GetMSBuildProject();
            var dteproject      = project.GetDTEProject();
            var items           = GetIceBuilderItems(project);
            var propertyStorage = project as IVsBuildPropertyStorage;
            var projectDir      = GetProjectBaseDirectory(project);

            var vcutil = Package.Instance.VCUtil;

            foreach (var item in items)
            {
                var fileset = new GeneratedFileSet
                {
                    filename = item,
                    sources  = new Dictionary <string, List <EnvDTE.Configuration> >(),
                    headers  = new Dictionary <string, List <EnvDTE.Configuration> >()
                };

                var outputDir       = msproject.GetItemMetadata(item, "OutputDir");
                var headerOutputDir = msproject.GetItemMetadata(item, "HeaderOutputDir");
                var headerExt       = msproject.GetItemMetadata(item, "HeaderExt");
                var sourceExt       = msproject.GetItemMetadata(item, "SourceExt");
                foreach (EnvDTE.Configuration configuration in dteproject.ConfigurationManager)
                {
                    var evaluatedOutputDir       = vcutil.Evaluate(configuration, outputDir);
                    var evaluatedHeaderOutputDir = string.IsNullOrEmpty(headerOutputDir) ?
                                                   evaluatedOutputDir : vcutil.Evaluate(configuration, headerOutputDir);
                    var cppFilename = string.Format("{0}.{1}", Path.GetFileNameWithoutExtension(item), sourceExt);
                    var hFilename   = string.Format("{0}.{1}", Path.GetFileNameWithoutExtension(item), headerExt);

                    cppFilename = Path.GetFullPath(Path.Combine(projectDir, evaluatedOutputDir, cppFilename));
                    hFilename   = Path.GetFullPath(Path.Combine(projectDir, evaluatedHeaderOutputDir, hFilename));

                    if (fileset.sources.ContainsKey(cppFilename))
                    {
                        fileset.sources[cppFilename].Add(configuration);
                    }
                    else
                    {
                        var configurations = new List <EnvDTE.Configuration>();
                        configurations.Add(configuration);
                        fileset.sources[cppFilename] = configurations;
                    }

                    if (fileset.headers.ContainsKey(hFilename))
                    {
                        fileset.headers[hFilename].Add(configuration);
                    }
                    else
                    {
                        var configurations = new List <EnvDTE.Configuration>();
                        configurations.Add(configuration);
                        fileset.headers[hFilename] = configurations;
                    }
                }
                generated.Add(fileset);
            }
            return(generated);
        }
        public bool Build(IVsProject p, BuildCallback buildCallback, BuildLogger buildLogger)
        {
            MSBuildProject project = p.GetMSBuildProject(false);

            //
            // We need to set this before we acquire the build resources otherwise Msbuild
            // will not see the changes.
            //
            bool onlyLogCriticalEvents = project.ProjectCollection.OnlyLogCriticalEvents;

            project.ProjectCollection.Loggers.Add(buildLogger);
            project.ProjectCollection.OnlyLogCriticalEvents = false;

            uint cookie;
            int  err = BuildManagerAccessor.AcquireBuildResources(VSBUILDMANAGERRESOURCE.VSBUILDMANAGERRESOURCE_DESIGNTIME |
                                                                  VSBUILDMANAGERRESOURCE.VSBUILDMANAGERRESOURCE_UITHREAD, out cookie);

            if (err != VSConstants.E_PENDING && err != VSConstants.S_OK)
            {
                ErrorHandler.ThrowOnFailure(err);
            }

            if (err == VSConstants.E_PENDING)
            {
                project.ProjectCollection.Loggers.Remove(buildLogger);
                project.ProjectCollection.OnlyLogCriticalEvents = onlyLogCriticalEvents;

                Dispatcher          = Dispatcher.CurrentDispatcher;
                BuildAvailableEvent = new ManualResetEvent(false);
                BuildAvailableEvent.SafeWaitHandle = new SafeWaitHandle(BuildManagerAccessor.DesignTimeBuildAvailable, false);

                Thread t = new Thread(() =>
                {
                    BuildAvailableEvent.WaitOne();
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        Package.Instance.BuildNextProject();
                    }));
                });
                t.Start();
                return(false);
            }
            else
            {
                try
                {
                    Dictionary <string, string> properties = new Dictionary <string, string>();
                    string platform = buildCallback.ProjectConfiguration.PlatformName;
                    properties["Platform"]      = platform.Equals("Any CPU") ? "AnyCPU" : platform;
                    properties["Configuration"] = buildCallback.ProjectConfiguration.ConfigurationName;

                    BuildRequestData buildRequest = new BuildRequestData(
                        ProjectUtil.GetProjectFullPath(p),
                        properties,
                        null,
                        new string[] { "SliceCompile" },
                        project.ProjectCollection.HostServices,
                        BuildRequestDataFlags.ProvideProjectStateAfterBuild |
                        BuildRequestDataFlags.IgnoreExistingProjectState |
                        BuildRequestDataFlags.ReplaceExistingProjectInstance);

                    BuildSubmission submission = BuildManager.DefaultBuildManager.PendBuildRequest(buildRequest);
                    ErrorHandler.ThrowOnFailure(BuildManagerAccessor.RegisterLogger(submission.SubmissionId, buildLogger));
                    buildCallback.BeginBuild();
                    submission.ExecuteAsync(s =>
                    {
                        Dispatcher.BeginInvoke(DispatcherPriority.Send, new Action(() =>
                        {
                            project.ProjectCollection.Loggers.Remove(buildLogger);
                            project.ProjectCollection.OnlyLogCriticalEvents = onlyLogCriticalEvents;
                            BuildManagerAccessor.ReleaseBuildResources(cookie);
                            s.BuildManager.ResetCaches();
                            BuildManagerAccessor.UnregisterLoggers(s.SubmissionId);
                            buildCallback.EndBuild(s.BuildResult.OverallResult == BuildResultCode.Success);
                        }));
                    }, null);

                    return(true);
                }
                catch (Exception)
                {
                    project.ProjectCollection.Loggers.Remove(buildLogger);
                    project.ProjectCollection.OnlyLogCriticalEvents = onlyLogCriticalEvents;
                    BuildManagerAccessor.ReleaseBuildResources(cookie);
                    throw;
                }
            }
        }
 public static bool IsCSharpProject(IVsProject p)
 {
     return(ProjectUtil.GetProjecTypeGuid(p).Equals(csharpProjectGUID) &&
            MSBuildUtils.IsCSharpProject(p.GetMSBuildProject(true)));
 }