[gnome-builder] plugins/npm: port plugin to C



commit af0a489eeec9438278bb8709df9a71cb1909ae90
Author: Christian Hergert <chergert redhat com>
Date:   Mon Jul 11 23:08:57 2022 -0700

    plugins/npm: port plugin to C

 src/plugins/npm/gbp-npm-build-system-discovery.c |  47 ++++
 src/plugins/npm/gbp-npm-build-system-discovery.h |  31 +++
 src/plugins/npm/gbp-npm-build-system.c           | 166 ++++++++++++++
 src/plugins/npm/gbp-npm-build-system.h           |  33 +++
 src/plugins/npm/gbp-npm-pipeline-addin.c         | 106 +++++++++
 src/plugins/npm/gbp-npm-pipeline-addin.h         |  31 +++
 src/plugins/npm/gbp-npm-run-command-provider.c   | 271 +++++++++++++++++++++++
 src/plugins/npm/gbp-npm-run-command-provider.h   |  31 +++
 src/plugins/npm/meson.build                      |  22 +-
 src/plugins/npm/npm-plugin.c                     |  49 ++++
 src/plugins/npm/npm.gresource.xml                |   6 +
 src/plugins/npm/npm.plugin                       |  12 +-
 src/plugins/npm/npm_plugin.py                    | 206 -----------------
 13 files changed, 791 insertions(+), 220 deletions(-)
