[gnome-builder/wip/chergert/code-index-revamp: 9/16] code-index: start on an --index option to index a project



commit ef0f1ef9bcdb2857abeb26be696ddb3677124edc
Author: Christian Hergert <chergert redhat com>
Date:   Thu Jan 31 14:30:05 2019 -0800

    code-index: start on an --index option to index a project
    
    This just uses the index plan to display a dry run of what the indexer
    will want to do. We still need an executor of that work to make this
    all complete.

 .../code-index/gbp-code-index-application-addin.c  | 177 ++++++++++++++++++++-
 1 file changed, 175 insertions(+), 2 deletions(-)
---
diff --git a/src/plugins/code-index/gbp-code-index-application-addin.c 
b/src/plugins/code-index/gbp-code-index-application-addin.c
index e90af9286..337dc1123 100644
--- a/src/plugins/code-index/gbp-code-index-application-addin.c
+++ b/src/plugins/code-index/gbp-code-index-application-addin.c
@@ -26,8 +26,10 @@
 #include <libide-code.h>
 #include <libide-foundry.h>
 #include <libide-gui.h>
+#include <stdlib.h>
 
 #include "gbp-code-index-application-addin.h"
+#include "gbp-code-index-plan.h"
 
 struct _GbpCodeIndexApplicationAddin
 {
@@ -51,12 +53,162 @@ gbp_code_index_application_addin_add_option_entries (IdeApplicationAddin *addin,
                                  _("PROJECT_FILE"));
 }
 
