beeselmane
beeselmane

Reputation: 1218

Combine many ruby source files into a single file

I'm working on a project in ruby, and I have many source files each declaring a few classes and methods in a module. I've been looking around and I may just be missing something, but I can't seem to find a way to merge all of my source files into a single file. I want a single file because the end product here is meant to be a command line tool, and users won't want to install 20 files in order to run a single command.

Is there any way to take many ruby source files and process all of the require statements ahead of time to create a single file that can be run by itself as a stand-alone program?

A few things that may be useful (or harmful?):

Example (A few files from my project):

<filename> --> <include1>
               <include2> 
               ...

build.rb [the main file] --> BuildSystem.rb
                             Utilities.rb
BuildSystem.rb           --> Project.rb
                             YamlFile.rb
                             XmlFile.rb
                             Utilities.rb
Project.rb               --> YamlFile.rb
                             XmlFile.rb
                             Utilities.rb

What I'm looking for would be something that would allow me to combine all 5 of these files into a single build file that can be installed just by putting it in the right place. Any help would be great, thanks!

Upvotes: 4

Views: 1311

Answers (1)

Martin Konecny
Martin Konecny

Reputation: 59611

The only file with code that is not within a function is the main file. Therefore, only the main file will have code that is run immediately after the file is parsed.

Because of this, you may be able to simply run the following from the shell (assuming a OS X / *nix system):

touch main.rb
cat Utilities.rb >> main.rb
cat XmlFile.rb >> main.rb
cat YamlFile.rb >> main.rb
cat Project.rb >> main.rb
cat BuildSystem.rb >> main.rb
cat build.rb >> main.rb #  must be appended last

You can put this into a shell script to "build" your output file each time you make a change.

Using this method, you will have require statements scattered throughout the output main.rb file, but since they are idempotent it won't have any negative effects.

Upvotes: 2

Related Questions