---
diff --git a/src/plugins/npm/gbp-npm-build-system-discovery.c 
b/src/plugins/npm/gbp-npm-build-system-discovery.c
new file mode 100644
index 000000000..ee96413ee
--- /dev/null
+++ b/src/plugins/npm/gbp-npm-build-system-discovery.c
@@ -0,0 +1,47 @@
+/* gbp-npm-build-system-discovery.c
+ *
+ * Copyright 2016-2022 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-npm-build-system-discovery"
+
+#include "config.h"
+
+#include "gbp-npm-build-system-discovery.h"
+
+struct _GbpNpmBuildSystemDiscovery
+{
+  IdeSimpleBuildSystemDiscovery parent_instance;
+};
+
+G_DEFINE_FINAL_TYPE (GbpNpmBuildSystemDiscovery, gbp_npm_build_system_discovery, 
IDE_TYPE_SIMPLE_BUILD_SYSTEM_DISCOVERY)
+
+static void
+gbp_npm_build_system_discovery_class_init (GbpNpmBuildSystemDiscoveryClass *klass)
+{
+}
+
+static void
+gbp_npm_build_system_discovery_init (GbpNpmBuildSystemDiscovery *self)
+{
+  g_object_set (self,
+                "glob", "package.json",
+                "hint", "npm",
+                "priority", 100,
+                NULL);
+}
diff --git a/src/plugins/npm/gbp-npm-build-system-discovery.h 
b/src/plugins/npm/gbp-npm-build-system-discovery.h
new file mode 100644
index 000000000..2e69a175c
--- /dev/null
+++ b/src/plugins/npm/gbp-npm-build-system-discovery.h
@@ -0,0 +1,31 @@
+/* gbp-npm-build-system-discovery.h
+ *
+ * Copyright 2016-2022 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 <libide-foundry.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_NPM_BUILD_SYSTEM_DISCOVERY (gbp_npm_build_system_discovery_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpNpmBuildSystemDiscovery, gbp_npm_build_system_discovery, GBP, 
NPM_BUILD_SYSTEM_DISCOVERY, IdeSimpleBuildSystemDiscovery)
+
+G_END_DECLS
diff --git a/src/plugins/npm/gbp-npm-build-system.c b/src/plugins/npm/gbp-npm-build-system.c
new file mode 100644
index 000000000..e7849698b
--- /dev/null
+++ b/src/plugins/npm/gbp-npm-build-system.c
@@ -0,0 +1,166 @@
+/* gbp-npm-build-system.c
+ *
+ * Copyright 2016-2022 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-npm-build-system"
+
+#include "config.h"
+
+#include "gbp-npm-build-system.h"
+
+struct _GbpNpmBuildSystem
+{
+  IdeObject  parent_instance;
+  GFile     *project_file;
+};
+
+enum {
+  PROP_0,
+  PROP_PROJECT_FILE,
+  N_PROPS
+};
+
+static char *
+gbp_npm_build_system_get_id (IdeBuildSystem *build_system)
+{
+  return g_strdup ("npm");
+}
+
+static char *
+gbp_npm_build_system_get_display_name (IdeBuildSystem *build_system)
+{
+  return g_strdup ("Npm");
+}
+
+static int
+gbp_npm_build_system_get_priority (IdeBuildSystem *build_system)
+{
+  return 100;
+}
+
+static void
+build_system_iface_init (IdeBuildSystemInterface *iface)
+{
+  iface->get_id = gbp_npm_build_system_get_id;
+  iface->get_display_name = gbp_npm_build_system_get_display_name;
+  iface->get_priority = gbp_npm_build_system_get_priority;
+}
+
+G_DEFINE_FINAL_TYPE_WITH_CODE (GbpNpmBuildSystem, gbp_npm_build_system, IDE_TYPE_OBJECT,
+                               G_IMPLEMENT_INTERFACE (IDE_TYPE_BUILD_SYSTEM, build_system_iface_init))
+
+static GParamSpec *properties [N_PROPS];
+
+static void
+gbp_npm_build_system_dispose (GObject *object)
+{
+  GbpNpmBuildSystem *self = (GbpNpmBuildSystem *)object;
+
+  g_clear_object (&self->project_file);
+
+  G_OBJECT_CLASS (gbp_npm_build_system_parent_class)->dispose (object);
+}
+
+static void
+gbp_npm_build_system_get_property (GObject    *object,
+                                   guint       prop_id,
+                                   GValue     *value,
+                                   GParamSpec *pspec)
+{
+  GbpNpmBuildSystem *self = GBP_NPM_BUILD_SYSTEM (object);
+
+  switch (prop_id)
+    {
+    case PROP_PROJECT_FILE:
+      g_value_set_object (value, self->project_file);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gbp_npm_build_system_set_property (GObject      *object,
+                                   guint         prop_id,
+                                   const GValue *value,
+                                   GParamSpec   *pspec)
+{
+  GbpNpmBuildSystem *self = GBP_NPM_BUILD_SYSTEM (object);
+
+  switch (prop_id)
+    {
+    case PROP_PROJECT_FILE:
+      g_set_object (&self->project_file, g_value_get_object (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gbp_npm_build_system_class_init (GbpNpmBuildSystemClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->dispose = gbp_npm_build_system_dispose;
+  object_class->get_property = gbp_npm_build_system_get_property;
+  object_class->set_property = gbp_npm_build_system_set_property;
+
+  properties [PROP_PROJECT_FILE] =
+    g_param_spec_object ("project-file",
+                         "Project File",
+                         "The project file (Npm.toml)",
+                         G_TYPE_FILE,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+gbp_npm_build_system_init (GbpNpmBuildSystem *self)
+{
+}
+
+char *
+gbp_npm_build_system_get_project_dir (GbpNpmBuildSystem *self)
+{
+  g_autoptr(GFile) workdir = NULL;
+  g_autofree char *base = NULL;
+  IdeContext *context;
+
+  g_return_val_if_fail (GBP_IS_NPM_BUILD_SYSTEM (self), NULL);
+
+  context = ide_object_get_context (IDE_OBJECT (self));
+  workdir = ide_context_ref_workdir (context);
+
+  if (self->project_file == NULL)
+    return g_strdup (g_file_peek_path (workdir));
+
+  base = g_file_get_basename (self->project_file);
+
+  if (strcasecmp (base, "package.json") == 0)
+    {
+      g_autoptr(GFile) parent = g_file_get_parent (self->project_file);
+      return g_file_get_path (parent);
+    }
+
+  return g_file_get_path (self->project_file);
+}
diff --git a/src/plugins/npm/gbp-npm-build-system.h b/src/plugins/npm/gbp-npm-build-system.h
new file mode 100644
index 000000000..0b4b87b63
--- /dev/null
+++ b/src/plugins/npm/gbp-npm-build-system.h
@@ -0,0 +1,33 @@
+/* gbp-npm-build-system.h
+ *
+ * Copyright 2016-2022 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 <libide-foundry.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_NPM_BUILD_SYSTEM (gbp_npm_build_system_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpNpmBuildSystem, gbp_npm_build_system, GBP, NPM_BUILD_SYSTEM, IdeObject)
+
+char *gbp_npm_build_system_get_project_dir (GbpNpmBuildSystem *self);
+
+G_END_DECLS
diff --git a/src/plugins/npm/gbp-npm-pipeline-addin.c b/src/plugins/npm/gbp-npm-pipeline-addin.c
new file mode 100644
index 000000000..814d45e7d
--- /dev/null
+++ b/src/plugins/npm/gbp-npm-pipeline-addin.c
@@ -0,0 +1,106 @@
+/* gbp-npm-pipeline-addin.c
+ *
+ * Copyright 2018 danigm <danigm wadobo com>
+ * Copyright 2018 Alberto Fanjul <albfan gnome org>
+ * Copyright 2022 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-npm-pipeline-addin"
+
+#include "config.h"
+
+#include <glib/gi18n.h>
+
+#include <libide-foundry.h>
+
+#include "gbp-npm-build-system.h"
+#include "gbp-npm-pipeline-addin.h"
+
+struct _GbpNpmPipelineAddin
+{
+  IdeObject parent_instance;
+};
+
+static void
+gbp_npm_pipeline_addin_load (IdePipelineAddin *addin,
+                             IdePipeline      *pipeline)
+{
+  g_autoptr(IdeSubprocessLauncher) fetch_launcher = NULL;
+  g_autoptr(IdePipelineStage) fetch_stage = NULL;
+  g_autofree char *project_dir = NULL;
+  IdeBuildSystem *build_system;
+  const char *npm;
+  IdeContext *context;
+  IdeConfig *config;
+  guint id;
+
+  IDE_ENTRY;
+
+  g_assert (GBP_IS_NPM_PIPELINE_ADDIN (addin));
+  g_assert (IDE_IS_PIPELINE (pipeline));
+
+  context = ide_object_get_context (IDE_OBJECT (addin));
+  build_system = ide_build_system_from_context (context);
+  config = ide_pipeline_get_config (pipeline);
+
+  if (!GBP_IS_NPM_BUILD_SYSTEM (build_system))
+    IDE_EXIT;
+
+  npm = ide_config_getenv (config, "NPM");
+  if (ide_str_empty0 (npm))
+    npm = "npm";
+
+  project_dir = gbp_npm_build_system_get_project_dir (GBP_NPM_BUILD_SYSTEM (build_system));
+
+  /* Fetch dependencies so that we no longer need network access */
+  fetch_launcher = ide_pipeline_create_launcher (pipeline, NULL);
+  ide_subprocess_launcher_set_cwd (fetch_launcher, project_dir);
+  ide_subprocess_launcher_push_argv (fetch_launcher, npm);
+  if (!ide_pipeline_is_native (pipeline))
+    {
+      IdeTriplet *triplet = ide_pipeline_get_host_triplet (pipeline);
+      const char *arch = ide_triplet_get_arch (triplet);
+      ide_subprocess_launcher_push_args (fetch_launcher, IDE_STRV_INIT ("--arch", arch));
+    }
+  ide_subprocess_launcher_push_argv (fetch_launcher, "install");
+  fetch_stage = ide_pipeline_stage_launcher_new (context, fetch_launcher);
+  ide_pipeline_stage_set_name (fetch_stage, _("Downloading npm dependencies"));
+  id = ide_pipeline_attach (pipeline, IDE_PIPELINE_PHASE_DOWNLOADS, 0, fetch_stage);
+  ide_pipeline_addin_track (addin, id);
+
+  IDE_EXIT;
+}
+
+static void
+pipeline_addin_iface_init (IdePipelineAddinInterface *iface)
+{
+  iface->load = gbp_npm_pipeline_addin_load;
+}
+
+G_DEFINE_FINAL_TYPE_WITH_CODE (GbpNpmPipelineAddin, gbp_npm_pipeline_addin, IDE_TYPE_OBJECT,
+                               G_IMPLEMENT_INTERFACE (IDE_TYPE_PIPELINE_ADDIN, pipeline_addin_iface_init))
+
+static void
+gbp_npm_pipeline_addin_class_init (GbpNpmPipelineAddinClass *klass)
+{
+}
+
+static void
+gbp_npm_pipeline_addin_init (GbpNpmPipelineAddin *self)
+{
+}
diff --git a/src/plugins/npm/gbp-npm-pipeline-addin.h b/src/plugins/npm/gbp-npm-pipeline-addin.h
new file mode 100644
index 000000000..2c08ae4a8
--- /dev/null
+++ b/src/plugins/npm/gbp-npm-pipeline-addin.h
@@ -0,0 +1,31 @@
+/* gbp-npm-pipeline-addin.h
+ *
+ * Copyright 2016-2022 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 <libide-core.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_NPM_PIPELINE_ADDIN (gbp_npm_pipeline_addin_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpNpmPipelineAddin, gbp_npm_pipeline_addin, GBP, NPM_PIPELINE_ADDIN, IdeObject)
+
+G_END_DECLS
diff --git a/src/plugins/npm/gbp-npm-run-command-provider.c b/src/plugins/npm/gbp-npm-run-command-provider.c
new file mode 100644
index 000000000..b6c3ce63b
--- /dev/null
+++ b/src/plugins/npm/gbp-npm-run-command-provider.c
@@ -0,0 +1,271 @@
+/* gbp-npm-run-command-provider.c
+ *
+ * Copyright 2016-2022 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-npm-run-command-provider"
+
+#include "config.h"
+
+#include <glib/gi18n.h>
+#include <json-glib/json-glib.h>
+
+#include <libide-foundry.h>
+#include <libide-threading.h>
+
+#include "gbp-npm-build-system.h"
+#include "gbp-npm-run-command-provider.h"
+
+struct _GbpNpmRunCommandProvider
+{
+  IdeObject parent_instance;
+};
+
+static const char * const npm_special_scripts[] = {
+  "prepare", "publish", "prepublishOnly", "install", "uninstall", "version", "shrinkwrap",
+  NULL
+};
+static const char * const npm_standard_scripts[] = {
+  "test", "start", "stop", "restart",
+  NULL
+};
+static const char *npm_start = "start";
+
+static gboolean
+is_ignored_script (const char         *script,
+                   const char * const *all_scripts)
+{
+  if (g_strv_contains (npm_special_scripts, script))
+    return TRUE;
+
+  if (g_str_has_prefix (script, "pre"))
+    {
+      const char *without_pre = script + strlen ("pre");
+
+      if (g_strv_contains (npm_special_scripts, without_pre) ||
+          g_strv_contains (npm_standard_scripts, without_pre) ||
+          g_strv_contains (all_scripts, without_pre))
+        return TRUE;
+    }
+
+  if (g_str_has_prefix (script, "post"))
+    {
+      const char *without_post = script + strlen ("post");
+
+      if (g_strv_contains (npm_special_scripts, without_post) ||
+          g_strv_contains (npm_standard_scripts, without_post) ||
+          g_strv_contains (all_scripts, without_post))
+        return TRUE;
+    }
+
+  return FALSE;
+}
+
+static void
+gbp_npm_run_command_provider_list_commands_worker (IdeTask      *task,
+                                                   gpointer      source_object,
+                                                   gpointer      task_data,
+                                                   GCancellable *cancellable)
+{
+  const char *package_json = task_data;
+  g_autoptr(GFile) package_json_file = NULL;
+  g_autoptr(GFile) project_dir = NULL;
+  g_autoptr(GFile) server_js = NULL;
+  g_autoptr(JsonParser) parser = NULL;
+  g_autoptr(GListStore) store = NULL;
+  g_autoptr(GArray) all_scripts = NULL;
+  g_autoptr(GError) error = NULL;
+  JsonObject *root_obj;
+  JsonArray *scripts_ar;
+  JsonNode *root;
+  JsonNode *scripts;
+  guint n_items;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_TASK (task));
+  g_assert (GBP_IS_NPM_RUN_COMMAND_PROVIDER (source_object));
+  g_assert (package_json != NULL);
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  package_json_file = g_file_new_for_path (package_json);
+  project_dir = g_file_get_parent (package_json_file);
+  server_js = g_file_get_child (project_dir, "server.js");
+
+  parser = json_parser_new ();
+
+  if (!json_parser_load_from_mapped_file (parser, package_json, &error))
+    {
+      ide_task_return_error (task, g_steal_pointer (&error));
+      IDE_EXIT;
+    }
+
+  if (!(root = json_parser_get_root (parser)) ||
+      !JSON_NODE_HOLDS_OBJECT (root) ||
+      !(root_obj = json_node_get_object (root)))
+    {
+      ide_task_return_error (task, g_steal_pointer (&error));
+      IDE_EXIT;
+    }
+
+  store = g_list_store_new (IDE_TYPE_RUN_COMMAND);
+
+  /* If there are no scripts, just short-circuit */
+  if (!json_object_has_member (root_obj, "scripts") ||
+      !(scripts = json_object_get_member (root_obj, "scripts")) ||
+      !JSON_NODE_HOLDS_ARRAY (scripts) ||
+      !(scripts_ar = json_node_get_array (scripts)))
+    IDE_GOTO (complete);
+
+  n_items = json_array_get_length (scripts_ar);
+  all_scripts = g_array_sized_new (TRUE, FALSE, sizeof (char *), n_items);
+
+  for (guint i = 0; i < n_items; i++)
+    {
+      JsonNode *node = json_array_get_element (scripts_ar, i);
+      const char *str;
+      if (JSON_NODE_HOLDS_VALUE (node) && (str = json_node_get_string (node)))
+        g_array_append_val (all_scripts, str);
+    }
+
+  /* if no start script is specified, but server.js exists,
+   * we can still run "npm start".
+   */
+  if (!g_strv_contains ((const char * const *)(gpointer)all_scripts->data, "start") &&
+      g_file_query_exists (server_js, NULL))
+    g_array_append_val (all_scripts, npm_start);
+
+  ide_strv_sort ((char **)(gpointer)all_scripts->data, all_scripts->len);
+
+  for (guint i = 0; i < all_scripts->len; i++)
+    {
+      const char *script = g_array_index (all_scripts, const char *, i);
+      g_autoptr(IdeRunCommand) run_command = NULL;
+      g_autofree char *id = NULL;
+      g_autofree char *display_name = NULL;
+      int priority;
+
+      if (is_ignored_script (script, (const char * const *)(gpointer)all_scripts->data))
+        continue;
+
+      id = g_strconcat ("npm:", script, NULL);
+      display_name = g_strconcat ("npm run ", script, NULL);
+
+      if (g_strv_contains (IDE_STRV_INIT ("start"), script))
+        priority = -10;
+      else if (g_strv_contains (IDE_STRV_INIT ("stop", "restart"), script))
+        priority = 5;
+      else if (g_strv_contains (IDE_STRV_INIT ("test"), script))
+        priority = 10;
+      else
+        priority = 0;
+
+      run_command = ide_run_command_new ();
+      ide_run_command_set_id (run_command, id);
+      ide_run_command_set_priority (run_command, priority);
+      ide_run_command_set_display_name (run_command, display_name);
+      ide_run_command_set_cwd (run_command, g_file_peek_path (project_dir));
+      ide_run_command_set_argv (run_command, IDE_STRV_INIT ("npm", "run", "--silent", script));
+
+      g_list_store_append (store, run_command);
+    }
+
+complete:
+  ide_task_return_pointer (task, g_steal_pointer (&store), g_object_unref);
+
+  IDE_EXIT;
+}
+
+static void
+gbp_npm_run_command_provider_list_commands_async (IdeRunCommandProvider *provider,
+                                                  GCancellable          *cancellable,
+                                                  GAsyncReadyCallback    callback,
+                                                  gpointer               user_data)
+{
+  GbpNpmRunCommandProvider *self = (GbpNpmRunCommandProvider *)provider;
+  g_autoptr(IdeTask) task = NULL;
+  g_autofree char *project_dir = NULL;
+  g_autofree char *package_json = NULL;
+  IdeBuildSystem *build_system;
+  IdeContext *context;
+
+  IDE_ENTRY;
+
+  g_assert (GBP_IS_NPM_RUN_COMMAND_PROVIDER (self));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  task = ide_task_new (self, cancellable, callback, user_data);
+  ide_task_set_source_tag (task, gbp_npm_run_command_provider_list_commands_async);
+
+  context = ide_object_get_context (IDE_OBJECT (self));
+  build_system = ide_build_system_from_context (context);
+
+  if (!GBP_IS_NPM_BUILD_SYSTEM (build_system))
+    {
+      ide_task_return_new_error (task,
+                                 G_IO_ERROR,
+                                 G_IO_ERROR_NOT_SUPPORTED,
+                                 "Not a npm build system");
+      IDE_EXIT;
+    }
+
+  project_dir = gbp_npm_build_system_get_project_dir (GBP_NPM_BUILD_SYSTEM (build_system));
+  package_json = g_build_filename (project_dir, "package.json", NULL);
+
+  ide_task_set_task_data (task, g_steal_pointer (&package_json), g_free);
+  ide_task_run_in_thread (task, gbp_npm_run_command_provider_list_commands_worker);
+
+  IDE_EXIT;
+}
+
+static GListModel *
+gbp_npm_run_command_provider_list_commands_finish (IdeRunCommandProvider  *provider,
+                                                   GAsyncResult           *result,
+                                                   GError                **error)
+{
+  GListModel *ret;
+
+  IDE_ENTRY;
+
+  g_assert (GBP_IS_NPM_RUN_COMMAND_PROVIDER (provider));
+  g_assert (IDE_IS_TASK (result));
+
+  ret = ide_task_propagate_pointer (IDE_TASK (result), error);
+
+  IDE_RETURN (ret);
+}
+
+static void
+run_command_provider_iface_init (IdeRunCommandProviderInterface *iface)
+{
+  iface->list_commands_async = gbp_npm_run_command_provider_list_commands_async;
+  iface->list_commands_finish = gbp_npm_run_command_provider_list_commands_finish;
+}
+
+G_DEFINE_FINAL_TYPE_WITH_CODE (GbpNpmRunCommandProvider, gbp_npm_run_command_provider, IDE_TYPE_OBJECT,
+                               G_IMPLEMENT_INTERFACE (IDE_TYPE_RUN_COMMAND_PROVIDER, 
run_command_provider_iface_init))
+
+static void
+gbp_npm_run_command_provider_class_init (GbpNpmRunCommandProviderClass *klass)
+{
+}
+
+static void
+gbp_npm_run_command_provider_init (GbpNpmRunCommandProvider *self)
+{
+}
diff --git a/src/plugins/npm/gbp-npm-run-command-provider.h b/src/plugins/npm/gbp-npm-run-command-provider.h
new file mode 100644
index 000000000..96d90904c
--- /dev/null
+++ b/src/plugins/npm/gbp-npm-run-command-provider.h
@@ -0,0 +1,31 @@
+/* gbp-npm-run-command-provider.h
+ *
+ * Copyright 2016-2022 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 <libide-core.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_NPM_RUN_COMMAND_PROVIDER (gbp_npm_run_command_provider_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpNpmRunCommandProvider, gbp_npm_run_command_provider, GBP, NPM_RUN_COMMAND_PROVIDER, 
IdeObject)
+
+G_END_DECLS
diff --git a/src/plugins/npm/meson.build b/src/plugins/npm/meson.build
index 5716ffef9..6dd7bba04 100644
--- a/src/plugins/npm/meson.build
+++ b/src/plugins/npm/meson.build
@@ -1,13 +1,19 @@
 if get_option('plugin_npm')
 
-install_data('npm_plugin.py', install_dir: plugindir)
-
-configure_file(
-          input: 'npm.plugin',
-         output: 'npm.plugin',
-  configuration: config_h,
-        install: true,
-    install_dir: plugindir,
+plugins_sources += files([
+  'npm-plugin.c',
+  'gbp-npm-build-system-discovery.c',
+  'gbp-npm-build-system.c',
+  'gbp-npm-pipeline-addin.c',
+  'gbp-npm-run-command-provider.c',
+])
+
+plugin_npm_resources = gnome.compile_resources(
+  'npm-resources',
+  'npm.gresource.xml',
+  c_name: 'gbp_npm',
 )
 
+plugins_sources += plugin_npm_resources
+
 endif
diff --git a/src/plugins/npm/npm-plugin.c b/src/plugins/npm/npm-plugin.c
new file mode 100644
index 000000000..36fa1e28b
--- /dev/null
+++ b/src/plugins/npm/npm-plugin.c
@@ -0,0 +1,49 @@
+/* npm-plugin.c
+ *
+ * Copyright 2022 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 "npm-plugin"
+
+#include "config.h"
+
+#include <libpeas/peas.h>
+
+#include <libide-foundry.h>
+
+#include "gbp-npm-build-system.h"
+#include "gbp-npm-build-system-discovery.h"
+#include "gbp-npm-pipeline-addin.h"
+#include "gbp-npm-run-command-provider.h"
+
+_IDE_EXTERN void
+_gbp_npm_register_types (PeasObjectModule *module)
+{
+  peas_object_module_register_extension_type (module,
+                                              IDE_TYPE_BUILD_SYSTEM,
+                                              GBP_TYPE_NPM_BUILD_SYSTEM);
+  peas_object_module_register_extension_type (module,
+                                              IDE_TYPE_BUILD_SYSTEM_DISCOVERY,
+                                              GBP_TYPE_NPM_BUILD_SYSTEM_DISCOVERY);
+  peas_object_module_register_extension_type (module,
+                                              IDE_TYPE_PIPELINE_ADDIN,
+                                              GBP_TYPE_NPM_PIPELINE_ADDIN);
+  peas_object_module_register_extension_type (module,
+                                              IDE_TYPE_RUN_COMMAND_PROVIDER,
+                                              GBP_TYPE_NPM_RUN_COMMAND_PROVIDER);
+}
diff --git a/src/plugins/npm/npm.gresource.xml b/src/plugins/npm/npm.gresource.xml
new file mode 100644
index 000000000..8177f7e8c
--- /dev/null
+++ b/src/plugins/npm/npm.gresource.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/plugins/npm">
+    <file>npm.plugin</file>
+  </gresource>
+</gresources>
diff --git a/src/plugins/npm/npm.plugin b/src/plugins/npm/npm.plugin
index c8006a369..b3c3a049e 100644
--- a/src/plugins/npm/npm.plugin
+++ b/src/plugins/npm/npm.plugin
@@ -1,11 +1,11 @@
 [Plugin]
 Authors=Giovanni Campagna <gcampagn cs stanford edu>
 Builtin=true
-Copyright=Copyright © 2016-2018 Christian Hergert, 2017 Giovanni Campagna
+Copyright=Copyright © 2016-2022 Christian Hergert, Copyright © 2017 Giovanni Campagna
 Description=Provides integration with the NPM package system
-Loader=python3
-Module=npm_plugin
-Name=NPM/nodejs
-X-Builder-ABI=@PACKAGE_ABI@
-X-Project-File-Filter-Name=NPM package (package.json)
+Embedded=_gbp_npm_register_types
+Module=npm
+Name=NPM
+X-Category=buildsystems
+X-Project-File-Filter-Name=NPM Package (package.json)
 X-Project-File-Filter-Pattern=package.json


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