[gnome-builder/wip/gtk4-port: 1595/1774] libide/foundry: give access to pipeline variables




commit 8e8bf302acbbce5b0ffb94a359d15f321ac3bb53
Author: Christian Hergert <chergert redhat com>
Date:   Tue Jun 21 23:06:06 2022 -0700

    libide/foundry: give access to pipeline variables
    
    And we need these at the root of the context so that we can expand them
    for CWD by the run context.
    
    We might want to do this at each stage of the argv processing as well.

 src/libide/foundry/ide-run-context.c | 38 +++++++++++++++++++++++++++++++++++-
 src/libide/foundry/ide-run-manager.c | 11 +++++++++++
 2 files changed, 48 insertions(+), 1 deletion(-)
---
diff --git a/src/libide/foundry/ide-run-context.c b/src/libide/foundry/ide-run-context.c
index fe9bd4944..1782b0d61 100644
--- a/src/libide/foundry/ide-run-context.c
+++ b/src/libide/foundry/ide-run-context.c
@@ -629,6 +629,39 @@ ide_run_context_callback_layer (IdeRunContext       *self,
   return ret;
 }
 
+static char *
+ide_run_context_expand_cwd (IdeRunContext *self)
+{
+  g_autoptr(GString) str = NULL;
+  const char *envval;
+  const char *cwd;
+
+  g_assert (IDE_IS_RUN_CONTEXT (self));
+
+  if (!(cwd = ide_run_context_get_cwd (self)))
+    return NULL;
+
+  str = g_string_new (cwd);
+
+  if ((envval = ide_run_context_getenv (self, "BUILDDIR")))
+    g_string_replace (str, "$BUILDDIR", envval, 0);
+
+  if ((envval = ide_run_context_getenv (self, "SRCDIR")))
+    g_string_replace (str, "$SRCDIR", envval, 0);
+
+  if ((envval = ide_run_context_getenv (self, "HOME")))
+    g_string_replace (str, "$HOME", envval, 0);
+  else
+    g_string_replace (str, "$HOME", g_get_home_dir (), 0);
+
+  if ((envval = ide_run_context_getenv (self, "USER")))
+    g_string_replace (str, "$USER", envval, 0);
+  else
+    g_string_replace (str, "$USER", g_get_user_name (), 0);
+
+  return ide_path_expand (str->str);
+}
+
 /**
  * ide_run_context_end:
  * @self: a #IdeRunContext
@@ -641,6 +674,7 @@ ide_run_context_end (IdeRunContext  *self,
                      GError        **error)
 {
   g_autoptr(IdeSubprocessLauncher) launcher = NULL;
+  g_autofree char *cwd = NULL;
 
   g_return_val_if_fail (IDE_IS_RUN_CONTEXT (self), NULL);
   g_return_val_if_fail (self->ended == FALSE, NULL);
@@ -657,11 +691,13 @@ ide_run_context_end (IdeRunContext  *self,
         return FALSE;
     }
 
+  cwd = ide_run_context_expand_cwd (self);
+
   launcher = ide_subprocess_launcher_new (0);
 
   ide_subprocess_launcher_set_argv (launcher, ide_run_context_get_argv (self));
   ide_subprocess_launcher_set_environ (launcher, ide_run_context_get_environ (self));
-  ide_subprocess_launcher_set_cwd (launcher, ide_run_context_get_cwd (self));
+  ide_subprocess_launcher_set_cwd (launcher, cwd);
   ide_subprocess_launcher_set_run_on_host (launcher, self->run_on_host);
 
   return g_steal_pointer (&launcher);
diff --git a/src/libide/foundry/ide-run-manager.c b/src/libide/foundry/ide-run-manager.c
index 1dc59d5b3..d9de2859a 100644
--- a/src/libide/foundry/ide-run-manager.c
+++ b/src/libide/foundry/ide-run-manager.c
@@ -514,6 +514,16 @@ setup_basic_environment (IdeRunContext *run_context)
     }
 }
 
+static void
+setup_pipeline_environment (IdeRunContext *run_context,
+                            IdePipeline   *pipeline)
+{
+  ide_run_context_setenv (run_context, "BUILDDIR", ide_pipeline_get_builddir (pipeline));
+  ide_run_context_setenv (run_context, "SRCDIR", ide_pipeline_get_srcdir (pipeline));
+  ide_run_context_setenv (run_context, "HOME", g_get_home_dir ());
+  ide_run_context_setenv (run_context, "USER", g_get_user_name ());
+}
+
 static void
 apply_messages_debug (IdeRunContext *run_context,
                       gboolean       messages_debug_all)
@@ -880,6 +890,7 @@ ide_run_manager_run_deploy_cb (GObject      *object,
 
   /* Setup the run context */
   run_context = ide_run_context_new ();
+  setup_pipeline_environment (run_context, pipeline);
   ide_deploy_strategy_prepare_run_context (deploy_strategy, pipeline, run_context);
   ide_run_manager_prepare_run_context (self, run_context, self->current_run_command);
 


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