Ruby - watch for file with extension being updated - ruby

I am trying to auto compile my less files on centos.
Is it possible in ruby to watch a directory for changes to files ending in a specific extension and then execute a command when that happens?
I have tried inotify in a simple shell script but there are always problems when an ide creates temporary files etc.

You want inotify. A Ruby wrapper, rb-notify, is available.

The ZenTest gem includes the autotest command-line tool, which watches a test directory and runs tests when one of the files changes.
Go look at how that tool works. Using inotify is helpful but not necessary.
The basic idea for this is to write a loop with a sleep inside it. Use Ruby's Find class to locate the files that are candidates for processing. The Find documentation has example code to get that part started.

Related

Adding files when installing a gem

I'm writing a gem which needs a directory to store some config. I want to do this upon installation but I'm not sure how best to insert code into the installation process.
By adding the code to the Rakefile and adding spec.extensions = ["Rakefile"] to my gemspec, I've achieved what I want to do but this method means the installation process tells me I'm building native extensions; this is misleading.
Is there a built in way of doing this or via bundler? I'm assuming Bundler::GemHelper.install_tasks is close to what I'm after but I can't find helpful documentation.
EDIT: My current Rakefile.
It seems like the RubyGems designers intentionally left the option to run a script during installation... so I would say that the accepted way is to do the directory creation in the application.
The unix way is for the application to create the directory (vi and git do this).
FileUtils.mkdir_p is handy... it will save you from having to check if the directory is already there.

Creating .deb to install bash script program

I was wondering if the following is possible.
I have a BASH script that I want to make available for some people but I wanted them to only have to "install" the program and not messing around with terminal, so I thought a .deb would be cool.
So what would the "install" do?
Simple. I want to move the script and an icon to a folder (any folder, but I was wondering some hidden folder in Home) and then run a script that creates a launcher in the Applications menu for the first script. It seems there isn't much to it, but for what I've searched, there doesn't seem to be a lot of info...
How can I accomplish this?
By the way, I'm using Ubuntu 11.04.
Basically (install and) run dh-make to set up the debian/ directory, edit the generated files (mainly remove the many you do not need, and fill in a package description and any dependencies in debian/control), then debuild-us -uc -b.
You may also have to set up a simple Makefile for debian/rules to call; it probably only needs an install target to copy the binary to $(DESTDIR)/usr/bin.
Binaries install into /usr/bin and you should not try to override that. The way to have a menu is to add a .desktop file.
Once you have a good .deb you will need to set up a repo for distributing it. The simplest solution is probably to set up a launchpad.net account and create a personal PPA there.
It's not hard to find more information on these topics, but of course, you need to know what to look for. The canonical documentation is the Debian New Maintainer's Guide.
Found this video on youtube that explains IN FULL the process of creating a *.deb for a script or program and even mentions how to do it for a C program.
Full guide in how to build simple *.deb package
Has one bug, btw, that the author, during the making of the *.deb, didn't notice. The path in the *.desktop file for the EXEC parameter is wrong in the example.

What can I remove from Ruby and still have it support Rake/gems?

