Exemplo n.º 1
0
        public static (AssemblyLoadContext assemblyLoadContext, Assembly moduleAssembly) GetModuleAssembly(CommandEvaluationContext context, string moduleId)
        {
            var assys = GetModuleAssemblies(moduleId);

            var    versions = new Dictionary <VersionNumber, (AssemblyLoadContext alc, Assembly asy)>();
            string ver;

            foreach (var assy in assys)
            {
                try
                {
                    if (context.CommandLineProcessor.ModuleManager.IsModuleAssemblyLoaded(assy.FullName))
                    {
                        var assembly = context.CommandLineProcessor.ModuleManager.GetLoadedModuleAssembly(assy.FullName);
                        if (assembly.GetCustomAttribute <ShellModuleAttribute>() != null &&
                            (ver = assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>()?.InformationalVersion) != null)
                        {
                            versions.AddOrReplace(new VersionNumber(ver), (null, assembly));
                        }
                    }
                    else
                    {
                        var alc      = new AssemblyLoadContext($"module assembly load context for {assy.FullName}", true);
                        var assembly = alc.LoadFromAssemblyPath(assy.FullName);
                        if (assembly.GetCustomAttribute <ShellModuleAttribute>() != null &&
                            (ver = assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>()?.InformationalVersion) != null)
                        {
                            versions.AddOrReplace(new VersionNumber(ver), (alc, assembly));
                        }
                        alc.Unload();
                    }
                } catch (Exception notLoadableError) {
                    /* ignore error (maybe dll buggy due to bad dependencies -> not loadable == not handlable) */
                    context.Warningln($"a module assembly could not be loaded: '{assy.PrintableFullName}' due to error: '{notLoadableError.Message}'");
                }
            }

            // sort by version, select the most recent
            var vlist = versions.Keys.ToList();

            vlist.Sort();
            var sver = versions[vlist.Last()];

            return(sver);
        }
        public CommandResult <string> NugetPush(
            CommandEvaluationContext context,
            [Parameter(0, "package (.nuget) file path")] FilePath pkgFile,
            [Parameter(1, "target server api key")] string apiKey,
            [Option("u", "push-url", "nuget server api push service url", true, true)] string url = PushUrl,
            [Option("p", "protocol-version", "nuget thir party client protocol version", true, true)] string protocolVersion = ProtocolVersion
            )
        {
            var @return = "";

            if (pkgFile.CheckExists(context))
            {
                var ext   = Path.GetExtension(pkgFile.FullName);
                var atExt = ".nupkg";
                if (ext.ToLower() != atExt)
                {
                    context.Errorln($"bad file extension: '{ext}', should be '{atExt}'");
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(apiKey))
                    {
                        context.Errorln($"api key is required and can't be empty");
                    }
                    else
                    {
                        using var httpClient = new HttpClient();
                        using var request    = new HttpRequestMessage(new HttpMethod("PUT"), url);

                        request.Headers.Add("X-NuGet-ApiKey", apiKey);
                        request.Headers.AcceptEncoding.ParseAdd("gzip,deflate");
                        request.Headers.Add("X-NuGet-Protocol-Version", protocolVersion);
                        //request.Headers.Add("X-NuGet-Client-Version", "5.8.1");
                        //request.Headers.Add("user-agent", "NuGet Command Line / 5.8.1 (Microsoft Windows NT 10.0.19042.0)");

                        var content = new MultipartFormDataContent();

                        var fileContent = new ByteArrayContent(File.ReadAllBytes(pkgFile.FullName));

                        fileContent.Headers.ContentDisposition =
                            new ContentDispositionHeaderValue(
                                "form-data"
                                )
                        {
                            FileName = "package.nupkg"
                        };

                        fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

                        content.Add(fileContent);
                        request.Content = content;


                        context.Out.Echo(context.ShellEnv.Colors.Log + $"PUT {PushUrl} ... ");

                        var tsk = httpClient.SendAsync(request);

                        var result = tsk.Result;
                        if (result.IsSuccessStatusCode)
                        {
                            var res = result.Content.ReadAsStringAsync().Result;

                            context.Out.Echoln(" Done(rdc)");
                            context.Out.Echo(ANSI.RSTXTA + ANSI.CPL(1) + ANSI.EL(ANSI.ELParameter.p2));     // TODO: add as ANSI combo

                            @return = res;

                            if (res != null)
                            {
                                if (!string.IsNullOrWhiteSpace(res))
                                {
                                    context.Out.Echoln(res);
                                }
                            }
                            else
                            {
                                context.Warningln("quey result is empty");
                            }
                            context.Warningln("quey result is empty");

                            if (result.StatusCode == HttpStatusCode.Created ||
                                result.StatusCode == HttpStatusCode.Accepted)
                            {
                                context.Out.Echoln($"package '{Path.GetFileName(pkgFile.FullName)}' has been successfully pushed");
                            }
                        }
                        else
                        {
                            context.Errorln($"can't get response content: {(int)result.StatusCode} {result.ReasonPhrase}");
                        }
                    }
                }
            }
            return(new CommandResult <string>(@return));
        }