Running ruby gem sprockets from command line - ruby

I am finding very little documentation on running sprockets from the command line.
Does anyone know how to setup the .sprocketsrc file?
Examples would be great especially on how to configure the minification.

If you read directly the source, you can see there https://github.com/sstephenson/sprockets/blob/master/bin/sprockets#L8 that it uses something named Shellwords which comes with the standard ruby library : http://www.ruby-doc.org/stdlib-1.9.3/libdoc/shellwords/rdoc/Shellwords.html and http://www.ruby-doc.org/stdlib-1.9.3/libdoc/shellwords/rdoc/Shellwords.html#method-c-shellsplit
So we can guess from :
unless ARGV.delete("--noenv")
if File.exist?(path = "./.sprocketsrc")
rcflags = Shellwords.split(File.read(path))
ARGV.unshift(*rcflags)
end
end
That it basically prepends whatever it find in the sprocketsrc to the command line arguments.
https://github.com/sstephenson/sprockets/blob/master/bin/sprockets#L22 gives us the list of the options, meaning if you want to configure the minification you can create a .sprocketsrc
with something like
--include=assets/javascripts --output build/assets/javascripts
Sadly, the command line don't look to have any option to configure the minifying options.

Related

Run arbitrary ruby code in a chef cookbook

I have a simple chef cookbook and all it does is it sets the MOTD on a CentOS machine. It takes the content of the /tmp/mymotd.txt and turns it into the MOTD.
I also have a simple ruby script (a full-fledged ruby script) that simply reads the text from the web-server and puts in into the /tmp/mymotd.txt.
My questions are:
how do I run this ruby script from within the cookbook?
how do I pass some parameters to the script (e.g. the address of the web-server)
Thanks a lot beforehand.
Ad 1.
You can use libraries directory in scripts to place there your ruby script and declare it in a module. Example:
# includes
module MODULE_NAME
# here some code using your script
# Example function
def example_function (text)
# some code
end
end
You can use then
include MODULE_NAME
in your recipe to import those functions and just use it like
example_function(something)
What's good - you can use there also Chef functions and resources.
IMPORTANT INFO: Just remember that Chef has 2 compilation phases. First will be all of Ruby code, second all of Chef resources. This means, that you have to remember priority of code. I won't write here more info about it, since you haven't asked for this, but if you want, you can find it here.
Ad 2.
You can do this in several ways, but it seems to me, that the best option for you would be to use environments. You can find more info in here. Basically, you can set up environment for script before it will run - this way you can define some variables you would use later.
Hope this helps.

Can't generate ruby exe using ocra due to ARGV[0]

Running the command ocra script.rb --no-autoload --no-enc --add-all-core gives me the error initialize: can't convert nil into String (TypeError) for the following line:
doc = Nokogiri::XML(File.open(ARGV[0]))
Whats going on here? I want to build the executable to be able to take any argument and use that file as the xml configuration.
It seems a long time but the accept solution doesn't work for me.
The working solution is adding -- then any fake data to your argument to make the execution flow to be just as normal
example for:
so you need to do
ocra yourscript.rb -- ANYDATAHERE
Just add this above that line:
exit if defined? Ocra
# skip anything below this line when we're building the exe
Unless there's a require or otherwise loaded dependency below that line you should be fine.

Executing a ruby script on Windows 7 command line with multiple ruby files required

This might be a trivial question, but I've searched and found nothing about it. I'm trying to make a complex application on ruby and I have my classes on different .rb files which the "Main" class requires. As it's written in the code:
require 'Book.rb'
require 'Person.rb'
These files that contains the classes are in the same directory, so what I'm looking for is a Batch command which allows me to include these files, like the one for one file but extended:
ruby Main.rb
Which, of course fails saying that it can't find 'Book.rb' (returns error and doesn't look for the other)
Thank you for your help.
Thanks to the user Abhi y found this post. The command that worked for me was:
irb -I . -r Main.rb
From which you can get further information on the link.

A ruby script to run tail on a log file?

I want to write a ruby script that read from a config file that will have filenames, and then when I run the script it will take the tail of each file and output the console.
What's the best way to go about doing this?
Take a look at File::Tail gem.
You can invoke linux tail -number_of_lines file_name command from your ruby script and let it print on console or capture output and print it yourself (if you need to do something with these lines before you print it)
We have a configuration file that contain a list of the log files; for example, like this:
---
- C:\fe\logs\front_end.log
- C:\mt\logs\middle_tier.log
- C:\be\logs\back_end.log
The format of the configuration file is a yaml simple sequence , therefore suppose we named this file 'settings.yaml'
The ruby script that take the tail of each file and output the console could be like this:
require 'yaml'
require 'file-tail'
logs = YAML::load(File.open('settings.yaml'))
threads = []
logs.each do |the_log|
threads << Thread.new(the_log) { |log_filename|
File.open(log_filename) do |log|
log.extend(File::Tail)
log.interval = 10
log.backward(10)
log.tail { |line| p "#{File.basename(the_log,".log")} - #{line}" }
end
}
end
threads.each { |the_thread| the_thread.join }
Note: displaying each line I wanted to prefix it with the name of the file from which it originates, ...this for me is a good option but you can edit the script to change as you like ; is the same for the tails parameters.
if file-tail is missing in your environment, follow the link as #Mark Thomas posts in his answear; i.e you need to:
> gem install file-tail
I found the file-tail gem to be a bit buggy. I would write to a file and it would read the entire file again instead of just thelines appended. This happened even though I had log.backward set to 0. I ended up writing my own and figured that I would share it here in case any one else is looking for a Ruby alternative to the file-tail gem. You can find the repo here. It uses non_blocking io, so it will catch amendments to the file immediately. There is one caveat that can be easily fixed if you can program in the Ruby programming language; log.backward is hard coded to be -1.

Easiest way to parse gem-style command line arguments in Ruby

I would like to implement gem-style console app, and when I say gem (or apt-get etc) style, I mean that it will have invocation syntax like:
program.rb verb [argument] [--options ...]
For example
greeter.rb say "Hello world" --bold
I have used optparse but I think it is not suitable for anything except --option style arguments. Am I wrong about it or there is more suitable library to achieve this?
I suggest not to parse from scratch; I suggest to use GLI by which you can provide (via its DSL) a git like interface to your users. Get started here to see how it works.
You might also be interested in looking at a real (humble) implementation in a project of mine. Check these files:
https://github.com/empo/RuGPost/blob/master/bin/rugpost
https://github.com/empo/RuGPost/blob/master/lib/rugpost/commands.rb
https://github.com/empo/RuGPost/blob/master/lib/rugpost/commands/post_commands.rb
https://github.com/empo/RuGPost/blob/master/lib/rugpost/commands/project_commands.rb

Resources