github-copywriter 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c84504ed9a92dd7dcbdd28884905da1fcdeb32a9
4
- data.tar.gz: 7e046c94212fbc19527daa8dbf761fca3e772b0b
3
+ metadata.gz: 55ba1c50fe50049d63c0896d776523db6c848497
4
+ data.tar.gz: 64f7c36d11b8a2b724146816904bb17f2677bc00
5
5
  SHA512:
6
- metadata.gz: b7a3cdd5d25311fc57aaf0856483dff6dd756d52cb54611aee4f24ec2bfc392026d25f66ac9556cf264475c5d7884bab21a4e7bb2268a2c454e4317dfeac159d
7
- data.tar.gz: 2d6ddcfaa1b8c657193845174d22511b4d4a768b146d7986e48bc8e679d397601280f64e231a1c97cd6bf9d2d7347748bb1ffd8febe592bcc5e70e5c3b4077ca
6
+ metadata.gz: 4feb393e92984308a3898cefbae8e1022db77f0cd72915dd222cd13ea7c4d99e525e1108688cc18e17b2f9f068f735b0c6db345215cfbf721d9d7cbb065794cb
7
+ data.tar.gz: bd3a7beb5eb870dcf40db87416c3f36667eb7c80d7413d645aca666a654f7b2f059b509fd67617c6a294d061da0a918019dbe18c5a87787f38f0f6675c6850c6
data/CONTRIBUTORS.md CHANGED
@@ -1,6 +1,7 @@
1
1
  ## Contributors
2
2
 
