[gnome-builder] runtime: add IdeRuntime, IdeRuntimeManager, IdeRuntimeProvider



commit b4b42baed8e1941ea85051153e1252107566df03
Author: Christian Hergert <chergert redhat com>
Date:   Sun Feb 14 20:44:11 2016 -0800

    runtime: add IdeRuntime, IdeRuntimeManager, IdeRuntimeProvider
    
    Add IdeRuntime, which contains the necessary machinery to perform
    commands inside of a runtime. Such examples of a runtime might be
    jhbuild, xdg-app, or the host system.

 libide/ide-context.c          |   62 ++++++
 libide/ide-context.h          |    1 +
 libide/ide-internal.h         |    1 +
 libide/ide-runtime-manager.c  |  239 ++++++++++++++++++++++++
 libide/ide-runtime-manager.h  |   39 ++++
 libide/ide-runtime-provider.c |   61 ++++++
 libide/ide-runtime-provider.h |   49 +++++
 libide/ide-runtime.c          |  408 +++++++++++++++++++++++++++++++++++++++++
 libide/ide-runtime.h          |   93 ++++++++++
 libide/ide-types.h            |    4 +
 10 files changed, 957 insertions(+), 0 deletions(-)
---
diff --git a/libide/ide-context.c b/libide/ide-context.c
index b6c5edd..63e976e 100644
--- a/libide/ide-context.c
+++ b/libide/ide-context.c
@@ -36,6 +36,7 @@
 #include "ide-project.h"
 #include "ide-project-item.h"
 #include "ide-project-files.h"
+#include "ide-runtime-manager.h"
 #include "ide-script-manager.h"
 #include "ide-search-engine.h"
 #include "ide-search-provider.h"
@@ -62,6 +63,7 @@ struct _IdeContext
   IdeDeviceManager         *device_manager;
   IdeDoap                  *doap;
   GtkRecentManager         *recent_manager;
+  IdeRuntimeManager        *runtime_manager;
   IdeScriptManager         *script_manager;
   IdeSearchEngine          *search_engine;
   IdeSourceSnippetsManager *snippets_manager;
