private async Task <object> Status(JArray args)
            {
                var app  = (PahkatApp)Application.Current;
                var keys = new List <PackageKey>();

                foreach (var arg in args)
                {
                    var rawKey = arg.ToObject <string>();
                    if (rawKey != null)
                    {
                        try {
                            keys.Add(PackageKey.From(rawKey));
                        }
                        catch {
                        }
                    }
                }

                var map    = new JObject();
                var pahkat = app.PackageStore;

                foreach (var packageKey in keys)
                {
                    var status = await pahkat.Status(packageKey);

                    var obj = new JObject();
                    obj["status"] = (int)status;
                    obj["target"] = "system";
                    map[packageKey.ToString()] = obj;
                }

                return(map);
            }
        private Task <List <(PackageKey, Dictionary <string, string>)> > ResolvePackageActions(PahkatClient pahkat,
                                                                                               LanguageItem selectedLanguage)
        {
            return(Task.Run(async() => {
                var repos = await pahkat.GetRepoRecords();
                var mainRepo = repos.Filter((repo) =>
                {
                    return repo.Key.AbsoluteUri == "https://pahkat.uit.no/main/";
                });

                if (mainRepo == null)
                {
                    await pahkat.SetRepo(new Uri("https://pahkat.uit.no/main/"), new RepoRecord());
                }

                var result = await pahkat.ResolvePackageQuery(new PackageQuery()
                {
                    Tags = new[] { $"lang:{selectedLanguage.Tag}" },
                });
                Log.Debug(result);

                var obj = JObject.Parse(result);
                var descriptors = obj["descriptors"]?.ToObject <List <JObject> >() ?? new List <JObject>();
                var packageKeys = descriptors
                                  .FilterMap((o) => {
                    var key = o["key"]?.ToObject <string>();
                    var name = o["name"]?.ToObject <Dictionary <string, string> >();

                    if (key == null || name == null)
                    {
                        return null;
                    }

                    ValueTuple <string, Dictionary <string, string> >?tup = ValueTuple.Create(key, name);

                    return tup;
                })
                                  .Map(tup => (PackageKey.From(tup.Item1), tup.Item2))
                                  .ToList();
                return packageKeys;
            }));
        }
            private async Task <bool> Transaction(JArray args)
            {
                var actions = new List <PackageAction>();

                foreach (var arg in args)
                {
                    if (arg.Type != JTokenType.Object)
                    {
                        continue;
                    }

                    JObject obj       = (JObject)arg;
                    var     rawKey    = obj.Value <string>("key");
                    var     rawAction = obj.Value <string>("action");
                    // var target = obj.Value<string>("target");

                    try {
                        var key    = PackageKey.From(rawKey);
                        var action = InstallAction.Uninstall;
                        if (rawAction == "install")
                        {
                            action = InstallAction.Install;
                        }

                        actions.Add(new PackageAction(key, action));
                    }
                    catch {
                    }
                }

                var primaryButton = string.Format(Strings.InstallUninstallNPackages, actions.Count);

                // Resolve the names for the package keys
                var strings = actions.Map(x => {
                    var package = Repo.Packages.Packages[x.PackageKey.Id];
                    var release = Repo.Release(x.PackageKey);
                    if (release == null || package == null)
                    {
                        return(null);
                    }

                    return($"{x.Action.NativeName()}: {package.NativeName()} {release.Version}");
                });

                return(await await PahkatApp.Current.Dispatcher.InvokeAsync(async() => {
                    var dialog = new ConfirmationDialog(
                        "Confirm Selection",
                        "Do you wish to do the following actions:",
                        string.Join("\n", strings),
                        primaryButton);

                    try {
                        WebView.Visibility = Visibility.Hidden;
                        var result = await dialog.ShowAsync();
                        WebView.Visibility = Visibility.Visible;

                        if (result == ContentDialogResult.Primary)
                        {
                            var app = (PahkatApp)Application.Current;
                            await app.StartTransaction(actions.ToArray());
                            return true;
                        }
                    }
                    catch (Exception e) {
                        Log.Debug(e, "wat");
                    }

                    return false;
                }));
            }
