[gnome-builder/wip/gtk4-port] plugins/make: add simple makefile template



commit 638759ea4a3d57427f11dcc11b369f2934074db4
Author: Christian Hergert <chergert redhat com>
Date:   Tue Jun 7 13:10:29 2022 -0700

    plugins/make: add simple makefile template

 src/plugins/make/gbp-make-template-provider.c |  23 +++++
 src/plugins/make/gbp-make-template.c          | 122 ++++++++++++++++++++++++++
 src/plugins/make/resources/Makefile           |   9 +-
 3 files changed, 149 insertions(+), 5 deletions(-)
---
diff --git a/src/plugins/make/gbp-make-template-provider.c b/src/plugins/make/gbp-make-template-provider.c
index 60c0c21bf..e78ed177f 100644
--- a/src/plugins/make/gbp-make-template-provider.c
+++ b/src/plugins/make/gbp-make-template-provider.c
@@ -23,8 +23,11 @@
 
 #include "config.h"
 
+#include <glib/gi18n.h>
+
 #include <libide-projects.h>
 
+#include "gbp-make-template.h"
 #include "gbp-make-template-provider.h"
 
 struct _GbpMakeTemplateProvider
@@ -32,9 +35,29 @@ struct _GbpMakeTemplateProvider
   GObject parent_instance;
 };
 
