bake-modernize 0.13.5 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2047cc401761b2a99b5e702ee54e45500f4b1dae3d01079754023c07797ea7ea
4
- data.tar.gz: 73ead30a3b412d0010321ab7ce4a88abebc978e14ac912cb06375da1ddd4a457
3
+ metadata.gz: 23c20cae9dae81e435771d980f8c7944a78e818809cca3161ac428d94fd690f6
4
+ data.tar.gz: 1bb494fe765f62e0da2ad5b4a690e519499acc01db7e19ec66949d574cb83a7b
5
5
  SHA512:
6
- metadata.gz: c628b25804a29175e23d2d9a34d8f2609eea158799d9a51b68f71f5327d463f050881b7a246c0b98422d17ed6aa7a90e8c68110ef9597425e94433cd49f572a8
7
- data.tar.gz: 35ae365477c6f1c2d287771635870eb58132c651e73dfcc43741cb7b55933d71ab64a8d38b614b1cd33abb11db8ca29edc203809c7a0230f4ceeca9c55d00ab8
6
+ metadata.gz: f287b0e61d32d6d3f9b60e6b93a796a3de8529581617691b6bfc784a5a6f30e9c7405b994736d5c0cbed774a89371431fd2b2202ec044b92705a06e981ed390c
7
+ data.tar.gz: e36f6ef0a58d24e8196726d732fdb2b4d09643ea299636ff66279b326e2eccd07c47ab1387c903edb03442dc540b93118b8ca811123040a6d030014e4490ee4e
checksums.yaml.gz.sig CHANGED
Binary file
@@ -3,7 +3,7 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2022, by Samuel Williams.
5
5
 
6
- require 'console'
6
+ require 'bake/modernize'
7
7
 
8
8
  LICENSE = <<~LICENSE
9
9
  Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -31,16 +31,15 @@ def license
31
31
  end
32
32
 
33
33
  def update(root:)
34
- authors, paths = self.authors(root)
34
+ authorship = Bake::Modernize::License::Authorship.new
35
+ authorship.extract(root)
35
36
 
36
37
  buffer = StringIO.new
37
38
  buffer.puts "# MIT License"
38
39
  buffer.puts
39
40
 
40
- authors.map do |author, dates|
41
- years = dates.map(&:year).uniq
42
-
43
- buffer.puts "Copyright, #{years.join('-')}, by #{author}. "
41
+ authorship.copyrights.each do |copyright|
42
+ buffer.puts "#{copyright.statement} "
44
43
  end
45
44
 
46
45
  buffer.puts
@@ -50,19 +49,21 @@ def update(root:)
50
49
  file.write(buffer.string)
51
50
  end
52
51
 
53
- paths.each do |path, authors|
52
+ authorship.paths.each do |path, modifications|
54
53
  next unless File.exist?(path)
55
54
 
56
55
  case path
57
56
  when /\.rb$/
58
- self.update_source_file_authors(path, authors)
57
+ self.update_source_file_authors(authorship, path, modifications)
59
58
  end
60
59
  end
61
60
  end
62
61
 
63
62
  private
64
63
 
65
- def update_source_file_authors(path, authors)
64
+ def update_source_file_authors(authorship, path, modifications)
65
+ copyrights = authorship.copyrights_for_modifications(modifications)
66
+
66
67
  input = File.readlines(path)
67
68
  output = []
68
69
 
@@ -90,10 +91,8 @@ def update_source_file_authors(path, authors)
90
91
 
91
92
  output << "# Released under the MIT License.\n"
92
93
 
93
- authors.each do |author, dates|
94
- years = dates.map(&:year).uniq
95
-
96
- output << "# Copyright, #{years.join('-')}, by #{author}.\n"
94
+ copyrights.each do |copyright|
95
+ output << "# #{copyright.statement}\n"
97
96
  end
98
97
 
99
98
  output << "\n"
@@ -104,67 +103,3 @@ def update_source_file_authors(path, authors)
104
103
  file.puts(output)
105
104
  end
106
105
  end
