jvtrudel
jvtrudel

Reputation: 1345

Execute mustache (as ruby script) in cmake execute_process

I would like to use mustache as a simple templating engine in cmake for code generation.

I tried to execute it with execute_process as follow:

execute_process( COMMAND "/path/to/mustache" "<data> <template>" )

But it said its not a valid WIN32 application. And indeed, mustache is a ruby script:

#!D:/programs/Ruby23/bin/ruby.exe
#
# This file was generated by RubyGems.
#
# The application 'mustache' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

version = ">= 0.a"

if ARGV.first

    ...

So I tried:

execute_process( COMMAMD "/path/to/ruby" "/path/to/mustache --help" )

But it don't work either... No such file or directory -- D:/programs/Ruby23/bin/mustache --help (LoadError)

How to execute a ruby script in cmake execute_process?

Upvotes: 0

Views: 426

Answers (1)

jvtrudel
jvtrudel

Reputation: 1345

execute_process(COMMAND < cmd1 > [args1...]] ...)

Arguments must be passed as list, not as string.

    # path to executables
    set(RUBY_EXECUTABLE D:/programs/Ruby23/bin/ruby.exe CACHE STRING "ruby executable")
    set(MUSTACHE_SCRIPT D:/programs/Ruby23/bin/mustache CACHE STRING "mustache ruby script")

    # function that call mustache
    function(apply_mustache data template result)
        execute_process(
            COMMAND ${RUBY_EXECUTABLE} -E UTF-8 ${MUSTACHE_SCRIPT} ${data} ${template}
            OUTPUT_VARIABLE result_t
            )
        set(${result} ${result_t} PARENT_SCOPE)
    endfunction()

bonus: -E UTF-8 prevent ruby to mess with utf-8 characters...

Upvotes: 1

Related Questions