Ejemplo n.º 1
0
 public static IEnumerable <ProjectItem> ShouldContainItem(this Project project, string itemType, string because = "", params object[] becauseArgs)
 {
     return(project.GetItemsIgnoringCondition(itemType)
            .Should()
            .NotBeNull(because, becauseArgs)
            .And
            .Subject
            .As <IEnumerable <ProjectItem> >());
 }
Ejemplo n.º 2
0
        private ClassifiedReferences ClassifyReferences(
            Microsoft.Build.Evaluation.Project project,
            IReadOnlyCollection <INuGetPackage> nuGetPackages,
            ISolution solution)
        {
            var projectReferences = project.GetItemsIgnoringCondition("ProjectReference").Select(r => new ProjectReference(solution, r));

            var dllReferences =
                project.GetItemsIgnoringCondition("Reference")
                .Select(r => new ReferenceItem(r.EvaluatedInclude, r.Metadata.ToDictionary(m => m.Name, m => m.EvaluatedValue)));

            var gacReferences   = new List <GacReference>();
            var fileReferences  = new List <FileReference>();
            var nuGetReferences = new List <NuGetReference>();

            foreach (var reference in dllReferences)
            {
                if (reference.HintPath == null)
                {
                    gacReferences.Add(new GacReference(reference.AssemblyName));
                    continue;
                }

                var matchingNuGetPackage =
                    nuGetPackages.SingleOrDefault(p => reference.HintPath.StartsWith($@"..\packages\{p.PackageDirectoryName}", StringComparison.Ordinal));

                if (matchingNuGetPackage != null)
                {
                    nuGetReferences.Add(
                        new NuGetReference(
                            matchingNuGetPackage,
                            reference.AssemblyName,
                            reference.Metadata.GetValueOrDefault("Private") == "True",
                            reference.HintPath,
                            project.DirectoryPath));
                }
                else
                {
                    fileReferences.Add(new FileReference(reference.AssemblyName, reference.HintPath, project.DirectoryPath));
                }
            }

            return(new ClassifiedReferences(gacReferences, fileReferences, nuGetReferences, projectReferences));
        }
Ejemplo n.º 3
0
        private IEnumerable<ProcessPath> ProcessProjectFile(ProcessPath process, ScanResult result)
        {
            List<ProcessPath> newFiles = new List<ProcessPath>();

            // If this project is not processed already, read through it all.
            ScannedProject scannedProject;
            string key = ScannedProject.CalculateKey(process.Path, process.Properties);
            if (!result.ProjectFiles.TryGetValue(key, out scannedProject))
            {
                Project project = new Project(process.Path, process.Properties, null);
                string projectFolder = Path.GetDirectoryName(project.FullPath);
                string type = project.GetPropertyValue("OutputType");

                scannedProject = new ScannedProject(type, project.FullPath, process.Properties, null);
                ICollection<ProjectItem> projectReferences = project.GetItemsIgnoringCondition("ProjectReference");
                if (this.RecurseProjects && projectReferences != null)
                {
                    foreach (ProjectItem projectReference in projectReferences)
                    {
                        // TODO: process Property metadata.
                        string include = Path.Combine(projectFolder, projectReference.EvaluatedInclude);
                        newFiles.Add(new ProcessPath(include) { Project = scannedProject });
                    }
                }

                ICollection<ProjectItem> compiles = project.GetItemsIgnoringCondition("Compile");
                if (compiles != null)
                {
                    foreach (ProjectItem item in compiles)
                    {
                        // TODO: process DefineConstants property.
                        string include = Path.Combine(projectFolder, item.EvaluatedInclude);
                        newFiles.Add(new ProcessPath(include) { Project = scannedProject });
                    }
                }

                Debug.Assert(key == scannedProject.Key, String.Format("{0} should equal {1}", key, scannedProject.Key));
                result.ProjectFiles.Add(scannedProject.Key, scannedProject);
            }

            // If there is a parent project, create a reference between the two projects.
            if (process.Project != null)
            {
                process.Project.TargetProjects.Add(scannedProject);
                scannedProject.SourceProjects.Add(process.Project);
                //result.ProjectToProjectReferences.Add(new ScannedProjectProjectReference() { SourceProject = process.Project, TargetProject = scannedProject });
            }

            return newFiles;
        }
