[gnome-builder/gnome-builder-43] plugins/shellcheck: add shellcheck plugin
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder/gnome-builder-43] plugins/shellcheck: add shellcheck plugin
- Date: Thu, 6 Oct 2022 03:01:18 +0000 (UTC)
commit 00ae476b132f00fcfa72cfab59d51e9f5160d5ab
Author: Christian Hergert <chergert redhat com>
Date: Wed Oct 5 19:42:08 2022 -0700
plugins/shellcheck: add shellcheck plugin
The format is somewhat similar to eslint when using JSON.
Fixes #1846
meson_options.txt | 1 +
src/plugins/meson.build | 1 +
.../gbp-shellcheck-diagnostic-provider.c | 179 +++++++++++++++++++++
.../gbp-shellcheck-diagnostic-provider.h | 31 ++++
src/plugins/shellcheck/meson.build | 16 ++
src/plugins/shellcheck/shellcheck-plugin.c | 37 +++++
src/plugins/shellcheck/shellcheck.gresource.xml | 6 +
src/plugins/shellcheck/shellcheck.plugin | 10 ++
8 files changed, 281 insertions(+)
---
diff --git a/meson_options.txt b/meson_options.txt
index 700232d91..e98074798 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -70,6 +70,7 @@ option('plugin_rstcheck', type: 'boolean')
option('plugin_rubocop', type: 'boolean')
option('plugin_rust_analyzer', type: 'boolean')
option('plugin_shellcmd', type: 'boolean')
+option('plugin_shellcheck', type: 'boolean')
option('plugin_spellcheck', type: 'boolean')
option('plugin_sphinx_preview', type: 'boolean')
option('plugin_stylelint', type: 'boolean')
diff --git a/src/plugins/meson.build b/src/plugins/meson.build
index 01da6e064..8b7c22396 100644
--- a/src/plugins/meson.build
+++ b/src/plugins/meson.build
@@ -114,6 +114,7 @@ subdir('rubocop')
subdir('rust-analyzer')
subdir('sdkui')
subdir('sessionui')
+subdir('shellcheck')
subdir('shellcmd')
subdir('snippets')
subdir('spellcheck')
diff --git a/src/plugins/shellcheck/gbp-shellcheck-diagnostic-provider.c
b/src/plugins/shellcheck/gbp-shellcheck-diagnostic-provider.c
new file mode 100644
index 000000000..9042b79c7
--- /dev/null
+++ b/src/plugins/shellcheck/gbp-shellcheck-diagnostic-provider.c
@@ -0,0 +1,179 @@
+/* gbp-shellcheck-diagnostic-provider.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 "gbp-shellcheck-diagnostic-provider"
+
+#include "config.h"
+
+#include <json-glib/json-glib.h>
+
+#include "gbp-shellcheck-diagnostic-provider.h"
+
+struct _GbpShellcheckDiagnosticProvider
+{
+ IdeDiagnosticTool parent_instance;
+};
+
+G_DEFINE_FINAL_TYPE (GbpShellcheckDiagnosticProvider, gbp_shellcheck_diagnostic_provider,
IDE_TYPE_DIAGNOSTIC_TOOL)
+
+static gboolean
+gbp_shellcheck_diagnostic_provider_prepare_run_context (IdeDiagnosticTool *tool,
+ IdeRunContext *run_context,
+ GFile *file,
+ GBytes *contents,
+ const char *language_id,
+ GError **error)
+{
+ GbpShellcheckDiagnosticProvider *self = (GbpShellcheckDiagnosticProvider *)tool;
+
+ g_assert (GBP_IS_SHELLCHECK_DIAGNOSTIC_PROVIDER (self));
+ g_assert (IDE_IS_RUN_CONTEXT (run_context));
+ g_assert (G_IS_FILE (file));
+
+ if (!IDE_DIAGNOSTIC_TOOL_CLASS (gbp_shellcheck_diagnostic_provider_parent_class)->prepare_run_context
(tool, run_context, file, contents, language_id, error))
+ return FALSE;
+
+ ide_run_context_append_argv (run_context, "--format=json");
+
+ if (contents != NULL)
+ ide_run_context_append_argv (run_context, "-");
+ else
+ ide_run_context_append_argv (run_context, g_file_peek_path (file));
+
+ return TRUE;
+}
+
+static inline IdeDiagnosticSeverity
+parse_severity (const char *level)
+{
+ if (ide_str_equal0 (level, "error"))
+ return IDE_DIAGNOSTIC_ERROR;
+
+ if (ide_str_equal0 (level, "warning"))
+ return IDE_DIAGNOSTIC_WARNING;
+
+ if (ide_str_equal0 (level, "info"))
+ return IDE_DIAGNOSTIC_NOTE;
+
+ if (ide_str_equal0 (level, "style"))
+ return IDE_DIAGNOSTIC_NOTE;
+
+ return IDE_DIAGNOSTIC_NOTE;
+}
+
+static void
+gbp_shellcheck_diagnostic_provider_populate_diagnostics (IdeDiagnosticTool *tool,
+ IdeDiagnostics *diagnostics,
+ GFile *file,
+ const char *stdout_buf,
+ const char *stderr_buf)
+{
+ GbpShellcheckDiagnosticProvider *self = (GbpShellcheckDiagnosticProvider *)tool;
+ g_autoptr(JsonParser) parser = NULL;
+ g_autoptr(GError) error = NULL;
+ JsonNode *root;
+ JsonArray *results;
+
+ g_assert (GBP_IS_SHELLCHECK_DIAGNOSTIC_PROVIDER (self));
+ g_assert (IDE_IS_DIAGNOSTICS (diagnostics));
+ g_assert (G_IS_FILE (file));
+
+ if (ide_str_empty0 (stdout_buf))
+ return;
+
+#if 0
+ [{"file":"-","line":1,"endLine":1,"column":1,"endColumn":1,"level":"error","code":1073,"message":"Couldn't
parse this function. Fix to allow more checks.","fix":null},
+
{"file":"-","line":1,"endLine":1,"column":7,"endColumn":7,"level":"error","code":1064,"message":"Expected a {
to open the function definition.","fix":null},
+ {"file":"-","line":1,"endLine":1,"column":7,"endColumn":7,"level":"error","code":1072,"message":"Fix any
mentioned problems and try again.","fix":null}]
+#endif
+
+ parser = json_parser_new ();
+
+ if (!json_parser_load_from_data (parser, stdout_buf, -1, &error))
+ {
+ g_debug ("%s", error->message);
+ return;
+ }
+
+ if ((root = json_parser_get_root (parser)) &&
+ JSON_NODE_HOLDS_ARRAY (root) &&
+ (results = json_node_get_array (root)))
+ {
+ guint n_results = json_array_get_length (results);
+
+ for (guint r = 0; r < n_results; r++)
+ {
+ JsonObject *message = json_array_get_object_element (results, r);
+ g_autoptr(IdeDiagnostic) diagnostic = NULL;
+ g_autoptr(IdeLocation) start = NULL;
+ g_autoptr(IdeLocation) end = NULL;
+ IdeDiagnosticSeverity severity;
+ const char *level;
+ guint start_line;
+ guint start_col;
+
+ if (!json_object_has_member (message, "file") ||
+ !json_object_has_member (message, "line"))
+ continue;
+
+ start_line = MAX (json_object_get_int_member (message, "line"), 1) - 1;
+ start_col = MAX (json_object_get_int_member (message, "column"), 1) - 1;
+ start = ide_location_new (file, start_line, start_col);
+
+ if (json_object_has_member (message, "endLine") &&
+ json_object_has_member (message, "endColumn"))
+ {
+ guint end_line = MAX (json_object_get_int_member (message, "endLine"), 1) - 1;
+ guint end_col = MAX (json_object_get_int_member (message, "endColumn"), 1) - 1;
+
+ end = ide_location_new (file, end_line, end_col);
+ }
+
+ if ((level = json_object_get_string_member (message, "level")))
+ severity = parse_severity (level);
+ else
+ severity = IDE_DIAGNOSTIC_ERROR;
+
+ diagnostic = ide_diagnostic_new (severity,
+ json_object_get_string_member (message, "message"),
+ start);
+
+ if (end != NULL)
+ ide_diagnostic_take_range (diagnostic, ide_range_new (start, end));
+
+ ide_diagnostics_add (diagnostics, diagnostic);
+ }
+ }
+}
+
+static void
+gbp_shellcheck_diagnostic_provider_class_init (GbpShellcheckDiagnosticProviderClass *klass)
+{
+ IdeDiagnosticToolClass *diagnostic_tool_class = IDE_DIAGNOSTIC_TOOL_CLASS (klass);
+
+ diagnostic_tool_class->prepare_run_context = gbp_shellcheck_diagnostic_provider_prepare_run_context;
+ diagnostic_tool_class->populate_diagnostics = gbp_shellcheck_diagnostic_provider_populate_diagnostics;
+}
+
+static void
+gbp_shellcheck_diagnostic_provider_init (GbpShellcheckDiagnosticProvider *self)
+{
+ ide_diagnostic_tool_set_program_name (IDE_DIAGNOSTIC_TOOL (self), "shellcheck");
+}
diff --git a/src/plugins/shellcheck/gbp-shellcheck-diagnostic-provider.h
b/src/plugins/shellcheck/gbp-shellcheck-diagnostic-provider.h
new file mode 100644
index 000000000..ea39f45d4
--- /dev/null
+++ b/src/plugins/shellcheck/gbp-shellcheck-diagnostic-provider.h
@@ -0,0 +1,31 @@
+/* gbp-shellcheck-diagnostic-provider.h
+ *
+ * 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
+ */
+
+#pragma once
+
+#include <libide-foundry.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_SHELLCHECK_DIAGNOSTIC_PROVIDER (gbp_shellcheck_diagnostic_provider_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpShellcheckDiagnosticProvider, gbp_shellcheck_diagnostic_provider, GBP,
SHELLCHECK_DIAGNOSTIC_PROVIDER, IdeDiagnosticTool)
+
+G_END_DECLS
diff --git a/src/plugins/shellcheck/meson.build b/src/plugins/shellcheck/meson.build
new file mode 100644
index 000000000..e0ce11f57
--- /dev/null
+++ b/src/plugins/shellcheck/meson.build
@@ -0,0 +1,16 @@
+if get_option('plugin_shellcheck')
+
+plugins_sources += files([
+ 'shellcheck-plugin.c',
+ 'gbp-shellcheck-diagnostic-provider.c',
+])
+
+plugin_shellcheck_resources = gnome.compile_resources(
+ 'shellcheck-resources',
+ 'shellcheck.gresource.xml',
+ c_name: 'gbp_shellcheck',
+)
+
+plugins_sources += plugin_shellcheck_resources
+
+endif
diff --git a/src/plugins/shellcheck/shellcheck-plugin.c b/src/plugins/shellcheck/shellcheck-plugin.c
new file mode 100644
index 000000000..d9e46e7a4
--- /dev/null
+++ b/src/plugins/shellcheck/shellcheck-plugin.c
@@ -0,0 +1,37 @@
+/* shellcheck-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 "shellcheck-plugin"
+
+#include "config.h"
+
+#include <libpeas/peas.h>
+
+#include <libide-code.h>
+
+#include "gbp-shellcheck-diagnostic-provider.h"
+
+_IDE_EXTERN void
+_gbp_shellcheck_register_types (PeasObjectModule *module)
+{
+ peas_object_module_register_extension_type (module,
+ IDE_TYPE_DIAGNOSTIC_PROVIDER,
+ GBP_TYPE_SHELLCHECK_DIAGNOSTIC_PROVIDER);
+}
diff --git a/src/plugins/shellcheck/shellcheck.gresource.xml b/src/plugins/shellcheck/shellcheck.gresource.xml
new file mode 100644
index 000000000..02e417d6a
--- /dev/null
+++ b/src/plugins/shellcheck/shellcheck.gresource.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+ <gresource prefix="/plugins/shellcheck">
+ <file>shellcheck.plugin</file>
+ </gresource>
+</gresources>
diff --git a/src/plugins/shellcheck/shellcheck.plugin b/src/plugins/shellcheck/shellcheck.plugin
new file mode 100644
index 000000000..f72eac67c
--- /dev/null
+++ b/src/plugins/shellcheck/shellcheck.plugin
@@ -0,0 +1,10 @@
+[Plugin]
+Authors=Christian Hergert <chergert redhat com>
+Copyright=Copyright © 2022 Christian Hergert
+Description=Provides linting of shell scripts using shellcheck
+Embedded=_gbp_shellcheck_register_types
+Module=shellcheck
+Name=Shellcheck
+X-Category=diagnostics
+X-Diagnostic-Provider-Languages-Priority=100
+X-Diagnostic-Provider-Languages=sh
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]