[gnome-builder] plugins/cmake: implement run command provider for CMake



commit f946bceff4eb5f182e5094ec224fae7b72dac459
Author: Christian Hergert <chergert redhat com>
Date:   Tue Sep 27 13:34:43 2022 -0700

    plugins/cmake: implement run command provider for CMake
    
    This just looks for `install_manifest.txt` in the builddir and extracts
    things found in a /bin/ directory. Certainly something more advanced could
    be used, but this is better than nothing.

 src/plugins/cmake/cmake-plugin.c                   |   8 +-
 src/plugins/cmake/gbp-cmake-run-command-provider.c | 238 +++++++++++++++++++++
 src/plugins/cmake/gbp-cmake-run-command-provider.h |  31 +++
 src/plugins/cmake/meson.build                      |   1 +
 4 files changed, 276 insertions(+), 2 deletions(-)
---
diff --git a/src/plugins/cmake/cmake-plugin.c b/src/plugins/cmake/cmake-plugin.c
index a0505448e..fbadfeee6 100644
--- a/src/plugins/cmake/cmake-plugin.c
+++ b/src/plugins/cmake/cmake-plugin.c
@@ -23,11 +23,12 @@
 #include <libpeas/peas.h>
 #include <libide-foundry.h>
 
-#include "gbp-cmake-build-system.h"
 #include "gbp-cmake-build-system-discovery.h"
+#include "gbp-cmake-build-system.h"
+#include "gbp-cmake-build-target-provider.h"
 #include "gbp-cmake-pipeline-addin.h"
+#include "gbp-cmake-run-command-provider.h"
 #include "gbp-cmake-toolchain-provider.h"
-#include "gbp-cmake-build-target-provider.h"
 
 _IDE_EXTERN void
 _gbp_cmake_register_types (PeasObjectModule *module)
@@ -47,4 +48,7 @@ _gbp_cmake_register_types (PeasObjectModule *module)
   peas_object_module_register_extension_type (module,
                                               IDE_TYPE_BUILD_TARGET_PROVIDER,
                                               GBP_TYPE_CMAKE_BUILD_TARGET_PROVIDER);
+  peas_object_module_register_extension_type (module,
+                                              IDE_TYPE_RUN_COMMAND_PROVIDER,
+                                              GBP_TYPE_CMAKE_RUN_COMMAND_PROVIDER);
 }
