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?
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...
Related
Running an installed gem is much slower than running its local source counterpart.
Installed gem:
$ time wmctile switch_to Thunderbird
real 0m0.682s
user 0m0.491s
sys 0m0.091s
Local source:
$ time ./work/wmctile/bin/wmctile switch_to Thunderbird
real 0m0.197s
user 0m0.118s
sys 0m0.064s
Why? Could it be because of RVM, or is this a "feature" of Ruby gems in general? Is there a way to speed it up?
This is the generated bin file:
$ which wmctile
/home/some_user_name/.rvm/gems/ruby-2.1.2/bin/wmctile
$ cat $( which wmctile )
#!/usr/bin/env ruby_executable_hooks
#
# This file was generated by RubyGems.
#
# The application 'wmctile' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
version = ">= 0"
if ARGV.first
str = ARGV.first
str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
if str =~ /\A_(.*)_\z/ and Gem::Version.correct?($1) then
version = $1
ARGV.shift
end
end
gem 'wmctile', version
load Gem.bin_path('wmctile', 'wmctile', version)
RVM puts the proper directories for your Ruby version and gemset in the path whenever the RVM Ruby is set. My PATH begins with this:
/Users/kbennett/.rvm/gems/ruby-2.3.0/bin
/Users/kbennett/.rvm/gems/ruby-2.3.0#global/bin
/Users/kbennett/.rvm/rubies/ruby-2.3.0/bin
/Users/kbennett/.rvm/bin
So, I think it's the OS and not Ruby itself that is responsible for the delay. You could test this by putting a simple shell script file in that gem bin directory, and calling it with and without its absolute location to see if you get the same difference.
When we install Rails, we get this rails "executable":
#!/usr/bin/env ruby
#
# This file was generated by RubyGems.
#
# The application 'railties' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
version = ">= 0"
if ARGV.first
str = ARGV.first
str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
if str =~ /\A_(.*)_\z/ and Gem::Version.correct?($1) then
version = $1
ARGV.shift
end
end
gem 'railties', version
load Gem.bin_path('railties', 'rails', version)
I'm wondering what the point of doing force_encoding("BINARY") is on that String. What possible values could it be that force_encoding is necessary? I would think that people would only specify versions using numbers and letters here.
This isn't a rails specific thing - it's a wrapper rubygems will generate for any ruby executable in a gem. The call to force_encoding was added in 6bf71914
The reason for the change is that the first argument might not be a version at all - we want to test if it is a version, but it could be anything and we don't want the regex check to blow up. For example some executables accept a list of file names as arguments, and those file names could be invalid in the default external encoding used by ruby.
There is a bit more discussion on the issue which prompted this change.
So everything is in the title.
Is there a way I can pass arguments to :
msf> resource path/to/resource.rc <arg1> <arg2>
Or
msfconsole -r resource.rc <arg1> <arg2>
Those arguments would passed into the ruby resource code as follow:
<ruby>
ip = ARGV[1]
port = ARGV[2]
...
...
</ruby>
Unfortunately resource files don't accept arguments, but they do accept ruby blocks. So you can do it with a bit of trickery. Make a resource file that looks something like this:
Where it's using the ruby ENV command to pull in the environmental variable "DSTIP"
metasploit-framework [git:master]$ cat /tmp/test.rc
<ruby>
run_single("set RHOST #{ENV['DSTIP']}")
</ruby>
Now when I run msfconsole, I can set that DSTIP variable and it will set the RHOST when I start up MSF to whatever was in that environmental variable:
metasploit-framework [git:master]$ DSTIP=192.168.1.1 ./msfconsole -r /tmp/test.rc -Lq
[*] Processing /tmp/test.rc for ERB directives.
[*] resource (/tmp/test.rc)> Ruby Code (40 bytes)
RHOST => 192.168.1.1
You can do this with as many environmental variables as you want. Now if you want to run it from within MSFCONSOLE I tried changing the environmental variable after msfconsole was running with no luck. I'm sure there is a way that a beardy linux master will have to do it but I don't I'm sorry.
Side note: you can also use ruby file reads to pull in text from. (Think configuration file)
Hope this help!
mubix
Having gone through several posts on this issue I still can't add new directory to $LOAD_PATH. I use Ubuntu 12. My $LOAD_PATH is:
2.0.0-p247 :002 > puts $LOAD_PATH
/home/ajax/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0
/home/ajax/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/x86_64-linux
/home/ajax/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby
/home/ajax/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/vendor_ruby/2.0.0
/home/ajax/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/vendor_ruby/2.0.0/x86_64-linux
/home/ajax/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/vendor_ruby
/home/ajax/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0
/home/ajax/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/x86_64-linux
=> nil
How to add '/home/ajax/Ruby/Projects' to $LOAD_PATH through terminal?
You can add additional entries to the $LOAD_PATH as a command line parameter to your ruby (or irb) command using the -I argument (which can be used multiple times)
$ ruby -I '/home/ajax/Ruby/Projects' -e 'puts $LOAD_PATH'
/home/ajax/Ruby/Projects
/home/ajax/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0
/home/ajax/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/x86_64-linux
/home/ajax/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby
/home/ajax/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/vendor_ruby/2.0.0
/home/ajax/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/vendor_ruby/2.0.0/x86_64-linux
/home/ajax/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/vendor_ruby
/home/ajax/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0
/home/ajax/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/x86_64-linux
in your terminal
export RUBYLIB=/home/ajax/Ruby/Projects
From Add $LOAD_PATH externally
RUBYLIB environment variable is a colon separated list of paths which ruby will prepend the the standard LOAD_PATH. ruby -I path on the command line is also the same as $LOAD_PATH.unshift 'path' in your code. Ruby will also process options from environment var RUBYOPT.
I'd like to execute Cucumber features from within Ruby code.
Typically the cucumber binary installed with the gem is executed on the command line with one or more features specified.
However, I'd like to define logic that creates a dynamic feature execution flow. In other words, the program can work out which features should be executed.
Is it possible to instantiate Cucumber with specified feature files from Ruby code as opposed to the command line?
I discovered how from the mailing list and some API reading.
features="path/to/first.feature path/to/second.feature"
runtime = Cucumber::Runtime.new
runtime.load_programming_language('rb')
Cucumber::Cli::Main.new([features]).execute!(runtime)
If you want all features within your gem's features/ directory to be executed, pass an empty array to Main.new instead.
To convert this example command, with features and options specified:
cucumber features/first.feature features/second.feature -d -f Cucumber::Formatter::Custom
into Ruby code, it boils down to passing Cucumber an args array:
require 'cucumber'
# Method 1 - hardcoded features
args = %w(features/first.feature features/second.feature -d -f Cucumber::Formatter::Custom)
# Method 2 - dynamic features
features = 'features/first.feature features/second.feature'
args = features.split.concat %w(-d -f Cucumber::Formatter::Custom)
# Run cucumber
begin
Cucumber::Cli::Main.new(args).execute!
rescue SystemExit
puts "Cucumber calls #kernel.exit(), killing your script unless you rescue"
end
Tested using Ruby 2.0.0p598 and Cucumber 1.3.17