[gnome-builder: 61/139] build-system: add build-system plugin



commit ad6b156b41e405fd05ba2954269e1d45903be9c1
Author: Christian Hergert <chergert redhat com>
Date:   Wed Jan 9 17:04:06 2019 -0800

    build-system: add build-system plugin
    
    The build-system plugin attempts to discover a build system when a project
    has been loaded. It uses IdeBuildSystemDiscovery to perform the discovery
    and assign the build system to the workbench for plugins to use.

 src/plugins/buildsystem/buildsystem-plugin.c       |  37 +++
 src/plugins/buildsystem/buildsystem.gresource.xml  |   6 +
 src/plugins/buildsystem/buildsystem.plugin         |   9 +
 .../buildsystem/gbp-buildsystem-workbench-addin.c  | 298 +++++++++++++++++++++
 .../buildsystem/gbp-buildsystem-workbench-addin.h  |  31 +++
 src/plugins/buildsystem/meson.build                |  12 +
 6 files changed, 393 insertions(+)
---
diff --git a/src/plugins/buildsystem/buildsystem-plugin.c b/src/plugins/buildsystem/buildsystem-plugin.c
new file mode 100644
index 000000000..be2511922
--- /dev/null
+++ b/src/plugins/buildsystem/buildsystem-plugin.c
@@ -0,0 +1,37 @@
+/* buildsystem-plugin.c
+ *
+ * Copyright 2018-2019 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 "buildsystem-plugin"
+
+#include "config.h"
+
+#include <libpeas/peas.h>
+#include <libide-gui.h>
+#include <libide-foundry.h>
+
+#include "gbp-buildsystem-workbench-addin.h"
+
+_IDE_EXTERN void
+_gbp_buildsystem_register_types (PeasObjectModule *module)
+{
+  peas_object_module_register_extension_type (module,
+                                              IDE_TYPE_WORKBENCH_ADDIN,
+                                              GBP_TYPE_BUILDSYSTEM_WORKBENCH_ADDIN);
+}
diff --git a/src/plugins/buildsystem/buildsystem.gresource.xml 
b/src/plugins/buildsystem/buildsystem.gresource.xml
new file mode 100644
index 000000000..da5669249
--- /dev/null
+++ b/src/plugins/buildsystem/buildsystem.gresource.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/plugins/buildsystem">
+    <file>buildsystem.plugin</file>
+  </gresource>
+</gresources>
diff --git a/src/plugins/buildsystem/buildsystem.plugin b/src/plugins/buildsystem/buildsystem.plugin
new file mode 100644
index 000000000..d50f00a19
--- /dev/null
+++ b/src/plugins/buildsystem/buildsystem.plugin
@@ -0,0 +1,9 @@
+[Plugin]
+Authors=Christian Hergert <christian hergert me>
+Builtin=true
+Copyright=Copyright © 2015-2018 Christian Hergert
+Description=Initializes build system support
+Embedded=_gbp_buildsystem_register_types
+Hidden=true
+Module=buildsystem
+Name=Buildsystem Support
diff --git a/src/plugins/buildsystem/gbp-buildsystem-workbench-addin.c 
b/src/plugins/buildsystem/gbp-buildsystem-workbench-addin.c
new file mode 100644
index 000000000..14f25b949
--- /dev/null
+++ b/src/plugins/buildsystem/gbp-buildsystem-workbench-addin.c
@@ -0,0 +1,298 @@
+/* gbp-buildsystem-workbench-addin.c
+ *
+ * Copyright 2018-2019 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 "gbp-buildsystem-workbench-addin"
+
+#include "config.h"
+
+#include <libide-gui.h>
+#include <libide-foundry.h>
+#include <libide-plugins.h>
+#include <libide-threading.h>
+#include <libpeas/peas.h>
+
+#include "gbp-buildsystem-workbench-addin.h"
+
+struct _GbpBuildsystemWorkbenchAddin
+{
+  GObject       parent_instance;
+  IdeWorkbench *workbench;
+};
+
+typedef struct
+{
+  IdeExtensionSetAdapter *set;
+  GFile                  *directory;
+  const gchar            *best_match;
+  gint                    best_match_priority;
+  guint                   n_active;
+} Discovery;
+
+static void
+discovery_free (Discovery *state)
+{
+  g_assert (state);
+  g_assert (state->n_active == 0);
+
+  g_clear_object (&state->directory);
+  ide_clear_and_destroy_object (&state->set);
+  g_slice_free (Discovery, state);
+}
+
+static void
+discovery_foreach_cb (IdeExtensionSetAdapter *set,
+                      PeasPluginInfo         *plugin_info,
+                      PeasExtension          *exten,
+                      gpointer                user_data)
+{
+  IdeBuildSystemDiscovery *addin = (IdeBuildSystemDiscovery *)exten;
+  Discovery *state = user_data;
+  g_autofree gchar *ret = NULL;
+  gint priority = 0;
+
+  g_assert (IDE_IS_EXTENSION_SET_ADAPTER (set));
+  g_assert (plugin_info != NULL);
+  g_assert (IDE_IS_BUILD_SYSTEM_DISCOVERY (addin));
+  g_assert (state != NULL);
+
+  if ((ret = ide_build_system_discovery_discover (addin,
+                                                  state->directory,
+                                                  NULL,
+                                                  &priority,
+                                                  NULL)))
+    {
+      if (priority < state->best_match_priority || state->best_match == NULL)
+        {
+          state->best_match = g_intern_string (ret);
+          state->best_match_priority = priority;
+        }
+    }
+}
+
+static void
+discovery_worker (IdeTask      *task,
+                  gpointer      source_object,
+                  gpointer      task_data,
+                  GCancellable *cancellable)
+{
+  Discovery *state = task_data;
+
+  g_assert (IDE_IS_TASK (task));
+  g_assert (GBP_IS_BUILDSYSTEM_WORKBENCH_ADDIN (source_object));
+  g_assert (state != NULL);
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  ide_extension_set_adapter_foreach (state->set, discovery_foreach_cb, state);
+
+  if (state->best_match != NULL)
+    ide_task_return_pointer (task, (gpointer)state->best_match, NULL);
+  else
+    ide_task_return_new_error (task,
+                               G_IO_ERROR,
+                               G_IO_ERROR_NOT_SUPPORTED,
+                               "Failed to discover a build system");
+}
+
+static void
+discover_async (GbpBuildsystemWorkbenchAddin *self,
+                GFile                        *directory,
+                GCancellable                 *cancellable,
+                GAsyncReadyCallback           callback,
+                gpointer                      user_data)
+{
+  g_autoptr(IdeTask) task = NULL;
+  IdeContext *context;
+  Discovery *state;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_BUILDSYSTEM_WORKBENCH_ADDIN (self));
+  g_assert (G_IS_FILE (directory));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+  g_assert (IDE_IS_WORKBENCH (self->workbench));
+
+  context = ide_workbench_get_context (self->workbench);
+
+  task = ide_task_new (self, cancellable, callback, user_data);
+  ide_task_set_source_tag (task, discover_async);
+
+  state = g_slice_new0 (Discovery);
+  state->directory = g_file_dup (directory);
+  state->set = ide_extension_set_adapter_new (IDE_OBJECT (context),
+                                              peas_engine_get_default (),
+                                              IDE_TYPE_BUILD_SYSTEM_DISCOVERY,
+                                              NULL, NULL);
+  ide_task_set_task_data (task, state, discovery_free);
+  ide_task_run_in_thread (task, discovery_worker);
+}
+
+static const gchar *
+discover_finish (GbpBuildsystemWorkbenchAddin  *self,
+                 GAsyncResult                  *result,
+                 GError                       **error)
+{
+  return ide_task_propagate_pointer (IDE_TASK (result), error);
+}
+
+static void
+discover_cb (GObject      *object,
+             GAsyncResult *result,
+             gpointer      user_data)
+{
+  GbpBuildsystemWorkbenchAddin *self = (GbpBuildsystemWorkbenchAddin *)object;
+  g_autoptr(IdeBuildSystem) build_system = NULL;
+  g_autoptr(IdeTask) task = user_data;
+  g_autoptr(GError) error = NULL;
+  PeasPluginInfo *plugin_info;
+  IdeProjectInfo *project_info;
+  const gchar *plugin_name;
+  PeasEngine *engine;
+  GFile *file;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_BUILDSYSTEM_WORKBENCH_ADDIN (self));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (IDE_IS_TASK (task));
+
+  if (!(plugin_name = discover_finish (self, result, &error)))
+    {
+      ide_task_return_boolean (task, TRUE);
+      return;
+    }
+
+  if (self->workbench == NULL)
+    {
+      ide_task_return_new_error (task,
+                                 G_IO_ERROR,
+                                 G_IO_ERROR_CANCELLED,
+                                 "Workbench was destroyed");
+      return;
+    }
+
+  engine = peas_engine_get_default ();
+
+  if (!(plugin_info = peas_engine_get_plugin_info (engine, plugin_name)) ||
+      !peas_engine_provides_extension (engine, plugin_info, IDE_TYPE_BUILD_SYSTEM))
+    {
+      ide_task_return_new_error (task,
+                                 G_IO_ERROR,
+                                 G_IO_ERROR_NOT_FOUND,
+                                 "Failed to locate build system plugin %s",
+                                 plugin_name);
+      return;
+    }
+
+  project_info = ide_task_get_task_data (task);
+  g_assert (IDE_IS_PROJECT_INFO (project_info));
+
+  file = ide_project_info_get_file (project_info);
+  g_assert (G_IS_FILE (file));
+
+  build_system = (IdeBuildSystem *)
+    peas_engine_create_extension (engine,
+                                  plugin_info,
+                                  IDE_TYPE_BUILD_SYSTEM,
+                                  "project-file", file,
+                                  NULL);
+
+  ide_workbench_set_build_system (self->workbench, build_system);
+
+  ide_task_return_boolean (task, TRUE);
+}
+
+static void
+gbp_buildsystem_workbench_addin_load_project_async (IdeWorkbenchAddin   *addin,
+                                                    IdeProjectInfo      *project_info,
+                                                    GCancellable        *cancellable,
+                                                    GAsyncReadyCallback  callback,
+                                                    gpointer             user_data)
+{
+  GbpBuildsystemWorkbenchAddin *self = (GbpBuildsystemWorkbenchAddin *)addin;
+  g_autoptr(IdeTask) task = NULL;
+  GFile *directory;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_BUILDSYSTEM_WORKBENCH_ADDIN (self));
+  g_assert (IDE_IS_PROJECT_INFO (project_info));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+  g_assert (IDE_IS_WORKBENCH (self->workbench));
+
+  task = ide_task_new (self, cancellable, callback, user_data);
+  ide_task_set_source_tag (task, gbp_buildsystem_workbench_addin_load_project_async);
+  ide_task_set_task_data (task, g_object_ref (project_info), g_object_unref);
+
+  directory = ide_project_info_get_directory (project_info);
+
+  discover_async (self,
+                  directory,
+                  cancellable,
+                  discover_cb,
+                  g_steal_pointer (&task));
+}
+
+static gboolean
+gbp_buildsystem_workbench_addin_load_project_finish (IdeWorkbenchAddin  *addin,
+                                                     GAsyncResult       *result,
+                                                     GError            **error)
+{
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_BUILDSYSTEM_WORKBENCH_ADDIN (addin));
+  g_assert (IDE_IS_TASK (result));
+
+  return ide_task_propagate_boolean (IDE_TASK (result), error);
+}
+
+static void
+gbp_buildsystem_workbench_addin_load (IdeWorkbenchAddin *addin,
+                                      IdeWorkbench      *workbench)
+{
+  GBP_BUILDSYSTEM_WORKBENCH_ADDIN (addin)->workbench = workbench;
+}
+
+static void
+gbp_buildsystem_workbench_addin_unload (IdeWorkbenchAddin *addin,
+                                        IdeWorkbench      *workbench)
+{
+  GBP_BUILDSYSTEM_WORKBENCH_ADDIN (addin)->workbench = NULL;
+}
+
+static void
+workbench_addin_iface_init (IdeWorkbenchAddinInterface *iface)
+{
+  iface->load = gbp_buildsystem_workbench_addin_load;
+  iface->unload = gbp_buildsystem_workbench_addin_unload;
+  iface->load_project_async = gbp_buildsystem_workbench_addin_load_project_async;
+  iface->load_project_finish = gbp_buildsystem_workbench_addin_load_project_finish;
+}
+
+G_DEFINE_TYPE_WITH_CODE (GbpBuildsystemWorkbenchAddin,
+                         gbp_buildsystem_workbench_addin,
+                         G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (IDE_TYPE_WORKBENCH_ADDIN,
+                                                workbench_addin_iface_init))
+
+static void
+gbp_buildsystem_workbench_addin_class_init (GbpBuildsystemWorkbenchAddinClass *klass)
+{
+}
+
+static void
+gbp_buildsystem_workbench_addin_init (GbpBuildsystemWorkbenchAddin *self)
+{
+}
diff --git a/src/plugins/buildsystem/gbp-buildsystem-workbench-addin.h 
b/src/plugins/buildsystem/gbp-buildsystem-workbench-addin.h
new file mode 100644
index 000000000..5477e349f
--- /dev/null
+++ b/src/plugins/buildsystem/gbp-buildsystem-workbench-addin.h
@@ -0,0 +1,31 @@
+/* gbp-buildsystem-workbench-addin.h
+ *
+ * Copyright 2018-2019 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 <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_BUILDSYSTEM_WORKBENCH_ADDIN (gbp_buildsystem_workbench_addin_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpBuildsystemWorkbenchAddin, gbp_buildsystem_workbench_addin, GBP, 
BUILDSYSTEM_WORKBENCH_ADDIN, GObject)
+
+G_END_DECLS
diff --git a/src/plugins/buildsystem/meson.build b/src/plugins/buildsystem/meson.build
new file mode 100644
index 000000000..2de1e508c
--- /dev/null
+++ b/src/plugins/buildsystem/meson.build
@@ -0,0 +1,12 @@
+plugins_sources += files([
+  'buildsystem-plugin.c',
+  'gbp-buildsystem-workbench-addin.c',
+])
+
+plugin_buildsystem_resources = gnome.compile_resources(
+  'buildsystem-resources',
+  'buildsystem.gresource.xml',
+  c_name: 'gbp_buildsystem',
+)
+
+plugins_sources += plugin_buildsystem_resources[0]


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