On Fri, 2023-12-15 at 12:45 +0000, Raphael Mankin wrote:
> Motivation:
> 
> I keep my Lilypond files in a git repository. I would like to 
> interpolate the repo's version number in the Lilypond output by
> running 
> a command like "git log|head -1". I canĀ  do this from raw SchemeĀ 
> using 
> the system() function, but this does not seem to work in Lilypond.
> 
> So, now the question:
> 
> How do I run a system command from within Lilypond? I realise that
> being 
> able to do so opens up a massive security hole, but that is not a 
> problem here.
> 
Here's what I do.  There will be some lines in here that are redundant
for your purposes.  And the code probably needs a cleanup for recent
versions (getting rid of "layout props" args), and for my cargo-cult
coding.  Apologies if email introduces errant linebreaks:

~~~~~~~~~~~~ filestats.ily ~~~~~~~~~~~~~
\version "2.25.0"

% from snippet 197:
#(define comml    (object->string (command-line)))
#(define loc      (+ (string-rindex comml #\space ) 2)) 
#(define commllen (- (string-length comml) 2))
#(define filen    (substring comml loc commllen))                   %
filename
#(define siz      (object->string (stat:size (stat filen))))        %
file size
#(define ver      (object->string (lilypond-version)))              %
Lilypond version
#(define dat      (strftime "%m/%d/%Y" (localtime (current-time)))) %
Date processed
#(define tim      (strftime "%H:%M:%S" (localtime (current-time)))) %
Time processed
#(define modt     (stat:mtime (stat filen)))                        %
Last modified
#(define modts    (strftime "%d %b %Y %H:%M:%S" (localtime modt)))  %
Command line


% gitver and gitrev (from
http://lilypondblog.org/2014/01/why-use-version-control-for-engraving-scores/#more-2151
)
#(use-modules (ice-9 popen))
#(use-modules (ice-9 rdelim))

% NOTE: This function only reads the first line of the command!
#(define (strsystem_internal cmd)
   (let* ((port (open-input-pipe cmd))
          (str (read-line port)))
     (close-pipe port)
     str))

#(define-markup-command (strsystem layout props cmd) (markup?)
   (interpret-markup layout props
     (strsystem_internal cmd)))

%gitver = \markup { \strsystem  "/usr/bin/git log --oneline " \filen "
| wc -l" }
gitrev = \markup { \strsystem "/usr/bin/git rev-parse --short HEAD" }

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Then in my source file:
~~~~~~~~~~~~ some-music.ly ~~~~~~~~~~~~~
\include "filestats.ily"

\header {
  tagline = \markup{
    \center-column{
      \line { "Version control for this edition: gitrev " \gitrev " "
\modts }}}}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

HTH
-- Graham

Reply via email to