[gnome-builder/wip/gtk4-port] libide/foundry: add run commands pipeline stage



commit be73b25681cbfd2754b2c3e7a5a3444596a8e03f
Author: Christian Hergert <chergert redhat com>
Date:   Tue Jun 28 15:02:45 2022 -0700

    libide/foundry: add run commands pipeline stage
    
    Similar to the launcher, but supports a run command for build/clean. We
    would want as many pipeline stages moved over to this that we can, in time.

 src/libide/foundry/ide-pipeline-stage-command.c | 305 ++++++++++++++++++++++++
 src/libide/foundry/ide-pipeline-stage-command.h |  43 ++++
 src/libide/foundry/libide-foundry.h             |   1 +
 src/libide/foundry/meson.build                  |   2 +
 4 files changed, 351 insertions(+)
---
diff --git a/src/libide/foundry/ide-pipeline-stage-command.c b/src/libide/foundry/ide-pipeline-stage-command.c
new file mode 100644
index 000000000..4301afb7d
--- /dev/null
+++ b/src/libide/foundry/ide-pipeline-stage-command.c
@@ -0,0 +1,305 @@
+/* ide-pipeline-stage-command.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-pipeline-stage-command"
+
+#include "config.h"
+
+#include <libide-threading.h>
+
+#include "ide-pipeline.h"
+#include "ide-pipeline-stage-command.h"
+#include "ide-run-command.h"
+#include "ide-run-context.h"
+
+struct _IdePipelineStageCommand
+{
+  IdePipelineStage  parent_instance;
+  IdeRunCommand    *build_command;
+  IdeRunCommand    *clean_command;
+};
+
+enum {
+  PROP_0,
+  PROP_BUILD_COMMAND,
+  PROP_CLEAN_COMMAND,
+  N_PROPS
+};
+
+G_DEFINE_FINAL_TYPE (IdePipelineStageCommand, ide_pipeline_stage_command, IDE_TYPE_PIPELINE_STAGE)
+
+static GParamSpec *properties [N_PROPS];
+
+static void
+ide_pipeline_stage_command_wait_check_cb (GObject      *object,
+                                          GAsyncResult *result,
+                                          gpointer      user_data)
+{
+  IdeSubprocess *subprocess = (IdeSubprocess *)object;
+  g_autoptr(IdeTask) task = user_data;
+  g_autoptr(GError) error = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (IDE_IS_SUBPROCESS (subprocess));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (IDE_IS_TASK (task));
+
+  if (!ide_subprocess_wait_check_finish (subprocess, result, &error))
+    ide_task_return_error (task, g_steal_pointer (&error));
+  else
+    ide_task_return_boolean (task, TRUE);
+
+  IDE_EXIT;
+}
+
+static void
+ide_pipeline_stage_command_build_async (IdePipelineStage    *stage,
+                                        IdePipeline         *pipeline,
+                                        GCancellable        *cancellable,
+                                        GAsyncReadyCallback  callback,
+                                        gpointer             user_data)
+{
+  IdePipelineStageCommand *self = (IdePipelineStageCommand *)stage;
+  g_autoptr(IdeRunContext) run_context = NULL;
+  g_autoptr(IdeSubprocess) subprocess = NULL;
+  g_autoptr(IdeTask) task = NULL;
+  g_autoptr(GError) error = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (IDE_IS_PIPELINE_STAGE_COMMAND (self));
+  g_assert (IDE_IS_PIPELINE (pipeline));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  task = ide_task_new (self, cancellable, callback, user_data);
+  ide_task_set_source_tag (task, ide_pipeline_stage_command_build_async);
+
+  if (self->build_command == NULL)
+    {
+      ide_task_return_boolean (task, TRUE);
+      IDE_EXIT;
+    }
+
+  run_context = ide_pipeline_create_run_context (pipeline, self->build_command);
+
+  if (!(subprocess = ide_run_context_spawn (run_context, &error)))
+    {
+      ide_task_return_error (task, g_steal_pointer (&error));
+      IDE_EXIT;
+    }
+
+  ide_subprocess_wait_check_async (subprocess,
+                                   cancellable,
+                                   ide_pipeline_stage_command_wait_check_cb,
+                                   g_steal_pointer (&task));
+
+  IDE_EXIT;
+}
+
+static gboolean
+ide_pipeline_stage_command_build_finish (IdePipelineStage  *stage,
+                                         GAsyncResult      *result,
+                                         GError           **error)
+{
+  gboolean ret;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (IDE_IS_PIPELINE_STAGE (stage));
+  g_assert (IDE_IS_TASK (result));
+
+  ret = ide_task_propagate_boolean (IDE_TASK (result), error);
+
+  IDE_RETURN (ret);
+}
+
+static void
+ide_pipeline_stage_command_clean_async (IdePipelineStage    *stage,
+                                        IdePipeline         *pipeline,
+                                        GCancellable        *cancellable,
+                                        GAsyncReadyCallback  callback,
+                                        gpointer             user_data)
+{
+  IdePipelineStageCommand *self = (IdePipelineStageCommand *)stage;
+  g_autoptr(IdeRunContext) run_context = NULL;
+  g_autoptr(IdeSubprocess) subprocess = NULL;
+  g_autoptr(IdeTask) task = NULL;
+  g_autoptr(GError) error = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (IDE_IS_PIPELINE_STAGE_COMMAND (self));
+  g_assert (IDE_IS_PIPELINE (pipeline));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  task = ide_task_new (self, cancellable, callback, user_data);
+  ide_task_set_source_tag (task, ide_pipeline_stage_command_clean_async);
+
+  if (self->clean_command == NULL)
+    {
+      ide_task_return_boolean (task, TRUE);
+      IDE_EXIT;
+    }
+
+  run_context = ide_pipeline_create_run_context (pipeline, self->clean_command);
+
+  if (!(subprocess = ide_run_context_spawn (run_context, &error)))
+    {
+      ide_task_return_error (task, g_steal_pointer (&error));
+      IDE_EXIT;
+    }
+
+  ide_subprocess_wait_check_async (subprocess,
+                                   cancellable,
+                                   ide_pipeline_stage_command_wait_check_cb,
+                                   g_steal_pointer (&task));
+
+  IDE_EXIT;
+}
+
+static gboolean
+ide_pipeline_stage_command_clean_finish (IdePipelineStage  *stage,
+                                         GAsyncResult      *result,
+                                         GError           **error)
+{
+  gboolean ret;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (IDE_IS_PIPELINE_STAGE (stage));
+  g_assert (IDE_IS_TASK (result));
+
+  ret = ide_task_propagate_boolean (IDE_TASK (result), error);
+
+  IDE_RETURN (ret);
+}
+
+static void
+ide_pipeline_stage_command_finalize (GObject *object)
+{
+  IdePipelineStageCommand *self = (IdePipelineStageCommand *)object;
+
+  g_clear_object (&self->build_command);
+  g_clear_object (&self->clean_command);
+
+  G_OBJECT_CLASS (ide_pipeline_stage_command_parent_class)->finalize (object);
+}
+
+static void
+ide_pipeline_stage_command_get_property (GObject    *object,
+                                         guint       prop_id,
+                                         GValue     *value,
+                                         GParamSpec *pspec)
+{
+  IdePipelineStageCommand *self = IDE_PIPELINE_STAGE_COMMAND (object);
+
+  switch (prop_id)
+    {
+    case PROP_BUILD_COMMAND:
+      g_value_set_object (value, self->build_command);
+      break;
+
+    case PROP_CLEAN_COMMAND:
+      g_value_set_object (value, self->clean_command);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_pipeline_stage_command_set_property (GObject      *object,
+                                         guint         prop_id,
+                                         const GValue *value,
+                                         GParamSpec   *pspec)
+{
+  IdePipelineStageCommand *self = IDE_PIPELINE_STAGE_COMMAND (object);
+
+  switch (prop_id)
+    {
+    case PROP_BUILD_COMMAND:
+      g_set_object (&self->build_command, g_value_get_object (value));
+      break;
+
+    case PROP_CLEAN_COMMAND:
+      g_set_object (&self->clean_command, g_value_get_object (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_pipeline_stage_command_class_init (IdePipelineStageCommandClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  IdePipelineStageClass *pipeline_stage_class = IDE_PIPELINE_STAGE_CLASS (klass);
+
+  object_class->finalize = ide_pipeline_stage_command_finalize;
+  object_class->get_property = ide_pipeline_stage_command_get_property;
+  object_class->set_property = ide_pipeline_stage_command_set_property;
+
+  pipeline_stage_class->build_async = ide_pipeline_stage_command_build_async;
+  pipeline_stage_class->build_finish = ide_pipeline_stage_command_build_finish;
+  pipeline_stage_class->clean_async = ide_pipeline_stage_command_clean_async;
+  pipeline_stage_class->clean_finish = ide_pipeline_stage_command_clean_finish;
+
+  properties [PROP_BUILD_COMMAND] =
+    g_param_spec_object ("build-command",
+                         "Build Command",
+                         "The build command to execute",
+                         IDE_TYPE_RUN_COMMAND,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  properties [PROP_CLEAN_COMMAND] =
+    g_param_spec_object ("clean-command",
+                         "Clean Command",
+                         "The clean command to execute",
+                         IDE_TYPE_RUN_COMMAND,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+ide_pipeline_stage_command_init (IdePipelineStageCommand *self)
+{
+}
+
+IdePipelineStage *
+ide_pipeline_stage_command_new (IdeRunCommand *build_command,
+                                IdeRunCommand *clean_command)
+{
+  g_return_val_if_fail (!build_command || IDE_IS_RUN_COMMAND (build_command), NULL);
+  g_return_val_if_fail (!clean_command || IDE_IS_RUN_COMMAND (clean_command), NULL);
+
+  return g_object_new (IDE_TYPE_PIPELINE_STAGE_COMMAND,
+                       "build-command", build_command,
+                       "clean-command", clean_command,
+                       NULL);
+}
diff --git a/src/libide/foundry/ide-pipeline-stage-command.h b/src/libide/foundry/ide-pipeline-stage-command.h
new file mode 100644
index 000000000..36003634d
--- /dev/null
+++ b/src/libide/foundry/ide-pipeline-stage-command.h
@@ -0,0 +1,43 @@
+/* ide-pipeline-stage-command.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 "ide-foundry-types.h"
+#include "ide-pipeline-stage.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_PIPELINE_STAGE_COMMAND (ide_pipeline_stage_command_get_type())
+
+IDE_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (IdePipelineStageCommand, ide_pipeline_stage_command, IDE, PIPELINE_STAGE_COMMAND, 
IdePipelineStage)
+
+IDE_AVAILABLE_IN_ALL
+IdePipelineStage *ide_pipeline_stage_command_new (IdeRunCommand *build_command,
+                                                  IdeRunCommand *clean_command);
+
+G_END_DECLS
diff --git a/src/libide/foundry/libide-foundry.h b/src/libide/foundry/libide-foundry.h
index 5a2066856..ec1e11a20 100644
--- a/src/libide/foundry/libide-foundry.h
+++ b/src/libide/foundry/libide-foundry.h
@@ -50,6 +50,7 @@ G_BEGIN_DECLS
 #include "ide-foundry-global.h"
 #include "ide-local-device.h"
 #include "ide-pipeline-addin.h"
+#include "ide-pipeline-stage-command.h"
 #include "ide-pipeline-stage-launcher.h"
 #include "ide-pipeline-stage-mkdirs.h"
 #include "ide-pipeline-stage-transfer.h"
diff --git a/src/libide/foundry/meson.build b/src/libide/foundry/meson.build
index 9b3e529d3..5cd0d9246 100644
--- a/src/libide/foundry/meson.build
+++ b/src/libide/foundry/meson.build
@@ -35,6 +35,7 @@ libide_foundry_public_headers = [
   'ide-local-device.h',
   'ide-pipeline-addin.h',
   'ide-pipeline-phase.h',
+  'ide-pipeline-stage-command.h',
   'ide-pipeline-stage-launcher.h',
   'ide-pipeline-stage-mkdirs.h',
   'ide-pipeline-stage-transfer.h',
@@ -115,6 +116,7 @@ libide_foundry_public_sources = [
   'ide-foundry-global.c',
   'ide-local-device.c',
   'ide-pipeline-addin.c',
+  'ide-pipeline-stage-command.c',
   'ide-pipeline-stage-launcher.c',
   'ide-pipeline-stage-mkdirs.c',
   'ide-pipeline-stage-transfer.c',


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