Пример #1
0
        internal static BuildErrorEventArgs AsNuGetMSBuildError(
            this String errorCode,
            String projectFilePath,
            InitializationArgs args,
            Exception exc = null
            )
        {
            String errorMessage;

            switch (errorCode)
            {
            case "NMSBT001":
                errorMessage = $"Exception in initialization: {exc}";
                break;

            case "NMSBT002":
                errorMessage = $"Failed to find main package, check that you have suitable \"{NuGetExecutionTaskFactory.PACKAGE_ID}\" or \"{NuGetExecutionTaskFactory.PACKAGE_ID_IS_SELF}\" element in task body.";
                break;

            case "NMSBT003":
                errorMessage = $"The \"{NuGetExecutionTaskFactory.PACKAGE_ID_IS_SELF}\" element is not supported when the caller file of this task factory is not known.";
                break;

            case "NMSBT004":
                errorMessage = $"Failed to deduce self package ID from file {projectFilePath}.";
                break;

            case "NMSBT005":
                errorMessage = $"The parameters \"{NuGetExecutionTaskFactory.PACKAGE_ID}\" and \"{NuGetExecutionTaskFactory.PACKAGE_ID_IS_SELF}\" are mutually exclusive, please specify exactly one of them.";
                break;

            case "NMSBT006":
                errorMessage = $"Failed to find any package with ID {args.PackageID} which would have {projectFilePath} stored within it.";
                break;

            default:
                errorMessage = $"Unrecognized error code: {errorCode}.";
                break;
            }

            return(new BuildErrorEventArgs(
                       "Task factory",
                       errorCode,
                       null,
                       -1,
                       -1,
                       -1,
                       -1,
                       errorMessage,
                       null,
                       NuGetExecutionTaskFactory.FACTORY_NAME
                       ));
        }
Пример #2
0
 internal TaskProxy(
     NuGetUtilsExecProcessMonitor processMonitor,
     InitializationArgs initializationArgs,
     EnvironmentValue environment,
     InspectionValue entrypoint,
     MethodInspectionInfo entrypointMethod,
     TypeGenerationResult generationResult
     )
 {
     this._processMonitor     = ArgumentValidator.ValidateNotNull(nameof(processMonitor), processMonitor);
     this._initializationArgs = ArgumentValidator.ValidateNotNull(nameof(initializationArgs), initializationArgs);
     this._environment        = ArgumentValidator.ValidateNotNull(nameof(environment), environment);
     this._entrypoint         = ArgumentValidator.ValidateNotNull(nameof(entrypoint), entrypoint);
     this._entrypointMethod   = ArgumentValidator.ValidateNotNull(nameof(entrypointMethod), entrypointMethod);
     this._propertyInfos      = generationResult
                                .Properties
                                .Select((p, idx) => (p, idx))
                                .ToImmutableDictionary(t => t.p.Name, t => new TaskPropertyHolder(generationResult.PropertyTypeNames[t.idx], t.p.Output, !Equals(t.p.PropertyType, typeof(String))));
     this._cancellationTokenSource = new CancellationTokenSource();
 }
Пример #3
0
        private static async Task <InitializationResult> InitializeAsync(
            InitializationArgs args
            )
        {
            using (var cancellationTokenSource = new CancellationTokenSource())
            {
                var token = cancellationTokenSource.Token;
                void OnCancel(Object sender, ConsoleCancelEventArgs e)
                {
                    cancellationTokenSource.Cancel();
                }

                Console.CancelKeyPress += OnCancel;

                using (var usingHelper = new UsingHelper(() => Console.CancelKeyPress -= OnCancel))
                {
                    var be = args.BuildEngine;
                    var projectFilePath = be?.ProjectFileOfTaskNode;
                    var env             = await _cache.DetectEnvironmentAsync(new EnvironmentKeyInfo(
                                                                                  new EnvironmentKey(
                                                                                      args.Framework,
                                                                                      args.RuntimeID,
                                                                                      args.SDKPackageID,
                                                                                      args.SDKPackageVersion,
                                                                                      args.SettingsLocation,
                                                                                      args.PackageIDIsSelf ? projectFilePath : args.PackageID,
                                                                                      args.PackageVersion
                                                                                      ),
                                                                                  args.PackageIDIsSelf,
                                                                                  projectFilePath
                                                                                  ),
                                                                              be,
                                                                              token);

                    InitializationResult initializationResult = null;
                    if (env.Errors.Length > 0)
                    {
                        if (be == null)
                        {
                            Console.Error.WriteLine("Errors in environment detection: " + String.Join(";", env.Errors));
                        }
                        else
                        {
                            foreach (var error in env.Errors.Select(errorCode => errorCode.AsNuGetMSBuildError(projectFilePath, args)))
                            {
                                be.LogErrorEvent(error);
                            }
                        }
                        initializationResult = null;
                    }
                    else
                    {
                        await be.LogMessageOrWriteToConsoleOut($"Detected current NuGet framework to be \"{env.ThisFramework}\", with RID \"{env.ThisRuntimeID}\".");

                        var inspection = await _cache.InspectPackageAsync(env, new InspectionKey(
                                                                              env.ThisFramework,
                                                                              args.SettingsLocation,
                                                                              env.PackageID,
                                                                              env.PackageVersion,
                                                                              args.AssemblyPath
                                                                              ),
                                                                          args.RestoreSDKPackage,
                                                                          be,
                                                                          token
                                                                          );

                        var epType   = args.TypeName;
                        var epMethod = args.MethodName;
                        if (epType.IsNullOrEmpty() && epMethod.IsNullOrEmpty())
                        {
                            // TODO load the assembly using System.Reflection.Metadata stuff and inspect it.
                            // OR just invoke the process which will use the existing stuff in NuGetUtils.Lib.Exec
                            throw new NotImplementedException("The scenario where both type and method names of entrypoint are not specified is not yet supported.");
                        }


                        Func <MethodInspectionInfo, Boolean> matcher;
                        if (epType.IsNullOrEmpty())
                        {
                            // Get the first method matching given method name
                            matcher = m => String.Equals(m.MethodName, epMethod);
                        }
                        else
                        {
                            if (epMethod.IsNullOrEmpty())
                            {
                                // Get the first method contained within given type
                                matcher = m => String.Equals(m.TypeName, epType);
                            }
                            else
                            {
                                // Get the first method matching given method name which is contained by type with given type name
                                matcher = m => String.Equals(m.MethodName, epMethod) && String.Equals(m.TypeName, epType);
                            }
                        }

                        var epInfo = inspection.SuitableMethods.FirstOrDefault(matcher);
                        if (epInfo == null)
                        {
                            throw new InvalidOperationException($"Could not find suitable method with the following information: entrypoint type {epType}, and entrypoing method {epMethod}.");
                        }

                        var typeGenResult = TaskTypeGenerator.Instance.GenerateTaskType(
                            true,
                            epInfo.InputParameters,
                            epInfo.OutputParameters
                            );

                        initializationResult = new InitializationResult(
                            typeGenResult,
                            () => (ITask)typeGenResult.GeneratedType.GetTypeInfo().DeclaredConstructors.First().Invoke(new[]
                        {
                            new TaskProxy(_cache.ProcessMonitor, args, env, inspection, epInfo, typeGenResult)
                        })
                            );
                    }

                    return(initializationResult);
                }
            }
        }