I'm building a large .NET project using Rake . We committed the Ruby environment to our source control and install gems to this "local" environment (like Albacore, Nokogiri, etc). We're essentially following this guidance, using the RubyInstaller for Windows zip package.
trunk/
source/
packages/
ruby/ <-- ruby here!
build.bat <-- helper scripts (call Ruby/rake)
Solution.sln
However, our source control server is remotely hosted, on a virtual machine, and the link speed is slow. I've read that some people have trimmed down the Ruby environment, like in the Machine.Specifications project. But, that looks extreme! (is there even gem support there?)
I could just start deleting files and directories to see what works, but there must be some known set of files that can be removed and still support basic Ruby, Rake, and gems?
From my own experimentation and investigation, it's kind of hard to determine what's "required" and what's optional. There are some files that appear very safe and easy to remove and others that I'd rather not play with. My results below only save about 5 MB... probably not enough to warrant doing this at all.
bin
The ruby/bin directory contains many batch scripts that are simply helpers to invoke the ruby executable with the correct "applicaton", like Rake or Gem. For example, rake.bat contains this line
#"%~dp0ruby.exe" "%~dpn0" %*
You can replace this with the right comamnd in your build.bat script (assuming it's "up" one level above the ruby environment, probably at the solution root)
#"%~dp0ruby\bin\ruby.exe" "%~dp0ruby\bin\rake" %*
You could dump the rest of the batch scripts, but then you'll have a little more trouble installing gems into the local environment.
cmd> ruby\bin\ruby.exe ruby\bin\gem install <whatever>
And, as of Ruby 1.9.3, that's only saving you 1.38KB. I'm sure some of those other non-extension files in this directory can be removed, but, without knowing what they do, it's unsafe to proceed.
include (175 KB)
The ruby/include directory appears to contain only C header files for developing against Ruby (I'm winging it here). I removed it and everything ran fine.
lib/tcltk (4.7 MB)
The ruby/lib/tcltk looks a lot like the include directory, but for Tcl programming.
lib/ruby
The ruby/lib/ruby directory contains the gems for this installation and a whole lot more scary stuff. I'd stay out of this directory unless you want to inadvertently cripple your environment.
share (27 KB)
The ruby/share directory appears to contain only help pages, dump 'em!

Is it possible Ruby installation by copying the ruby folder?

This is a very silly question...
I have a doubt regarding the ruby installation. Will the ruby be installed in a windows server if we copy the Ruby187 folder from another server and add that in the PATH of the environment variable?
Or Do we need to install the Ruby from the installer always, in order to install it ?
Thanks in advance
I actually did something similar to this yesterday at work. A fellow co-worker, who does not have Ruby installed on their system, needs the ability to run my scripts while I am on vacation. So, I copied the entire Ruby folder from my C: to a shared network drive.
You will not only have to add the path to the Ruby folder to your PATH variable but you also may want to associate the Ruby extension .rb with Ruby. Not needed but just a thought.
One issue I ran into was here at work we don't have permission to alter our PATH variable manually. So, in order for my co-worker to launch the scripts I needed ran, I wrote a small C++ app that merely run a command line call to the Ruby interpreter and then to the script to run.
So, in short, yes, it is possible. =)
EDIT: In regards to why you would add the path to the Ruby installation to the PATH variable, it is so you can call Ruby from the command line with simply C:\>ruby some_script.rb. Without that added to your PATH, you'd need to type the entire path every time like C:\>C:\ruby192\bin\ruby some_script.rb. However, you'd still need to type "ruby" first.
In regards to the association of ".rb" files to the Ruby Interpreter, it is an option while installing Ruby on Windows using the installer provided at ruby-lang.org. With that, you would not need to type "ruby" before the script name on the command line. C:\>some_script.rb would work. I don't know exactly how to do this with a network version of Ruby but one way might be to right-click on a ".rb" file, choose "Open with..." and locate the Ruby.exe file in \ruby192\bin\ruby.exe.
I hope that explains what you were asking about in the comments.

Deploying a Ruby project

I have a finished Ruby project that has the standard structure for a multiple file Ruby program:
project/
lib/ # Files the driver program uses go here.
bin/ # Driver program goes here.
tests/ # Unit tests go here.
What I want to be able to do is type in project into the command line from any directory and have my program run (which means it needs to be in my $PATH). My question is how do I do add a multiple file Ruby project to my PATH so that I can call by name in the terminal? (Or perhaps my approach is wrong, and I should do something else like make it into a Gem? I just don't know what is normally done).
package it as a gem and install that gem. If not and you're on linux then you can set your shebang line to be the right ruby, and chmod to make your script executable, and add your bin dir to your path (or what not).
You could go the quick and dirty route, and write a bash script that calls your main function to run the project, and then stick the bash script over in /usr/bin.

Resources