Exemplo n.º 1
0
        private Match GetElementMatch(ElementPath elementPath, PackageReference package)
        {
            Match match = null;

            if (elementPath.Element != null)
            {
                switch (elementPath.Element)
                {
                case ProjectItemElement itemElement:

                    if (itemElement.ItemType.Equals("Analyzer", StringComparison.OrdinalIgnoreCase))
                    {
                        match = RegularExpressions.Analyzers[package.PackageIdentity].Match(elementPath.FullPath);

                        if (match.Success)
                        {
                            package.AnalyzerItems.Add(itemElement);
                        }
                    }
                    else if (itemElement.ItemType.Equals("Reference", StringComparison.OrdinalIgnoreCase))
                    {
                        if (File.Exists(elementPath.FullPath))
                        {
                            match = RegularExpressions.AssemblyReferences[package.PackageIdentity].Match(elementPath.FullPath);

                            if (match.Success)
                            {
                                package.AssemblyReferences.Add(itemElement);
                            }
                        }
                    }

                    break;

                case ProjectImportElement importElement:

                    match = RegularExpressions.Imports[package.PackageIdentity].Match(elementPath.FullPath);

                    if (match.Success)
                    {
                        package.Imports.Add(importElement);
                    }

                    break;
                }
            }

            return(match);
        }
Exemplo n.º 2
0
        private void ProcessElement(ProjectElement element, List <PackageReference> packages)
        {
            switch (element)
            {
            case ProjectPropertyElement propertyElement:
                if (PropertiesToRemove.Contains(propertyElement.Name))
                {
                    Log.Debug($"    {element.Location}: Removing property \"{element.ElementName}\"");
                    propertyElement.Remove();
                    return;
                }

                break;

            case ProjectItemElement itemElement:
                if (ItemsToRemove.Contains(itemElement.ItemType) || ItemsToRemove.Contains(itemElement.Include))
                {
                    Log.Debug($"    {element.Location}: Removing item \"{itemElement.ItemType}\"");
                    itemElement.Remove();
                    return;
                }

                break;

            case ProjectTargetElement targetElement:
                if (TargetsToRemove.Contains(targetElement.Name))
                {
                    Log.Debug($"    {element.Location}: Removing target \"{targetElement.Name}\"");
                    targetElement.Remove();
                    return;
                }

                break;
            }

            ElementPath elementPath = new ElementPath(element);

            bool elementPathIsValid = false;

            foreach (PackageReference package in packages.Where(i => !i.IsMissingTransitiveDependency))
            {
                Match match = GetElementMatch(elementPath, package);

                if (match != null && match.Success && match.Groups["version"] != null && match.Groups["version"].Success && NuGetVersion.TryParse(match.Groups["version"].Value, out NuGetVersion version))
                {
                    elementPathIsValid = true;

                    if (!version.Equals(package.PackageIdentity.Version))
                    {
                        Log.Warn($"  {element.Location}: The package version \"{version}\" specified in the \"{element.ElementName}\" element does not match the package version \"{package.PackageVersion}\".  After conversion, the project will reference ONLY version \"{package.PackageVersion}\"");
                    }
                }
            }

            if (!elementPathIsValid && IsPathRootedInRepositoryPath(elementPath.FullPath))
            {
                PackageReference package = packages.FirstOrDefault(i => !string.IsNullOrEmpty(i.RepositoryInstalledPath) && elementPath.FullPath.StartsWith(i.RepositoryInstalledPath));

                if (package == null)
                {
                    // TODO: Using a package that isn't referenced
                }
                else
                {
                    string path = Path.Combine(package.GlobalInstalledPath, elementPath.FullPath.Substring(package.RepositoryInstalledPath.Length + 1));

                    if (!File.Exists(path) && !Directory.Exists(path))
                    {
                        // TODO: Using a path that doesn't exist?
                    }
                    else
                    {
                        var generatedProperty = $"$(Pkg{package.PackageId.Replace(".", "_").Replace("-", "_")})";
                        path = $"{generatedProperty}{path.Substring(_globalPackagesFolder.Length + package.PackageId.Length + package.PackageVersion.ToFullString().Length + 1 + 1)}";

                        package.GeneratePathProperty = true;
                        elementPath.Set(path);
                    }
                }
            }
        }