[gnome-builder] flatpak: Bring back support for adding default remotes on startup



commit 13a2b1f2ce8ee781c97bc94c8fd5bed47b1b0d12
Author: vanadiae <vanadiae35 gmail com>
Date:   Tue Jul 27 20:49:29 2021 +0200

    flatpak: Bring back support for adding default remotes on startup
    
    It was removed as part of 5469bc327ad0e16689890ff10e8fe525415fa8aa, but
    it's something that's useful to keep as it avoids having to install
    the gnome-nightly and flathub remotes, the same way as needed SDKs are
    downloaded automatically by Builder.

 src/plugins/flatpak/flatpak-plugin.c               |   4 +
 .../flatpak/gbp-flatpak-application-addin.c        | 127 +++++++++++++++++++++
 .../flatpak/gbp-flatpak-application-addin.h        |  32 ++++++
 src/plugins/flatpak/meson.build                    |   1 +
 4 files changed, 164 insertions(+)
---
diff --git a/src/plugins/flatpak/flatpak-plugin.c b/src/plugins/flatpak/flatpak-plugin.c
index 120e1365f..f3786f062 100644
--- a/src/plugins/flatpak/flatpak-plugin.c
+++ b/src/plugins/flatpak/flatpak-plugin.c
@@ -27,6 +27,7 @@
 #include <libide-foundry.h>
 #include <libide-gui.h>
 
+#include "gbp-flatpak-application-addin.h"
 #include "gbp-flatpak-build-system-discovery.h"
 #include "gbp-flatpak-build-target-provider.h"
 #include "gbp-flatpak-config-provider.h"