107
-
108
- def authors(root)
109
- paths = {}
110
- authors = {}
111
-
112
- total = `git rev-list --count HEAD`.to_i
113
-
114
- input, output = IO.pipe
115
- pid = Process.spawn("git", "log", "--name-only", "--pretty=format:%ad\t%an", out: output, chdir: root)
116
-
117
- output.close
118
-
119
- progress = Console.logger.progress(self, total)
120
-
121
- while header = (input.readline.chomp rescue nil)
122
- break if header.empty?
123
- progress.increment
124
-
125
- date, author = header.split("\t", 2)
126
- date = Date.parse(date)
127
-
128
- while path = (input.readline.chomp rescue nil)
129
- break if path.empty?
130
-
131
- paths[path] ||= {}
132
- paths[path][author] ||= []
133
- paths[path][author] << date
134
-
135
- authors[author] ||= []
136
- authors[author] << date
137
- end
138
- end
139
-
140
- input.close
141
- Process.wait2(pid)
142
-
143
- paths.each do |path, authors|
144
- authors.transform_values! do |dates|
145
- dates.minmax
146
- end
147
- end
148
-
149
- paths.transform_values! do |authors|
150
- authors.transform_values! do |dates|
151
- dates.minmax
152
- end
153
-
154
- authors.sort_by do |author, dates|
155
- dates
156
- end
157
- end
158
-
159
- authors.transform_values! do |dates|
160
- dates.minmax
161
- end
162
-
163
- authors = authors.to_a
164
-
165
- authors.sort_by! do |author, dates|
166
- dates
167
- end
168
-
169
- return authors, paths
170
- end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2022, by Samuel Williams.
5
+
6
+ require 'rugged'
7
+
8
+ module Bake
9
+ module Modernize
10
+ module License
11
+ class Mailmap
12
+ def self.for(root)
13
+ full_path = File.join(root, '.mailmap')
14
+
15
+ if File.exist?(full_path)
16
+ mailmap = self.new
17
+ mailmap.extract(full_path)
18
+ return mailmap
19
+ end
20
+ end
21
+
22
+ def initialize
23
+ @names = {}
24
+ end
25
+
26
+ attr :names
27
+
28
+ def extract(path)
29
+ File.open(path, 'r') do |file|
30
+ file.each_line do |line|
31
+ # Skip comments
32
+ next if line =~ /^#/
33
+ # Skip empty lines
34
+ next if line =~ /^\s*$/
35
+ # Parse line
36
+ # Format: Full Name <email@address>
37
+ line.match(/^(.*) <(.*)>$/) do |match|
38
+ @names[match[2]] = match[1]
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ class Authorship
46
+ Modification = Struct.new(:author, :time, :path) do
47
+ def full_name
48
+ author[:name]
49
+ end
50
+ end
51
+
52
+ Copyright = Struct.new(:dates, :author) do
53
+ def <=> other
54
+ self.to_a <=> other.to_a
55
+ end
56
+
57
+ def statement
58
+ years = self.dates.map(&:year).uniq
59
+ return "Copyright, #{years.join('-')}, by #{author}."
60
+ end
61
+ end
62
+
63
+ def initialize
64
+ @paths = {}
65
+ end
66
+
67
+ attr :paths
68
+
69
+ def extract(root = Dir.pwd)
70
+ mailmap = Mailmap.for(root)
71
+
72
+ walk(Rugged::Repository.discover(root), mailmap: mailmap)
73
+
74
+ return self
75
+ end
76
+
77
+ def copyrights
78
+ copyrights_for_modifications(@paths.values.flatten)
79
+ end
80
+
81
+ def copyrights_for_path(path)
82
+ copyrights_for_modifications(@paths[path])
83
+ end
84
+
85
+ def copyrights_for_modifications(modifications)
86
+ authors = modifications.group_by{|modification| modification.full_name}
87
+
88
+ authors.map do |name, modifications|
89
+ Copyright.new(modifications.map(&:time).minmax, name)
90
+ end.sort
91
+ end
92
+
93
+ private
94
+
95
+ DEFAULT_SORT = Rugged::SORT_DATE | Rugged::SORT_TOPO | Rugged::SORT_REVERSE
96
+
97
+ def walk(repository, mailmap: nil, show: "HEAD")
98
+ Rugged::Walker.walk(repository, show: show, sort: DEFAULT_SORT) do |commit|
99
+ diff = commit.diff
100
+ diff.find_similar!
101
+
102
+ diff.each_delta do |delta|
103
+ old_path = delta.old_file[:path]
104
+ new_path = delta.new_file[:path]
105
+
106
+ @paths[new_path] ||= []
107
+
108
+ if old_path != new_path
109
+ # The file was moved, move copyright information too:
110
+ @paths[new_path].concat(@paths[old_path])
111
+ end
112
+
113
+ author = commit.author
114
+
115
+ if mailmap
116
+ if name = mailmap.names[author[:email]]
117
+ author[:name] = name
118
+ end
119
+ end
120
+
121
+ @paths[new_path] << Modification.new(author, commit.time, new_path)
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
@@ -5,6 +5,6 @@
5
5
 
6
6
  module Bake
7
7
  module Modernize
8
- VERSION = "0.13.5"
8
+ VERSION = "0.14.0"
9
9
  end
10
10
  end
@@ -3,7 +3,8 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2020-2022, by Samuel Williams.
5
5
 
6
- require "bake/modernize/version"
6
+ require_relative 'modernize/license'
7
+ require_relative 'modernize/version'
7
8
  require 'build/files/glob'
8
9
  require 'fileutils'
9
10
 
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bake-modernize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.5
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -38,7 +38,7 @@ cert_chain:
38
38
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
39
39
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
40
40
  -----END CERTIFICATE-----
41
- date: 2022-08-27 00:00:00.000000000 Z
41
+ date: 2022-09-02 00:00:00.000000000 Z
42
42
  dependencies:
43
43
  - !ruby/object:Gem::Dependency
44
44
  name: async-http
@@ -141,6 +141,7 @@ files:
141
141
  - bake/modernize/readme.rb
142
142
  - bake/modernize/signing.rb
143
143
  - lib/bake/modernize.rb
144
+ - lib/bake/modernize/license.rb
144
145
  - lib/bake/modernize/version.rb
145
146
  - license.md
146
147
  - readme.md
metadata.gz.sig CHANGED
Binary file