3
3
  * Ryan Jacobs ([ryanmjacobs](//github.com/ryanmjacobs))
4
+ * Pavel Dostál ([pdostal](//github.com/pdostal))
4
5
 
5
6
  Want to be added to this list? Checkout
6
7
  [CONTRIBUTING.md](//github.com/ryanmjacobs/github-copywriter/blob/master/CONTRIBUTING.md)
data/README.md CHANGED
@@ -4,11 +4,14 @@
4
4
 
5
5
  ![test-repo demo](https://raw.githubusercontent.com/ryanmjacobs/github-copywriter/master/test_repo_demo.gif)
6
6
 
7
- See the commit [here](https://github.com/ryanmjacobs/test-repo/commit/7b095e65adf4cedcd7891326ec32c5f526838dd5).
7
+ **See the example commit [here](//github.com/ryanmjacobs/test-repo/commit/7b095e65adf4cedcd7891326ec32c5f526838dd5). See the pretty website [here](//ryanmjacobs.github.io/github-copywriter).**
8
8
 
9
- Updates your copyrights... so you don't have to!
9
+ **Wanna see something a little more...
10
+ [complex](https://github.com/ryanmjacobs/test-repo/commit/7fbc410ce1171a2e858972b1c7cfed32b86d98e2)?**
10
11
 
11
- `$ gem install github-copywriter`
12
+ ## Updates your copyrights... so you don't have to!
13
+
14
+ ### `$ gem install github-copywriter`
12
15
 
13
16
  ## Basic Usage
14
17
 
@@ -2,7 +2,7 @@
2
2
  ################################################################################
3
3
  # github-copywriter
4
4
  #
5
- # Author: Ryan Jacobs <ryan.mjacobs@gmail.com
5
+ # Author: Ryan Jacobs <ryan.mjacobs@gmail.com>
6
6
  ################################################################################
7
7
 
8
8
  require "optparse"
@@ -3,7 +3,7 @@
3
3
  #
4
4
  # Octokit wrapper for commiting multiple files to a GitHub repo.
5
5
  #
6
- # Author: Ryan Jacobs <ryan.mjacobs@gmail.com
6
+ # Author: Ryan Jacobs <ryan.mjacobs@gmail.com>
7
7
  ################################################################################
8
8
 
9
9
  class Copywriter
@@ -4,7 +4,7 @@
4
4
  #
5
5
  # All of the regex that's behind this amazing project :)
6
6
  #
7
- # Author: Ryan Jacobs <ryan.mjacobs@gmail.com
7
+ # Author: Ryan Jacobs <ryan.mjacobs@gmail.com>
8
8
  ################################################################################
9
9
 
10
10
  class Copywriter
@@ -30,64 +30,88 @@ class Copywriter
30
30
  #
31
31
  # @param year [String] Year to update to, e.g. "2024"
32
32
  # @param content [String] Text with outdated copyright
33
- # @return [String] Hash object {:new_content, :updated_now, :found_copyright}
33
+ # @return [Hash] {:new_content, :copyrights_found, :copyrights_updated}
34
34
  #
35
35
  # Example return hashes:
36
+ # ======================
36
37
  #
37
- # Input:
38
- # "...Copyright 2014..."
38
+ # Input -- an out-of-date copyright:
39
+ # "...Copyright 2013..."
39
40
  # Return:
40
- # {:content => "...Copyright 2014...", :updated_now => false, :found_copyright => true}
41
+ # {:content => "...Copyright 2014...", :copyrights_found => 1, :copyrights_updated => 1}
41
42
  #
42
- # Input:
43
- # "...no copyright here..."
43
+ # Input -- an already up-to-date copyright:
44
+ # "...Copyright 2014..."
44
45
  # Return:
45
- # {:content => "...no copyright here...", :updated_now => false, :found_copyright => false}
46
+ # {:content => "...Copyright 2014...", :copyrights_found => 1, :copyrights_updated => 0}
46
47
  #
47
- # Input:
48
- # "...Copyright 2013..."
48
+ # Input -- no copyright whatsoever:
49
+ # "...no copyright here..."
49
50
  # Return:
50
- # {:content => "...Copyright 2014...", :updated_now => true, :found_copyright => true}
51
+ # {:content => "...no copyright here...", :copyrights_found => 0, :copyrights_updated => 0}
51
52
  def update_copyright(year, old_content)
52
53
  data = {
53
54
  :content => "",
54
- :updated_now => false,
55
- :found_copyright => false
55
+ :copyrights_found => 0,
56
+ :copyrights_updated => 0
56
57
  }
57
58
 
58
- # The Glorious Regular Expression
59
- #
60
- # Matches:
61
- # Copyright 2014
62
- # copyright 2014
63
- #
64
- # Copyright (C) 2014
65
- # copyright (c) 2014
66
- # Copyright © 2014
67
- # copyright © 2014
68
- #
69
- # (c) 2014
70
- # (C) 2014
71
- # © 2014
72
- utf_regex = /([Cc]opyright( \([Cc]\)| ©)?|\([Cc]\)|©) \d{4}/
73
- ascii_regex = /([Cc]opyright( \([Cc]\))?|\([Cc]\)) \d{4}/
74
- regex_replace = "\\1 #{year}"
75
-
76
- # Do the substitution
77
- begin
78
- # Do UTF-8 Regex
79
-
80
- data[:found_copyright] = (utf_regex === old_content)
81
- data[:content] = old_content.gsub(utf_regex, regex_replace)
82
- rescue
83
- # Do Ascii Regex if the above fails
84
- data[:found_copyright] = (ascii_regex === old_content)
85
- data[:content] = old_content.gsub(ascii_regex, regex_replace)
86
- end
59
+ # All teh regex
60
+ prefix = /([Cc]opyright( \([Cc]\)| ©)?|\([Cc]\)|©) /
61
+ suffix = /(\.| |$)/
62
+ comma_sep = /((\d{4},|\d{4}-\d{4},)*)/
63
+ regexs = [
64
+ # Singular -> (c) 2012 -> (c) 2014
65
+ # for any year
66
+ { :regex => /#{prefix}\d{4}#{suffix}/, :replace => "\\1 #{year}\\3" },
67
+
68
+ # Multiple comma separated, ending w/ year_before
69
+ # (c) 2009-2011,2013 -> (c) 2009-2011,2013-2014
70
+ # for year before
71
+ { :regex => /#{prefix}#{comma_sep}#{year-1}#{suffix}/, :replace => "\\1 \\3#{year-1}-#{year}\\5" },
72
+
73
+ # Multiple comma separated, ending w/ some_year DASH year_before
74
+ # (c) 2008-2010,2012-2013 -> (c) 2008-2010,2012-2014
75
+ # for year before
76
+ { :regex => /#{prefix}#{comma_sep}((\d{4}-)?#{year-1})#{suffix}/, :replace => "\\1 \\3\\6#{year}\\7" },
77
+
78
+ # Multiple comma separated with dash
79
+ # (c) 2009,2012 -> (c) 2009,2012,2014
80
+ # (c) 2009-2012 -> (c) 2009-2012,2014
81
+ # for any year
82
+ { :regex => /#{prefix}#{comma_sep}((\d{4}-)?\d{4})#{suffix}/, :replace => "\\1 \\3\\5,#{year}\\7" },
83
+ ]
84
+
85
+ already_updated = /#{prefix}#{comma_sep}(\d{4}-)?#{year}#{suffix}/
86
+ has_copyright = /#{prefix}#{comma_sep}(\d{4}-)?\d{4}#{suffix}/
87
+
88
+ # Loop through each line of the input text
89
+ old_content.lines.each do |line|
90
+ # Is there even a copyright? If there isn't, goto the next line.
91
+ if not (has_copyright === line) then
92
+ data[:content] << line
93
+ next
94
+ end
95
+
96
+ # Are we already updated? If so, goto the next line.
97
+ if (already_updated === line) then
98
+ data[:copyrights_found] += 1
99
+ data[:content] << line
100
+ next
101
+ end
102
+
103
+ # Loop through our regex until we get one to work
104
+ regexs.each do |r|
105
+ data[:copyrights_found] += 1 if (r[:regex] === line)
106
+ updated_line = line.gsub(r[:regex], r[:replace])
87
107
 
88
- # Update whether or not we had to update the copyright
89
- if data[:content] != old_content then
90
- data[:updated_now] = true
108
+ # Did the regex update the copyright? If so, goto the next line.
109
+ if updated_line != line then
110
+ data[:content] << updated_line
111
+ data[:copyrights_updated] += 1
112
+ break
113
+ end
114
+ end
91
115
  end
92
116
 
93
117
  return data
@@ -1,13 +1,13 @@
1
1
  ################################################################################
2
2
  # github-copywriter/version.rb
3
3
  #
4
- # Author: Ryan Jacobs <ryan.mjacobs@gmail.com
4
+ # Author: Ryan Jacobs <ryan.mjacobs@gmail.com>
5
5
  ################################################################################
6
6
 
7
7
  class Copywriter
8
8
 
9
9
  # Current version
10
10
  # @return [String]
11
- VERSION = "1.1.0".freeze
11
+ VERSION = "1.2.0".freeze
12
12
 
13
13
  end
@@ -2,7 +2,7 @@
2
2
  ################################################################################
3
3
  # github-copywriter/github-copywriter.rb
4
4
  #
5
- # Author: Ryan Jacobs <ryan.mjacobs@gmail.com
5
+ # Author: Ryan Jacobs <ryan.mjacobs@gmail.com>
6
6
  ################################################################################
7
7
 
8
8
  require "github-copywriter/regex"
@@ -19,9 +19,12 @@ class Copywriter
19
19
 
20
20
  def initialize(username, password)
21
21
  # Auth to GitHub
22
+ Octokit.auto_paginate = true
22
23
  @client = Octokit::Client.new(:login => username, :password => password)
23
24
 
24
- # Check if the basic_auth worked; TODO: Find a better way to do this
25
+ # Check if the basic_auth worked.
26
+ # TODO: Find a better way to do this
27
+ # https://github.com/ryanmjacobs/github-copywriter/issues/1
25
28
  begin
26
29
  @client.authorizations
27
30
  rescue Octokit::Unauthorized
@@ -117,10 +120,10 @@ class Copywriter
117
120
  new_content = Copywriter::Regex.update_copyright(cur_year, Base64.decode64(file[:content]))
118
121
 
119
122
  # Skip to the next file if we didn't even find a copyright in the text
120
- next if not new_content[:found_copyright]
123
+ next if new_content[:copyrights_found] == 0
121
124
 
122
125
  # Add to list of files to commit, only if the file has changed
123
- if new_content[:updated_now] then
126
+ if new_content[:copyrights_updated] > 0 then
124
127
  @modified_files << {:path => file[:path], :content => new_content[:content]}
125
128
  puts " #{file[:path]} is now up-to-date.".green
126
129
  else
@@ -1,6 +1,8 @@
1
1
  # encoding: utf-8
2
2
  require "spec_helper"
3
3
 
4
+ ### This script assumes the current year is 2014. ###
5
+
4
6
  ###
5
7
  # This spec desperately needs a rewrite.
6
8
  # It works, but the display is all wrong
@@ -21,6 +23,43 @@ copyrights = [
21
23
  { :old => "(c) 1970", :new => "(c) 2014" },
22
24
  { :old => "(C) 1970", :new => "(C) 2014" },
23
25
  { :old => "© 1970", :new => "© 2014" },
26
+
27
+ ###
28
+
29
+ { :old => "Copyright (C) 2010-2013", :new => "Copyright (C) 2010-2014" },
30
+ { :old => "Copyright (C) 2010-2012", :new => "Copyright (C) 2010-2012,2014" },
31
+ { :old => "Copyright (C) 2009,2012", :new => "Copyright (C) 2009,2012,2014" },
32
+ { :old => "Copyright (C) 2010,2013", :new => "Copyright (C) 2010,2013-2014" },
33
+
34
+ { :old => "Copyright (C) 2006,2008,2010,2012", :new => "Copyright (C) 2006,2008,2010,2012,2014" },
35
+ { :old => "Copyright (C) 2001,2003,2006,2008,2010,2012", :new => "Copyright (C) 2001,2003,2006,2008,2010,2012,2014" },
36
+
37
+ { :old => "Copyright (C) 1996-1998,2001-2002,2010-2013", :new => "Copyright (C) 1996-1998,2001-2002,2010-2014" },
38
+ { :old => "Copyright (C) 1996-1998,2001-2002,2010-2012", :new => "Copyright (C) 1996-1998,2001-2002,2010-2012,2014" },
39
+ { :old => "Copyright (C) 2001-2002,2010-2013", :new => "Copyright (C) 2001-2002,2010-2014" },
40
+ { :old => "Copyright (C) 2001,2003,2010-2013", :new => "Copyright (C) 2001,2003,2010-2014" },
41
+
42
+ { :old => "copyright © 1970 more words", :new => "copyright © 2014 more words" },
43
+ { :old => "words copyright © 1970", :new => "words copyright © 2014" },
44
+
45
+ ##
46
+
47
+ {
48
+ :old => "This is my paragraph. Blah blah blah... Copyright 2001.\nCopyright (C) 2001,2003,2006,2008,2010,2012",
49
+ :new => "This is my paragraph. Blah blah blah... Copyright 2014.\nCopyright (C) 2001,2003,2006,2008,2010,2012,2014"
50
+ },
51
+ {
52
+ :old => "This is my paragraph. Blah blah blah... Copyright 2001.\nCopyright (C) 2001,2003,2006,2008,2010,2013",
53
+ :new => "This is my paragraph. Blah blah blah... Copyright 2014.\nCopyright (C) 2001,2003,2006,2008,2010,2013-2014"
54
+ },
55
+ {
56
+ :old => "This is my paragraph. Blah blah blah... Copyright 2001.\nCopyright (C) 2001,2003,2006,2008,2010-2013",
57
+ :new => "This is my paragraph. Blah blah blah... Copyright 2014.\nCopyright (C) 2001,2003,2006,2008,2010-2014"
58
+ },
59
+ {
60
+ :old => "This is my paragraph. Blah blah blah... Copyright 2001.\nCopyright (C) 2001,2003,2006,2008,2010-2013\n",
61
+ :new => "This is my paragraph. Blah blah blah... Copyright 2014.\nCopyright (C) 2001,2003,2006,2008,2010-2014\n"
62
+ }
24
63
  ]
25
64
 
26
65
  garbage_text = [
@@ -33,7 +72,8 @@ garbage_text = [
33
72
  "(c) (c) (C)",
34
73
  "_dfasdf_",
35
74
  "© © ©",
36
- "REALLYLONGSTRINGWITHNOSPACES"
75
+ "REALLYLONGSTRINGWITHNOSPACES",
76
+ "copyright © 1970more words"
37
77
  ]
38
78
 
39
79
  # ./lib/github-copywriter/regex.rb
@@ -43,33 +83,39 @@ describe Copywriter do
43
83
  # Copywriter::Regex.update_copyright
44
84
  context "an out-of-date copyright..." do
45
85
  copyrights.each do |copyright|
46
- updated_copyright = regex.update_copyright(2014, copyright[:old])
86
+ context(copyright[:old]) do
87
+ updated_copyright = regex.update_copyright(2014, copyright[:old])
47
88
 
48
- it { expect(updated_copyright[:updated_now]) .to be(true) }
49
- it { expect(updated_copyright[:found_copyright]).to be(true) }
50
- it { expect(updated_copyright[:content]) .to eq(copyright[:new]) }
89
+ it { expect(updated_copyright[:content]) .to eq(copyright[:new]) }
90
+ it { expect(updated_copyright[:copyrights_found]) .to be > 0 }
91
+ it { expect(updated_copyright[:copyrights_updated]) .to be > 0 }
92
+ end
51
93
  end
52
94
  end
53
95
 
54
96
  # Copywriter::Regex.update_copyright
55
97
  context "an up-to-date copyright..." do
56
98
  copyrights.each do |copyright|
57
- updated_copyright = regex.update_copyright(2014, copyright[:new])
99
+ context(copyright[:new]) do
100
+ updated_copyright = regex.update_copyright(2014, copyright[:new])
58
101
 
59
- it { expect(updated_copyright[:updated_now]) .to be(false) }
60
- it { expect(updated_copyright[:found_copyright]).to be(true) }
61
- it { expect(updated_copyright[:content]) .to eq(copyright[:new]) }
102
+ it { expect(updated_copyright[:content]) .to eq(copyright[:new]) }
103
+ it { expect(updated_copyright[:copyrights_found]) .to be > 0 }
104
+ it { expect(updated_copyright[:copyrights_updated]) .to be(0) }
105
+ end
62
106
  end
63
107
  end
64
108
 
65
109
  # Copywriter::Regex.update_copyright
66
110
  context "garbage text input..." do
67
111
  garbage_text.each do |garbage|
68
- updated_copyright = regex.update_copyright(2014, garbage)
112
+ context(garbage) do
113
+ updated_copyright = regex.update_copyright(2014, garbage)
69
114
 
70
- it { expect(updated_copyright[:updated_now]) .to be(false) }
71
- it { expect(updated_copyright[:found_copyright]).to be(false) }
72
- it { expect(updated_copyright[:content]) .to eq(garbage) }
115
+ it { expect(updated_copyright[:content]) .to eq(garbage) }
116
+ it { expect(updated_copyright[:copyrights_found]) .to be(0) }
117
+ it { expect(updated_copyright[:copyrights_updated]) .to be(0) }
118
+ end
73
119
  end
74
120
  end
75
121
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: github-copywriter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Jacobs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-19 00:00:00.000000000 Z
11
+ date: 2014-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler