[tracker/sam/cli-uninstalled-fixes: 8/9] cli: Add a way to run from the source tree



commit 092a55fcddeeb49eb44e37287caadddc999b8d2d
Author: Sam Thursfield <sam afuera me uk>
Date:   Mon Feb 24 21:52:48 2020 +0100

    cli: Add a way to run from the source tree
    
    We need to run the CLI from the source tree if we are going to test it
    with `meson test`. This adds an env var `TRACKER_CLI_SUBCOMMANDS_DIR`
    which can be used by tests and developers.

 src/tracker/make-uninstalled-subcommand-links.sh | 22 ++++++++++++
 src/tracker/meson.build                          |  3 ++
 src/tracker/tracker-main.c                       | 45 ++++++++++++++++--------
 3 files changed, 56 insertions(+), 14 deletions(-)
---
diff --git a/src/tracker/make-uninstalled-subcommand-links.sh 
b/src/tracker/make-uninstalled-subcommand-links.sh
new file mode 100644
index 000000000..bce8d1124
--- /dev/null
+++ b/src/tracker/make-uninstalled-subcommand-links.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+set -e
+
+cli_dir=$MESON_BUILD_ROOT/$MESON_SUBDIR
+subcommands_dir=$MESON_BUILD_ROOT/$MESON_SUBDIR/subcommands
+
+mkdir -p $subcommands_dir
+
+for l in `find $subcommands_dir -type l`
+do
+# Delete all previous links to our own binary
+if [ `readlink $l` = "$cli_dir/tracker" ]
+then
+    rm $l
+fi
+done
+
+for subcommand in $@
+do
+    ln -s $cli_dir/tracker $subcommands_dir/$subcommand
+done
diff --git a/src/tracker/meson.build b/src/tracker/meson.build
index 650d5155d..f02f1986a 100644
--- a/src/tracker/meson.build
+++ b/src/tracker/meson.build
@@ -35,3 +35,6 @@ if get_option('bash_completion')
         sources: 'bash-completion/tracker',
         install_dir: bash_completion_dir)
 endif
+
+run_command('make-uninstalled-subcommand-links.sh', modules,
+            check: true)
diff --git a/src/tracker/tracker-main.c b/src/tracker/tracker-main.c
index 5b8866d3a..82fe1523e 100644
--- a/src/tracker/tracker-main.c
+++ b/src/tracker/tracker-main.c
@@ -161,6 +161,13 @@ print_usage_list_cmds (void)
        GFileEnumerator *enumerator;
        GFileInfo *info;
        GFile *dir;
+       GError *error = NULL;
+       const gchar *subcommands_dir;
+
+       subcommands_dir = g_getenv ("TRACKER_CLI_SUBCOMMANDS_DIR");
+       if (!subcommands_dir) {
+               subcommands_dir = LIBEXECDIR "/tracker/";
+       }
 
        for (i = 0; i < G_N_ELEMENTS(commands); i++) {
                if (longest < strlen (commands[i].cmd))
@@ -175,27 +182,31 @@ print_usage_list_cmds (void)
                puts (_(commands[i].help));
        }
 
-       dir = g_file_new_for_path (LIBEXECDIR "/tracker/");
+       dir = g_file_new_for_path (subcommands_dir);
        enumerator = g_file_enumerate_children (dir,
                                                G_FILE_ATTRIBUTE_STANDARD_NAME ","
                                                G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK ","
                                                G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
                                                G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
-                                               NULL, NULL);
+                                               NULL, &error);
        g_object_unref (dir);
 
-       while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL) {
-               /* Filter builtin commands */
-               if (g_file_info_get_is_symlink (info) &&
-                   g_strcmp0 (g_file_info_get_symlink_target (info), BINDIR "/tracker") == 0)
-                       continue;
+       if (enumerator) {
+               while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL) {
+                       /* Filter builtin commands */
+                       if (g_file_info_get_is_symlink (info) &&
+                           g_strcmp0 (g_file_info_get_symlink_target (info), BINDIR "/tracker") == 0)
+                               continue;
 
-               extra_commands = g_list_prepend (extra_commands,
-                                                g_strdup (g_file_info_get_name (info)));
-               g_object_unref (info);
-       }
+                       extra_commands = g_list_prepend (extra_commands,
+                                                        g_strdup (g_file_info_get_name (info)));
+                       g_object_unref (info);
+               }
 
-       g_object_unref (enumerator);
+               g_object_unref (enumerator);
+       } else {
+               g_warning ("Failed to list extra commands: %s", error->message);
+       }
 
        if (extra_commands) {
                extra_commands = g_list_sort (extra_commands, (GCompareFunc) g_strcmp0);
@@ -224,6 +235,7 @@ main (int argc, char *argv[])
 {
        gboolean basename_is_bin = FALSE;
        gchar *command_basename;
+       const gchar *subcommands_dir;
 
        setlocale (LC_ALL, "");
 
@@ -235,8 +247,13 @@ main (int argc, char *argv[])
        basename_is_bin = g_strcmp0 (command_basename, "tracker") == 0;
        g_free (command_basename);
 
+       subcommands_dir = g_getenv ("TRACKER_CLI_SUBCOMMANDS_DIR");
+       if (!subcommands_dir) {
+               subcommands_dir = LIBEXECDIR "/tracker/";
+       }
+
        if (g_path_is_absolute (argv[0]) &&
-           g_str_has_prefix (argv[0], LIBEXECDIR "/tracker/")) {
+           g_str_has_prefix (argv[0], subcommands_dir)) {
                /* This is a subcommand call */
                handle_command (argc, (const gchar **) argv);
                exit (EXIT_FAILURE);
@@ -255,7 +272,7 @@ main (int argc, char *argv[])
                                subcommand = "help";
                        }
 
-                       path = g_build_filename (LIBEXECDIR, "tracker", subcommand, NULL);
+                       path = g_build_filename (subcommands_dir, subcommand, NULL);
 
                        if (g_file_test (path, G_FILE_TEST_EXISTS)) {
                                /* Manipulate argv in place, in order to launch subcommand */


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