Skip to content

Commit a078454

Browse files
author
Ryan Jacobs
committed
Split up files.
Update version too.
1 parent 7c1e52c commit a078454

6 files changed

Lines changed: 184 additions & 127 deletions

File tree

bin/github-copywriter

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
# Author: Ryan Jacobs <ryan.mjacobs@gmail.com
66
################################################################################
77

8-
require "github-copywriter"
98
require "optparse"
9+
require "highline/import"
10+
require "github-copywriter"
1011

12+
# To store command-line options/parameters
1113
options = {}
1214

15+
# Grab command-line options/parameters
1316
ARGV << "-h" if ARGV.empty?
1417
OptionParser.new do |opts|
1518
executable_name = File.split($0)[1]
@@ -44,12 +47,22 @@ Usage: #{executable_name} [repos...]
4447
end
4548
end.parse!
4649

50+
# Must have at least one repo!
4751
if ARGV.empty? and not options[:all]
4852
puts "error: No repositories supplied!"
4953
exit
5054
end
5155

56+
# Pass input repos into options
5257
options[:repos] = ARGV
5358

54-
Copywriter.login!
55-
Copywriter.run!(options)
59+
# Get username and password
60+
puts "Obtaining OAuth2 access_token from github."
61+
username = ask("GitHub username: ") { |q| q.echo = true }
62+
password = ask("GitHub password: ") { |q| q.echo = "*" }
63+
64+
# Auth to GitHub
65+
cw = Copywriter.new(username, password)
66+
67+
# Do it!
68+
cw.run!(options)

