[gnome-builder] Meson Templates: Add Rust GNOME Application template



commit c562310f5664702b4ef76d4b910452fb9e34df47
Author: Bilal Elmoussaoui <bil elmoussaoui gmail com>
Date:   Sun Aug 25 02:54:12 2019 +0000

    Meson Templates: Add Rust GNOME Application template
    
    Currently, gtk-rs doesn't fully support subclassing which explains the various changes on window.ui

 .../meson-templates/meson-templates.gresource.xml  |  6 +++
 src/plugins/meson-templates/meson_templates.py     | 11 ++++-
 .../meson-templates/resources/build-aux/cargo.sh   | 24 ++++++++++
 src/plugins/meson-templates/resources/flatpak.json | 20 +++++++--
 .../meson-templates/resources/src/Cargo.toml       | 11 +++++
 .../meson-templates/resources/src/config.rs.in     |  3 ++
 src/plugins/meson-templates/resources/src/main.rs  | 35 +++++++++++++++
 .../meson-templates/resources/src/meson-rs.build   | 52 ++++++++++++++++++++++
 .../meson-templates/resources/src/window.rs        | 19 ++++++++
 .../meson-templates/resources/src/window.ui        |  8 ++++
 10 files changed, 183 insertions(+), 6 deletions(-)
---
diff --git a/src/plugins/meson-templates/meson-templates.gresource.xml 
b/src/plugins/meson-templates/meson-templates.gresource.xml
index 0a9bc5085..78c87d33b 100644
--- a/src/plugins/meson-templates/meson-templates.gresource.xml
+++ b/src/plugins/meson-templates/meson-templates.gresource.xml
@@ -10,6 +10,7 @@
     <file compressed="true">resources/src/hello-version.h.in</file>
     <file compressed="true">resources/src/meson-clib.build</file>
     <file compressed="true">resources/src/meson-js.build</file>
+    <file compressed="true">resources/src/meson-rs.build</file>
     <file compressed="true">resources/src/hello.c</file>
     <file compressed="true">resources/src/hello.h</file>
     <file compressed="true">resources/src/__init__.py</file>
@@ -25,13 +26,18 @@
     <file compressed="true">resources/src/window.hpp</file>
     <file compressed="true">resources/src/window.py</file>
     <file compressed="true">resources/src/window.vala</file>
+    <file compressed="true">resources/src/config.rs.in</file>
+    <file compressed="true">resources/src/window.rs</file>
     <file compressed="true">resources/src/main.vala</file>
     <file compressed="true">resources/src/main-cli.vala</file>
     <file compressed="true">resources/src/hello.py.in</file>
     <file compressed="true">resources/src/main.py</file>
+    <file compressed="true">resources/src/main.rs</file>
     <file compressed="true">resources/src/application.in</file>
     <file compressed="true">resources/src/main.cs</file>
+    <file compressed="true">resources/src/Cargo.toml</file>
     <file compressed="true">resources/src/meson-cs.build</file>
+    <file compressed="true">resources/build-aux/cargo.sh</file>
     <file compressed="true">resources/build-aux/meson/postinstall.py</file>
     <file compressed="true">resources/meson.build</file>
     <file compressed="true">resources/src/meson-cli.build</file>
diff --git a/src/plugins/meson-templates/meson_templates.py b/src/plugins/meson-templates/meson_templates.py
index b920dc827..bc7e78b1e 100644
--- a/src/plugins/meson-templates/meson_templates.py
+++ b/src/plugins/meson-templates/meson_templates.py
@@ -102,7 +102,7 @@ class MesonTemplate(Ide.TemplateBase, Ide.ProjectTemplate):
         else:
             self.language = 'c'
 
-        if self.language not in ('c', 'c♯', 'c++', 'javascript', 'python', 'vala'):
+        if self.language not in ('c', 'c♯', 'c++', 'javascript', 'python', 'vala', 'rust'):
             task.return_error(GLib.Error('Language %s not supported' % self.language))
             return
 
@@ -249,7 +249,7 @@ class GnomeProjectTemplate(MesonTemplate):
             _('GNOME Application'),
             'pattern-gnome',
             _('Create a new GNOME application'),
-            ['C', 'C++', 'C♯', 'Python', 'JavaScript', 'Vala'],
+            ['C', 'C++', 'C♯', 'Python', 'JavaScript', 'Vala', 'Rust'],
             0
          )
 
@@ -301,6 +301,13 @@ class GnomeProjectTemplate(MesonTemplate):
             files['resources/src/window.py'] = 'src/window.py'
             files['resources/src/main.py'] = 'src/main.py'
             meson_file = 'resources/src/meson-py.build'
+        elif self.language == 'rust':
+            files['resources/src/config.rs.in'] = 'src/config.rs.in'
+            files['resources/src/main.rs'] = 'src/main.rs'
+            files['resources/src/window.rs'] = 'src/window.rs'
+            files['resources/src/Cargo.toml'] = 'Cargo.toml'
+            files['resources/build-aux/cargo.sh'] = 'build-aux/cargo.sh'
+            meson_file = 'resources/src/meson-rs.build'
 
         if resource_name:
             files['resources/src/hello.gresource.xml'] = resource_name
diff --git a/src/plugins/meson-templates/resources/build-aux/cargo.sh 
b/src/plugins/meson-templates/resources/build-aux/cargo.sh
new file mode 100755
index 000000000..2c667a884
--- /dev/null
+++ b/src/plugins/meson-templates/resources/build-aux/cargo.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+export MESON_BUILD_ROOT="$1"
+export MESON_SOURCE_ROOT="$2"
+export CARGO_TARGET_DIR="$MESON_BUILD_ROOT"/target
+export CARGO_HOME="$CARGO_TARGET_DIR"/cargo-home
+export OUTPUT="$3"
+export BUILDTYPE="$4"
+export APP_BIN="$5"
+
+
+if [[ $BUILDTYPE = "release" ]]
+then
+    echo "RELEASE MODE"
+    cargo build --manifest-path \
+        "$MESON_SOURCE_ROOT"/Cargo.toml --release && \
+        cp "$CARGO_TARGET_DIR"/release/"$APP_BIN" "$OUTPUT"
+else
+    echo "DEBUG MODE"
+    cargo build --manifest-path \
+        "$MESON_SOURCE_ROOT"/Cargo.toml --verbose && \
+        cp "$CARGO_TARGET_DIR"/debug/"$APP_BIN" "$OUTPUT"
+fi
+
diff --git a/src/plugins/meson-templates/resources/flatpak.json 
b/src/plugins/meson-templates/resources/flatpak.json
index e16a4ccda..df5a5e6ce 100644
--- a/src/plugins/meson-templates/resources/flatpak.json
+++ b/src/plugins/meson-templates/resources/flatpak.json
@@ -3,6 +3,11 @@
     "runtime": "org.gnome.Platform",
     "runtime-version": "3.32",
     "sdk": "org.gnome.Sdk",
+{{if language == "rust"}}
+    "sdk-extensions" : [
+        "org.freedesktop.Sdk.Extension.rust-stable"
+    ],
+{{end}}
     "command": "{{exec_name}}",
     "finish-args": [
         "--share=network",
@@ -14,13 +19,20 @@
         "--talk-name=ca.desrt.dconf",
         "--env=DCONF_USER_CONFIG_DIR=.config/dconf"
     ],
+{{if language == "rust"}}
     "build-options": {
-        "cflags": "-O2 -g",
-        "cxxflags": "-O2 -g",
-        "env": {
-            "V": "1"
+        "append-path" : "/usr/lib/sdk/rust-stable/bin",
+        "build-args" : [
+            "--share=network"
+        ],
+        "env" : {
+            "RUSTFLAGS" : "--remap-path-prefix =../ --error-format=human",
+            "CARGO_HOME" : "/run/build/{{name}}/cargo",
+            "RUST_BACKTRACE" : "1",
+            "RUST_LOG" : "{{name}}=debug"
         }
     },
+{{end}}
     "cleanup": [
         "/include",
         "/lib/pkgconfig",
diff --git a/src/plugins/meson-templates/resources/src/Cargo.toml 
b/src/plugins/meson-templates/resources/src/Cargo.toml
new file mode 100644
index 000000000..9e55c2982
--- /dev/null
+++ b/src/plugins/meson-templates/resources/src/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "{{name}}"
+version = "0.1.0"
+edition = "2018"
+
+[dependencies]
+gtk = { version = "0.7", features = ["v3_22"] }
+glib = "0.8"
+gio = { version = "0.7", features = ["v2_46"] }
+gdk = "0.11"
+gettext-rs= { version = "0.4", features = ["gettext-system"] }
diff --git a/src/plugins/meson-templates/resources/src/config.rs.in 
b/src/plugins/meson-templates/resources/src/config.rs.in
new file mode 100644
index 000000000..8735eda35
--- /dev/null
+++ b/src/plugins/meson-templates/resources/src/config.rs.in
@@ -0,0 +1,3 @@
+pub static PKGDATADIR: &'static str = @pkgdatadir@;
+pub static VERSION: &'static str = @VERSION@;
+pub static LOCALEDIR: &'static str = @localedir@;
\ No newline at end of file
diff --git a/src/plugins/meson-templates/resources/src/main.rs 
b/src/plugins/meson-templates/resources/src/main.rs
new file mode 100644
index 000000000..86b55d8aa
--- /dev/null
+++ b/src/plugins/meson-templates/resources/src/main.rs
@@ -0,0 +1,35 @@
+use gtk::prelude::*;
+use gio::prelude::*;
+use gettextrs::*;
+use std::env;
+
+mod config;
+mod window;
+use crate::window::Window;
+
+fn main() {
+    gtk::init().unwrap_or_else(|_| panic!("Failed to initialize GTK."));
+
+    setlocale(LocaleCategory::LcAll, "");
+    bindtextdomain("{{name}}", config::LOCALEDIR);
+    textdomain("{{name}}");
+
+    let res = gio::Resource::load(config::PKGDATADIR.to_owned() + "/{{name}}.gresource")
+                                .expect("Could not load resources");
+    gio::resources_register(&res);
+
+    let app = gtk::Application::new(Some("{{appid}}"), Default::default()).unwrap();
+    app.connect_activate(move |app| {
+        let window = Window::new();
+
+        window.widget.set_application(Some(app));
+        app.add_window(&window.widget);
+        window.widget.present();
+    });
+
+
+    let args: Vec<String> = env::args().collect();
+    app.run(&args);
+
+}
+
diff --git a/src/plugins/meson-templates/resources/src/meson-rs.build 
b/src/plugins/meson-templates/resources/src/meson-rs.build
new file mode 100644
index 000000000..9ba679dc3
--- /dev/null
+++ b/src/plugins/meson-templates/resources/src/meson-rs.build
@@ -0,0 +1,52 @@
+pkgdatadir = join_paths(get_option('prefix'), get_option('datadir'), meson.project_name())
+gnome = import('gnome')
+
+gnome.compile_resources('{{name}}',
+  '{{name}}.gresource.xml',
+  gresource_bundle: true,
+  install: true,
+  install_dir: pkgdatadir,
+)
+
+conf = configuration_data()
+conf.set_quoted('VERSION', meson.project_version())
+conf.set_quoted('localedir', join_paths(get_option('prefix'), get_option('localedir')))
+conf.set_quoted('pkgdatadir', pkgdatadir)
+
+configure_file(
+    input: 'config.rs.in',
+    output: 'config.rs',
+    configuration: conf
+)
+# Copy the config.rs output to the source directory.
+run_command(
+  'cp',
+  join_paths(meson.build_root(), 'src', 'config.rs'),
+  join_paths(meson.source_root(), 'src', 'config.rs'),
+  check: true
+)
+
+sources = files(
+  'config.rs',
+  'main.rs',
+  'window.rs',
+)
+
+cargo_script = find_program(join_paths(meson.source_root(), 'build-aux/cargo.sh'))
+cargo_release = custom_target(
+  'cargo-build',
+  build_by_default: true,
+  input: sources,
+  output: meson.project_name(),
+  console: true,
+  install: true,
+  install_dir: get_option('bindir'),
+  command: [
+    cargo_script,
+    meson.build_root(),
+    meson.source_root(),
+    '@OUTPUT@',
+    get_option('buildtype'),
+    meson.project_name(),
+  ]
+)
diff --git a/src/plugins/meson-templates/resources/src/window.rs 
b/src/plugins/meson-templates/resources/src/window.rs
new file mode 100644
index 000000000..a2af97e90
--- /dev/null
+++ b/src/plugins/meson-templates/resources/src/window.rs
@@ -0,0 +1,19 @@
+use gtk::prelude::*;
+
+pub struct Window {
+    pub widget: gtk::ApplicationWindow,
+}
+
+impl Window {
+
+    pub fn new() -> Self {
+        let builder = gtk::Builder::new_from_resource("{{appid_path}}/{{ui_file}}");
+        let widget: gtk::ApplicationWindow = builder.get_object("window").expect("Failed to find the window 
object");
+
+        Self {
+            widget,
+        }
+    }
+
+}
+
diff --git a/src/plugins/meson-templates/resources/src/window.ui 
b/src/plugins/meson-templates/resources/src/window.ui
index 0aac2a867..1263098c1 100644
--- a/src/plugins/meson-templates/resources/src/window.ui
+++ b/src/plugins/meson-templates/resources/src/window.ui
@@ -16,7 +16,11 @@
     </attributes>
   </object>
 {{else}}
+  {{if language == "rust"}}
+  <object class="GtkApplicationWindow" id="window">
+  {{else}}
   <template class="{{PreFix}}Window" parent="GtkApplicationWindow">
+  {{end}}
     <property name="default-width">600</property>
     <property name="default-height">300</property>
     <child type="titlebar">
@@ -40,6 +44,10 @@
         </attributes>
       </object>
     </child>
+  {{if language == "rust"}}
+  </object>
+  {{else}}
   </template>
+  {{end}}
 {{end}}
 </interface>


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