[gnome-builder] make: add simple makefile template



commit 1f05d712ce8bb0f10ca4d8de589c6c6c3e6b06bd
Author: Christian Hergert <chergert redhat com>
Date:   Sat Jan 20 15:53:15 2018 -0800

    make: add simple makefile template
    
    This is a simple makefile with the targets necessary for basic integration
    in Builder. We still can't debug these sort of projects (because we don't
    have control of the inferior executed via "Run").

 src/plugins/make/make.gresource.xml   |   8 ++
 src/plugins/make/make_plugin.py       | 175 ++++++++++++++++++++++++++++++++++
 src/plugins/make/meson.build          |   8 ++
 src/plugins/make/resources/.gitignore |   4 +
 src/plugins/make/resources/Makefile   |  23 +++++
 src/plugins/make/resources/main.c     |  13 +++
 6 files changed, 231 insertions(+)
---
diff --git a/src/plugins/make/make.gresource.xml b/src/plugins/make/make.gresource.xml
new file mode 100644
index 000000000..ef074e9be
--- /dev/null
+++ b/src/plugins/make/make.gresource.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/org/gnome/builder/plugins/make_plugin">
+    <file compressed="true">resources/Makefile</file>
+    <file compressed="true">resources/main.c</file>
+    <file compressed="true">resources/.gitignore</file>
+  </gresource>
+</gresources>
diff --git a/src/plugins/make/make_plugin.py b/src/plugins/make/make_plugin.py
index e79f315aa..10fa5ae15 100644
--- a/src/plugins/make/make_plugin.py
+++ b/src/plugins/make/make_plugin.py
@@ -16,13 +16,18 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 import gi
+import os
+from os import path
 
 gi.require_version('Ide', '1.0')
+gi.require_version('Template', '1.0')
 
 from gi.repository import GObject
 from gi.repository import Gio
 from gi.repository import GLib
+from gi.repository import GtkSource
 from gi.repository import Ide
+from gi.repository import Template
 
 _ = Ide.gettext
 
@@ -189,3 +194,173 @@ class MakeBuildTargetProvider(Ide.Object, Ide.BuildTargetProvider):
     def do_get_targets_finish(self, result):
         if result.propagate_boolean():
             return result.targets
