Skip to content

Instantly share code, notes, and snippets.

@nilium
Created June 23, 2009 00:27
Show Gist options
  • Save nilium/134284 to your computer and use it in GitHub Desktop.
Save nilium/134284 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -w
if RUBY_VERSION.match(/(\d+).(\d+)/) && "#{$1}#{$2}".to_i < 19 then
puts "Must be using Ruby version >= 1.9"
exit(1)
end
GITIGNORE = <<IGNORE
# BMax ignores
*.x86.[ai]
*.ppc.[ai]
.bmx
doc/commands.html
IGNORE
select_dir_only = Proc.new do
|entry|
File.directory?(entry) && entry.end_with?('.mod') && !entry.start_with?(".")
end
select_dir_exclude_git = Proc.new do
|entry|
select_dir_only.call(entry) && !File.exists?("#{entry}/.git")
end
select_repo_only = Proc.new do
|entry|
if not File.file?(entry) then
if entry === ".bmx" or entry.start_with?(".") then
false
else
true
end
elsif /\.(?i:b(m[xk]|in)|c(c|pp|xx)?|h(h|pp)?|s|lua)/ === File.extname(entry) or entry === "blitz_classes.i" then
# blitz_classes.i is sort of a special case.
true
else
false
end
end
force = false
$*.each do
|option|
#namespace = Dir.pwd.match(%r<^/([^/]+/)*([^\.]+)\.mod\b>)[2]
if option == "-f" then
force = true
next
end
namespace = option.downcase
if namespace.match(/^([^\.]+)\.(.+)$/) then
namespace = $1
modules = ["#{$2}.mod"]
end
namespaceDir = "#{namespace}.mod/"
begin
Dir.chdir(namespaceDir) do
modules = Dir.entries('.') if modules.nil?
modules = if force then
modules.select(&select_dir_only)
else
modules.select(&select_dir_exclude_git)
end
modules.each do
|entry|
curmodule = "#{namespace}.#{entry.downcase.match(/^([^\.]+)\.mod$/)[1]}"
begin
Dir.chdir(entry) do
puts "Establishing repository for #{curmodule}.."
`git init`
ignore_io = File.new(".gitignore", "w")
ignore_io.write(GITIGNORE)
ignore_io.close
`git add ".gitignore"`
Dir.entries('.').select(&select_repo_only).each do
|repoFile|
`git add "#{repoFile}"`
end
`git commit -m 'Initial commit of #{curmodule}'`
puts "Repository created"
end # chdir module
rescue
puts "Failed to create repository for module #{curmodule}"
raise $!
end
end # modules.each
modules = nil
end # chdir namespace
rescue Errno::ENOENT
puts "Failed to enter namespace '#{namespace}'"
end
force = false
end # process args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment