[gnome-builder/wip/gtk4-port] libide/foundry: add new run tool abstraction



commit ec9fcaf8c271fa22ebf2c42b8818e6382bf6445a
Author: Christian Hergert <chergert redhat com>
Date:   Fri Jun 24 03:32:56 2022 -0700

    libide/foundry: add new run tool abstraction
    
    This is going to allow us to remove IdeRunHandler from the run manager
    in favor of something typed we can reuse.

 src/libide/foundry/ide-no-tool-private.h  |  33 ++++
 src/libide/foundry/ide-no-tool.c          |  72 +++++++++
 src/libide/foundry/ide-run-tool-private.h |  31 ++++
 src/libide/foundry/ide-run-tool.c         | 252 ++++++++++++++++++++++++++++++
 src/libide/foundry/ide-run-tool.h         |  71 +++++++++
 src/libide/foundry/libide-foundry.h       |   1 +
 src/libide/foundry/meson.build            |   5 +
 7 files changed, 465 insertions(+)
---
diff --git a/src/libide/foundry/ide-no-tool-private.h b/src/libide/foundry/ide-no-tool-private.h
new file mode 100644
index 000000000..f597660d1
--- /dev/null
+++ b/src/libide/foundry/ide-no-tool-private.h
@@ -0,0 +1,33 @@
+/* ide-no-tool.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 "ide-run-tool.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_NO_TOOL (ide_no_tool_get_type())
+
+G_DECLARE_FINAL_TYPE (IdeNoTool, ide_no_tool, IDE, NO_TOOL, IdeRunTool)
+
+IdeRunTool *ide_no_tool_new (void);
+
+G_END_DECLS
diff --git a/src/libide/foundry/ide-no-tool.c b/src/libide/foundry/ide-no-tool.c
new file mode 100644
index 000000000..26c80d2a4
--- /dev/null
+++ b/src/libide/foundry/ide-no-tool.c
@@ -0,0 +1,72 @@
+/* ide-no-tool.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 "ide-no-tool"
+
+#include "config.h"
+
+#include "ide-no-tool-private.h"
+#include "ide-pipeline.h"
+#include "ide-run-command.h"
+#include "ide-run-context.h"
+
+struct _IdeNoTool
+{
+  IdeRunTool parent_instance;
+};
+
+G_DEFINE_FINAL_TYPE (IdeNoTool, ide_no_tool, IDE_TYPE_RUN_TOOL)
+
+static void
+ide_no_tool_prepare_to_run (IdeRunTool    *run_tool,
+                            IdePipeline   *pipeline,
+                            IdeRunCommand *run_command,
+                            IdeRunContext *run_context)
+{
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (IDE_IS_PIPELINE (pipeline));
+  g_assert (IDE_IS_RUN_COMMAND (run_command));
+  g_assert (IDE_IS_RUN_CONTEXT (run_context));
+
+  IDE_EXIT;
+}
+
+static void
+ide_no_tool_class_init (IdeNoToolClass *klass)
+{
+  IdeRunToolClass *run_tool_class = IDE_RUN_TOOL_CLASS (klass);
+
+  run_tool_class->prepare_to_run = ide_no_tool_prepare_to_run;
+}
+
+static void
+ide_no_tool_init (IdeNoTool *self)
+{
+  ide_run_tool_set_icon_name (IDE_RUN_TOOL (self),
+                              "builder-run-start-symbolic");
+}
+
+IdeRunTool *
+ide_no_tool_new (void)
+{
+  return g_object_new (IDE_TYPE_NO_TOOL, NULL);
+}
diff --git a/src/libide/foundry/ide-run-tool-private.h b/src/libide/foundry/ide-run-tool-private.h
new file mode 100644
index 000000000..43a30d015
--- /dev/null
+++ b/src/libide/foundry/ide-run-tool-private.h
@@ -0,0 +1,31 @@
+/* ide-run-tool-private.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 "ide-run-tool.h"
+
+G_BEGIN_DECLS
+
+void _ide_run_tool_emit_started (IdeRunTool    *self,
+                                 IdeSubprocess *subprocess);
+void _ide_run_tool_emit_stopped (IdeRunTool    *self);
+
+G_END_DECLS
diff --git a/src/libide/foundry/ide-run-tool.c b/src/libide/foundry/ide-run-tool.c
new file mode 100644
index 000000000..d5213a053
--- /dev/null
+++ b/src/libide/foundry/ide-run-tool.c
@@ -0,0 +1,252 @@
+/* ide-run-tool.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 "ide-run-tool"
+
+#include "config.h"
+
+#include "ide-pipeline.h"
+#include "ide-run-command.h"
+#include "ide-run-context.h"
+#include "ide-run-tool-private.h"
+
+typedef struct
+{
+  IdeSubprocess *subprocess;
+  char *icon_name;
+} IdeRunToolPrivate;
+
+G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (IdeRunTool, ide_run_tool, IDE_TYPE_OBJECT)
+
+enum {
+  PROP_0,
+  PROP_ICON_NAME,
+  N_PROPS
+};
+
+enum {
+  STARTED,
+  STOPPED,
+  N_SIGNALS
+};
+
+static GParamSpec *properties[N_PROPS];
+static guint signals[N_SIGNALS];
+
+static void
+ide_run_tool_real_force_exit (IdeRunTool *self)
+{
+  IdeRunToolPrivate *priv = ide_run_tool_get_instance_private (self);
+
+  g_assert (IDE_IS_RUN_TOOL (self));
+
+  if (priv->subprocess != NULL)
+    ide_subprocess_force_exit (priv->subprocess);
+}
+
+static void
+ide_run_tool_real_send_signal (IdeRunTool *self,
+                               int         signum)
+{
+  IdeRunToolPrivate *priv = ide_run_tool_get_instance_private (self);
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (IDE_IS_RUN_TOOL (self));
+
+  g_debug ("Sending signal %d to subprocess %p", signum, priv->subprocess);
+
+  if (priv->subprocess != NULL)
+    ide_subprocess_send_signal (priv->subprocess, signum);
+}
+
+static void
+ide_run_tool_dispose (GObject *object)
+{
+  IdeRunTool *self = (IdeRunTool *)object;
+  IdeRunToolPrivate *priv = ide_run_tool_get_instance_private (self);
+
+  g_clear_object (&priv->subprocess);
+  g_clear_pointer (&priv->icon_name, g_free);
+
+  G_OBJECT_CLASS (ide_run_tool_parent_class)->dispose (object);
+}
+
+static void
+ide_run_tool_get_property (GObject    *object,
+                           guint       prop_id,
+                           GValue     *value,
+                           GParamSpec *pspec)
+{
+  IdeRunTool *self = IDE_RUN_TOOL (object);
+
+  switch (prop_id)
+    {
+    case PROP_ICON_NAME:
+      g_value_set_string (value, ide_run_tool_get_icon_name (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_run_tool_set_property (GObject      *object,
+                           guint         prop_id,
+                           const GValue *value,
+                           GParamSpec   *pspec)
+{
+  IdeRunTool *self = IDE_RUN_TOOL (object);
+
+  switch (prop_id)
+    {
+    case PROP_ICON_NAME:
+      ide_run_tool_set_icon_name (self, g_value_get_string (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_run_tool_class_init (IdeRunToolClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->dispose = ide_run_tool_dispose;
+  object_class->get_property = ide_run_tool_get_property;
+  object_class->set_property = ide_run_tool_set_property;
+
+  klass->force_exit = ide_run_tool_real_force_exit;
+  klass->send_signal = ide_run_tool_real_send_signal;
+
+  signals[STARTED] =
+    g_signal_new ("started",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST,
+                  G_STRUCT_OFFSET (IdeRunToolClass, started),
+                  NULL, NULL,
+                  NULL,
+                  G_TYPE_NONE, 1, IDE_TYPE_SUBPROCESS);
+
+  signals[STOPPED] =
+    g_signal_new ("stopped",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST,
+                  G_STRUCT_OFFSET (IdeRunToolClass, stopped),
+                  NULL, NULL,
+                  NULL,
+                  G_TYPE_NONE, 0);
+}
+
+static void
+ide_run_tool_init (IdeRunTool *self)
+{
+}
+
+void
+ide_run_tool_force_exit (IdeRunTool *self)
+{
+  g_return_if_fail (IDE_IS_RUN_TOOL (self));
+
+  IDE_RUN_TOOL_GET_CLASS (self)->force_exit (self);
+}
+
+void
+ide_run_tool_send_signal (IdeRunTool *self,
+                          int         signum)
+{
+  g_return_if_fail (IDE_IS_RUN_TOOL (self));
+
+  IDE_RUN_TOOL_GET_CLASS (self)->send_signal (self, signum);
+}
+
+void
+ide_run_tool_prepare_to_run (IdeRunTool    *self,
+                             IdePipeline   *pipeline,
+                             IdeRunCommand *run_command,
+                             IdeRunContext *run_context)
+{
+  g_return_if_fail (IDE_IS_RUN_TOOL (self));
+  g_return_if_fail (IDE_IS_PIPELINE (pipeline));
+  g_return_if_fail (IDE_IS_RUN_COMMAND (run_command));
+  g_return_if_fail (IDE_IS_RUN_CONTEXT (run_context));
+
+  if (IDE_RUN_TOOL_GET_CLASS (self)->prepare_to_run)
+    IDE_RUN_TOOL_GET_CLASS (self)->prepare_to_run (self, pipeline, run_command, run_context);
+}
+
+void
+_ide_run_tool_emit_started (IdeRunTool    *self,
+                            IdeSubprocess *subprocess)
+{
+  IdeRunToolPrivate *priv = ide_run_tool_get_instance_private (self);
+
+  IDE_ENTRY;
+
+  g_return_if_fail (IDE_IS_MAIN_THREAD ());
+  g_return_if_fail (IDE_IS_RUN_TOOL (self));
+
+  g_debug ("%s started", G_OBJECT_TYPE_NAME (self));
+  g_set_object (&priv->subprocess, subprocess);
+  g_signal_emit (self, signals[STARTED], 0, subprocess);
+
+  IDE_EXIT;
+}
+
+void
+_ide_run_tool_emit_stopped (IdeRunTool *self)
+{
+  IdeRunToolPrivate *priv = ide_run_tool_get_instance_private (self);
+
+  IDE_ENTRY;
+
+  g_return_if_fail (IDE_IS_MAIN_THREAD ());
+  g_return_if_fail (IDE_IS_RUN_TOOL (self));
+
+  g_debug ("%s stopped", G_OBJECT_TYPE_NAME (self));
+  g_clear_object (&priv->subprocess);
+  g_signal_emit (self, signals[STOPPED], 0);
+
+  IDE_EXIT;
+}
+
+const char *
+ide_run_tool_get_icon_name (IdeRunTool *self)
+{
+  IdeRunToolPrivate *priv = ide_run_tool_get_instance_private (self);
+
+  g_return_val_if_fail (IDE_IS_RUN_TOOL (self), NULL);
+
+  return priv->icon_name;
+}
+
+void
+ide_run_tool_set_icon_name (IdeRunTool *self,
+                            const char *icon_name)
+{
+  IdeRunToolPrivate *priv = ide_run_tool_get_instance_private (self);
+
+  g_return_if_fail (IDE_IS_RUN_TOOL (self));
+
+  if (!ide_set_string (&priv->icon_name, icon_name))
+    g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ICON_NAME]);
+}
diff --git a/src/libide/foundry/ide-run-tool.h b/src/libide/foundry/ide-run-tool.h
new file mode 100644
index 000000000..eea53e592
--- /dev/null
+++ b/src/libide/foundry/ide-run-tool.h
@@ -0,0 +1,71 @@
+/* ide-run-tool.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
+
+#if !defined (IDE_FOUNDRY_INSIDE) && !defined (IDE_FOUNDRY_COMPILATION)
+# error "Only <libide-foundry.h> can be included directly."
+#endif
+
+#include <libide-core.h>
+#include <libide-threading.h>
+
+#include "ide-foundry-types.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_RUN_TOOL (ide_run_tool_get_type())
+
+IDE_AVAILABLE_IN_ALL
+G_DECLARE_DERIVABLE_TYPE (IdeRunTool, ide_run_tool, IDE, RUN_TOOL, IdeObject)
+
+struct _IdeRunToolClass
+{
+  IdeObjectClass parent_class;
+
+  void (*started)        (IdeRunTool    *self,
+                          IdeSubprocess *subprocess);
+  void (*stopped)        (IdeRunTool    *self);
+  void (*prepare_to_run) (IdeRunTool    *self,
+                          IdePipeline   *pipeline,
+                          IdeRunCommand *run_command,
+                          IdeRunContext *run_context);
+  void (*force_exit)     (IdeRunTool    *self);
+  void (*send_signal)    (IdeRunTool    *self,
+                          int            signum);
+};
+
+IDE_AVAILABLE_IN_ALL
+void        ide_run_tool_force_exit     (IdeRunTool    *self);
+IDE_AVAILABLE_IN_ALL
+void        ide_run_tool_send_signal    (IdeRunTool    *self,
+                                         int            signum);
+IDE_AVAILABLE_IN_ALL
+void        ide_run_tool_prepare_to_run (IdeRunTool    *self,
+                                         IdePipeline   *pipeline,
+                                         IdeRunCommand *run_command,
+                                         IdeRunContext *run_context);
+IDE_AVAILABLE_IN_ALL
+const char *ide_run_tool_get_icon_name  (IdeRunTool    *self);
+IDE_AVAILABLE_IN_ALL
+void        ide_run_tool_set_icon_name  (IdeRunTool    *self,
+                                         const char    *icon_name);
+
+G_END_DECLS
diff --git a/src/libide/foundry/libide-foundry.h b/src/libide/foundry/libide-foundry.h
index 6ed70b3ba..1e33862c1 100644
--- a/src/libide/foundry/libide-foundry.h
+++ b/src/libide/foundry/libide-foundry.h
@@ -59,6 +59,7 @@ G_BEGIN_DECLS
 #include "ide-run-command-provider.h"
 #include "ide-run-context.h"
 #include "ide-run-manager.h"
+#include "ide-run-tool.h"
 #include "ide-runner-addin.h"
 #include "ide-runner.h"
 #include "ide-runtime-manager.h"
diff --git a/src/libide/foundry/meson.build b/src/libide/foundry/meson.build
index 6cf96bcbd..c16abb462 100644
--- a/src/libide/foundry/meson.build
+++ b/src/libide/foundry/meson.build
@@ -44,6 +44,7 @@ libide_foundry_public_headers = [
   'ide-run-command-provider.h',
   'ide-run-context.h',
   'ide-run-manager.h',
+  'ide-run-tool.h',
   'ide-runner-addin.h',
   'ide-runner.h',
   'ide-runtime-manager.h',
@@ -70,7 +71,9 @@ libide_foundry_private_headers = [
   'ide-device-private.h',
   'ide-foundry-init.h',
   'ide-local-deploy-strategy.h',
+  'ide-no-tool-private.h',
   'ide-run-manager-private.h',
+  'ide-run-tool-private.h',
   'ide-runtime-private.h',
   'ide-toolchain-private.h',
 ]
@@ -123,6 +126,7 @@ libide_foundry_public_sources = [
   'ide-run-command-provider.c',
   'ide-run-context.c',
   'ide-run-manager.c',
+  'ide-run-tool.c',
   'ide-runner-addin.c',
   'ide-runner.c',
   'ide-runtime-manager.c',
@@ -146,6 +150,7 @@ libide_foundry_private_sources = [
   'ide-build-utils.c',
   'ide-foundry-init.c',
   'ide-local-deploy-strategy.c',
+  'ide-no-tool.c',
 ]
 
 libide_foundry_sources += libide_foundry_public_sources


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