Пример #1
0
        private static Command r()
        {
            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 is not null &&
                        !token.StartsWith("nuget:") &&
                        !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 &&
                    context.HandlingKernel is ISupportNuget kernel)
                {
                    var alreadyGotten = kernel.ResolvedPackageReferences
                                        .Concat(kernel.RequestedPackageReferences)
                                        .FirstOrDefault(r => r.PackageName.Equals(pkg.PackageName, StringComparison.OrdinalIgnoreCase));

                    if (alreadyGotten is { } && !string.IsNullOrWhiteSpace(pkg.PackageVersion) && pkg.PackageVersion != alreadyGotten.PackageVersion)
                    {
                        if (!pkg.IsPackageVersionSpecified || pkg.PackageVersion is "*-*" or "*")
                        {
                            // we will reuse the the already loaded since this is a wildcard
                            var added = kernel.GetOrAddPackageReference(alreadyGotten.PackageName, alreadyGotten.PackageVersion);

                            if (added is null)
                            {
                                var errorMessage = GenerateErrorMessage(pkg).ToString(OutputMode.NonAnsi);
                                context.Fail(context.Command, message: errorMessage);
                            }
                        }
Пример #2
0
        private static Command r()
        {
            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:") &&
                        !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 kernel        = context.HandlingKernel as ISupportNuget;
                    var alreadyGotten = kernel.ResolvedPackageReferences
                                        .Concat(kernel.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));
                    }