Edit Filenames and Content with Ruby.
Very interesting piece of code here. Essentially this will take all .php files, and change their extensions to a .sphp file extension. Then it will replace all instances of ".php" in the files, and replace them with ".sphp" for purposes of linking, and inclusion. Very neat script.
RUBY:
-
<p style="font-weight: normal; font-family: 'Andale Mono', 'Courier New', Courier, monospace">#!/usr/bin/env ruby</p>
-
require 'find'
-
-
require 'fileutils'
-
<p style="font-weight: normal; font-family: 'Andale Mono', 'Courier New', Courier, monospace"># Used to go through, starting at the directory start_dir and working</p>
-
<p style="font-weight: normal; font-family: 'Andale Mono', 'Courier New', Courier, monospace"># recursively, and rename all files that end in .php to end in .sphp</p>
-
<p style="font-weight: normal; font-family: 'Andale Mono', 'Courier New', Courier, monospace"># because the CS admin got a wild hair. Also goes through all .php</p>
-
<p style="font-weight: normal; font-family: 'Andale Mono', 'Courier New', Courier, monospace"># and .sphp files and replaces all instances of ".php" in them with</p>
-
<p style="font-weight: normal; font-family: 'Andale Mono', 'Courier New', Courier, monospace"># ".sphp".</p>
-
def finder( start_dir )
-
-
Find.find( start_dir ) do |path|
-
-
if FileTest.file?( path )
-
-
if path =~ /\.php$/i
-
-
old_name = File.basename( path )
-
-
new_name = old_name.gsub( /\.php$/, '.sphp' )
-
-
dir = path.gsub( /#{old_name}$/, '' )
-
-
-
-
if File.exists?( dir + old_name )
-
-
puts "#{dir + old_name} to #{dir + new_name}\n"
-
-
-
-
# Since the code is part of a Subversion repository, we use
-
-
# the 'svn' command to let Subversion rename the file
-
-
system( "svn mv #{dir + old_name} #{dir + new_name}" )
-
-
end
-
-
elsif path =~ /\.sphp$/i || path =~ /\.php$/i
-
-
puts "Replacing instances of '.php' in #{path}\n"
-
-
system( %Q{ruby -pe 'gsub(/\\.php/, ".sphp")' -i #{path} } )
-
-
end
-
-
end
-
-
end
-
<p style="font-weight: normal; font-family: 'Andale Mono', 'Courier New', Courier, monospace">end</p>
-
finder( '.' )
Source: 3Till7








(No Ratings Yet)





















Edit Filenames and Content with Ruby. « PHP Net said,
Wrote on February 12, 2008 @ 3:00 pm
[...] Filenames and Content with Ruby. This entry was written by Menekali. Bookmark the permalink. Follow any comments here with the RSS feed for this post.Content related [...]