commit 34dc6053c2de1c58b0e3b516da9780cf10b176b6 Author: a-Sansara Date: Mon Aug 27 11:20:36 2018 +0200 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..ab62866 --- /dev/null +++ b/build.sh @@ -0,0 +1,13 @@ +valac -v \ +--pkg gee-0.8 \ +--pkg gio-2.0 \ +--pkg pluie-echo-0.2 \ +--pkg pluie-yaml-0.5 \ +src/Pluie/Berel.Meta.vala \ +src/Pluie/Berel.HeaderDef.vala \ +src/Pluie/Berel.Sepline.vala \ +src/Pluie/Berel.Comment.vala \ +src/Pluie/Berel.Extension.vala \ +src/Pluie/Berel.Key.vala \ +src/Pluie/Berel.Keys.vala \ +main.vala -o main diff --git a/main.vala b/main.vala new file mode 100644 index 0000000..30c197a --- /dev/null +++ b/main.vala @@ -0,0 +1,36 @@ +using GLib; +using Gee; +using Pluie; + +int main (string[] args) +{ + Echo.init(false); + + var path = "./resources/berel.yml"; + of.echo (path); + var done = false; + + of.title ("Pluie Yaml Library", Pluie.Yaml.VERSION, "a-sansara"); + Pluie.Yaml.DEBUG = true; + var config = new Yaml.Config (path, true); + var root = config.root_node (); + if ((done = root != null)) { + root.display_childs (); + var berel = (Berel.Meta) Yaml.Builder.from_node (root.first ()); + of.echo("software : %s %s".printf (berel.keys.software.name, berel.keys.software.address)); + of.echo("version : %s".printf (berel.keys.version)); + of.echo("type : %s".printf (berel.keys.ltype)); + of.echo("date : %s".printf (berel.keys.date)); + of.echo("license : %s %s".printf (berel.keys.license.name, berel.keys.license.address)); + of.echo("author : %s %s".printf (berel.keys.author.name, berel.keys.author.address)); + of.echo("copyright : %s %s".printf (berel.keys.copyright.name, berel.keys.copyright.address)); + print (berel.tpl); + of.echo (); + print(berel.get_template ("vala")); + } + of.rs (done); + of.echo (); + return (int) done; + +} + diff --git a/resources/berel.yml b/resources/berel.yml new file mode 100644 index 0000000..6bf6eab --- /dev/null +++ b/resources/berel.yml @@ -0,0 +1,87 @@ +%YAML 1.2 +%TAG !v! tag:pluie.org,2018:vala/ +--- +!v!Pluie.Berel.Meta meta : + !v!Pluie.Berel.Keys keys : + software : + name : pluie-yaml + address : + version : 0.53 + ltype : library + date : 2018 + license : + name : GPLv3.0 + address : + author : + name : a-Sansara + address : <[dev]at[pluie]dot[org]> + copyright : + name : pluie.org + address : + !v!Gee.ArrayList headers : + - !v!Pluie.Berel.HeaderDef sh : + file : + - build.sh + startline : 2 + !v!Pluie.Berel.Sepline sepline : + motif : "# " + repeat : 40 + extension : + - .sh + !v!Pluie.Berel.Comment comment : + start : "#^#" + end : "#^#" + begin : "# " + - !v!Pluie.Berel.HeaderDef meson : + file : + - meson.sh + startline : 1 + !v!Pluie.Berel.Sepline sepline : + motif : "# " + repeat : 40 + extension : + - .build + !v!Pluie.Berel.Comment comment : + start : "#^#" + end : "#^#" + begin : "# " + - !v!Pluie.Berel.HeaderDef vala : + dir : + - src + - samples + file : + - install.vala.in + extension : + - .vala + - .c + startline : 1 + sepline : + motif : " *" + repeat : 40 + comment : + start : /*^ + end : ^*/ + begin : " * " + tpl : | + ^sepline^ + + ^keys^ + + ^sepline^ + + This file is part of ^software.name^. + + ^software.name^ is free software (free as in speech) : 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. + + ^software.name^ 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 ^software.name^. If not, see . + + ^sepline^ diff --git a/src/Pluie/Berel.Comment.vala b/src/Pluie/Berel.Comment.vala new file mode 100644 index 0000000..5e2d909 --- /dev/null +++ b/src/Pluie/Berel.Comment.vala @@ -0,0 +1,44 @@ +using GLib; +using Pluie; +/** + * + */ +public class Pluie.Berel.Comment : Yaml.Object +{ + /** + * + */ + public string start { get; set; } + /** + * + */ + public string end { get; set; } + /** + * + */ + public string begin { get; set; } + + /** + * + */ + public bool match_start (string data) + { + return data.index_of (this.start) == 0; + } + + /** + * + */ + public bool match_end (string data) + { + return data.last_index_of (this.end) == data.length - begin.length; + } + + /** + * + */ + public string get_line (string data) + { + return "%s%s".printf (this.begin, data); + } +} diff --git a/src/Pluie/Berel.Extension.vala b/src/Pluie/Berel.Extension.vala new file mode 100644 index 0000000..698e56d --- /dev/null +++ b/src/Pluie/Berel.Extension.vala @@ -0,0 +1,7 @@ +using GLib; +using Pluie; + +public class Pluie.Berel.Extension : Yaml.Object +{ + public Gee.ArrayList list { get; set; } +} diff --git a/src/Pluie/Berel.HeaderDef.vala b/src/Pluie/Berel.HeaderDef.vala new file mode 100644 index 0000000..56f7d07 --- /dev/null +++ b/src/Pluie/Berel.HeaderDef.vala @@ -0,0 +1,58 @@ +using GLib; +using Pluie; + +public class Pluie.Berel.HeaderDef : Yaml.Object +{ + public Gee.ArrayList file { get; set; } + public Gee.ArrayList dir { get; set; } + public int startline { get; set; } + public Berel.Sepline sepline { get; set; } + public Gee.ArrayList extension { get; set; } + public Berel.Comment comment { get; set; } + + /** + * + */ + static construct + { + Yaml.Register.add_type ( + typeof (Berel.HeaderDef), + typeof (Gee.ArrayList) + ); + } + + /** + * + */ + protected override void yaml_construct () + { + this.file = new Gee.ArrayList (); + this.extension = new Gee.ArrayList (); + this.dir = new Gee.ArrayList (); + } + + /** + * + */ + public override void populate_from_node (string name, GLib.Type type, Yaml.Node node) { + if (type == typeof (Gee.ArrayList)) { + foreach (var child in node) { + switch (name) { + case "extension": + this.extension.add(child.data); + break; + case "file": + this.file.add(child.data); + break; + case "dir": + this.dir.add(child.data); + break; + } + } + } + else { + var obj = Yaml.Builder.from_node(node, type); + this.set (node.name, obj); + } + } +} diff --git a/src/Pluie/Berel.Key.vala b/src/Pluie/Berel.Key.vala new file mode 100644 index 0000000..45d6a53 --- /dev/null +++ b/src/Pluie/Berel.Key.vala @@ -0,0 +1,8 @@ +using GLib; +using Pluie; + +public class Pluie.Berel.Key : Yaml.Object +{ + public string name { get; internal set; } + public string address { get; internal set; } +} diff --git a/src/Pluie/Berel.Keys.vala b/src/Pluie/Berel.Keys.vala new file mode 100644 index 0000000..747dc6f --- /dev/null +++ b/src/Pluie/Berel.Keys.vala @@ -0,0 +1,84 @@ +using GLib; +using Pluie; +/** + * + */ +public class Pluie.Berel.Keys : Yaml.Object +{ + /** + * + */ + public Berel.Key software { get; internal set; } + /** + * + */ + public string version { get; internal set; } + /** + * + */ + public string ltype { get; internal set; } + /** + * + */ + public string date { get; internal set; } + /** + * + */ + public Berel.Key license { get; internal set; } + /** + * + */ + public Berel.Key author { get; internal set; } + /** + * + */ + public Berel.Key copyright { get; internal set; } + + /** + * + */ + public string get_all (HeaderDef hd) + { + var properties = this.get_class ().list_properties; + Berel.Key? ck = null; + string? cs = null; + foreach (var p in properties) { + if (p.value_type.is_a(typeof (Berel.Key))) { + + } + } + foreach + var str = """@software : %15s %s +@version : %s %s +@type : %s +@date : %s + ".printf ( + """; + + } + + /** + * + */ + public string replace_var (string varname) + { + string val = ""; + var list = varname.split(".", 2); + if (list.length == 1) { + if (this.get_class ().find_property (varname) != null) { + this.get(varname, out val); + } + + } + else { + Berel.Key? k = null; + this.get(list[0], out k); + if (k != null) { + if (k.get_class ().find_property (list[1]) != null) { + k.get(list[1], out val); + } + } + } + return val; + } +} diff --git a/src/Pluie/Berel.Meta.vala b/src/Pluie/Berel.Meta.vala new file mode 100644 index 0000000..996e350 --- /dev/null +++ b/src/Pluie/Berel.Meta.vala @@ -0,0 +1,147 @@ +using GLib; +using Pluie; +/** + * + */ +public class Pluie.Berel.Meta : Yaml.Object +{ + /** + * + */ + public Berel.Keys keys { get; internal set; } + /** + * + */ + public Gee.ArrayList headers { get; internal set; } + /** + * + */ + public string tpl { get; internal set; } + /** + * + */ + private Gee.HashMap varlist { get; internal set; } + + /** + * + */ + static construct + { + Yaml.Register.add_type ( + typeof (Berel.Meta), + typeof (Gee.ArrayList) + ); + } + + /** + * + */ + protected override void yaml_construct () + { + this.headers = new Gee.ArrayList (); + this.varlist = new Gee.HashMap (); + Yaml.Register.add_namespace("Gee", "Pluie.Berel"); + Dbg.msg ("%s (%s) instantiated".printf (this.yaml_name, this.get_type().name ()), Log.LINE, Log.FILE); + } + + /** + * + */ + public override void populate_from_node (string name, GLib.Type type, Yaml.Node node) { + if (type == typeof (Gee.ArrayList)) { + foreach (var child in node) { + switch (name) { + case "headers": + this.headers.add((Berel.HeaderDef) Yaml.Builder.from_node (child, typeof (Berel.HeaderDef))); + break; + } + } + } + else { + var obj = Yaml.Builder.from_node(node, type); + if (name == "keys") { + this.set (node.name, (Berel.Keys ) obj); + } + else { + this.set (node.name, obj); + } + } + } + + /** + * + */ + public string get_template (string type) + { + var headerdef = this.get_header_by_type (type); + var count = 1; + this.tpl_replace_var (headerdef); + StringBuilder sb = new StringBuilder (""); + foreach (string line in this.tpl.split ("\\n")) { + if (count == 1) { +//~ sb.append (this.comment.start + } + count++; + } + return ""; + } + + /** + * + */ + public void tpl_replace_var (HeaderDef hd) + { + MatchInfo? mi = null; + Regex reg = new Regex ("\\^([^\\^]+)\\^"); + if (reg.match (this.tpl, 0, out mi)) { + this.replacing (mi.fetch (1), hd); + while (mi.next ()) {; + this.replacing (mi.fetch (1), hd); + } + } + foreach (var entry in this.varlist.entries) { + if (entry.value.length > 0) { + this.tpl = this.tpl.replace("^%s^".printf (entry.key), entry.value); + } + } + var s = this.tpl.split("\\n"); + foreach (unowned string str in s) { + print ("%s\n", hd.comment.get_line(str)); + } + } + + /** + * + */ + private void replacing (string key, HeaderDef hd) + { + if (!this.varlist.has_key (key)) { + switch (key) { + case "sepline" : + this.varlist.set (key, hd.sepline.to_string ()); + break; + case "keys" : + this.varlist.set (key, this.keys.get_all (hd)); + break; + default : + this.varlist.set (key, this.keys.replace_var(key)); + break; + } + } + } + + /** + * + */ + public Berel.HeaderDef? get_header_by_type (string type) + { + Berel.HeaderDef? hd = null; + foreach (var def in this.headers) { + if (def.yaml_name == type) { + hd = def; + break; + } + } + return hd; + } +} diff --git a/src/Pluie/Berel.Sepline.vala b/src/Pluie/Berel.Sepline.vala new file mode 100644 index 0000000..f912388 --- /dev/null +++ b/src/Pluie/Berel.Sepline.vala @@ -0,0 +1,34 @@ +using GLib; +using Pluie; + +/** + * + */ +public class Pluie.Berel.Sepline : Yaml.Object +{ + /** + * + */ + public string motif { get; set; } + /** + * + */ + public int repeat { get; set; } + /** + * + */ + string? data { internal get; internal set; } + + /** + * + */ + public string to_string () + { + if (this.data == null) { + StringBuilder sb = new StringBuilder(""); + for (var i = 0; i < this.repeat; i++) sb.append (this.motif); + data = sb.str; + } + return this.data; + } +}