Ejemplo n.º 4
0
        public void RemoveItem_IncludingFromIgnoringConditionList()
        {
            XmlReader content = XmlReader.Create(new StringReader(ObjectModelHelpers.CleanupFileContents(
@"<Project ToolsVersion=""msbuilddefaulttoolsversion"" xmlns=""msbuildnamespace"">
  <ItemGroup Condition=""false"">
    <i Include=""i1"" />
  </ItemGroup>
</Project>")));

            Project project = new Project(content);

            Assert.Equal(0, Helpers.MakeList(project.GetItems("i")).Count);
            List<ProjectItem> itemsIgnoringCondition = Helpers.MakeList(project.GetItemsIgnoringCondition("i"));
            Assert.Equal(1, itemsIgnoringCondition.Count);
            ProjectItem item = itemsIgnoringCondition[0];
            Assert.Equal("i1", item.EvaluatedInclude);

            bool result = project.RemoveItem(item);

            Assert.Equal(false, result); // false as it was not in the regular items collection
            itemsIgnoringCondition = Helpers.MakeList(project.GetItemsIgnoringCondition("i"));
            Assert.Equal(0, itemsIgnoringCondition.Count);
        }
Ejemplo n.º 5
0
        public void ChangeItemType()
        {
            Project project = new Project();
            project.AddItem("i", "i1");

            project.ReevaluateIfNecessary();

            ProjectItem item = Helpers.GetFirst(project.GetItems("i"));
            item.ItemType = "j";

            Assert.Equal("j", item.ItemType);

            string expected = ObjectModelHelpers.CleanupFileContents(
@"<Project ToolsVersion=""msbuilddefaulttoolsversion"" xmlns=""msbuildnamespace"">
  <ItemGroup>
    <j Include=""i1"" />
  </ItemGroup>
</Project>");

            Helpers.VerifyAssertProjectContent(expected, project.Xml);

            ProjectItemGroupElement itemGroupElement = Helpers.GetFirst(project.Xml.ItemGroups);
            Assert.Equal(1, Helpers.MakeList(itemGroupElement.Items).Count);
            Assert.Equal(true, Object.ReferenceEquals(itemGroupElement, item.Xml.Parent));

            Assert.Equal(1, Helpers.MakeList(project.Items).Count);
            Assert.Equal(1, Helpers.MakeList(project.ItemsIgnoringCondition).Count);

            Assert.Equal(0, Helpers.MakeList(project.GetItems("i")).Count);
            Assert.Equal(0, Helpers.MakeList(project.GetItemsIgnoringCondition("i")).Count);

            Assert.Equal(true, Object.ReferenceEquals(item, Helpers.GetFirst(project.GetItems("j"))));
            Assert.Equal(true, Object.ReferenceEquals(item, Helpers.GetFirst(project.GetItemsIgnoringCondition("j"))));
            Assert.Equal(true, Object.ReferenceEquals(item, Helpers.GetFirst(project.GetItemsByEvaluatedInclude("i1"))));
        }
Ejemplo n.º 6
0
 public static ICollection <ProjectItem> GetProjectReferences([NotNull] this Microsoft.Build.Evaluation.Project project) =>
 project.GetItemsIgnoringCondition(_projectReferenceItemType);
        public DependencyResult Process()
        {
            try
            {
                var tree = new NugetTreeResolver(NugetSearchService);

                Microsoft.Build.Evaluation.Project proj = new Microsoft.Build.Evaluation.Project(ProjectPath);

                List <NugetDependency> deps = new List <NugetDependency>();
                foreach (ProjectItem reference in proj.GetItemsIgnoringCondition("PackageReference"))
                {
                    var versionMetaData = reference.Metadata.Where(meta => meta.Name == "Version").FirstOrDefault().EvaluatedValue;
                    NuGet.Versioning.VersionRange version;
                    if (NuGet.Versioning.VersionRange.TryParse(versionMetaData, out version))
                    {
                        var dep = new NugetDependency(reference.EvaluatedInclude, version);
                        deps.Add(dep);
                    }
                    else
                    {
                        Console.WriteLine("Framework dependency had no version, will not be included: " + reference.EvaluatedInclude);
                    }
                }

                foreach (ProjectItem reference in proj.GetItemsIgnoringCondition("Reference"))
                {
                    if (reference.Xml != null && !String.IsNullOrWhiteSpace(reference.Xml.Include) && reference.Xml.Include.Contains("Version="))
                    {
                        string packageInfo = reference.Xml.Include;

                        var artifact = packageInfo.Substring(0, packageInfo.IndexOf(","));

                        string versionKey                 = "Version=";
                        int    versionKeyIndex            = packageInfo.IndexOf(versionKey);
                        int    versionStartIndex          = versionKeyIndex + versionKey.Length;
                        string packageInfoAfterVersionKey = packageInfo.Substring(versionStartIndex);

                        string seapirater = ",";
                        string version;
                        if (packageInfoAfterVersionKey.Contains(seapirater))
                        {
                            int firstSeapirater = packageInfoAfterVersionKey.IndexOf(seapirater);
                            version = packageInfoAfterVersionKey.Substring(0, firstSeapirater);
                        }
                        else
                        {
                            version = packageInfoAfterVersionKey;
                        }

                        var dep = new NugetDependency(artifact, NuGet.Versioning.VersionRange.Parse(version));
                        deps.Add(dep);
                    }
                }
                ProjectCollection.GlobalProjectCollection.UnloadProject(proj);

                foreach (var dep in deps)
                {
                    tree.Add(dep);
                }

                var result = new DependencyResult()
                {
                    Success      = true,
                    Packages     = tree.GetPackageList(),
                    Dependencies = new List <PackageId>()
                };

                foreach (var package in result.Packages)
                {
                    var anyPackageReferences = result.Packages.Where(pkg => pkg.Dependencies.Contains(package.PackageId)).Any();
                    if (!anyPackageReferences)
                    {
                        result.Dependencies.Add(package.PackageId);
                    }
                }

                return(result);
            }
            catch (InvalidProjectFileException e)
            {
                return(new DependencyResult()
                {
                    Success = false
                });
            }
        }