diff --git a/src/plugins/cmake/gbp-cmake-run-command-provider.c 
b/src/plugins/cmake/gbp-cmake-run-command-provider.c
new file mode 100644
index 000000000..e1ecc374c
--- /dev/null
+++ b/src/plugins/cmake/gbp-cmake-run-command-provider.c
@@ -0,0 +1,238 @@
+/* gbp-cmake-run-command-provider.c
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * 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-cmake-run-command-provider"
+
+#include "config.h"
+
+#include <libide-foundry.h>
+#include <libide-io.h>
+#include <libide-threading.h>
+
+#include "gbp-cmake-build-system.h"
+#include "gbp-cmake-run-command-provider.h"
+
+struct _GbpCmakeRunCommandProvider
+{
+  IdeObject parent_instance;
+};
+
+static GListModel *
+parse_manifest_text (char  *contents,
+                     gsize  length)
+{
+  GListStore *store;
+  IdeLineReader reader;
+  char *line;
+  gsize line_length;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (contents != NULL);
+
+  store = g_list_store_new (IDE_TYPE_RUN_COMMAND);
+
+  /* TODO: this is the perfect kind of thing to push off to a thread. If
+   *       only we had a more competent tasking library.
+   */
+
+  ide_line_reader_init (&reader, contents, length);
+  while ((line = ide_line_reader_next (&reader, &line_length)))
+    {
+      const char *slash_bin_slash;
+      const char *name;
+
+      /* Allow us to treat this as a \0 terminated line. We can do
+       * this because g_file_load_contents() (and it's variants) will
+       * ensure we have a trailing \0 byte at the end of the contents
+       * which is not part of the reported "length".
+       */
+      line[line_length] = 0;
+
+      if (!(slash_bin_slash = strstr (line, "/bin/")))
+        continue;
+
+      /* Get the remaining text after /bin/ */
+      name = slash_bin_slash + strlen ("/bin/");
+
+      /* Just the /bin/ directory, skip it */
+      if (name[0] == 0)
+        continue;
+
+      /* If for some reason it's in a further subdirectory, skip it */
+      if (strchr (name, G_DIR_SEPARATOR) == NULL)
+        {
+          g_autoptr(IdeRunCommand) run_command = ide_run_command_new ();
+          g_autofree char *id = g_strdup_printf ("cmake:%s", name);
+
+          ide_run_command_set_kind (run_command, IDE_RUN_COMMAND_KIND_APPLICATION);
+          ide_run_command_set_id (run_command, id);
+          ide_run_command_set_display_name (run_command, name);
+          ide_run_command_append_argv (run_command, line);
+          ide_run_command_set_can_default (run_command, TRUE);
+
+          g_list_store_append (store, run_command);
+        }
+    }
+
+  IDE_RETURN (G_LIST_MODEL (store));
+}
+
+static void
+gbp_cmake_run_command_provider_list_commands_load_cb (GObject      *object,
+                                                      GAsyncResult *result,
+                                                      gpointer      user_data)
+{
+  GFile *file = (GFile *)object;
+  g_autoptr(IdeTask) task = user_data;
+  g_autoptr(GError) error = NULL;
+  g_autofree char *contents = NULL;
+  gsize length = 0;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (G_IS_FILE (file));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (IDE_IS_TASK (task));
+
+  if (!g_file_load_contents_finish (file, result, &contents, &length, NULL, &error))
+    ide_task_return_error (task, g_steal_pointer (&error));
+  else
+    ide_task_return_pointer (task, parse_manifest_text (contents, length), g_object_unref);
+
+  IDE_EXIT;
+}
+
+static void
+gbp_cmake_run_command_provider_list_commands_async (IdeRunCommandProvider *provider,
+                                                    GCancellable          *cancellable,
+                                                    GAsyncReadyCallback    callback,
+                                                    gpointer               user_data)
+{
+  g_autoptr(IdeTask) task = NULL;
+  g_autoptr(GFile) manifest_file = NULL;
+  g_autofree char *manifest_path = NULL;
+  IdeBuildManager *build_manager;
+  IdeBuildSystem *build_system;
+  IdePipeline *pipeline;
+  IdeContext *context;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_CMAKE_RUN_COMMAND_PROVIDER (provider));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  task = ide_task_new (provider, cancellable, callback, user_data);
+  ide_task_set_source_tag (task, gbp_cmake_run_command_provider_list_commands_async);
+
+  context = ide_object_get_context (IDE_OBJECT (provider));
+  build_system = ide_build_system_from_context (context);
+
+  if (!GBP_IS_CMAKE_BUILD_SYSTEM (build_system))
+    {
+      ide_task_return_new_error (task,
+                                 G_IO_ERROR,
+                                 G_IO_ERROR_NOT_SUPPORTED,
+                                 "Not a CMake-based build-system, ignoring request");
+      IDE_EXIT;
+    }
+
+  build_manager = ide_build_manager_from_context (context);
+  pipeline = ide_build_manager_get_pipeline (build_manager);
+
+  if (!ide_pipeline_is_ready (pipeline))
+    {
+      ide_task_return_new_error (task,
+                                 G_IO_ERROR,
+                                 G_IO_ERROR_NOT_SUPPORTED,
+                                 "Pipeline not yet ready, cannot list run commands");
+      IDE_EXIT;
+    }
+
+  manifest_path = ide_pipeline_build_builddir_path (pipeline, "install_manifest.txt", NULL);
+  manifest_file = g_file_new_for_path (manifest_path);
+
+  g_file_load_contents_async (manifest_file,
+                              cancellable,
+                              gbp_cmake_run_command_provider_list_commands_load_cb,
+                              g_steal_pointer (&task));
+
+  IDE_EXIT;
+}
+
+static GListModel *
+gbp_cmake_run_command_provider_list_commands_finish (IdeRunCommandProvider  *provider,
+                                                     GAsyncResult           *result,
+                                                     GError                **error)
+{
+  GListModel *ret;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_CMAKE_RUN_COMMAND_PROVIDER (provider));
+  g_assert (IDE_IS_TASK (result));
+
+  ret = ide_task_propagate_pointer (IDE_TASK (result), error);
+
+  IDE_RETURN (ret);
+}
+
+static void
+run_command_provider_iface_init (IdeRunCommandProviderInterface *iface)
+{
+  iface->list_commands_async = gbp_cmake_run_command_provider_list_commands_async;
+  iface->list_commands_finish = gbp_cmake_run_command_provider_list_commands_finish;
+}
+
+G_DEFINE_FINAL_TYPE_WITH_CODE (GbpCmakeRunCommandProvider, gbp_cmake_run_command_provider, IDE_TYPE_OBJECT,
+                               G_IMPLEMENT_INTERFACE (IDE_TYPE_RUN_COMMAND_PROVIDER, 
run_command_provider_iface_init))
+
+static void
+gbp_cmake_run_command_provider_parent_set (IdeObject *object,
+                                           IdeObject *parent)
+{
+  IDE_ENTRY;
+
+  g_assert (GBP_IS_CMAKE_RUN_COMMAND_PROVIDER (object));
+  g_assert (!parent || IDE_IS_OBJECT (parent));
+
+  if (parent != NULL)
+    ide_run_command_provider_invalidates_at_phase (IDE_RUN_COMMAND_PROVIDER (object),
+                                                   IDE_PIPELINE_PHASE_CONFIGURE);
+
+  IDE_EXIT;
+}
+
+static void
+gbp_cmake_run_command_provider_class_init (GbpCmakeRunCommandProviderClass *klass)
+{
+  IdeObjectClass *i_object_class = IDE_OBJECT_CLASS (klass);
+
+  i_object_class->parent_set = gbp_cmake_run_command_provider_parent_set;
+}
+
+static void
+gbp_cmake_run_command_provider_init (GbpCmakeRunCommandProvider *self)
+{
+}
diff --git a/src/plugins/cmake/gbp-cmake-run-command-provider.h 
b/src/plugins/cmake/gbp-cmake-run-command-provider.h
new file mode 100644
index 000000000..924abfd15
--- /dev/null
+++ b/src/plugins/cmake/gbp-cmake-run-command-provider.h
@@ -0,0 +1,31 @@
+/* gbp-cmake-run-command-provider.h
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * 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-core.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_CMAKE_RUN_COMMAND_PROVIDER (gbp_cmake_run_command_provider_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpCmakeRunCommandProvider, gbp_cmake_run_command_provider, GBP, 
CMAKE_RUN_COMMAND_PROVIDER, IdeObject)
+
+G_END_DECLS
diff --git a/src/plugins/cmake/meson.build b/src/plugins/cmake/meson.build
index 31765f413..be7091efe 100644
--- a/src/plugins/cmake/meson.build
+++ b/src/plugins/cmake/meson.build
@@ -8,6 +8,7 @@ plugins_sources += files([
   'gbp-cmake-build-target.c',
   'gbp-cmake-build-target-provider.c',
   'gbp-cmake-pipeline-addin.c',
+  'gbp-cmake-run-command-provider.c',
   'gbp-cmake-toolchain.c',
   'gbp-cmake-toolchain-provider.c',
   'gbp-cmake-build-stage-codemodel.c',


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