[gnome-builder/wip/tingping/meson-template: 1/2] meson-templates: Create plugin



commit 328dcf9d1c9a2a4255c359c0a2693954f6edaf1f
Author: Patrick Griffis <tingping tingping se>
Date:   Sun Oct 30 22:02:23 2016 -0400

    meson-templates: Create plugin

 configure.ac                                       |    2 +
 plugins/Makefile.am                                |    1 +
 .../autotools_templates/__init__.py                |    4 +-
 plugins/meson-templates/Makefile.am                |   22 ++
 plugins/meson-templates/configure.ac               |   12 +
 plugins/meson-templates/meson-templates.plugin     |    9 +
 .../meson-templates/meson_templates/__init__.py    |  212 ++++++++++++++++++++
 .../meson_templates/resources/meson.build          |   19 ++
 .../meson_templates/resources/po/POTFILES          |    1 +
 .../meson_templates/resources/po/meson.build       |   16 ++
 .../meson_templates/resources/src/main.c           |   21 ++
 .../meson_templates/resources/src/meson.build      |   12 +
 12 files changed, 329 insertions(+), 2 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index e0bd388..61d9910 100644
--- a/configure.ac
+++ b/configure.ac
@@ -305,6 +305,7 @@ m4_include([plugins/html-preview/configure.ac])
 m4_include([plugins/jedi/configure.ac])
 m4_include([plugins/jhbuild/configure.ac])
 m4_include([plugins/meson/configure.ac])
+m4_include([plugins/meson-templates/configure.ac])
 m4_include([plugins/mingw/configure.ac])
 m4_include([plugins/project-tree/configure.ac])
 m4_include([plugins/python-gi-imports-completion/configure.ac])
@@ -614,6 +615,7 @@ echo "  HTML Autocompletion .................. : ${enable_html_completion_plugin
 echo "  HTML and Markdown Preview ............ : ${enable_html_preview_plugin}"
 echo "  JHBuild .............................. : ${enable_jhbuild_plugin}"
 echo "  Meson ................................ : ${enable_meson_plugin}"
+echo "  Meson Templates ...................... : ${enable_meson_template_plugin}"
 echo "  MinGW ................................ : ${enable_mingw_plugin}"
 echo "  Project Creation ..................... : ${enable_create_project_plugin}"
 echo "  Project Tree ......................... : ${enable_project_tree_plugin}"
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 7828dac..99fa451 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -26,6 +26,7 @@ SUBDIRS = \
        jhbuild \
        mingw \
        meson \
+       meson-templates \
        project-tree \
        python-gi-imports-completion \
        python-pack \
