Beispiel #1
0
        private static Command r(PackageRestoreContext restoreContext)
        {
            var rDirective = new Command("#r")
            {
                new Argument <PackageReferenceOrFileInfo>(
                    result =>
                {
                    var token = result.Tokens
                                .Select(t => t.Value)
                                .SingleOrDefault();

                    if (PackageReference.TryParse(token, out var reference))
                    {
                        return(reference);
                    }

                    if (token != null &&
                        !token.StartsWith("nuget:") &&
                        !Path.EndsInDirectorySeparator(token))
                    {
                        return(new FileInfo(token));
                    }

                    result.ErrorMessage = $"Unable to parse package reference: \"{token}\"";

                    return(null);
                })
                {
                    Name = "package"
                }
            };

            rDirective.Handler = CommandHandler.Create <PackageReferenceOrFileInfo, KernelInvocationContext>(HandleAddPackageReference);

            return(rDirective);

            Task HandleAddPackageReference(
                PackageReferenceOrFileInfo package,
                KernelInvocationContext context)
            {
                if (package?.Value is PackageReference pkg)
                {
                    var alreadyGotten = restoreContext.ResolvedPackageReferences
                                        .Concat(restoreContext.RequestedPackageReferences)
                                        .FirstOrDefault(r => r.PackageName.Equals(pkg.PackageName, StringComparison.OrdinalIgnoreCase));

                    if (alreadyGotten is { } && !string.IsNullOrWhiteSpace(pkg.PackageVersion) && pkg.PackageVersion != alreadyGotten.PackageVersion)
                    {
                        var errorMessage = GenerateErrorMessage(pkg, alreadyGotten).ToString(OutputMode.NonAnsi);
                        context.Publish(new ErrorProduced(errorMessage));
                    }
        public static bool TryParse(string value, out PackageReference reference)
        {
            var result = _regex.Match(value);

            if (!result.Success)
            {
                reference = null;
                return(false);
            }

            var packageName    = result.Groups["packageName"].Value;
            var packageVersion = result.Groups["packageVersion"].Value;
            var restoreSources = result.Groups["restoreSources"].Value;

            reference = new PackageReference(packageName, packageVersion, restoreSources);

            return(true);
        }
        public AddNugetResult(
            bool succeeded,
            PackageReference requestedPackage,
            IReadOnlyCollection <string> errors = null)
        {
            if (requestedPackage == null)
            {
                throw new ArgumentNullException(nameof(requestedPackage));
            }

            Succeeded = succeeded;

            if (!succeeded &&
                errors?.Count == 0)
            {
                throw new ArgumentException("Must provide errors when succeeded is false.");                //TBD: Localize
            }

            Errors = errors ?? Array.Empty <string>();
        }
        public PackageReference GetOrAddPackageReference(
            string packageName,
            string packageVersion = null)
        {
            // Package names are case insensitive.
            var key = packageName.ToLower(CultureInfo.InvariantCulture);

            if (_resolvedPackageReferences.TryGetValue(key, out var resolvedPackage))
            {
                if (string.IsNullOrWhiteSpace(packageVersion) ||
                    packageVersion == "*" ||
                    string.Equals(resolvedPackage.PackageVersion.Trim(), packageVersion.Trim(), StringComparison.OrdinalIgnoreCase))
                {
                    return(resolvedPackage);
                }
                else
                {
                    // It was previously resolved at a different version than the one requested
                    return(null);
                }
            }

            // we use a lock because we are going to be looking up and inserting
            if (_requestedPackageReferences.TryGetValue(key, out PackageReference existingPackage))
            {
                if (string.Equals(existingPackage.PackageVersion.Trim(), packageVersion.Trim(), StringComparison.OrdinalIgnoreCase))
                {
                    return(existingPackage);
                }
                else
                {
                    return(null);
                }
            }

            // Verify version numbers match note: wildcards/previews are considered distinct
            var newPackageRef = new PackageReference(packageName, packageVersion);

            _requestedPackageReferences.TryAdd(key, newPackageRef);
            return(newPackageRef);
        }
 private bool TryGetPackageAndVersionFromPackageRoot(DirectoryInfo packageRoot, out PackageReference packageReference)
 {
     try
     {
         // packageRoot looks similar to:
         //    C:/Users/kevinr/.nuget/packages/fsharp.data/3.3.3/
         //    3.3.3 is the package version
         // fsharp.data is the package name
         var packageName    = packageRoot.Parent.Name;
         var packageVersion = packageRoot.Name;
         if (_requestedPackageReferences.TryGetValue(packageName.ToLower(CultureInfo.InvariantCulture), out var requested))
         {
             packageName = requested.PackageName;
         }
         packageReference = new PackageReference(packageName, packageVersion);
         return(true);
     }
     catch (Exception)
     {
         packageReference = default(PackageReference);
         return(false);
     }
 }
Beispiel #6
0
        public static bool TryParse(string value, out PackageReference reference)
        {
            value = value.Trim();

            if (!value.StartsWith("nuget:"))
            {
                reference = null;
                return(false);
            }

            var parts = value.Split(",", 2)
                        .Select(v => v.Trim())
                        .ToArray();

            if (parts.Length == 0)
            {
                reference = null;
                return(false);
            }

            var packageName = parts[0].Substring(6).Trim();

            if (string.IsNullOrWhiteSpace(packageName))
            {
                reference = null;
                return(false);
            }

            var packageVersion = parts.Length > 1
                                     ? parts[1]
                                     : null;

            reference = new PackageReference(packageName, packageVersion);

            return(true);
        }
Beispiel #7
0
 public PackageReferenceOrFileInfo(PackageReference packageReference)
 {
     PackageReference = packageReference;
     _case            = 2;
 }