+
+class MakeProjectTemplateProvider(GObject.Object, Ide.TemplateProvider):
+    def do_get_project_templates(self):
+        return [SimpleMakefileTemplate()]
+
+class MakeTemplateLocator(Template.TemplateLocator):
+    license = None
+
+    def empty(self):
+        return Gio.MemoryInputStream()
+
+    def do_locate(self, path):
+        if path.startswith('license.'):
+            filename = GLib.basename(path)
+            manager = GtkSource.LanguageManager.get_default()
+            language = manager.guess_language(filename, None)
+
+            if self.license is None or language is None:
+                return self.empty()
+
+            header = Ide.language_format_header(language, self.license)
+            gbytes = GLib.Bytes(header.encode())
+
+            return Gio.MemoryInputStream.new_from_bytes(gbytes)
+
+        return super().do_locate(self, path)
+
+class MakeTemplateBase(Ide.TemplateBase, Ide.ProjectTemplate):
+    def __init__(self, id, name, icon_name, description, languages):
+        super().__init__()
+        self.id = id
+        self.name = name
+        self.icon_name = icon_name
+        self.description = description
+        self.languages = languages
+
+        # Work around https://bugzilla.gnome.org/show_bug.cgi?id=687522
+        self.locator = MakeTemplateLocator()
+        self.props.locator = self.locator
+
+    def do_get_id(self):
+        return self.id
+
+    def do_get_name(self):
+        return self.name
+
+    def do_get_icon_name(self):
+        return self.icon_name
+
+    def do_get_description(self):
+        return self.description
+
+    def do_get_languages(self):
+        return self.languages
+
+    def do_expand_async(self, params, cancellable, callback, data):
+        self.reset()
+
+        task = Gio.Task.new(self, cancellable, callback)
+
+        if 'language' in params:
+            self.language = params['language'].get_string().lower()
+        else:
+            self.language = 'c'
+
+        if self.language not in ('c', 'c++'):
+            task.return_error(GLib.Error('Language %s not supported' % self.language))
+            return
+
+        if 'versioning' in params:
+            self.versioning = params['versioning'].get_string()
+        else:
+            self.versioning = ''
+
+        if 'author' in params:
+            author_name = params['author'].get_string()
+        else:
+            author_name = GLib.get_real_name()
+
+        scope = Template.Scope.new()
+        scope.get('template').assign_string(self.id)
+
+        name = params['name'].get_string().lower()
+        name_ = name.lower().replace('-', '_')
+        scope.get('name').assign_string(name)
+        scope.get('name_').assign_string(name_)
+        scope.get('NAME').assign_string(name.upper().replace('-','_'))
+
+        prefix = name if not name.endswith('-glib') else name[:-5]
+        PREFIX = prefix.upper().replace('-','_')
+        prefix_ = prefix.lower().replace('-','_')
+        PreFix = ''.join([word.capitalize() for word in prefix.lower().split('-')])
+
+        scope.get('prefix').assign_string(prefix)
+        scope.get('Prefix').assign_string(prefix.capitalize())
+        scope.get('PreFix').assign_string(PreFix)
+        scope.get('prefix_').assign_string(prefix_)
+        scope.get('PREFIX').assign_string(PREFIX)
+
+        scope.get('language').assign_string(self.language)
+        scope.get('author').assign_string(author_name)
+        scope.get('exec_name').assign_string(name)
+
+        expands = {
+            'prefix': prefix,
+            'name_': name_,
+            'name': name,
+            'exec_name': name,
+        }
+
+        files = {
+            'resources/.gitignore': '.gitignore',
+            'resources/Makefile': 'Makefile',
+            'resources/main.c': '%(exec_name)s.c',
+        }
+        self.prepare_files(files)
+
+        modes = {}
+
+        if 'license_full' in params:
+            license_full_path = params['license_full'].get_string()
+            files[license_full_path] = 'COPYING'
+
+        if 'license_short' in params:
+            license_short_path = params['license_short'].get_string()
+            license_base = Gio.resources_lookup_data(license_short_path[11:], 0).get_data().decode()
+            self.locator.license = license_base
+
+        if 'path' in params:
+            dir_path = params['path'].get_string()
+        else:
+            dir_path = name
+        directory = Gio.File.new_for_path(dir_path)
+        scope.get('project_path').assign_string(directory.get_path())
+
+        for src, dst in files.items():
+            destination = directory.get_child(dst % expands)
+            if src.startswith('resource://'):
+                self.add_resource(src[11:], destination, scope, modes.get(src, 0))
+            else:
+                path = os.path.join('/org/gnome/builder/plugins/make_plugin', src)
+                self.add_resource(path, destination, scope, modes.get(src, 0))
+
+        self.expand_all_async(cancellable, self.expand_all_cb, task)
+
+    def do_expand_finish(self, result):
+        return result.propagate_boolean()
+
+    def expand_all_cb(self, obj, result, task):
+        try:
+            self.expand_all_finish(result)
+            task.return_boolean(True)
+        except Exception as exc:
+            if isinstance(exc, GLib.Error):
+                task.return_error(exc)
+            else:
+                task.return_error(GLib.Error(repr(exc)))
+
+class SimpleMakefileTemplate(MakeTemplateBase):
+    def __init__(self):
+        super().__init__(
+            'empty-makefile',
+            _('Empty Makefile Project'),
+            'pattern-cli',
+            _('Create a new empty project using a simple Makefile'),
+            ['C', 'C++'],
+         )
+
+    def prepare_files(self, files):
+        pass
diff --git a/src/plugins/make/meson.build b/src/plugins/make/meson.build
index bd398a904..e9768ef44 100644
--- a/src/plugins/make/meson.build
+++ b/src/plugins/make/meson.build
@@ -1,5 +1,13 @@
 if get_option('with_make')
 
+make_resources = gnome.compile_resources(
+  'make_plugin',
+  'make.gresource.xml',
+  gresource_bundle: true,
+           install: true,
+       install_dir: plugindir,
+)
+
 install_data('make_plugin.py', install_dir: plugindir)
 
 configure_file(
diff --git a/src/plugins/make/resources/.gitignore b/src/plugins/make/resources/.gitignore
new file mode 100644
index 000000000..ba1d643a1
--- /dev/null
+++ b/src/plugins/make/resources/.gitignore
@@ -0,0 +1,4 @@
+*~
+*.swp
+{{exec_name}}
+
diff --git a/src/plugins/make/resources/Makefile b/src/plugins/make/resources/Makefile
new file mode 100644
index 000000000..22735a6f1
--- /dev/null
+++ b/src/plugins/make/resources/Makefile
@@ -0,0 +1,23 @@
+all: {{exec_name}}
+
+
+WARNINGS = -Wall
+DEBUG = -ggdb -fno-omit-frame-pointer
+OPTIMIZE = -O2
+
+{{exec_name}}: Makefile {{exec_name}}.c
+       $(CC) -o $@ $(WARNINGS) $(DEBUG) $(OPTIMIZE) {{exec_name}}.c
+
+clean:
+       rm -f {{exec_name}}
+
+
+# Builder will call this to install the application before running.
+install:
+       echo "Installing is not supported"
+
+# Builder uses this target to run your application.
+run:
+       ./{{exec_name}}
+
+
diff --git a/src/plugins/make/resources/main.c b/src/plugins/make/resources/main.c
new file mode 100644
index 000000000..b20db3962
--- /dev/null
+++ b/src/plugins/make/resources/main.c
@@ -0,0 +1,13 @@
+{{include "license.c"}}
+
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int
+main (int argc,
+      char *argv[])
+{
+  printf ("Hello, World!\n");
+  return EXIT_SUCCESS;
+}


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