+static const gchar *
+reason_string (GbpCodeIndexReason reason)
+{
+  switch (reason)
+    {
+    case GBP_CODE_INDEX_REASON_INITIAL: return "initial";
+    case GBP_CODE_INDEX_REASON_CHANGES: return "changes";
+    case GBP_CODE_INDEX_REASON_REMOVE_INDEX: return "remove-index";
+    case GBP_CODE_INDEX_REASON_EXPIRED: return "expired";
+    default: return "unknown";
+    }
+}
+
+static gboolean
+gbp_code_index_application_addin_foreach_cb (GFile              *directory,
+                                             GPtrArray          *plan_items,
+                                             GbpCodeIndexReason  reason,
+                                             gpointer            user_data)
+{
+  GApplicationCommandLine *cmdline = user_data;
+  g_autofree gchar *path = NULL;
+  g_autoptr(GFile) file = NULL;
+
+  g_assert (G_IS_FILE (directory));
+  g_assert (plan_items != NULL);
+  g_assert (G_IS_APPLICATION_COMMAND_LINE (cmdline));
+
+  g_application_command_line_print (cmdline,
+                                    "%s [reason=%s]\n",
+                                    g_file_peek_path (directory),
+                                    reason_string (reason));
+
+  for (guint i = 0; i < plan_items->len; i++)
+    {
+      const GbpCodeIndexPlanItem *item = g_ptr_array_index (plan_items, i);
+      const gchar *name = g_file_info_get_name (item->file_info);
+
+      g_application_command_line_print (cmdline,
+                                        "  %s [indexer=%s]\n",
+                                        name,
+                                        item->indexer_module_name);
+    }
+
+  return FALSE;
+}
+
+static void
+gbp_code_index_application_addin_cull_cb (GObject      *object,
+                                          GAsyncResult *result,
+                                          gpointer      user_data)
+{
+  GbpCodeIndexPlan *plan = (GbpCodeIndexPlan *)object;
+  g_autoptr(GApplicationCommandLine) cmdline = user_data;
+  g_autoptr(GError) error = NULL;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_CODE_INDEX_PLAN (plan));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (G_IS_APPLICATION_COMMAND_LINE (cmdline));
+
+  if (!gbp_code_index_plan_cull_indexed_finish (plan, result, &error))
+    {
+      g_application_command_line_printerr (cmdline,
+                                           _("Failed to cull index plan: %s"),
+                                           error->message);
+      g_application_command_line_set_exit_status (cmdline, EXIT_FAILURE);
+      return;
+    }
+
+  gbp_code_index_plan_foreach (plan,
+                               gbp_code_index_application_addin_foreach_cb,
+                               cmdline);
+
+  g_application_command_line_set_exit_status (cmdline, EXIT_SUCCESS);
+}
+
+static void
+gbp_code_index_application_addin_populate_cb (GObject      *object,
+                                              GAsyncResult *result,
+                                              gpointer      user_data)
+{
+  GbpCodeIndexPlan *plan = (GbpCodeIndexPlan *)object;
+  g_autoptr(GApplicationCommandLine) cmdline = user_data;
+  g_autoptr(GError) error = NULL;
+  IdeWorkbench *workbench;
+  IdeContext *context;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_CODE_INDEX_PLAN (plan));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (G_IS_APPLICATION_COMMAND_LINE (cmdline));
+
+  if (!gbp_code_index_plan_populate_finish (plan, result, &error))
+    {
+      g_application_command_line_printerr (cmdline,
+                                           _("Failed to populate index plan: %s"),
+                                           error->message);
+      g_application_command_line_set_exit_status (cmdline, EXIT_FAILURE);
+      return;
+    }
+
+  workbench = g_object_get_data (G_OBJECT (cmdline), "WORKBENCH");
+  context = ide_workbench_get_context (workbench);
+
+  gbp_code_index_plan_cull_indexed_async (plan,
+                                          context,
+                                          NULL,
+                                          gbp_code_index_application_addin_cull_cb,
+                                          g_steal_pointer (&cmdline));
+}
+
+static void
+gbp_code_index_application_addin_load_project_cb (GObject      *object,
+                                                  GAsyncResult *result,
+                                                  gpointer      user_data)
+{
+  IdeWorkbench *workbench = (IdeWorkbench *)object;
+  g_autoptr(GApplicationCommandLine) cmdline = user_data;
+  g_autoptr(GbpCodeIndexPlan) plan = NULL;
+  g_autoptr(GError) error = NULL;
+  IdeContext *context;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (IDE_IS_WORKBENCH (workbench));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (G_IS_APPLICATION_COMMAND_LINE (cmdline));
+
+  if (!ide_workbench_load_project_finish (workbench, result, &error))
+    {
+      g_application_command_line_printerr (cmdline,
+                                           _("Failed to load project: %s"),
+                                           error->message);
+      g_application_command_line_set_exit_status (cmdline, EXIT_FAILURE);
+      return;
+    }
+
+  context = ide_workbench_get_context (workbench);
+  plan = gbp_code_index_plan_new ();
+
+  gbp_code_index_plan_populate_async (plan,
+                                      context,
+                                      NULL,
+                                      gbp_code_index_application_addin_populate_cb,
+                                      g_steal_pointer (&cmdline));
+}
+
 static void
 gbp_code_index_application_addin_handle_command_line (IdeApplicationAddin     *addin,
                                                       IdeApplication          *application,
                                                       GApplicationCommandLine *cmdline)
 {
   g_autofree gchar *project_path = NULL;
+  g_autoptr(IdeWorkbench) workbench = NULL;
+  g_autoptr(IdeProjectInfo) project_info = NULL;
+  g_autoptr(GFile) project_file = NULL;
+  g_autoptr(GFile) project_dir = NULL;
   GVariantDict *options;
 
   g_assert (IDE_IS_MAIN_THREAD ());
@@ -69,9 +221,30 @@ gbp_code_index_application_addin_handle_command_line (IdeApplicationAddin     *a
       !g_variant_dict_lookup (options, "index", "^ay", &project_path))
     return;
 
-  ide_application_set_command_line_handled (application, cmdline, TRUE);
+  project_file = g_file_new_for_path (project_path);
+
+  if (g_file_test (project_path, G_FILE_TEST_IS_DIR))
+    project_dir = g_object_ref (project_file);
+  else
+    project_file = g_file_get_parent (project_file);
+
+  project_info = ide_project_info_new ();
+  ide_project_info_set_file (project_info, project_file);
+  ide_project_info_set_directory (project_info, project_dir);
 
-  g_print ("Re-index %s\n", project_path);
+  workbench = ide_workbench_new ();
+  ide_application_add_workbench (application, workbench);
+  ide_workbench_load_project_async (workbench,
+                                    project_info,
+                                    G_TYPE_INVALID,
+                                    NULL,
+                                    gbp_code_index_application_addin_load_project_cb,
+                                    g_object_ref (cmdline));
+
+  ide_application_set_command_line_handled (application, cmdline, TRUE);
+  g_object_set_data_full (G_OBJECT (cmdline), "WORKBENCH", g_steal_pointer (&workbench), g_object_unref);
+  g_object_set_data_full (G_OBJECT (cmdline), "APP", application, (GDestroyNotify)g_application_release);
+  g_application_hold (G_APPLICATION (application));
 }
 
 static void


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