Skip to content

Instantly share code, notes, and snippets.

@dhc02
Last active Dec 10, 2015
Embed
What would you like to do?
Ruby script to iterate over files and subdirectories in directorie[s] given as arguments and add raw text of those files to merged.txt in the working directory where the script is called
# Command line script which iterates over files and subdirectories in directorie[s] given as arguments
# and adds raw text of those files to merged.txt in the working directory where the script is called.
# When called subsequent times from the same working directory, merged.txt will be appended
# Call like this:
# ruby merge.rb {absolute path portion to delete} {directory to scan} [{directory to scan}]
# For example:
# ruby merge.rb /Users/donnieclapp/Projects/ ~/Projects/htl-website/myproject/static_media/stylesheets
################################################################
# Sample resulting merged.txt:
################################################################
#
#
# =========================================================
# ../htl-website/myproject/static_media/stylesheets/config.rb
# =========================================================
#
# require 'susy'
# # Require any additional compass plugins here.
#
# # Set this to the root of your project when deployed:
# http_path = "/"
# css_dir = "stylesheets"
# sass_dir = "sass"
# images_dir = "images"
# javascripts_dir = "javascripts"
# [...]
#
#
# =========================================================
# ../htl-website/myproject/static_media/stylesheets/sass/_base.sass
# =========================================================
#
# // Copyright 2011-2012, Donnie Clapp
# // donnieclapp.com
#
# // Use this file for defining variables and mixins
#
# // Requirements
# [...]
################################################################
# This is a list of strings which, if present in a filename, will cause that file to be ignored by the script.
# Customize to your needs (this is a ruby regex, so separate each string with a pipe)
ignoreList = /jpg|png|gif|modernizr|fancybox|jquery/
# save first argument as portion of path to delete from output, then remove from argument array
dirTree = File.absolute_path(ARGV[0])
ARGV.shift
# create or open the merged.txt file for writing (in working directory)
File.open('merged.txt','a') do |mergedFile|
# For each argument given,
ARGV.each do |directory|
# find its real path (to account for users adding trailing slashes or not), and
topDir = File.absolute_path(directory)
# create an array of all the files in that directory and its subdirectories.
filesInDir = Dir["#{topDir}/**/**/*.*"]
# Then for each file in that array,
filesInDir.each do |file|
# add a header to merged.txt with the relative path of that file
# (removing path given as first argument to script),
unless File.basename(file) =~ ignoreList
relativePath = File.absolute_path(file).gsub("#{dirTree}","..")
puts "processing: #{relativePath}"
mergedFile << "\n\n=========================================================\n"
mergedFile << "#{relativePath}\n"
mergedFile << "=========================================================\n\n"
# and finally open the current file and add each line to merged.txt
text = File.open(file, 'r').read
text.each_line do |line|
mergedFile << line
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment