Subject: [PATCH v2 5/5] gnu: Add G-Golf's Adw-1 examples.

From: Florian Pelz <[email-masked]>
Date: Mon, 17 Feb 2025 00:55:37 +0100

* gnu/packages/guile-xyz.scm (g-golf-adw-1-examples): New variable.

Change-Id: I160e58ceaeb4867f9b21a0a32f198113e36b5c88
---
 gnu/packages/guile-xyz.scm | 125 +++++++++++++++++++++++++++++++++++++
 1 file changed, 125 insertions(+)

diff --git a/gnu/packages/guile-xyz.scm b/gnu/packages/guile-xyz.scm
index 7ca6a1bd75..1ceb25be7d 100644
--- a/gnu/packages/guile-xyz.scm
+++ b/gnu/packages/guile-xyz.scm
@@ -2803,6 +2803,131 @@ (define-public g-golf-gtk-4-examples
 the @code{gtk:bin} Guix package output.  They also show how to wrap G-Golf
 applications in a Guix package.")))
 
+(define-public g-golf-adw-1-examples
+  (package
+    (inherit guile-g-golf)
+    (name "g-golf-adw-1-examples")
+    (build-system glib-or-gtk-build-system)
+    (arguments
+     (list
+      #:tests? #f ;there are no tests for examples
+      #:modules `(((guix build guile-build-system)
+                   #:select
+                   (target-guile-effective-version))
+                  (guix build glib-or-gtk-build-system)
+                  (srfi srfi-26)
+                  ,@%default-gnu-modules)
+      #:phases
+      (with-imported-modules `((guix build guile-build-system)
+                               ,@%default-gnu-imported-modules)
+        #~(modify-phases %standard-phases
+            (add-after 'unpack 'prepare-examples
+              (lambda _
+                (chdir "examples/adw-1")
+                ;; Re-use the existing Makefile for its wildcard syntax.
+                (rename-file "Makefile.am" "Makefile")
+                (substitute* "Makefile"
+                  ;; Fix syntax error.
+                  (("hello-world")
+                   "hello-world \\"))
+                ;; Add a rule to install the examples.  This also
+                ;; installs UI, CSS and Scheme files to /bin, but
+                ;; better keep how G-Golf packages them.
+                (let ((port (open-file "Makefile" "al")))
+                  (format port "
+prefix = ~a
+bindir = $(prefix)/bin
+.PHONY: install
+install:
+	mkdir -p $(bindir)/demo/icons
+	mkdir -p $(bindir)/demo/pages
+	for f in $(EXTRA_DIST); do \\
+	  cp -r $$f $(bindir)/$$f; \\
+	done
+	cp demo/g-resources $(bindir)/demo/g-resources
+" #$output)
+                  (close-port port))))
+            (delete 'configure)
+            (replace 'build
+              (lambda _
+                ;; Create files for adwaita-1-demo needed in install phase.
+                (with-directory-excursion "demo"
+                  (system* "make")
+                  (system* "glib-compile-resources"
+                           "--target" "g-resources"
+                           "g-resources.xml"))))
+            (add-before 'install 'patch-scm-files
+              (lambda* (#:key inputs #:allow-other-keys)
+                ;; `current-filename' calls in examples are broken.
+                (map (lambda (binary)
+                       (let ((installed-binary (string-append
+                                                #$output "/bin/" binary)))
+                         (substitute* binary
+                           (("\\(current-filename\\)")
+                            (string-append "\"" installed-binary "\""))
+                           (("^exec guile ")
+                            (string-append
+                             "exec " (search-input-file inputs "/bin/guile")
+                             " ")))))
+                     (map (cut string-drop <> 2) ;strip ./ prefix
+                          (find-files "." (lambda (file stat)
+                                        ;executables or .scm modules
+                                            (or (= (stat:perms stat) #o755)
+                                                (string-suffix? ".scm"
+                                                                file))))))))
+            (add-after 'install 'wrap-binaries
+              (lambda* (#:key inputs #:allow-other-keys)
+                (let* ((version (target-guile-effective-version))
+                       (g-golf (assoc-ref inputs "guile-g-golf"))
+                       (adwaita-icons (assoc-ref inputs "adwaita-icon-theme"))
+                       (libadwaita-icons (assoc-ref inputs "libadwaita"))
+                       (scm (string-append "/share/guile/site/" version))
+                       (go (string-append "/lib/guile/"
+                                          version "/site-ccache"))
+                       (binaries
+                        (find-files "." (lambda (file stat) ;executables
+                                          (= (stat:perms stat) #o755)))))
+                  (map (lambda (binary)
+                         (let ((installed-binary (string-append
+                                                  #$output "/bin/" binary)))
+                           (wrap-program installed-binary
+                             `("GUILE_LOAD_PATH" prefix
+                               (,(string-append g-golf scm)))
+                             `("GUILE_LOAD_COMPILED_PATH" prefix
+                               (,(string-append g-golf go)))
+                             `("GI_TYPELIB_PATH" prefix
+                               (,(getenv "GI_TYPELIB_PATH")))
+                             `("GUIX_GDK_PIXBUF_MODULE_FILES" suffix
+                               (,(getenv "GUIX_GDK_PIXBUF_MODULE_FILES")))
+                             `("XDG_DATA_DIRS" suffix
+                               (,(string-append #$output "/bin/demo")
+                                ,(string-append adwaita-icons "/share")
+                                ,(string-append libadwaita-icons "/share"))))))
+                       binaries))))
+            (add-after 'wrap-binaries 'move-binaries
+              (lambda _
+                ;; Move the adwaita-1-demo wrapper to place it directly in /bin.
+                (rename-file (string-append
+                              #$output "/bin/demo/adwaita-1-demo")
+                             (string-append
+                              #$output "/bin/adwaita-1-demo"))))))))
+    (inputs
+     (list adwaita-icon-theme
+           bash-minimal
+           libadwaita
+           (librsvg-for-system)
+           gtk
+           guile-3.0
+           guile-g-golf))
+    (native-inputs (list `(,glib "bin") ;for glib-compile-resources
+                         guile-3.0))
+    (propagated-inputs (list))
+    (synopsis "Example Adwaita 1 apps written in Guile with G-Golf")
+    (description
+     "G-Golf port of (a subset of) the upstream @code{adwaita-1-demo} examples
+in the @code{libadwaita} Guix package.  They also show how to wrap G-Golf
+applications in a Guix package.")))
+
 (define-public g-wrap
   (package
     (name "g-wrap")
-- 
2.48.1