github-copywriter.gemspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# encoding: utf-8
2-
3-
# Update gem version here
4-
VERSION = "0.0.4"
2+
lib = File.expand_path("../lib", __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
require "github-copywriter/version"
55

66
Gem::Specification.new do |gem|
77
gem.name = "github-copywriter"
8-
gem.version = VERSION
8+
gem.version = Copywriter::VERSION
99
gem.authors = ["Ryan Jacobs"]
1010
gem.email = ["ryan.mjacobs@gmail.com"]
1111
gem.summary = %q{github-copywriter updates your copyrights... so you don't have to!}

lib/github-copywriter.rb

Lines changed: 23 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1-
# encoding: utf-8
21
################################################################################
3-
# github-copywriter
2+
# github-copywriter/github-copywriter.rb
43
#
54
# Author: Ryan Jacobs <ryan.mjacobs@gmail.com
65
################################################################################
76

7+
require "github-copywriter/regex"
8+
require "github-copywriter/version"
9+
require "github-copywriter/octikit_wrapper"
10+
811
require "base64"
912
require "octokit"
1013
require "colorize"
11-
require "highline/import"
1214

13-
module Copywriter
14-
extend self
15+
class Copywriter
1516

1617
COMMIT_MSG = "Update copyright. ♥ github-copywriter\nFor more info, visit http://ryanmjacobs.github.io/github-copywriter"
1718

18-
def login!
19-
# Grab username and pass
20-
puts "Obtaining OAuth2 access_token from github."
21-
username = ask("GitHub username: ") { |q| q.echo = true }
22-
password = ask("GitHub password: ") { |q| q.echo = "*" }
23-
19+
def initialize(username, password)
2420
# Auth to GitHub
2521
@client = Octokit::Client.new(:login => username, :password => password)
2622

@@ -33,110 +29,6 @@ def login!
3329
end
3430
end
3531

36-
##
37-
# Returns true if this is a file that we will update.
38-
def accepted_name?(filename)
39-
filename = File.basename(filename)
40-
41-
names = ["readme", "license"]
42-
extensions = [".md", ".txt", ".html"]
43-
44-
if names.include? filename.downcase then return true end
45-
if extensions.include? File.extname(filename.downcase) then return true end
46-
47-
return false
48-
end
49-
50-
##
51-
# Updates copyright using regex, then commits it.
52-
#
53-
# repo = Repo to commit to, e.g. "user/repo_name"
54-
# ref = Branch to commit to, e.g. "heads/master"
55-
# file_path = File path on repo, e.g. "readme", "src/file.rb", etc.
56-
#
57-
def update_copyright(repo, ref, file_path)
58-
59-
# Grab file from repo
60-
file = @client.contents(repo, :path => file_path)
61-
if file[:type] != "file" then raise "error: #{file_path} on #{repo} is not a file!" end
62-
63-
# Have to do separate assignments b/c ruby shares strings,
64-
# TODO: find a way around this
65-
content = Base64.decode64(file[:content])
66-
old_content = Base64.decode64(file[:content])
67-
68-
# Do the substitution
69-
#
70-
# Matches:
71-
# Copyright 2014
72-
# copyright 2014
73-
#
74-
# Copyright (C) 2014
75-
# copyright (c) 2014
76-
# Copyright © 2014
77-
# copyright © 2014
78-
#
79-
# (c) 2014
80-
# (C) 2014
81-
# © 2014
82-
begin
83-
content.gsub!(/([Cc]opyright( \([Cc]\)| ©)?|\([Cc]\)|©) \d{4}/, "\\1 #{@cur_year}")
84-
rescue
85-
# try w/o "©" symbol if we had errors
86-
content.gsub!(/([Cc]opyright( \([Cc]\))?|\([Cc]\)) \d{4}/, "\\1 #{@cur_year}")
87-
end
88-
89-
# Only commit if we need to
90-
if content != old_content then
91-
@modified_files << {:path => file_path, :content => content}
92-
puts " #{file_path} is now up-to-date.".green
93-
else
94-
puts " #{file_path} is already up-to-date."
95-
end
96-
end
97-
98-
##
99-
# Commits files to a GitHub repo.
100-
#
101-
# repo = Repo to commit to, e.g. "user/repo_name"
102-
# ref = Branch to commit to, e.g. "heads/master"
103-
# file_mode = Filemode on repo, e.g. "100644"
104-
# files = Array of {:path, :content}
105-
# commit_msg = Commit message, e.g. "Update file.rb"
106-
#
107-
def commit_files(repo, ref, file_mode, files, commit_msg)
108-
109-
# Return if we don't have any files to commit
110-
if files.size == 0 then return end
111-
112-
# Force file_mode to be either 100644 or 100775
113-
if file_mode != "100644" or file_mode != "100775" then
114-
file_mode = "100644"
115-
end
116-
117-
# Construct temp. tree of files to commit
118-
trees = Array.new
119-
files.each do |file|
120-
blob_sha = @client.create_blob(repo, Base64.encode64(file[:content]), "base64")
121-
122-
trees << {
123-
:path => file[:path],
124-
:mode => file_mode,
125-
:type => "blob",
126-
:sha => blob_sha
127-
}
128-
end
129-
130-
# Contruct the commit
131-
latest_commit = @client.ref(repo, ref).object
132-
base_tree = @client.commit(repo, latest_commit.sha).commit.tree
133-
new_tree = @client.create_tree(repo, trees, :base_tree => base_tree.sha)
134-
new_commit = @client.create_commit(repo, commit_msg, new_tree.sha, latest_commit.sha)
135-
136-
# Commit it!
137-
@client.update_ref(repo, ref, new_commit.sha)
138-
end
139-
14032
def run!(options={})
14133
# Default options
14234
options = {all: false, skip_forks: false, year: nil}.merge(options)
@@ -157,7 +49,7 @@ def run!(options={})
15749
end
15850

15951
# Get copyright year
160-
@cur_year = options[:year] || Time.now.year
52+
cur_year = options[:year] || Time.now.year
16153

16254
# Loop through each repo
16355
repos.each do |repo|
@@ -190,8 +82,21 @@ def run!(options={})
19082
tree[:tree].each do |file|
19183
next if file[:type] != "blob"
19284

193-
if accepted_name?(file[:path]) then
194-
update_copyright(repo_name, ref, file[:path])
85+
if Copywriter::Regex.accepted_name?(file[:path]) then
86+
# Grab file from repo
87+
file = @client.contents(repo_name, :path => file[:path])
88+
if file[:type] != "file" then raise "error: #{file_path} on #{repo} is not a file!" end
89+
90+
# Update the copyright
91+
new_content = Copywriter::Regex.update_copyright(cur_year, Base64.decode64(file[:content]))
92+
93+
# Add to list of files to commit, only if the file has changed
94+
if new_content != nil then
95+
@modified_files << {:path => file[:path], :content => new_content}
96+
puts " #{file[:path]} is now up-to-date.".green
97+
else
98+
puts " #{file[:path]} is already up-to-date."
99+
end
195100
end
196101
end
197102

@@ -205,6 +110,4 @@ def run!(options={})
205110
end
206111
end
207112
end
208-
209-
private :update_copyright, :accepted_name?, :commit_files
210113
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
################################################################################
2+
# github-copywriter/octikit_wrapper.rb
3+
#
4+
# Octokit wrapper for commiting multiple files to a GitHub repo.
5+
#
6+
# Author: Ryan Jacobs <ryan.mjacobs@gmail.com
7+
################################################################################
8+
9+
class Copywriter
10+
private
11+
# Commits files to a GitHub repo.
12+
# Requires @client to be already authorized with Copywriter.login!
13+
#
14+
# @param repo Repo to commit to, e.g. "user/repo_name"
15+
# @param ref Branch to commit to, e.g. "heads/master"
16+
# @param file_mode Filemode on repo, e.g. "100644"
17+
# @param files Array of {:path, :content}
18+
# @param commit_msg Commit message, e.g. "Update file.rb"
19+
#
20+
# @example
21+
# commit_files("user/repo", "heads/master", "100644", [{:path => "file.md", :content => "files content}], "modify file.md")
22+
def commit_files(repo, ref, file_mode, files, commit_msg)
23+
# Return if we don't have any files to commit
24+
if files.size == 0 then return false end
25+
26+
# Force file_mode to be either 100644 or 100775
27+
if file_mode != "100644" or file_mode != "100775" then
28+
file_mode = "100644"
29+
end
30+
31+
begin
32+
# Construct temp. tree of files to commit
33+
trees = Array.new
34+
files.each do |file|
35+
blob_sha = @client.create_blob(repo, Base64.encode64(file[:content]), "base64")
36+
37+
trees << {
38+
:path => file[:path],
39+
:mode => file_mode,
40+
:type => "blob",
41+
:sha => blob_sha
42+
}
43+
end
44+
45+
# Contruct the commit
46+
latest_commit = @client.ref(repo, ref).object
47+
base_tree = @client.commit(repo, latest_commit.sha).commit.tree
48+
new_tree = @client.create_tree(repo, trees, :base_tree => base_tree.sha)
49+
new_commit = @client.create_commit(repo, commit_msg, new_tree.sha, latest_commit.sha)
50+
51+
# Commit it!
52+
@client.update_ref(repo, ref, new_commit.sha)
53+
return true
54+
rescue
55+
return false
56+
end
57+
end
58+
end

lib/github-copywriter/regex.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# encoding: utf-8
2+
################################################################################
3+
# github-copywriter/regex.rb
4+
#
5+
# All of the regex that's behind this amazing project :)
6+
#
7+
# Author: Ryan Jacobs <ryan.mjacobs@gmail.com
8+
################################################################################
9+
10+
class Copywriter
11+
module Regex
12+
extend self
13+
14+
# Returns true if this is a file that we will update, (otherwise false).
15+
#
16+
# @return [Boolean] True if name is accepted. False if otherwise.
17+
def accepted_name?(filename)
18+
filename = File.basename(filename)
19+
20+
names = ["readme", "license"]
21+
extensions = [".md", ".txt", ".html"]
22+
23+
if names.include? filename.downcase then return true end
24+
if extensions.include? File.extname(filename.downcase) then return true end
25+
26+
return false
27+
end
28+
29+
# Updates copyright using regex, then commits it.
30+
#
31+
# @param year [Numeric] Year to update to, e.g. 2024
32+
# @param content [String] Text with outdated copyright
33+
# @return [String] Text with updated copyright. Nil if file was already
34+
# up to date.
35+
def update_copyright(year, old_content)
36+
# Have to do separate assignments b/c ruby shares strings,
37+
# TODO: find a way around this
38+
39+
# Do the substitution
40+
#
41+
# Matches:
42+
# Copyright 2014
43+
# copyright 2014
44+
#
45+
# Copyright (C) 2014
46+
# copyright (c) 2014
47+
# Copyright © 2014
48+
# copyright © 2014
49+
#
50+
# (c) 2014
51+
# (C) 2014
52+
# © 2014
53+
begin
54+
new_content = \
55+
old_content.gsub(/([Cc]opyright( \([Cc]\)| ©)?|\([Cc]\)|©) \d{4}/, "\\1 #{year}")
56+
rescue
57+
# try w/o "©" symbol if we had errors
58+
new_content = \
59+
old_content.gsub(/([Cc]opyright( \([Cc]\))?|\([Cc]\)) \d{4}/, "\\1 #{year}")
60+
end
61+
62+
# Only commit if we need to
63+
if new_content != old_content then
64+
return new_content
65+
else
66+
return nil
67+
end
68+
end
69+
end
70+
end

lib/github-copywriter/version.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
################################################################################
2+
# github-copywriter/version.rb
3+
#
4+
# Author: Ryan Jacobs <ryan.mjacobs@gmail.com
5+
################################################################################
6+
7+
class Copywriter
8+
9+
# Current version
10+
# @return [String]
11+
VERSION = "0.1.0".freeze
12+
13+
end

0 commit comments

Comments
 (0)