/**
 * gis_page_util_run_reformatter:
 *
 * Launches the reformatter, and arranges for the assistant to be hidden for
 * the duration of its runtime. @callback will be called when the reformatter
 * exits.
 *
 * There is no corresponding _finish() function because (at present) neither
 * caller actually cares about the result.
 */
void
gis_page_util_run_reformatter (GisPage            *page,
                               GAsyncReadyCallback callback,
                               gpointer            user_data)
{
  g_autoptr(GTask) task = g_task_new (page, NULL, callback, user_data);
  g_autoptr(GSubprocessLauncher) launcher = NULL;
  g_autoptr(GSubprocess) subprocess = NULL;
  const gchar *locale = setlocale (LC_MESSAGES, NULL);
  const gchar *command = "/usr/lib/eos-installer/gnome-image-installer";
  g_autoptr(GError) error = NULL;

  launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_NONE);
  g_subprocess_launcher_setenv (launcher, "LANG", locale, TRUE);
  subprocess = g_subprocess_launcher_spawn (launcher, &error, command, NULL);

  if (error)
    {
      on_reformatter_exited (task, g_steal_pointer (&error));
      return;
    }

  gis_driver_hide_window (page->driver);
  g_subprocess_wait_check_async (subprocess, NULL, reformatter_exited_cb,
                                 g_steal_pointer (&task));
}
static void
simple_make_command (GFile            *directory,
                     const gchar      *target,
                     GTask            *task,
                     IdeConfiguration *configuration)
{
  g_autoptr(IdeSubprocessLauncher) launcher = NULL;
  g_autoptr(GSubprocess) subprocess = NULL;
  GCancellable *cancellable;
  IdeRuntime *runtime;
  GError *error = NULL;

  g_assert (G_IS_FILE (directory));
  g_assert (target != NULL);
  g_assert (G_IS_TASK (task));
  g_assert (IDE_IS_CONFIGURATION (configuration));

  cancellable = g_task_get_cancellable (task);

  if (!g_file_is_native (directory))
    {
      g_task_return_new_error (task,
                               G_IO_ERROR,
                               G_IO_ERROR_NOT_REGULAR_FILE,
                               "Cannot use non-local directories.");
      return;
    }

  if (NULL == (runtime = ide_configuration_get_runtime (configuration)))
    {
      g_task_return_new_error (task,
                               G_IO_ERROR,
                               G_IO_ERROR_NOT_FOUND,
                               "Failed to locate runtime");
      return;
    }

  if (NULL == (launcher = ide_runtime_create_launcher (runtime, &error)))
    {
      g_task_return_error (task, error);
      return;
    }

  ide_subprocess_launcher_set_cwd (launcher, g_file_get_path (directory));

  if (ide_runtime_contains_program_in_path (runtime, "gmake", cancellable))
    ide_subprocess_launcher_push_argv (launcher, "gmake");
  else
    ide_subprocess_launcher_push_argv (launcher, "make");

  ide_subprocess_launcher_push_argv (launcher, target);

  if (NULL == (subprocess = ide_subprocess_launcher_spawn_sync (launcher, cancellable, &error)))
    {
      g_task_return_error (task, error);
      return;
    }

  g_subprocess_wait_check_async (subprocess,
                                 g_task_get_cancellable (task),
                                 simple_make_command_cb,
                                 g_object_ref (task));
}