Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@FiXato
Forked from Narnach/githubify.rb
Created July 31, 2009 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FiXato/159212 to your computer and use it in GitHub Desktop.
Save FiXato/159212 to your computer and use it in GitHub Desktop.
creates/overwrites a file named <username>-<gemspec_file> and updates the s.name in there have the same project name format.
#!/usr/bin/env ruby
# Usage: githubify <gemspec_file> [username]
# Requires: github.name to be set for the current git project or [username] to be specified
# Result: creates/overwrites a file named <username>-<gemspec_file> and updates the s.name in there have the same project name format.
#
# Author: Wes Oldenbeuving
# E-mail: narnach@gmail.com
# License: MIT-LICENSE
class String
def starts_with?(substr)
self[0...substr.size] == substr
end
def replace_end(old_str, new_str)
s = old_str.size
self[-s,s]=new_str
end
end
class Githubify
attr_accessor :input, :username
attr_reader :errors, :project_name, :project_line
def initialize
@errors = []
end
def run
return false unless valid?
update_gemspec!
store_gemspec!
true
end
def report_errors
errors.each do |error|
$stderr.puts error
end
nil
end
def username
@username ||= `git config github.user`.strip
end
private
def gemspec
@gemspec ||= File.read(input)
end
def output
input_base = File.basename(input)
output_base = "#{username}-#{input_base}"
input.replace_end(input_base, output_base)
end
def project_line
@project_line ||= project_line_match[0]
end
def project_line_match
@project_line_match ||= gemspec.match(/s.name\s*=\s*['"]([^'"]+)['"]/)
end
def project_name
@project_name ||= project_line_match[1]
end
def store_gemspec!
File.open(output, 'w') {|f| f.puts(gemspec)}
end
def valid?
errors.clear
errors << 'No github.user defined!' if username.empty?
errors << 'Already githubified this project' if project_name.starts_with?("#{username}-")
errors.size == 0
end
def update_gemspec!
new_name = "#{username}-#{project_name}"
gemspec.sub!(project_line, project_line.sub(project_name, new_name))
nil
end
end
gh = Githubify.new
gh.input = File.expand_path(ARGV.shift)
gh.username = ARGV.shift
success = gh.run
gh.report_errors unless success
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment