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+
811require "base64"
912require "octokit"
1013require "colorize"
11- require "highline/import"
1214
13- module Copywriter
14- extend self
15+ class Copywriter
1516
1617 COMMIT_MSG = "Update copyright. ♥ github-copywriter\n For 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
210113end
0 commit comments