[gnome-builder/wip/gtk4-port] libide/foundry: add prepare_to_run virtual function



commit 2b203f67eaa7d1a9c401fb4c5f0543bc5735a380
Author: Christian Hergert <chergert redhat com>
Date:   Mon Jun 27 14:33:15 2022 -0700

    libide/foundry: add prepare_to_run virtual function
    
    The goal of this is to be able to remove a bunch of custom code from
    GbpShellcmdRunCommand and make it generic. This can also be used to help
    us move towards making IdeTerminalLauncher much simpler.

 src/libide/foundry/ide-run-command.c | 83 ++++++++++++++++++++++++++++++++++++
 src/libide/foundry/ide-run-command.h | 10 +++++
 2 files changed, 93 insertions(+)
---
diff --git a/src/libide/foundry/ide-run-command.c b/src/libide/foundry/ide-run-command.c
index 0ae301706..f9dc6a8b1 100644
--- a/src/libide/foundry/ide-run-command.c
+++ b/src/libide/foundry/ide-run-command.c
@@ -22,8 +22,11 @@
 
 #include "config.h"
 
+#include "ide-build-manager.h"
 #include "ide-foundry-enums.h"
+#include "ide-pipeline.h"
 #include "ide-run-command.h"
+#include "ide-run-context.h"
 
 typedef struct
 {
@@ -54,6 +57,58 @@ G_DEFINE_TYPE_WITH_PRIVATE (IdeRunCommand, ide_run_command, G_TYPE_OBJECT)
 
 static GParamSpec *properties [N_PROPS];
 
+static void
+ide_run_command_real_prepare_to_run (IdeRunCommand *self,
+                                     IdeRunContext *run_context,
+                                     IdeContext    *context)
+{
+  g_autoptr(GFile) workdir = NULL;
+  IdeBuildManager *build_manager = NULL;
+  g_auto(GStrv) environ = NULL;
+  IdePipeline *pipeline = NULL;
+  const char * const *argv;
+  const char * const *env;
+  const char *builddir;
+  const char *srcdir;
+  const char *cwd;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_RUN_COMMAND (self));
+  g_assert (IDE_IS_RUN_CONTEXT (run_context));
+  g_assert (IDE_IS_CONTEXT (context));
+
+  workdir = ide_context_ref_workdir (context);
+  srcdir = g_file_peek_path (workdir);
+  builddir = g_file_peek_path (workdir);
+
+  if (ide_context_has_project (context))
+    {
+      build_manager = ide_build_manager_from_context (context);
+      pipeline = ide_build_manager_get_pipeline (build_manager);
+      builddir = ide_pipeline_get_builddir (pipeline);
+      srcdir = ide_pipeline_get_srcdir (pipeline);
+    }
+
+  environ = g_environ_setenv (environ, "BUILDDIR", builddir, TRUE);
+  environ = g_environ_setenv (environ, "SRCDIR", srcdir, TRUE);
+  environ = g_environ_setenv (environ, "USER", g_get_user_name (), TRUE);
+  environ = g_environ_setenv (environ, "HOME", g_get_home_dir (), TRUE);
+
+  ide_run_context_push_expansion (run_context, (const char * const *)environ);
+
+  if ((cwd = ide_run_command_get_cwd (IDE_RUN_COMMAND (self))))
+    ide_run_context_set_cwd (run_context, cwd);
+
+  if ((argv = ide_run_command_get_argv (IDE_RUN_COMMAND (self))))
+    ide_run_context_append_args (run_context, argv);
+
+  if ((env = ide_run_command_get_environ (IDE_RUN_COMMAND (self))))
+    ide_run_context_add_environ (run_context, env);
+
+  IDE_EXIT;
+}
+
 static void
 ide_run_command_finalize (GObject *object)
 {
@@ -173,6 +228,8 @@ ide_run_command_class_init (IdeRunCommandClass *klass)
   object_class->get_property = ide_run_command_get_property;
   object_class->set_property = ide_run_command_set_property;
 
+  klass->prepare_to_run = ide_run_command_real_prepare_to_run;
+
   properties [PROP_ARGV] =
     g_param_spec_boxed ("argv", NULL, NULL,
                         G_TYPE_STRV,
@@ -458,3 +515,29 @@ ide_run_command_set_languages (IdeRunCommand      *self,
   priv->languages = g_strdupv ((char **)languages);
   g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_LANGUAGES]);
 }
+
+/**
+ * ide_run_command_prepare_to_run:
+ * @self: a #IdeRunCommand
+ * @run_context: an #IdeRunContext
+ * @context: an #IdeContext
+ *
+ * Prepares the run command to be run within @run_context.
+ *
+ * This requires that the run command add anything necessary to the
+ * @run_context so that the command can be run.
+ *
+ * Subclasses may override this to implement custom functionality such as
+ * locality-based execution (see shellcmd plugin).
+ */
+void
+ide_run_command_prepare_to_run (IdeRunCommand *self,
+                                IdeRunContext *run_context,
+                                IdeContext    *context)
+{
+  g_return_if_fail (IDE_IS_RUN_COMMAND (self));
+  g_return_if_fail (IDE_IS_RUN_CONTEXT (run_context));
+  g_return_if_fail (IDE_IS_CONTEXT (context));
+
+  IDE_RUN_COMMAND_GET_CLASS (self)->prepare_to_run (self, run_context, context);
+}
diff --git a/src/libide/foundry/ide-run-command.h b/src/libide/foundry/ide-run-command.h
index 8e711be14..a516dc41f 100644
--- a/src/libide/foundry/ide-run-command.h
+++ b/src/libide/foundry/ide-run-command.h
@@ -26,6 +26,8 @@
 
 #include <libide-core.h>
 
+#include "ide-foundry-types.h"
+
 G_BEGIN_DECLS
 
 #define IDE_TYPE_RUN_COMMAND (ide_run_command_get_type())
@@ -46,6 +48,10 @@ G_DECLARE_DERIVABLE_TYPE (IdeRunCommand, ide_run_command, IDE, RUN_COMMAND, GObj
 struct _IdeRunCommandClass
 {
   GObjectClass parent_class;
+
+  void (*prepare_to_run) (IdeRunCommand *self,
+                          IdeRunContext *run_context,
+                          IdeContext    *context);
 };
 
 IDE_AVAILABLE_IN_ALL
@@ -90,5 +96,9 @@ const char * const *ide_run_command_get_languages    (IdeRunCommand      *self);
 IDE_AVAILABLE_IN_ALL
 void                ide_run_command_set_languages    (IdeRunCommand      *self,
                                                       const char * const *languages);
+IDE_AVAILABLE_IN_ALL
+void                ide_run_command_prepare_to_run   (IdeRunCommand      *self,
+                                                      IdeRunContext      *run_context,
+                                                      IdeContext         *context);
 
 G_END_DECLS


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