Ejemplo n.º 1
0
        private void BuildFromNewCsproj()
        {
            var propGroup = CsprojDoc.Root
                            .FindAll("PropertyGroup")
                            .First(Ext.ContainsProjectInfo);

            //build info
            typeof(ProjectInfo)
            .GetProperties()
            .Select(prop => new { Prop = prop, Element = propGroup.Find(prop.Name) })
            .Where(map => map.Element != null)
            .ForAll(map => map.Prop.SetValue(Info, map.Element.Value));
            Info.AssemblyName = CsprojFile.Name.TrimEnd(CsprojFile.Extension);

            //build dependencies
            //1. assembly references
            CsprojDoc.Root
            .FindAll("ItemGroup/Reference")
            .ForAll(element => Assemblies.Add(new AssemblyDependencyRef
            {
                ProjectName = element.Attribute("Include").Value
            }));

            //2. external packages
            CsprojDoc.Root
            .FindAll("ItemGroup/PackageReference")
            .Where(IsExternalPackageReference)
            .ForAll(element => ExternalPackages.Add(new PackageDependencyRef
            {
                ProjectName = element.Attribute("Include").Value,
                Version     = SemVerRange.Parse(element.Attribute("Version").Value)
            }));

            //3. horizontal project references
            CsprojDoc.Root
            .FindAll("ItemGroup/ProjectReference")
            .Where(IsHorizontalProjectReference)
            .ForAll(element =>
            {
                var path        = element.Attribute("Include").Value;
                var projectName = path.ExtractProjectNameFromFilePath();
                var versionArg  = ResolveHorizontalVersionArg(new FileInfo(path));
                Horizontals.Add(new ObgDependencyRef
                {
                    ProjectPath = path,
                    ProjectName = projectName,
                    Version     = SemVerRange.Parse(propGroup.Find(versionArg).Value)
                });
            });

            //4. vertical project references
            CsprojDoc.Root
            .FindAll("ItemGroup/ProjectReference")
            .Where(IsVerticalProjectReference)
            .ForAll(element =>
            {
                var path        = element.Attribute("Include").Value;
                var projectName = path.ExtractProjectNameFromFilePath();
                Verticals.Add(new ObgDependencyRef
                {
                    ProjectPath = path,
                    ProjectName = projectName
                });
            });

            //5. plain project reference
            CsprojDoc.Root
            .FindAll("ItemGroup/ProjectReference")
            .Where(IsPlainProjectReference)
            .ForAll(element =>
            {
                var path        = element.Attribute("Include").Value;
                var projectName = path.ExtractProjectNameFromFilePath();
                PlainProjectRefs.Add(new PlainProjectRef
                {
                    ProjectPath = path,
                    ProjectName = projectName
                });
            });
        }
Ejemplo n.º 2
0
        private void BuildFromOldCsproj()
        {
            var nuspecFile = new FileInfo(Ext.ResolveNuspecPath(CsprojFile));
            var nuspecDoc  = nuspecFile.Exists
                ? nuspecFile.OpenRead().Using(XDocument.Load)
                : null;

            #region Generate project Info
            Info.AssemblyName =
                nuspecDoc?.Root.Find("metadata/id").Value
                ?? CsprojDoc.Root.Find("PropertyGroup/AssemblyName").Value;

            var textInfo = new CultureInfo("en-US", false).TextInfo;
            Info.Product = textInfo.ToTitleCase(Info.AssemblyName);

            Info.Authors = "OBG Api";

            Info.Company =
                nuspecDoc?.Root.Find("metadata/owners")?.Value
                ?? "Betsson Group";

            Info.Copyright = $"Copyright © {DateTimeOffset.Now.Year}";

            Info.Description = nuspecDoc?.Root.Find("metadata/description")?.Value;

            Info.PackageReleaseNotes = nuspecDoc?.Root.Find("metadata/releaseNotes")?.Value;

            Info.PackageTags = nuspecDoc?.Root.Find("metadata/tags")?.Value;

            Info.PackageProjectUrl = nuspecDoc?.Root.Find("metadata/projectUrl")?.Value;

            Info.PackageIconUrl = nuspecDoc?.Root.Find("metadata/iconUrl")?.Value;
            #endregion

            //dependencies
            //1. assembly references
            CsprojDoc.Root
            .FindAll("ItemGroup/Reference")
            .Where(IsGacReference)
            .ForAll(element => Assemblies.Add(new AssemblyDependencyRef
            {
                ProjectName = element.Attribute("Include").Value
            }));

            //2. external packages
            CsprojDoc.Root
            .FindAll("ItemGroup/Reference")
            .Where(IsExternalNuspecDependency)
            .ForAll(element =>
            {
                var assembly = element.Attribute("Include").Value
                               .Split(',')
                               .First();

                var dependency = nuspecDoc?.Root
                                 .FindAll(
                    "metadata/dependencies/dependency",
                    "metadata/dependencies/group/dependency")
                                 .FirstOrDefault(Ext.HasAttribute("id", assembly));

                if (dependency != null)
                {
                    var version = dependency.Attribute("version").Value;
                    ExternalPackages.Add(new PackageDependencyRef
                    {
                        ProjectName = assembly,
                        Version     = string.IsNullOrWhiteSpace(version) ? null : SemVerRange.Parse(version)
                    });
                }

                //else this is a transitive dependency; ignore it.
            });

            //3. horizontal project reference
            //check the project references and determine, using the solution name, which are, and which aren't
            //horizontal references. Then check the nuspec file to determine the version to use
            CsprojDoc.Root
            .FindAll("ItemGroup/ProjectReference")
            .Where(IsOldHorizontalProjectReference)
            .ForAll(element =>
            {
                var path       = element.Attribute("Include").Value;
                var assembly   = path.ExtractProjectNameFromFilePath();
                var dependency = nuspecDoc?.Root
                                 .FindAll(
                    "metadata/dependencies/group/dependency",
                    "metadata/dependencies/dependency")
                                 .FirstOrDefault(Ext.HasAttribute("id", assembly));

                if (dependency != null)
                {
                    var version = dependency
                                  .Attribute("version")
                                  .Value;

                    Horizontals.Add(new ObgDependencyRef
                    {
                        ProjectPath = path,
                        ProjectName = assembly,
                        Version     = string.IsNullOrWhiteSpace(version) ? null : SemVerRange.Parse(version)
                    });
                }

                //else it is a transitive dependency; ignore it.
                //{
                //    PlainProjectRefs.Add(new PlainProjectRef
                //    {
                //        ProjectName = assembly,
                //        ProjectPath = path
                //    });
                //}
            });

            //4. vertical project references
            //check the project references and determine, using the solution name, which are, and whicha aren't
            //vertical references.
            CsprojDoc.Root
            .FindAll("ItemGroup/ProjectReference")
            .Where(IsOldVerticalProjectReference)
            .ForAll(element =>
            {
                var path        = element.Attribute("Include").Value;
                var projectName = path.ExtractProjectNameFromFilePath();
                Verticals.Add(new ObgDependencyRef
                {
                    ProjectPath = path,
                    ProjectName = projectName
                });
            });
        }