@@ -39,6 +40,9 @@ _gbp_flatpak_register_types (PeasObjectModule *module)
 {
   ide_g_file_add_ignored_pattern (".flatpak-builder");
 
+  peas_object_module_register_extension_type (module,
+                                              IDE_TYPE_APPLICATION_ADDIN,
+                                              GBP_TYPE_FLATPAK_APPLICATION_ADDIN);
   peas_object_module_register_extension_type (module,
                                               IDE_TYPE_BUILD_SYSTEM_DISCOVERY,
                                               GBP_TYPE_FLATPAK_BUILD_SYSTEM_DISCOVERY);
diff --git a/src/plugins/flatpak/gbp-flatpak-application-addin.c 
b/src/plugins/flatpak/gbp-flatpak-application-addin.c
new file mode 100644
index 000000000..ae3b4948e
--- /dev/null
+++ b/src/plugins/flatpak/gbp-flatpak-application-addin.c
@@ -0,0 +1,127 @@
+/* gbp-flatpak-application-addin.c
+ *
+ * Copyright 2015-2019 Christian Hergert <christian hergert me>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "gbp-flatpak-application-addin"
+
+#include "config.h"
+
+#include "gbp-flatpak-application-addin.h"
+
+struct _GbpFlatpakApplicationAddin
+{
+  GObject    parent_instance;
+};
+
+typedef struct
+{
+  const gchar *name;
+  const gchar *url;
+} BuiltinFlatpakRepo;
+
+static BuiltinFlatpakRepo builtin_flatpak_repos[] = {
+  { "flathub",       "https://flathub.org/repo/flathub.flatpakrepo"; },
+  { "gnome-nightly", "https://nightly.gnome.org/gnome-nightly.flatpakrepo"; },
+};
+
+/*
+* Ensure we have our repositories that we need to locate various
+* runtimes for GNOME.
+*/
+static gboolean
+ensure_remotes_exist_sync (GError **error)
+{
+  IDE_ENTRY;
+
+  for (guint i = 0; i < G_N_ELEMENTS (builtin_flatpak_repos); i++)
+    {
+      g_autoptr(IdeSubprocessLauncher) launcher = NULL;
+      g_autoptr(IdeSubprocess) subprocess = NULL;
+      const gchar *name = builtin_flatpak_repos[i].name;
+      const gchar *url = builtin_flatpak_repos[i].url;
+
+      launcher = ide_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE |
+                                              G_SUBPROCESS_FLAGS_STDERR_PIPE);
+      ide_subprocess_launcher_set_run_on_host (launcher, TRUE);
+      ide_subprocess_launcher_set_clear_env (launcher, FALSE);
+      ide_subprocess_launcher_push_argv (launcher, "flatpak");
+      ide_subprocess_launcher_push_argv (launcher, "remote-add");
+      ide_subprocess_launcher_push_argv (launcher, "--user");
+      ide_subprocess_launcher_push_argv (launcher, "--if-not-exists");
+      ide_subprocess_launcher_push_argv (launcher, "--from");
+      ide_subprocess_launcher_push_argv (launcher, name);
+      ide_subprocess_launcher_push_argv (launcher, url);
+
+      subprocess = ide_subprocess_launcher_spawn (launcher, NULL, error);
+
+      if (subprocess == NULL || !ide_subprocess_wait_check (subprocess, NULL, error))
+        IDE_RETURN (FALSE);
+    }
+  IDE_RETURN (TRUE);
+}
+
+static void
+gbp_flatpak_application_addin_load (IdeApplicationAddin *addin,
+                                    IdeApplication      *application)
+{
+  GbpFlatpakApplicationAddin *self = (GbpFlatpakApplicationAddin *)addin;
+  g_autoptr(GError) error = NULL;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_FLATPAK_APPLICATION_ADDIN (self));
+  g_assert (IDE_IS_APPLICATION (application));
+
+  if (!ensure_remotes_exist_sync (&error))
+    g_warning ("Failed to add required flatpak remotes: %s", error->message);
+}
+
+static void
+gbp_flatpak_application_addin_unload (IdeApplicationAddin *addin,
+                                      IdeApplication      *application)
+{
+  GbpFlatpakApplicationAddin *self = (GbpFlatpakApplicationAddin *)addin;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_FLATPAK_APPLICATION_ADDIN (self));
+  g_assert (IDE_IS_APPLICATION (application));
+}
+
+static void
+application_addin_iface_init (IdeApplicationAddinInterface *iface)
+{
+  iface->load = gbp_flatpak_application_addin_load;
+  iface->unload = gbp_flatpak_application_addin_unload;
+}
+
+G_DEFINE_TYPE_EXTENDED (GbpFlatpakApplicationAddin,
+                        gbp_flatpak_application_addin,
+                        G_TYPE_OBJECT,
+                        0,
+                        G_IMPLEMENT_INTERFACE (IDE_TYPE_APPLICATION_ADDIN, application_addin_iface_init))
+
+static void
+gbp_flatpak_application_addin_class_init (GbpFlatpakApplicationAddinClass *klass)
+{
+}
+
+static void
+gbp_flatpak_application_addin_init (GbpFlatpakApplicationAddin *self)
+{
+}
+
diff --git a/src/plugins/flatpak/gbp-flatpak-application-addin.h 
b/src/plugins/flatpak/gbp-flatpak-application-addin.h
new file mode 100644
index 000000000..01d09e1e9
--- /dev/null
+++ b/src/plugins/flatpak/gbp-flatpak-application-addin.h
@@ -0,0 +1,32 @@
+/* gbp-flatpak-application-addin.h
+ *
+ * Copyright 2015-2019 Christian Hergert <christian hergert me>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <libide-gui.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_FLATPAK_APPLICATION_ADDIN (gbp_flatpak_application_addin_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpFlatpakApplicationAddin, gbp_flatpak_application_addin, GBP, 
FLATPAK_APPLICATION_ADDIN, GObject)
+
+G_END_DECLS
+
diff --git a/src/plugins/flatpak/meson.build b/src/plugins/flatpak/meson.build
index 4be33614d..0133d3455 100644
--- a/src/plugins/flatpak/meson.build
+++ b/src/plugins/flatpak/meson.build
@@ -15,6 +15,7 @@ subdir('daemon')
 
 plugins_sources += files([
   'flatpak-plugin.c',
+  'gbp-flatpak-application-addin.c',
   'gbp-flatpak-build-system-discovery.c',
   'gbp-flatpak-build-target-provider.c',
   'gbp-flatpak-build-target.c',


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]