@@ -98,6 +100,7 @@ enum {
   PROP_PROJECT_FILE,
   PROP_PROJECT,
   PROP_ROOT_BUILD_DIR,
+  PROP_RUNTIME_MANAGER,
   PROP_SCRIPT_MANAGER,
   PROP_SEARCH_ENGINE,
   PROP_SNIPPETS_MANAGER,
@@ -540,6 +543,7 @@ ide_context_finalize (GObject *object)
   g_clear_object (&self->project);
   g_clear_object (&self->project_file);
   g_clear_object (&self->recent_manager);
+  g_clear_object (&self->runtime_manager);
   g_clear_object (&self->unsaved_files);
   g_clear_object (&self->vcs);
 
@@ -594,6 +598,10 @@ ide_context_get_property (GObject    *object,
       g_value_set_string (value, ide_context_get_root_build_dir (self));
       break;
 
+    case PROP_RUNTIME_MANAGER:
+      g_value_set_object (value, ide_context_get_runtime_manager (self));
+      break;
+
     case PROP_SCRIPT_MANAGER:
       g_value_set_object (value, ide_context_get_script_manager (self));
       break;
@@ -710,6 +718,13 @@ ide_context_class_init (IdeContextClass *klass)
                          NULL,
                          (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+  properties [PROP_RUNTIME_MANAGER] =
+    g_param_spec_object ("runtime-manager",
+                         "Runtime Manager",
+                         "Runtime Manager",
+                         IDE_TYPE_RUNTIME_MANAGER,
+                         (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+
   properties [PROP_SCRIPT_MANAGER] =
     g_param_spec_object ("script-manager",
                          "Script Manager",
@@ -805,6 +820,10 @@ ide_context_init (IdeContext *self)
                                 "context", self,
                                 NULL);
 
+  self->runtime_manager = g_object_new (IDE_TYPE_RUNTIME_MANAGER,
+                                        "context", self,
+                                        NULL);
+
   self->unsaved_files = g_object_new (IDE_TYPE_UNSAVED_FILES,
                                       "context", self,
                                       NULL);
@@ -1721,6 +1740,7 @@ ide_context_do_unload_locked (IdeContext *self)
   self->delayed_unload_task = NULL;
 
   g_clear_object (&self->device_manager);
+  g_clear_object (&self->runtime_manager);
 
   ide_async_helper_run (self,
                         g_task_get_cancellable (task),
@@ -2020,3 +2040,45 @@ ide_context_release (IdeContext *self)
 
   g_object_unref (self);
 }
+
+/**
+ * ide_context_get_runtime_manager:
+ * @self: An #IdeContext
+ *
+ * Gets the #IdeRuntimeManager for the LibIDE context.
+ *
+ * The runtime manager provies access to #IdeRuntime instances via the
+ * #GListModel interface. These can provide support for building projects
+ * in various runtimes such as xdg-app.
+ *
+ * Returns: (transfer none): An #IdeRuntimeManager.
+ */
+IdeRuntimeManager *
+ide_context_get_runtime_manager (IdeContext *self)
+{
+  g_return_val_if_fail (IDE_IS_CONTEXT (self), NULL);
+
+  return self->runtime_manager;
+}
+
+/**
+ * ide_context_get_configuration_manager:
+ * @self: An #IdeContext
+ *
+ * Gets the #IdeConfigurationManager for the context.
+ *
+ * The configuration manager is responsible for loading and saving
+ * configurations. Configurations consist of information about how to
+ * perform a particular build. Such information includes the target
+ * #IdeDevice, the #IdeRuntime to use, and various other build options.
+ *
+ * Returns: (transfer none): An #IdeConfigurationManager.
+ */
+IdeConfigurationManager *
+ide_context_get_configuration_manager (IdeContext *self)
+{
+  g_return_val_if_fail (IDE_IS_CONTEXT (self), NULL);
+
+  return self->configuration_manager;
+}
+
diff --git a/libide/ide-context.h b/libide/ide-context.h
index 0742e48..f49525b 100644
--- a/libide/ide-context.h
+++ b/libide/ide-context.h
@@ -75,6 +75,7 @@ void                      ide_context_hold_for_object       (IdeContext
                                                              gpointer              instance);
 void                      ide_context_release               (IdeContext           *self);
 IdeConfigurationManager  *ide_context_get_configuration_manager (IdeContext           *self);
+IdeRuntimeManager        *ide_context_get_runtime_manager       (IdeContext           *self);
 
 G_END_DECLS
 
diff --git a/libide/ide-internal.h b/libide/ide-internal.h
index c9c5c9b..c040fde 100644
--- a/libide/ide-internal.h
+++ b/libide/ide-internal.h
@@ -59,6 +59,7 @@ IdeFixit           *_ide_fixit_new                          (IdeSourceRange
                                                              const gchar           *replacement_text);
 void                _ide_project_set_name                   (IdeProject            *project,
                                                              const gchar           *name);
+void                _ide_runtime_manager_unload             (IdeRuntimeManager     *self);
 void                _ide_search_context_add_provider        (IdeSearchContext      *context,
                                                              IdeSearchProvider     *provider,
                                                              gsize                  max_results);
diff --git a/libide/ide-runtime-manager.c b/libide/ide-runtime-manager.c
new file mode 100644
index 0000000..6f76442
--- /dev/null
+++ b/libide/ide-runtime-manager.c
@@ -0,0 +1,239 @@
+/* ide-runtime-manager.c
+ *
+ * Copyright (C) 2016 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/>.
+ */
+
+#define G_LOG_DOMAIN "ide-runtime-manager"
+
+#include <glib/gi18n.h>
+#include <libpeas/peas.h>
+
+#include "ide-runtime.h"
+#include "ide-runtime-manager.h"
+#include "ide-runtime-provider.h"
+
+struct _IdeRuntimeManager
+{
+  IdeObject         parent_instance;
+  PeasExtensionSet *extensions;
+  GPtrArray        *runtimes;
+};
+
+static void list_model_iface_init (GListModelInterface *iface);
+
+G_DEFINE_TYPE_EXTENDED (IdeRuntimeManager, ide_runtime_manager, IDE_TYPE_OBJECT, 0,
+                        G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
+
+static void
+ide_runtime_manager_extension_added (PeasExtensionSet *set,
+                                     PeasPluginInfo   *plugin_info,
+                                     PeasExtension    *exten,
+                                     gpointer          user_data)
+{
+  IdeRuntimeManager *self = user_data;
+  IdeRuntimeProvider *provider = (IdeRuntimeProvider *)exten;
+
+  g_assert (PEAS_IS_EXTENSION_SET (set));
+  g_assert (plugin_info != NULL);
+  g_assert (IDE_IS_RUNTIME_PROVIDER (provider));
+
+  ide_runtime_provider_load (provider, self);
+}
+
+static void
+ide_runtime_manager_extension_removed (PeasExtensionSet *set,
+                                       PeasPluginInfo   *plugin_info,
+                                       PeasExtension    *exten,
+                                       gpointer          user_data)
+{
+  IdeRuntimeManager *self = user_data;
+  IdeRuntimeProvider *provider = (IdeRuntimeProvider *)exten;
+
+  g_assert (PEAS_IS_EXTENSION_SET (set));
+  g_assert (plugin_info != NULL);
+  g_assert (IDE_IS_RUNTIME_PROVIDER (provider));
+
+  ide_runtime_provider_unload (provider, self);
+}
+
+static void
+ide_runtime_manager_constructed (GObject *object)
+{
+  IdeRuntimeManager *self = (IdeRuntimeManager *)object;
+  IdeContext *context;
+
+  G_OBJECT_CLASS (ide_runtime_manager_parent_class)->constructed (object);
+
+  context = ide_object_get_context (IDE_OBJECT (self));
+
+  self->extensions = peas_extension_set_new (peas_engine_get_default (),
+                                             IDE_TYPE_RUNTIME_PROVIDER,
+                                             NULL);
+
+  g_signal_connect (self->extensions,
+                    "extension-added",
+                    G_CALLBACK (ide_runtime_manager_extension_added),
+                    self);
+
+  g_signal_connect (self->extensions,
+                    "extension-removed",
+                    G_CALLBACK (ide_runtime_manager_extension_removed),
+                    self);
+
+  peas_extension_set_foreach (self->extensions,
+                              ide_runtime_manager_extension_added,
+                              self);
+
+  ide_runtime_manager_add (self, ide_runtime_new (context, "host", _("Host operating system")));
+}
+
+void
+_ide_runtime_manager_unload (IdeRuntimeManager *self)
+{
+  g_return_if_fail (IDE_IS_RUNTIME_MANAGER (self));
+
+  g_clear_object (&self->extensions);
+}
+
+static void
+ide_runtime_manager_dispose (GObject *object)
+{
+  IdeRuntimeManager *self = (IdeRuntimeManager *)object;
+
+  g_clear_object (&self->extensions);
+  g_clear_pointer (&self->runtimes, g_ptr_array_unref);
+
+  G_OBJECT_CLASS (ide_runtime_manager_parent_class)->dispose (object);
+}
+
+static void
+ide_runtime_manager_class_init (IdeRuntimeManagerClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->constructed = ide_runtime_manager_constructed;
+  object_class->dispose = ide_runtime_manager_dispose;
+}
+
+static void
+ide_runtime_manager_init (IdeRuntimeManager *self)
+{
+  self->runtimes = g_ptr_array_new_with_free_func (g_object_unref);
+}
+
+static GType
+ide_runtime_manager_get_item_type (GListModel *model)
+{
+  return IDE_TYPE_RUNTIME;
+}
+
+static guint
+ide_runtime_manager_get_n_items (GListModel *model)
+{
+  IdeRuntimeManager *self = (IdeRuntimeManager *)model;
+
+  g_return_val_if_fail (IDE_IS_RUNTIME_MANAGER (self), 0);
+
+  return self->runtimes->len;
+}
+
+static gpointer
+ide_runtime_manager_get_item (GListModel *model,
+                              guint       position)
+{
+  IdeRuntimeManager *self = (IdeRuntimeManager *)model;
+
+  g_return_val_if_fail (IDE_IS_RUNTIME_MANAGER (self), NULL);
+  g_return_val_if_fail (position < self->runtimes->len, NULL);
+
+  return g_object_ref (g_ptr_array_index (self->runtimes, position));
+}
+
+static void
+list_model_iface_init (GListModelInterface *iface)
+{
+  iface->get_item_type = ide_runtime_manager_get_item_type;
+  iface->get_n_items = ide_runtime_manager_get_n_items;
+  iface->get_item = ide_runtime_manager_get_item;
+}
+
+void
+ide_runtime_manager_add (IdeRuntimeManager *self,
+                         IdeRuntime        *runtime)
+{
+  guint idx;
+
+  g_return_if_fail (IDE_IS_RUNTIME_MANAGER (self));
+  g_return_if_fail (IDE_IS_RUNTIME (runtime));
+
+  idx = self->runtimes->len;
+  g_ptr_array_add (self->runtimes, g_object_ref (runtime));
+  g_list_model_items_changed (G_LIST_MODEL (self), idx, 0, 1);
+}
+
+void
+ide_runtime_manager_remove (IdeRuntimeManager *self,
+                            IdeRuntime        *runtime)
+{
+  guint i;
+
+  g_return_if_fail (IDE_IS_RUNTIME_MANAGER (self));
+  g_return_if_fail (IDE_IS_RUNTIME (runtime));
+
+  for (i = 0; i < self->runtimes->len; i++)
+    {
+      IdeRuntime *item = g_ptr_array_index (self->runtimes, i);
+
+      if (runtime == item)
+        {
+          g_ptr_array_remove_index (self->runtimes, i);
+          g_list_model_items_changed (G_LIST_MODEL (self), i, 1, 0);
+          break;
+        }
+    }
+}
+
+/**
+ * ide_runtime_manager_get_runtime:
+ * @self: An #IdeRuntimeManager
+ * @id: the identifier of the runtime
+ *
+ * Gets the runtime by it's internal identifier.
+ *
+ * Returns: (transfer none): An #IdeRuntime.
+ */
+IdeRuntime *
+ide_runtime_manager_get_runtime (IdeRuntimeManager *self,
+                                 const gchar       *id)
+{
+  guint i;
+
+  g_return_val_if_fail (IDE_IS_RUNTIME_MANAGER (self), NULL);
+  g_return_val_if_fail (id != NULL, NULL);
+
+  for (i = 0; i < self->runtimes->len; i++)
+    {
+      IdeRuntime *runtime = g_ptr_array_index (self->runtimes, i);
+      const gchar *runtime_id;
+
+      runtime_id = ide_runtime_get_id (runtime);
+
+      if (g_strcmp0 (runtime_id, id) == 0)
+        return runtime;
+    }
+
+  return NULL;
+}
diff --git a/libide/ide-runtime-manager.h b/libide/ide-runtime-manager.h
new file mode 100644
index 0000000..f7fd591
--- /dev/null
+++ b/libide/ide-runtime-manager.h
@@ -0,0 +1,39 @@
+/* ide-runtime-manager.h
+ *
+ * Copyright (C) 2016 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/>.
+ */
+
+#ifndef IDE_RUNTIME_MANAGER_H
+#define IDE_RUNTIME_MANAGER_H
+
+#include "ide-object.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_RUNTIME_MANAGER (ide_runtime_manager_get_type())
+
+G_DECLARE_FINAL_TYPE (IdeRuntimeManager, ide_runtime_manager, IDE, RUNTIME_MANAGER, IdeObject)
+
+IdeRuntime *ide_runtime_manager_get_runtime (IdeRuntimeManager *self,
+                                             const gchar       *id);
+void        ide_runtime_manager_add         (IdeRuntimeManager *self,
+                                             IdeRuntime        *runtime);
+void        ide_runtime_manager_remove      (IdeRuntimeManager *self,
+                                             IdeRuntime        *runtime);
+
+G_END_DECLS
+
+#endif /* IDE_RUNTIME_MANAGER_H */
diff --git a/libide/ide-runtime-provider.c b/libide/ide-runtime-provider.c
new file mode 100644
index 0000000..41483e6
--- /dev/null
+++ b/libide/ide-runtime-provider.c
@@ -0,0 +1,61 @@
+/* ide-runtime-provider.c
+ *
+ * Copyright (C) 2016 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/>.
+ */
+
+#include "ide-runtime-manager.h"
+#include "ide-runtime-provider.h"
+
+G_DEFINE_INTERFACE (IdeRuntimeProvider, ide_runtime_provider, G_TYPE_OBJECT)
+
+static void
+ide_runtime_provider_real_load (IdeRuntimeProvider *self,
+                                IdeRuntimeManager  *manager)
+{
+}
+
+static void
+ide_runtime_provider_real_unload (IdeRuntimeProvider *self,
+                                  IdeRuntimeManager  *manager)
+{
+}
+
+static void
+ide_runtime_provider_default_init (IdeRuntimeProviderInterface *iface)
+{
+  iface->load = ide_runtime_provider_real_load;
+  iface->unload = ide_runtime_provider_real_unload;
+}
+
+void
+ide_runtime_provider_load (IdeRuntimeProvider *self,
+                           IdeRuntimeManager  *manager)
+{
+  g_return_if_fail (IDE_IS_RUNTIME_PROVIDER (self));
+  g_return_if_fail (IDE_IS_RUNTIME_MANAGER (manager));
+
+  IDE_RUNTIME_PROVIDER_GET_IFACE (self)->load (self, manager);
+}
+
+void
+ide_runtime_provider_unload (IdeRuntimeProvider *self,
+                             IdeRuntimeManager  *manager)
+{
+  g_return_if_fail (IDE_IS_RUNTIME_PROVIDER (self));
+  g_return_if_fail (IDE_IS_RUNTIME_MANAGER (manager));
+
+  IDE_RUNTIME_PROVIDER_GET_IFACE (self)->unload (self, manager);
+}
diff --git a/libide/ide-runtime-provider.h b/libide/ide-runtime-provider.h
new file mode 100644
index 0000000..15098d9
--- /dev/null
+++ b/libide/ide-runtime-provider.h
@@ -0,0 +1,49 @@
+/* ide-runtime-provider.h
+ *
+ * Copyright (C) 2016 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/>.
+ */
+
+#ifndef IDE_RUNTIME_PROVIDER_H
+#define IDE_RUNTIME_PROVIDER_H
+
+#include <gio/gio.h>
+
+#include "ide-types.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_RUNTIME_PROVIDER (ide_runtime_provider_get_type ())
+
+G_DECLARE_INTERFACE (IdeRuntimeProvider, ide_runtime_provider, IDE, RUNTIME_PROVIDER, GObject)
+
+struct _IdeRuntimeProviderInterface
+{
+  GTypeInterface parent;
+
+  void   (*load)         (IdeRuntimeProvider *self,
+                          IdeRuntimeManager  *manager);
+  void   (*unload)       (IdeRuntimeProvider *self,
+                          IdeRuntimeManager  *manager);
+};
+
+void ide_runtime_provider_load   (IdeRuntimeProvider *self,
+                                  IdeRuntimeManager  *manager);
+void ide_runtime_provider_unload (IdeRuntimeProvider *self,
+                                  IdeRuntimeManager  *manager);
+
+G_END_DECLS
+
+#endif /* IDE_RUNTIME_PROVIDER_H */
diff --git a/libide/ide-runtime.c b/libide/ide-runtime.c
new file mode 100644
index 0000000..4bde63c
--- /dev/null
+++ b/libide/ide-runtime.c
@@ -0,0 +1,408 @@
+/* ide-runtime.c
+ *
+ * Copyright (C) 2016 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/>.
+ */
+
+#define G_LOG_DOMAIN "ide-runtime"
+
+#include "ide-builder.h"
+#include "ide-configuration.h"
+#include "ide-context.h"
+#include "ide-project.h"
+#include "ide-runtime.h"
+
+typedef struct
+{
+  gchar *id;
+  gchar *display_name;
+} IdeRuntimePrivate;
+
+G_DEFINE_TYPE_WITH_PRIVATE (IdeRuntime, ide_runtime, IDE_TYPE_OBJECT)
+
+enum {
+  PROP_0,
+  PROP_ID,
+  PROP_DISPLAY_NAME,
+  N_PROPS
+};
+
+static GParamSpec *properties [N_PROPS];
+
+static void
+ide_runtime_real_prebuild_async (IdeRuntime          *self,
+                                 GCancellable        *cancellable,
+                                 GAsyncReadyCallback  callback,
+                                 gpointer             user_data)
+{
+  g_autoptr(GTask) task = NULL;
+
+  g_assert (IDE_IS_RUNTIME (self));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_return_boolean (task, TRUE);
+}
+
+static gboolean
+ide_runtime_real_prebuild_finish (IdeRuntime    *self,
+                                  GAsyncResult  *result,
+                                  GError       **error)
+{
+  g_assert (IDE_IS_RUNTIME (self));
+  g_assert (G_IS_TASK (result));
+
+  return g_task_propagate_boolean (G_TASK (result), error);
+}
+
+static void
+ide_runtime_real_postbuild_async (IdeRuntime          *self,
+                                  GCancellable        *cancellable,
+                                  GAsyncReadyCallback  callback,
+                                  gpointer             user_data)
+{
+  g_autoptr(GTask) task = NULL;
+
+  g_assert (IDE_IS_RUNTIME (self));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_return_boolean (task, TRUE);
+}
+
+static gboolean
+ide_runtime_real_postbuild_finish (IdeRuntime    *self,
+                                   GAsyncResult  *result,
+                                   GError       **error)
+{
+  g_assert (IDE_IS_RUNTIME (self));
+  g_assert (G_IS_TASK (result));
+
+  return g_task_propagate_boolean (G_TASK (result), error);
+}
+
+static IdeSubprocessLauncher *
+ide_runtime_real_create_launcher (IdeRuntime  *self,
+                                  GError     **error)
+{
+  IdeSubprocessLauncher *ret;
+  g_auto(GStrv) env = NULL;
+
+  g_assert (IDE_IS_RUNTIME (self));
+
+  env = g_get_environ ();
+
+  ret = ide_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE | G_SUBPROCESS_FLAGS_STDERR_PIPE);
+  ide_subprocess_launcher_set_environ (ret, (const gchar * const *)env);
+
+  return ret;
+}
+
+static gboolean
+ide_runtime_real_contains_program_in_path (IdeRuntime   *self,
+                                           const gchar  *program,
+                                           GCancellable *cancellable)
+{
+  gchar *path;
+  gboolean ret;
+
+  g_assert (IDE_IS_RUNTIME (self));
+  g_assert (program != NULL);
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  path = g_find_program_in_path (program);
+  ret = path != NULL;
+  g_free (path);
+
+  return ret;
+}
+
+gboolean
+ide_runtime_contains_program_in_path (IdeRuntime   *self,
+                                      const gchar  *program,
+                                      GCancellable *cancellable)
+{
+  g_return_val_if_fail (IDE_IS_RUNTIME (self), FALSE);
+  g_return_val_if_fail (program != NULL, FALSE);
+  g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);
+
+  return IDE_RUNTIME_GET_CLASS (self)->contains_program_in_path (self, program, cancellable);
+}
+
+static void
+ide_runtime_real_prepare_configuration (IdeRuntime       *self,
+                                        IdeConfiguration *configuration)
+{
+  IdeRuntimePrivate *priv = ide_runtime_get_instance_private (self);
+  g_autofree gchar *install_path = NULL;
+  IdeContext *context;
+  IdeProject *project;
+  const gchar *project_name;
+
+  g_assert (IDE_IS_RUNTIME (self));
+  g_assert (IDE_IS_CONFIGURATION (configuration));
+
+  context = ide_object_get_context (IDE_OBJECT (self));
+  project = ide_context_get_project (context);
+  project_name = ide_project_get_name (project);
+
+  install_path = g_build_filename (g_get_user_cache_dir (),
+                                   "gnome-builder",
+                                   "install",
+                                   project_name,
+                                   priv->id,
+                                   NULL);
+
+  ide_configuration_set_prefix (configuration, install_path);
+}
+
+static void
+ide_runtime_finalize (GObject *object)
+{
+  IdeRuntime *self = (IdeRuntime *)object;
+  IdeRuntimePrivate *priv = ide_runtime_get_instance_private (self);
+
+  g_clear_pointer (&priv->display_name, g_free);
+
+  G_OBJECT_CLASS (ide_runtime_parent_class)->finalize (object);
+}
+
+static void
+ide_runtime_get_property (GObject    *object,
+                          guint       prop_id,
+                          GValue     *value,
+                          GParamSpec *pspec)
+{
+  IdeRuntime *self = IDE_RUNTIME (object);
+
+  switch (prop_id)
+    {
+    case PROP_ID:
+      g_value_set_string (value, ide_runtime_get_id (self));
+      break;
+
+    case PROP_DISPLAY_NAME:
+      g_value_set_string (value, ide_runtime_get_display_name (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_runtime_set_property (GObject      *object,
+                          guint         prop_id,
+                          const GValue *value,
+                          GParamSpec   *pspec)
+{
+  IdeRuntime *self = IDE_RUNTIME (object);
+
+  switch (prop_id)
+    {
+    case PROP_ID:
+      ide_runtime_set_id (self, g_value_get_string (value));
+      break;
+
+    case PROP_DISPLAY_NAME:
+      ide_runtime_set_display_name (self, g_value_get_string (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_runtime_class_init (IdeRuntimeClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = ide_runtime_finalize;
+  object_class->get_property = ide_runtime_get_property;
+  object_class->set_property = ide_runtime_set_property;
+
+  klass->prebuild_async = ide_runtime_real_prebuild_async;
+  klass->prebuild_finish = ide_runtime_real_prebuild_finish;
+  klass->postbuild_async = ide_runtime_real_postbuild_async;
+  klass->postbuild_finish = ide_runtime_real_postbuild_finish;
+  klass->create_launcher = ide_runtime_real_create_launcher;
+  klass->contains_program_in_path = ide_runtime_real_contains_program_in_path;
+  klass->prepare_configuration = ide_runtime_real_prepare_configuration;
+
+  properties [PROP_ID] =
+    g_param_spec_string ("id",
+                         "Id",
+                         "The runtime identifier",
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
+
+  properties [PROP_DISPLAY_NAME] =
+    g_param_spec_string ("display-name",
+                         "Display Name",
+                         "Display Name",
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+ide_runtime_init (IdeRuntime *self)
+{
+}
+
+const gchar *
+ide_runtime_get_id (IdeRuntime  *self)
+{
+  IdeRuntimePrivate *priv = ide_runtime_get_instance_private (self);
+
+  g_return_val_if_fail (IDE_IS_RUNTIME (self), NULL);
+
+  return priv->id;
+}
+
+void
+ide_runtime_set_id (IdeRuntime  *self,
+                    const gchar *id)
+{
+  IdeRuntimePrivate *priv = ide_runtime_get_instance_private (self);
+
+  g_return_if_fail (IDE_IS_RUNTIME (self));
+  g_return_if_fail (id != NULL);
+
+  if (0 != g_strcmp0 (id, priv->id))
+    {
+      g_free (priv->id);
+      priv->id = g_strdup (id);
+      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ID]);
+    }
+}
+
+const gchar *
+ide_runtime_get_display_name (IdeRuntime *self)
+{
+  IdeRuntimePrivate *priv = ide_runtime_get_instance_private (self);
+
+  g_return_val_if_fail (IDE_IS_RUNTIME (self), NULL);
+
+  return priv->display_name;
+}
+
+void
+ide_runtime_set_display_name (IdeRuntime  *self,
+                              const gchar *display_name)
+{
+  IdeRuntimePrivate *priv = ide_runtime_get_instance_private (self);
+
+  g_return_if_fail (IDE_IS_RUNTIME (self));
+  g_return_if_fail (display_name != NULL);
+
+  if (g_strcmp0 (display_name, priv->display_name) != 0)
+    {
+      g_free (priv->display_name);
+      priv->display_name = g_strdup (display_name);
+      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_DISPLAY_NAME]);
+    }
+}
+
+IdeRuntime *
+ide_runtime_new (IdeContext  *context,
+                 const gchar *id,
+                 const gchar *display_name)
+{
+  return g_object_new (IDE_TYPE_RUNTIME,
+                       "context", context,
+                       "id", id,
+                       "display-name", display_name,
+                       NULL);
+}
+
+void
+ide_runtime_prebuild_async (IdeRuntime          *self,
+                            GCancellable        *cancellable,
+                            GAsyncReadyCallback  callback,
+                            gpointer             user_data)
+{
+  g_return_if_fail (IDE_IS_RUNTIME (self));
+  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  IDE_RUNTIME_GET_CLASS (self)->prebuild_async (self, cancellable, callback, user_data);
+}
+
+gboolean
+ide_runtime_prebuild_finish (IdeRuntime    *self,
+                             GAsyncResult  *result,
+                             GError       **error)
+{
+  g_return_val_if_fail (IDE_IS_RUNTIME (self), FALSE);
+
+  return IDE_RUNTIME_GET_CLASS (self)->prebuild_finish (self, result, error);
+}
+
+void
+ide_runtime_postbuild_async (IdeRuntime          *self,
+                             GCancellable        *cancellable,
+                             GAsyncReadyCallback  callback,
+                             gpointer             user_data)
+{
+  g_return_if_fail (IDE_IS_RUNTIME (self));
+  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  IDE_RUNTIME_GET_CLASS (self)->postbuild_async (self, cancellable, callback, user_data);
+}
+
+gboolean
+ide_runtime_postbuild_finish (IdeRuntime    *self,
+                              GAsyncResult  *result,
+                              GError       **error)
+{
+  g_return_val_if_fail (IDE_IS_RUNTIME (self), FALSE);
+
+  return IDE_RUNTIME_GET_CLASS (self)->postbuild_finish (self, result, error);
+}
+
+/**
+ * ide_runtime_create_launcher:
+ *
+ * Creates a launcher for the runtime.
+ *
+ * This can be used to execute a command within a runtime.
+ * If you are doing a build, you probably want to ensure you call
+ * ide_runtime_prebuild_async() before using the launcher.
+ *
+ * It is important that this function can be run from a thread without
+ * side effects.
+ *
+ * Returns: (transfer full): An #IdeSubprocessLauncher or %NULL upon failure.
+ */
+IdeSubprocessLauncher *
+ide_runtime_create_launcher (IdeRuntime  *self,
+                             GError     **error)
+{
+  g_return_val_if_fail (IDE_IS_RUNTIME (self), NULL);
+
+  return IDE_RUNTIME_GET_CLASS (self)->create_launcher (self, error);
+}
+
+void
+ide_runtime_prepare_configuration (IdeRuntime       *self,
+                                   IdeConfiguration *configuration)
+{
+  g_return_if_fail (IDE_IS_RUNTIME (self));
+  g_return_if_fail (IDE_IS_CONFIGURATION (configuration));
+
+  IDE_RUNTIME_GET_CLASS (self)->prepare_configuration (self, configuration);
+}
diff --git a/libide/ide-runtime.h b/libide/ide-runtime.h
new file mode 100644
index 0000000..c5aa19e
--- /dev/null
+++ b/libide/ide-runtime.h
@@ -0,0 +1,93 @@
+/* ide-runtime.h
+ *
+ * Copyright (C) 2016 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/>.
+ */
+
+#ifndef IDE_RUNTIME_H
+#define IDE_RUNTIME_H
+
+#include <gio/gio.h>
+
+#include "ide-object.h"
+#include "ide-subprocess-launcher.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_RUNTIME (ide_runtime_get_type())
+
+G_DECLARE_DERIVABLE_TYPE (IdeRuntime, ide_runtime, IDE, RUNTIME, IdeObject)
+
+struct _IdeRuntimeClass
+{
+  IdeObjectClass parent;
+
+  void                   (*prebuild_async)           (IdeRuntime           *self,
+                                                      GCancellable         *cancellable,
+                                                      GAsyncReadyCallback   callback,
+                                                      gpointer              user_data);
+  gboolean               (*prebuild_finish)          (IdeRuntime           *self,
+                                                      GAsyncResult         *result,
+                                                      GError              **error);
+  void                   (*postbuild_async)          (IdeRuntime           *self,
+                                                      GCancellable         *cancellable,
+                                                      GAsyncReadyCallback   callback,
+                                                      gpointer              user_data);
+  gboolean               (*postbuild_finish)         (IdeRuntime           *self,
+                                                      GAsyncResult         *result,
+                                                      GError              **error);
+  gboolean               (*contains_program_in_path) (IdeRuntime           *self,
+                                                      const gchar          *program,
+                                                      GCancellable         *cancellable);
+  IdeSubprocessLauncher *(*create_launcher)          (IdeRuntime           *self,
+                                                      GError              **error);
+  void                   (*prepare_configuration)    (IdeRuntime           *self,
+                                                      IdeConfiguration     *configuration);
+};
+
+void                   ide_runtime_prebuild_async           (IdeRuntime           *self,
+                                                             GCancellable         *cancellable,
+                                                             GAsyncReadyCallback   callback,
+                                                             gpointer              user_data);
+gboolean               ide_runtime_prebuild_finish          (IdeRuntime           *self,
+                                                             GAsyncResult         *result,
+                                                             GError              **error);
+void                   ide_runtime_postbuild_async          (IdeRuntime           *self,
+                                                             GCancellable         *cancellable,
+                                                             GAsyncReadyCallback   callback,
+                                                             gpointer              user_data);
+gboolean               ide_runtime_postbuild_finish         (IdeRuntime           *self,
+                                                             GAsyncResult         *result,
+                                                             GError              **error);
+gboolean               ide_runtime_contains_program_in_path (IdeRuntime           *self,
+                                                             const gchar          *program,
+                                                             GCancellable         *cancellable);
+IdeSubprocessLauncher *ide_runtime_create_launcher          (IdeRuntime           *self,
+                                                             GError              **error);
+void                   ide_runtime_prepare_configuration    (IdeRuntime           *self,
+                                                             IdeConfiguration     *configuration);
+IdeRuntime            *ide_runtime_new                      (IdeContext           *context,
+                                                             const gchar          *id,
+                                                             const gchar          *title);
+const gchar           *ide_runtime_get_id                   (IdeRuntime           *self);
+void                   ide_runtime_set_id                   (IdeRuntime           *self,
+                                                             const gchar          *id);
+const gchar           *ide_runtime_get_display_name         (IdeRuntime           *self);
+void                   ide_runtime_set_display_name         (IdeRuntime           *self,
+                                                             const gchar          *display_name);
+
+G_END_DECLS
+
+#endif /* IDE_RUNTIME_H */
diff --git a/libide/ide-types.h b/libide/ide-types.h
index 43ce39a..2addcab 100644
--- a/libide/ide-types.h
+++ b/libide/ide-types.h
@@ -98,6 +98,10 @@ typedef struct _IdeProjectFiles                IdeProjectFiles;
 typedef struct _IdeRefactory                   IdeRefactory;
 typedef struct _IdeRefactoryInterface          IdeRefactoryInterface;
 
+typedef struct _IdeRuntime                     IdeRuntime;
+typedef struct _IdeRuntimeManager              IdeRuntimeManager;
+typedef struct _IdeRuntimeProvider             IdeRuntimeProvider;
+
 typedef struct _IdeScript                      IdeScript;
 
 typedef struct _IdeScriptManager               IdeScriptManager;



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