diff --git a/plugins/autotools-templates/autotools_templates/__init__.py 
b/plugins/autotools-templates/autotools_templates/__init__.py
index 72eb142..e85008f 100644
--- a/plugins/autotools-templates/autotools_templates/__init__.py
+++ b/plugins/autotools-templates/autotools_templates/__init__.py
@@ -287,8 +287,8 @@ class LibraryProjectTemplate(AutotoolsTemplate):
 class EmptyProjectTemplate(AutotoolsTemplate):
     def __init__(self):
         super().__init__(
-            'empty',
-            _("Empty Project"),
+            'empty-autotools',
+            _("Empty Autotools Project"),
             # it would be nice to have a different icon here.
             'pattern-library',
             _("Create a new empty autotools project"),
diff --git a/plugins/meson-templates/Makefile.am b/plugins/meson-templates/Makefile.am
new file mode 100644
index 0000000..d870430
--- /dev/null
+++ b/plugins/meson-templates/Makefile.am
@@ -0,0 +1,22 @@
+if ENABLE_MESON_TEMPLATES
+
+plugindir = $(libdir)/gnome-builder/plugins
+dist_plugin_DATA = meson-templates.plugin
+
+moduledir = $(libdir)/gnome-builder/plugins/meson_templates
+dist_module_DATA = meson_templates/__init__.py
+
+resourcedir = $(datadir)/gnome-builder/plugins/
+nobase_resource_DATA = \
+       meson_templates/resources/meson.build \
+       meson_templates/resources/src/meson.build \
+       meson_templates/resources/src/main.c \
+       meson_templates/resources/po/meson.build \
+       meson_templates/resources/po/LINGUAS \
+       meson_templates/resources/po/POTFILES
+
+EXTRA_DIST = $(nobase_resource_DATA)
+
+endif
+
+-include $(top_srcdir)/git.mk
diff --git a/plugins/meson-templates/configure.ac b/plugins/meson-templates/configure.ac
new file mode 100644
index 0000000..ab4efad
--- /dev/null
+++ b/plugins/meson-templates/configure.ac
@@ -0,0 +1,12 @@
+AC_ARG_ENABLE([meson-templates],
+              [AS_HELP_STRING([--enable-meson-templates=@<:@yes/no@:>@],
+                              [Build with support for creating meson projects.])],
+              [enable_meson_templates=$enableval],
+              [enable_meson_templates=yes])
+
+# for if ENABLE_MESON_TEMPLATE_PLUGIN in Makefile.am
+AM_CONDITIONAL(ENABLE_MESON_TEMPLATES, [test "x$enable_meson_templates" = "xyes"])
+
+# Ensure our makefile is generated by autoconf
+AC_CONFIG_FILES([plugins/meson-templates/Makefile])
+
diff --git a/plugins/meson-templates/meson-templates.plugin b/plugins/meson-templates/meson-templates.plugin
new file mode 100644
index 0000000..1bd65f6
--- /dev/null
+++ b/plugins/meson-templates/meson-templates.plugin
@@ -0,0 +1,9 @@
+[Plugin]
+Name=Meson Templates
+Description=Provides templates for creating meson projects
+Authors=Patrick Griffis <tingping tingping se>
+Copyright=Copyright © 2016 Patrick Griffis
+Builtin=true
+Hidden=true
+Loader=python3
+Module=meson_templates
diff --git a/plugins/meson-templates/meson_templates/__init__.py 
b/plugins/meson-templates/meson_templates/__init__.py
new file mode 100644
index 0000000..a8f8969
--- /dev/null
+++ b/plugins/meson-templates/meson_templates/__init__.py
@@ -0,0 +1,212 @@
+#!/usr/bin/env python3
+# __init__.py
+#
+# Copyright (C) 2016 Patrick Griffis <tingping tingping se>
+#
+# 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/>.
+
+import gi
+import os
+from os import path
+
+gi.require_version('Ide', '1.0')
+gi.require_version('Template', '1.0')
+
+from gi.repository import (
+    Ide,
+    Gio,
+    GLib,
+    GObject,
+    GtkSource,
+    Peas,
+    Template,
+)
+
+_ = Ide.gettext
+
+def get_module_data_path(name):
+    engine = Peas.Engine.get_default()
+    plugin = engine.get_plugin_info('meson_templates')
+    data_dir = plugin.get_data_dir()
+    return path.join(data_dir, name)
+
+class LibraryTemplateProvider(GObject.Object, Ide.TemplateProvider):
+    def do_get_project_templates(self):
+        return [EmptyProjectTemplate()]
+
+class MesonTemplateLocator(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 == None or language == 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)
+
+# Map builder langs to meson ones
+LANGUAGE_MAP = {
+    'c': 'c',
+    'c++': 'cpp',
+}
+
+class MesonTemplate(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
+        self.locator = MesonTemplateLocator()
+
+        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('project_version').assign_string('0.1.0')
+        scope.get('enable_i18n').assign_boolean(True)
+        scope.get('language').assign_string(LANGUAGE_MAP[self.language])
+        scope.get('author').assign_string(author_name)
+
+        modes = {
+        }
+
+        expands = {
+        }
+
+        files = {
+            'resources/meson.build': 'meson.build',
+            'resources/src/meson.build': 'src/meson.build',
+
+            # Translations
+            'resources/po/LINGUAS': 'po/LINGUAS',
+            'resources/po/meson.build': 'po/meson.build',
+            'resources/po/POTFILES': 'po/POTFILES',
+        }
+
+        if self.language == 'c':
+            files['resources/src/main.c'] = 'src/main.c'
+        elif self.language == 'c++':
+            files['resources/src/main.c'] = 'src/main.cpp'
+
+        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 = get_module_data_path(src)
+                self.add_path(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 EmptyProjectTemplate(MesonTemplate):
+    def __init__(self):
+        super().__init__(
+            'empty-meson',
+            _('Empty Meson Project'),
+            'pattern-library',
+            _('Create a new empty meson project'),
+            ['C', 'C++']
+         )
+
+
diff --git a/plugins/meson-templates/meson_templates/resources/meson.build 
b/plugins/meson-templates/meson_templates/resources/meson.build
new file mode 100644
index 0000000..af6194b
--- /dev/null
+++ b/plugins/meson-templates/meson_templates/resources/meson.build
@@ -0,0 +1,19 @@
+project('{{name}}', '{{language}}',
+  version: '{{project_version}}',
+  meson_version: '>= 0.36.0',
+)
+
+config_h = configuration_data()
+config_h.set_quoted('GETTEXT_PACKAGE', '{{name}}')
+config_h.set_quoted('LOCALEDIR', join_paths(get_option('prefix'), get_option('localedir')))
+configure_file(
+  output: 'config.h',
+  configuration: config_h,
+)
+add_global_arguments([
+  '-DHAVE_CONFIG_H',
+  '-I' + meson.build_root(),
+], language: '{{language}}')
+
+subdir('src')
+{{if enable_i18n}}subdir('po'){{end}}
diff --git a/plugins/meson-templates/meson_templates/resources/po/LINGUAS 
b/plugins/meson-templates/meson_templates/resources/po/LINGUAS
new file mode 100644
index 0000000..e69de29
diff --git a/plugins/meson-templates/meson_templates/resources/po/POTFILES 
b/plugins/meson-templates/meson_templates/resources/po/POTFILES
new file mode 100644
index 0000000..e723152
--- /dev/null
+++ b/plugins/meson-templates/meson_templates/resources/po/POTFILES
@@ -0,0 +1 @@
+src/main.c
diff --git a/plugins/meson-templates/meson_templates/resources/po/meson.build 
b/plugins/meson-templates/meson_templates/resources/po/meson.build
new file mode 100644
index 0000000..cf0948b
--- /dev/null
+++ b/plugins/meson-templates/meson_templates/resources/po/meson.build
@@ -0,0 +1,16 @@
+i18n = import('i18n')
+
+langs = [
+  # TODO: Translate app
+]
+
+if langs.length() > 0
+  i18n.gettext('{{name}}',
+    languages: langs,
+    args: [
+      '--from-code=UTF-8',
+      '--keyword=g_dngettext:2,3',
+      '--add-comments',
+    ],
+  )
+endif
diff --git a/plugins/meson-templates/meson_templates/resources/src/main.c 
b/plugins/meson-templates/meson_templates/resources/src/main.c
new file mode 100644
index 0000000..96fd552
--- /dev/null
+++ b/plugins/meson-templates/meson_templates/resources/src/main.c
@@ -0,0 +1,21 @@
+{{include "license.c"}}
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+{{if enable_i18n}}#include <glib/gi18n.h>{{end}}
+#include <glib.h>
+
+int
+main(int   argc,
+     char *argv[])
+{
+{{if enable_i18n}}
+  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
+       bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+       textdomain (GETTEXT_PACKAGE);
+{{end}}
+
+  return 0;
+}
diff --git a/plugins/meson-templates/meson_templates/resources/src/meson.build 
b/plugins/meson-templates/meson_templates/resources/src/meson.build
new file mode 100644
index 0000000..cc421d4
--- /dev/null
+++ b/plugins/meson-templates/meson_templates/resources/src/meson.build
@@ -0,0 +1,12 @@
+{{name_}}_sources = [
+  '{{if language == "c"}}main.c{{else if language == "cpp"}}main.cpp{{end}}',
+]
+
+{{name_}}_deps = [
+  dependency('glib-2.0'),
+]
+
+executable('{{name}}', {{name_}}_sources,
+  dependencies: {{name_}}_deps,
+  install: true,
+)


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