+GList *
+gbp_make_template_provider_get_project_templates (IdeTemplateProvider *provider)
+{
+  GList *list = NULL;
+
+  g_assert (GBP_IS_MAKE_TEMPLATE_PROVIDER (provider));
+
+  list = g_list_prepend (list,
+                         g_object_new (GBP_TYPE_MAKE_TEMPLATE,
+                                       "id", "empty-makefile",
+                                       "name", _("Empty Makefile Project"),
+                                       "description", _("Create a new empty project using a simple 
Makefile"),
+                                       "languages", IDE_STRV_INIT ("C", "C++"),
+                                       "priority", 1000,
+                                       NULL));
+
+  return list;
+}
+
 static void
 template_provider_iface_init (IdeTemplateProviderInterface *iface)
 {
+  iface->get_project_templates = gbp_make_template_provider_get_project_templates;
 }
 
 G_DEFINE_FINAL_TYPE_WITH_CODE (GbpMakeTemplateProvider, gbp_make_template_provider, G_TYPE_OBJECT,
diff --git a/src/plugins/make/gbp-make-template.c b/src/plugins/make/gbp-make-template.c
index da8cd90a9..0547a63bc 100644
--- a/src/plugins/make/gbp-make-template.c
+++ b/src/plugins/make/gbp-make-template.c
@@ -22,6 +22,8 @@
 
 #include "config.h"
 
+#include <libide-threading.h>
+
 #include "gbp-make-template.h"
 
 struct _GbpMakeTemplate
@@ -31,9 +33,129 @@ struct _GbpMakeTemplate
 
 G_DEFINE_FINAL_TYPE (GbpMakeTemplate, gbp_make_template, IDE_TYPE_PROJECT_TEMPLATE)
 
+static const struct {
+  const char *language;
+  const char *resource;
+  const char *path;
+  int mode;
+} mappings[] = {
+  { NULL, "/plugins/make/resources/Makefile", "Makefile", 0640 },
+  { "C", "/plugins/make/resources/main.c", "main.c", 0640 },
+  { "C++", "/plugins/make/resources/main.cpp", "main.cpp", 0640 },
+};
+
+static void
+gbp_make_template_expand_cb (GObject      *object,
+                             GAsyncResult *result,
+                             gpointer      user_data)
+{
+  GbpMakeTemplate *self = (GbpMakeTemplate *)object;
+  g_autoptr(IdeTask) task = user_data;
+  g_autoptr(GError) error = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (GBP_IS_MAKE_TEMPLATE (self));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (IDE_IS_TASK (task));
+
+  if (!ide_template_base_expand_all_finish (IDE_TEMPLATE_BASE (self), result, &error))
+    ide_task_return_error (task, g_steal_pointer (&error));
+  else
+    ide_task_return_boolean (task, TRUE);
+
+  IDE_EXIT;
+}
+
+static void
+gbp_make_template_expand_async (IdeProjectTemplate  *template,
+                                IdeTemplateInput    *input,
+                                TmplScope           *scope,
+                                GCancellable        *cancellable,
+                                GAsyncReadyCallback  callback,
+                                gpointer             user_data)
+{
+  GbpMakeTemplate *self = (GbpMakeTemplate *)template;
+  g_autoptr(IdeTask) task = NULL;
+  g_autoptr(GFile) destdir = NULL;
+  g_autofree char *exec_name = NULL;
+  const char *language;
+  const char *name;
+  GFile *directory;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_PROJECT_TEMPLATE (self));
+  g_assert (IDE_IS_TEMPLATE_INPUT (input));
+  g_assert (scope != NULL);
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  task = ide_task_new (self, cancellable, callback, user_data);
+  ide_task_set_source_tag (task, gbp_make_template_expand_async);
+
+  language = ide_template_input_get_language (input);
+
+  if (!g_strv_contains (IDE_STRV_INIT ("C", "C++"), language))
+    {
+      language = "C";
+      tmpl_scope_set_string (scope, "language", "c");
+    }
+
+  directory = ide_template_input_get_directory (input);
+  name = ide_template_input_get_name (input);
+  destdir = g_file_get_child (directory, name);
+
+  exec_name = g_strdelimit (g_strstrip (g_strdup (name)), " \t\n", '-');
+  tmpl_scope_set_string (scope, "exec_name", exec_name);
+
+  for (guint i = 0; i < G_N_ELEMENTS (mappings); i++)
+    {
+      g_autoptr(GFile) child = NULL;
+
+      if (mappings[i].language != NULL &&
+          !ide_str_equal0 (mappings[i].language, language))
+        continue;
+
+      child = g_file_get_child (destdir, mappings[i].path);
+      ide_template_base_add_resource (IDE_TEMPLATE_BASE (self),
+                                      mappings[i].resource,
+                                      child,
+                                      scope,
+                                      mappings[i].mode);
+    }
+
+  ide_template_base_expand_all_async (IDE_TEMPLATE_BASE (self),
+                                      cancellable,
+                                      gbp_make_template_expand_cb,
+                                      g_steal_pointer (&task));
+
+  IDE_EXIT;
+}
+
+static gboolean
+gbp_make_template_expand_finish (IdeProjectTemplate  *template,
+                                 GAsyncResult        *result,
+                                 GError             **error)
+{
+  gboolean ret;
+
+  IDE_ENTRY;
+
+  g_assert (GBP_IS_MAKE_TEMPLATE (template));
+  g_assert (IDE_IS_TASK (result));
+
+  ret = ide_task_propagate_boolean (IDE_TASK (result), error);
+
+  IDE_RETURN (ret);
+}
+
 static void
 gbp_make_template_class_init (GbpMakeTemplateClass *klass)
 {
+  IdeProjectTemplateClass *template_class = IDE_PROJECT_TEMPLATE_CLASS (klass);
+
+  template_class->expand_async = gbp_make_template_expand_async;
+  template_class->expand_finish = gbp_make_template_expand_finish;
 }
 
 static void
diff --git a/src/plugins/make/resources/Makefile b/src/plugins/make/resources/Makefile
index 859dd34b3..c906391ee 100644
--- a/src/plugins/make/resources/Makefile
+++ b/src/plugins/make/resources/Makefile
@@ -14,17 +14,16 @@ STANDARD = -std=c++2a
 {{end}}
 
 {{if language == "c"}}
-{{exec_name}}: Makefile {{exec_name}}.c
-       $(CC) -o $@ $(WARNINGS) $(DEBUG) $(OPTIMIZE) {{exec_name}}.c
+{{exec_name}}: Makefile main.c
+       $(CC) -o $@ $(WARNINGS) $(DEBUG) $(OPTIMIZE) main.c
 {{else if language == "c++"}}
-{{exec_name}}: Makefile {{exec_name}}.cpp
-       $(CXX) -o $@ $(WARNINGS) $(DEBUG) $(OPTIMIZE) $(STANDARD) {{exec_name}}.cpp
+{{exec_name}}: Makefile main.cpp
+       $(CXX) -o $@ $(WARNINGS) $(DEBUG) $(OPTIMIZE) $(STANDARD) main.cpp
 {{end}}
 
 clean:
        rm -f {{exec_name}}
 
-
 # Builder will call this to install the application before running.
 install:
        echo "Installing is not supported"


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