nub 0.0.59 → 0.0.60
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 +4 -4
- data/lib/nub/fileutils.rb +96 -10
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e93be4cfe9723de6906362477f61055ee38368002c7df4c4385934bf44d26316
|
4
|
+
data.tar.gz: eba15a1db0455f1b09390fab9c4e0600a029954945e70455af584a8f825c9f87
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e8fc6b5cb8e1e4d1a5f7c9f31a464c3b09eaac6a43fc946515ffb9f6296e6745e855682e99616881f227bf39fae053770a46cfc03065e5308f828952c2159587
|
7
|
+
data.tar.gz: 9ba29c5dc676eee5be89a96e10b26337f6a3f7eac5a46de09a0d020e655c3336533805ae468f8142bce33e796ac4f5174dcdb0b089afe22579e095cec160cd43
|
data/lib/nub/fileutils.rb
CHANGED
@@ -24,23 +24,109 @@ require 'fileutils'
|
|
24
24
|
# Monkey patch FileUtils with some useful methods
|
25
25
|
module FileUtils
|
26
26
|
|
27
|
+
# Check PATH for executable
|
28
|
+
# @param name [String] name of executable to find
|
29
|
+
# @param path [Array] path to search
|
30
|
+
# @return path of executable or nil
|
31
|
+
def self.exec?(name, path:nil)
|
32
|
+
(path || ENV['PATH'].split(File::PATH_SEPARATOR).uniq).each{|dir|
|
33
|
+
target = File.join(dir, name)
|
34
|
+
if stat = File.stat(target) rescue nil
|
35
|
+
return target if stat.file? && stat.executable?
|
36
|
+
end
|
37
|
+
}
|
38
|
+
return nil
|
39
|
+
end
|
40
|
+
|
41
|
+
# Increment SemVer formatted versions
|
42
|
+
# @param files [String/Array] files to increment version for
|
43
|
+
# @param regex [Regex] capture pattern for version match
|
44
|
+
# @param major [Bool] true then increment the major
|
45
|
+
# @param minor [Bool] true then increment the minor
|
46
|
+
# @param rev [Bool] true then increment the revision
|
47
|
+
# @param override [String] value to use for version if set
|
48
|
+
# @return true on change
|
49
|
+
def self.inc_version(files, regex, major:false, minor:false, rev:true, override:nil)
|
50
|
+
changed = nil
|
51
|
+
files = [files] if files.is_a?(String)
|
52
|
+
|
53
|
+
files.each{|filename|
|
54
|
+
changed |= self.update(filename){|line|
|
55
|
+
|
56
|
+
# Increment version
|
57
|
+
if ver = line[regex, 1]
|
58
|
+
new_ver = nil
|
59
|
+
if not override
|
60
|
+
new_ver = ver.strip.split('.')
|
61
|
+
new_ver[0] = new_ver[0].to_i + 1 if major
|
62
|
+
new_ver[1] = new_ver[1].to_i + 1 if minor
|
63
|
+
new_ver[2] = new_ver[2].to_i + 1 if rev
|
64
|
+
new_ver = new_ver * '.'
|
65
|
+
else
|
66
|
+
new_ver = override
|
67
|
+
end
|
68
|
+
|
69
|
+
# Modify the original line
|
70
|
+
line.gsub!(ver, new_ver)
|
71
|
+
end
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
return changed
|
76
|
+
end
|
77
|
+
|
78
|
+
# Update the file using a block, revert on failure.
|
79
|
+
# Use this for simple line edits. Block is passed in each line of the file
|
80
|
+
# @param filename [String] name of the file to change
|
81
|
+
# @return true on change
|
82
|
+
def self.update(filename)
|
83
|
+
changed = false
|
84
|
+
block = Proc.new # Most efficient way to get block
|
85
|
+
|
86
|
+
begin
|
87
|
+
lines = nil
|
88
|
+
File.open(filename, 'r+'){|f|
|
89
|
+
lines = f.readlines
|
90
|
+
lines.each{|line|
|
91
|
+
line_copy = line.dup
|
92
|
+
block.call(line)
|
93
|
+
changed |= line_copy != line
|
94
|
+
}
|
95
|
+
|
96
|
+
# Save the changes back to the file
|
97
|
+
f.seek(0)
|
98
|
+
f.truncate(0)
|
99
|
+
f.puts(lines)
|
100
|
+
}
|
101
|
+
rescue
|
102
|
+
# Revert back to the original incase of failure
|
103
|
+
File.open(filename, 'w'){|f| f.puts(lines)} if lines
|
104
|
+
raise
|
105
|
+
end
|
106
|
+
|
107
|
+
return changed
|
108
|
+
end
|
109
|
+
|
27
110
|
# Check copyright and update if required
|
28
111
|
# @param path [String] path of the file to update
|
29
112
|
# @param copyright [String] copyright match string e.g. Copyright (c)
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
113
|
+
# @param year [String] year to add if needed
|
114
|
+
# @return true on change
|
115
|
+
def self.update_copyright(path, copyright, year:Time.now.year)
|
116
|
+
changed = self.update(path){|line|
|
117
|
+
if line =~ /#{Regexp.quote(copyright)}/
|
118
|
+
_year = line[/#{Regexp.quote(copyright)}\s+((\d{4}\s)|(\d{4}-\d{4})).*/, 1].strip
|
119
|
+
if _year.include?("-")
|
120
|
+
years = _year.split("-")
|
121
|
+
line.gsub!(_year, "#{years.first}-#{year}") if years.last != year
|
122
|
+
elsif prev_year = _year != year.to_s ? year.to_i - 1 : nil
|
123
|
+
line.gsub!(_year.to_s, "#{prev_year}-#{year}")
|
40
124
|
end
|
41
125
|
end
|
42
126
|
}
|
127
|
+
return changed
|
43
128
|
end
|
129
|
+
|
44
130
|
end
|
45
131
|
|
46
132
|
# vim: ft=ruby:ts=2:sw=2:sts=2
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.60
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrick Crummett
|
@@ -31,27 +31,13 @@ dependencies:
|
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 5.11.3
|
34
|
-
type: :
|
34
|
+
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 5.11.3
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: rake
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '12.0'
|
48
|
-
type: :runtime
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '12.0'
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: coveralls
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|