private DirectoryInfo GetFolderPath(string projectFileFullPath, string propertyName)
        {
            Microsoft.Build.Evaluation.Project msbuildProject = ProjectLoader.LoadProject(projectFileFullPath);
            ProjectProperty property     = msbuildProject.GetProperty(propertyName);
            string          expandString = msbuildProject.ExpandString(property.UnevaluatedValue);

            return(new DirectoryInfo(expandString));
        }
Example #2
0
        public static string GetPropertyValueFrom(string projectFile, string propertyName)
        {
            using (var projectCollection = new ProjectCollection())
            {
                var project = new Microsoft.Build.Evaluation.Project(
                    projectFile, null, null, projectCollection, ProjectLoadSettings.Default);

                return(project.Properties
                       .Where(x => x.Name == propertyName)
                       .Select(x => x.EvaluatedValue)
                       .SingleOrDefault());
            }
        }
Example #3
0
        private static IEnumerable <ProjectReferenceToAdd> FindChanges(Project projectAdded)
        {
            Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
            string assemblyName = projectAdded.GetAssemblyName();

            var solution = projectAdded.DTE.Solution;

            Microsoft.Build.Evaluation.Project buildProjAdded =
                ProjectCollection.GlobalProjectCollection.GetLoadedProjects(projectAdded.FullName).First();

            var changes = new List <ProjectReferenceToAdd>();

            foreach (Project project in solution.Projects)
            {
                if (project.UniqueName == projectAdded.UniqueName)
                {
                    continue;
                }

                VSProject vsProject = project.Object as VSProject;

                if (vsProject == null)
                {
                    continue;
                }

                foreach (Reference reference in vsProject.References)
                {
                    //If this project already has a project reference to the project being added then we don't need to do anything.
                    if (reference.SourceProject != null && projectAdded == reference.SourceProject)
                    {
                        break;
                    }

                    if (Path.GetFileNameWithoutExtension(reference.Path).Equals(assemblyName))
                    {
                        changes.Add(new ProjectReferenceToAdd
                        {
                            Reference          = reference,
                            Project            = vsProject,
                            ProjectToReference = projectAdded
                        }
                                    );
                    }
                }
            }
            return(changes);
        }
        public static bool IsFileInProjectFile(string projectFilePath, string fileRelativePath)
        {
            try
            {
                Microsoft.Build.Evaluation.Project p = new Microsoft.Build.Evaluation.Project(projectFilePath, null, null, new ProjectCollection());

                foreach (Microsoft.Build.Evaluation.ProjectItem projectItem in p.Items)
                {
                    if (projectItem.EvaluatedInclude == fileRelativePath)
                    {
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.LogException(Logger, Resource.Error_UnableToReadProjectFile, ex);
            }

            return(false);
        }
Example #5
0
        private void ModifyProject(string projectFilePath, SolutionDataViewModel solutionData)
        {
            if (!string.Equals(Path.GetExtension(projectFilePath), ".csproj"))
            {
                return;
            }

            var buildProject = new Microsoft.Build.Evaluation.Project(projectFilePath);

            if (solutionData.MustRemoveConditions)
            {
                RemoveCondition(buildProject, solutionData);
            }

            if (!solutionData.CreateSamples && Path.GetFileNameWithoutExtension(projectFilePath).EndsWith("Shell"))
            {
                RemoveSamples(buildProject, solutionData);
            }

            ProjectCollection.GlobalProjectCollection.UnloadProject(buildProject);
            //System.Threading.Thread.Sleep(500);
        }
 private ProjectProperty GetLinkedProperty(Microsoft.Build.Evaluation.Project p)
 {
     return(p.GetProperty(LinkedPropertyName));
 }
Example #7
0
 private ProjectProperty GetDisableFastUpToDateCheck(Microsoft.Build.Evaluation.Project p)
 {
     return(p.GetProperty(DisableFastUpToDateCheckPropertyName));
 }
        private void NavigateProjectItems(ProjectItems projectItems, Microsoft.Build.Evaluation.Project buildProject, ISet <string> projectPhysicalFiles, ISet <string> projectLogicalFiles, ISet <string> processedPhysicalDirectories, IDictionary <string, string> physicalFileProjectMap)
        {
            if (projectItems == null)
            {
                return;
            }

            var projectDirectory = buildProject.DirectoryPath + Path.DirectorySeparatorChar;
            var projectFilename  = buildProject.FullPath;

            var errorCategory = Options.MessageLevel;

            foreach (ProjectItem item in projectItems)
            {
                NavigateProjectItems(item.ProjectItems, buildProject, projectPhysicalFiles, projectLogicalFiles, processedPhysicalDirectories, physicalFileProjectMap);

                if (item.Kind != "{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}") // VSConstants.GUID_ItemType_PhysicalFile
                {
                    continue;
                }

                string itemName = item.Name;
                Debug.WriteLine("\t" + itemName + item.Kind);

                for (short i = 0; i < item.FileCount; i++)
                {
                    var filePath = item.FileNames[i];

                    // Skip if this is a linked file
                    if (!filePath.StartsWith(projectDirectory, StringComparison.InvariantCultureIgnoreCase))
                    {
                        continue;
                    }

                    projectLogicalFiles.Add(filePath);

                    if (!File.Exists(filePath))
                    {
                        var relativePath = filePath.Replace(buildProject.DirectoryPath + @"\", "");

                        var found = buildProject.Items.Any(x => x.EvaluatedInclude == relativePath);

                        if (!found)
                        {
                            Debug.WriteLine("\t\tFile excluded due to Condition evaluation");
                            return;
                        }

                        IVsHierarchy hierarchyItem;
                        _solution.GetProjectOfUniqueName(projectFilename, out hierarchyItem);

                        var newError = new MissingErrorTask()
                        {
                            ErrorCategory = errorCategory,
                            Category      = TaskCategory.BuildCompile,
                            Text          = "File referenced in project does not exist",
                            Code          = "MI0001",
                            Document      = filePath,
                            HierarchyItem = hierarchyItem,
                            ProjectPath   = projectFilename
                        };

                        newError.Navigate += NewErrorOnNavigate;
                        Debug.WriteLine("\t\t** Missing");

                        _errorListProvider.Tasks.Add(newError);
                    }

                    string directoryName = Path.GetDirectoryName(filePath);

                    // If we haven't seen this directory before, find the files inside it
                    if (processedPhysicalDirectories.Add(directoryName))
                    {
                        AddGitIgnoreFromDirectory(directoryName);

                        var physicalFiles =
                            new DirectoryInfo(directoryName).GetFiles()
                            .Where(
                                f => f.Attributes != FileAttributes.Hidden && f.Attributes != FileAttributes.System)
                            .Where(f => !f.Name.EndsWith(".user", StringComparison.InvariantCultureIgnoreCase) &&
                                   !f.Name.EndsWith("proj", StringComparison.InvariantCultureIgnoreCase))
                            .Select(f => f.FullName)
                            .ToList();

                        foreach (var physicalFile in physicalFiles)
                        {
                            projectPhysicalFiles.Add(physicalFile);
                            physicalFileProjectMap.Add(physicalFile, projectFilename);
                        }
                    }
                }
            }
        }
Example #9
0
        private void AddReference(Project projectNeedingReference, Project projectRemoving, string filePath)
        {
            var vsProject = ((VSProject)projectNeedingReference.Object);

            filePath = ToAbsolute(filePath, vsProject.Project.FullName);

            foreach (Reference reference in vsProject.References)
            {
                if (reference.SourceProject == null)
                {
                    continue;
                }

                if (reference.SourceProject.FullName == projectRemoving.FullName)
                {
                    reference.Remove();
                    break;
                }
            }

            VSLangProj.Reference newRef = vsProject.References.Add(filePath);

            if (!newRef.Path.Equals(filePath, StringComparison.OrdinalIgnoreCase))
            {
                Microsoft.Build.Evaluation.Project     msBuildProj = Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection.GetLoadedProjects(projectNeedingReference.FullName).First();
                Microsoft.Build.Evaluation.ProjectItem msBuildRef  = null;

                AssemblyName newFileAssemblyName = AssemblyName.GetAssemblyName(filePath);
                foreach (var item in msBuildProj.GetItems("Reference"))
                {
                    AssemblyName refAssemblyName = null;
                    try
                    {
                        refAssemblyName = new AssemblyName(item.EvaluatedInclude);
                    }
                    catch { }

                    if (refAssemblyName != null)
                    {
                        var refToken = refAssemblyName.GetPublicKeyToken() ?? new byte[0];
                        var newToken = newFileAssemblyName.GetPublicKeyToken() ?? new byte[0];

                        if
                        (
                            refAssemblyName.Name.Equals(newFileAssemblyName.Name, StringComparison.OrdinalIgnoreCase) &&
                            ((refAssemblyName.Version != null && refAssemblyName.Version.Equals(newFileAssemblyName.Version)) ||
                             (refAssemblyName.Version == null && newFileAssemblyName.Version == null)) &&
                            (refAssemblyName.CultureInfo != null && (refAssemblyName.CultureInfo.Equals(newFileAssemblyName.CultureInfo)) ||
                             (refAssemblyName.CultureInfo == null && newFileAssemblyName.CultureInfo == null)) &&
                            ((Enumerable.SequenceEqual(refToken, newToken)))
                        )
                        {
                            msBuildRef = item;
                            break;
                        }
                    }
                }

                if (msBuildRef != null)
                {
                    _ = msBuildRef.SetMetadataValue("HintPath", ToRelative(filePath, projectNeedingReference.FullName));
                }
            }
        }