Ejemplo n.º 4
0
        public async Task <CancellationTokenSource> ProcessTransaction(PackageAction[] actions,
                                                                       Action <TransactionResponseValue> callback)
        {
            var cancellationToken = new CancellationTokenSource();
            var transaction       = new TransactionRequest.Types.Transaction();

            transaction.Actions.Add(actions.Map(u => new Grpc.PackageAction
            {
                Action = (uint)u.Action, Id = u.PackageKey.ToString(), Target = (uint)u.Target
            }));
            var call = innerClient.ProcessTransaction();

            _ = Task.Run(async() => {
                await foreach (var response in call.ResponseStream.ReadAllAsync(cancellationToken.Token))
                {
                    TransactionResponseValue responseValue = null;
                    switch (response.ValueCase)
                    {
                    case Grpc.TransactionResponse.ValueOneofCase.None:
                        break;

                    case Grpc.TransactionResponse.ValueOneofCase.TransactionStarted:
                        responseValue = new TransactionResponseValue.TransactionStarted {
                            Actions = response.TransactionStarted.Actions.Map(r =>
                                                                              new ResolvedAction {
                                Action = new PackageAction(PackageKey.From(r.Action.Id), (InstallAction)r.Action.Action,
                                                           (InstallTarget)r.Action.Target),
                                Name    = r.Name,
                                Version = r.Version,
                            }).ToArray(),
                            IsRebootRequired = response.TransactionStarted.IsRebootRequired,
                        };
                        break;

                    case Grpc.TransactionResponse.ValueOneofCase.TransactionProgress:
                        responseValue = new TransactionResponseValue.TransactionProgress {
                            Current = response.TransactionProgress.Current,
                            Total   = response.TransactionProgress.Total,
                            Message = response.TransactionProgress.Message,
                        };
                        break;

                    case Grpc.TransactionResponse.ValueOneofCase.TransactionComplete:
                        responseValue = new TransactionResponseValue.TransactionComplete();
                        break;

                    case Grpc.TransactionResponse.ValueOneofCase.TransactionError:
                        responseValue = new TransactionResponseValue.TransactionError {
                            Error      = response.TransactionError.Error,
                            PackageKey = response.TransactionError.PackageId,
                        };
                        break;

                    case Grpc.TransactionResponse.ValueOneofCase.TransactionQueued:
                        responseValue = new TransactionResponseValue.TransactionQueued();
                        break;

                    case Grpc.TransactionResponse.ValueOneofCase.DownloadProgress:
                        responseValue = new TransactionResponseValue.DownloadProgress {
                            Current    = response.DownloadProgress.Current,
                            PackageKey = PackageKey.From(response.DownloadProgress.PackageId),
                            Total      = response.DownloadProgress.Total,
                        };
                        break;

                    case Grpc.TransactionResponse.ValueOneofCase.DownloadComplete:
                        responseValue = new TransactionResponseValue.DownloadComplete {
                            PackageKey = PackageKey.From(response.DownloadComplete.PackageId),
                        };
                        break;

                    case Grpc.TransactionResponse.ValueOneofCase.InstallStarted:
                        responseValue = new TransactionResponseValue.InstallStarted {
                            PackageKey = PackageKey.From(response.InstallStarted.PackageId),
                        };
                        break;

                    case Grpc.TransactionResponse.ValueOneofCase.UninstallStarted:
                        responseValue = new TransactionResponseValue.UninstallStarted {
                            PackageKey = PackageKey.From(response.UninstallStarted.PackageId),
                        };
                        break;

                    case Grpc.TransactionResponse.ValueOneofCase.VerificationFailed:
                        responseValue = new TransactionResponseValue.VerificationFailed();
                        break;
                    }

                    callback(responseValue);
                }
            });

            await call.RequestStream.WriteAsync(new TransactionRequest {
                Transaction = transaction,
            });

            await call.RequestStream.CompleteAsync();

            return(cancellationToken);
        }