katana_stamp 0.1.1 → 1.0.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.
data/Guardfile CHANGED
@@ -2,6 +2,6 @@
2
2
  # More info at https://github.com/guard/guard#readme
3
3
 
4
4
  guard 'rspec', :version => 2 do
5
- watch(%r{^lib/*\.rb$})
5
+ watch(%r{^lib/**/*\.rb$}) { 'spec/katana_stamp_spec.rb' }
6
6
  watch(%r{^spec/.+_spec\.rb$})
7
7
  end
data/README.md CHANGED
@@ -1,55 +1,2 @@
1
- # Katana Code's Copyright Stamper
2
-
3
- This gem is used internally by Katana Code. Feel free to fork it and modify for your
4
- own organisation.
5
-
6
- ## Usage:
7
-
8
- $ katana_stamp
9
-
10
- adds **(c) Copyright 2012 Katana Code Ltd. All Rights Reserved.** to the end of every Ruby file under app/ and lib/.
11
-
12
- See options below for configuration
13
-
14
- ## Options
15
-
16
- ### --include-dirs (-i)
17
-
18
- Include these dir patterns in stamp list
19
- e.g.
20
- $ katana_stamp -i vendor/**/*.rb # will also stamp files matching vendor/**/*.rb
21
-
22
- ### --exclude-dirs (-x)
23
-
24
- Don't include these dir patterns in stamp list
25
- e.g.
26
-
27
- $ katana_stamp -x app/controllers/application_controller.rb # will not stamp files matching app/controllers/application_controller.rb
28
-
29
- ### --year (-y)
30
-
31
- Change the year of the Copyright
32
- e.g.
33
-
34
- $ katana_stamp -y 1999
35
-
36
-
37
- ### --owner (-o)
38
-
39
- Change the owner of the Copyright
40
- e.g.
41
-
42
- $ katana_stamp -o "Ace Rimmer!"
43
-
44
-
45
- ### --message (-m)
46
-
47
- Overwrite the entire message for the stamp.
48
- e.g.
49
-
50
- $ katana_stamp -m "Released under the MIT license"
51
-
52
- ## Known Issues
53
-
54
- At the moment there's no way to reverse this... make sure you commit any changes before you
55
- run this!
1
+ Files in this directory are used to replace those that are stamped in specs so we start
2
+ each test with fresh, unstamped files.
@@ -11,6 +11,11 @@ options = {}
11
11
  OptionParser.new do |opts|
12
12
  opts.banner = "Katana Stamp Usage:"
13
13
 
14
+ # TODO: Consider this? Is it safe?
15
+ # opts.on("-f", "--force", "Forceably overwrite any existing stamps") do |v|
16
+ # options[:force] = v
17
+ # end
18
+
14
19
  opts.on("-i", "--include-dirs x,y,x", Array,
15
20
  "Include these dir patterns in stamp list") do |v|
16
21
  options[:include_paths] = v
@@ -14,30 +14,14 @@ module KatanaStamp
14
14
 
15
15
  DIR_PATTERS.each do |main_dir|
16
16
  Dir.glob(main_dir).each do |path|
17
+ # skip to the next file if this file matches exclude_paths
17
18
  next if options[:exclude_paths].detect { |pattern| File.fnmatch(pattern, path) }
18
- custom_file = StampFile.new(path, options)
19
- if custom_file.has_stamp?
20
- print_colour("#{path} already stamped!", :yellow)
21
- else
22
- print_colour("#{path} stamped!", :green)
23
- custom_file.stamp
24
- end
19
+ StampFile.new(path, options).stamp!
25
20
  end
26
21
  end
27
- true # return true
28
- end
29
-
30
- def self.print_colour(message, colour)
31
- colour_id =
32
- case colour
33
- when :yellow then 33
34
- when :green then 32
35
- else
36
- 37 # white
37
- end
38
- puts "\033[0;#{colour_id}m#{message}\033[0m"
22
+ true # return true (success)
39
23
  end
40
24
 
41
25
  module_function :run!
42
26
 
43
- end
27
+ end
@@ -1,40 +1,108 @@
1
1
  module KatanaStamp
2
- class StampFile < Struct.new(:path, :options)
2
+ class StampFile
3
3
 
4
- # The default message stamped to each file.
5
- # The %s are placeholders for _year_ and _owner_ respectively
6
- COPYRIGHT_NOTICE = "(c) Copyright %s %s. All Rights Reserved."
4
+ def initialize(path, options)
5
+ @path = path
6
+ @comment_delimiter = options[:comment_delimiter] || '#'
7
+ @year = options[:year] || Time.now.year
8
+ @owner = options[:owner] || 'Katana Code Ltd'
9
+ @options_message = options[:message]
10
+ end
11
+
12
+ # Path to this file
13
+ attr_reader :path
14
+
15
+ # Comment delimeter used in stamp (defaults to '#')
16
+ attr_reader :comment_delimiter
7
17
 
18
+ # Copyright year (defaults to this Year)
19
+ attr_reader :year
20
+
21
+ # Copyright owner (defaults to Katana Code Ltd)
22
+ attr_reader :owner
23
+
24
+ # ANSI Colour code numbers
25
+ ANSI_COLORS = {
26
+ red: 31,
27
+ green: 32,
28
+ yellow: 33,
29
+ }
30
+
8
31
  # When called, will add a copyright notice comment to the end of the file
9
32
  # with path
10
- def stamp
11
- `echo "#{"\n" unless has_closing_break?}# #{message}" >> #{path}`
33
+ def stamp!
34
+ case
35
+ when has_stamp_with_another_owner?
36
+ print_colour("stamped by another owner!", :red, :warn)
37
+ when has_stamp?
38
+ print_colour("already stamped!", :yellow)
39
+ else
40
+ `echo "#{"\n" unless has_closing_break?}#{message}" >> #{path}`
41
+ print_colour("stamped!", :green)
42
+ end
12
43
  end
13
44
 
14
45
  # Does the file already have a copyright stamp?
46
+ def existing_stamp
47
+ @existing_stamp = begin
48
+ File.open(path, "r") do |file|
49
+ file.read.scan(%r{#{comment_delimiter}\s\(c\)(.+)\sAll\sRights\sReserved}).flatten.first
50
+ end
51
+ end
52
+ end
53
+
54
+ # The existing Copyright owner or nil
55
+ def existing_owner
56
+ existing_stamp.scan(%r{Copyright\s#{year}\s(.+)\.}).flatten.first
57
+ end
58
+
59
+ # Does this file already have a stamp?
15
60
  def has_stamp?
16
- File.open(path, "r") { |file| file.read.scan(message).any? }
61
+ !!existing_stamp
17
62
  end
18
63
 
64
+ # Regular expression to match stamps
65
+ def stamp_regexp
66
+ %r{#{comment_delimiter}\s\(c\)(.+)\sAll\sRights\sReserved}
67
+ end
68
+
69
+ # Does this file have a stamp with a different owner?
70
+ def has_stamp_with_another_owner?
71
+ has_stamp? and existing_owner != owner
72
+ end
73
+
19
74
  # The message to be stamped to each file
20
- # defaults to: COPYRIGHT_NOTICE
21
75
  def message
22
- options[:year] ||= Time.now.year
23
- options[:owner] ||= 'Katana Code Ltd'
24
- message = options[:message] || COPYRIGHT_NOTICE % [options[:year], options[:owner]]
76
+ @message ||= begin
77
+ message_prefix = "#{comment_delimiter} (c) "
78
+ if @options_message
79
+ message_prefix << @options_message
80
+ else
81
+ message_prefix << "Copyright #{year} #{owner}. All Rights Reserved"
82
+ end
83
+ message_prefix
84
+ end
25
85
  end
26
-
86
+
27
87
  private
28
-
29
88
 
89
+ # Print a message to the console with colour
90
+ def print_colour(msg, colour, method = :puts)
91
+ colour_id = ANSI_COLORS[colour] || 39
92
+ send(method, "\033[0;#{colour_id}m#{path} #{msg}\033[0m")
93
+ end
94
+
95
+ # Does this file have a line break at the end?
30
96
  def has_closing_break?
31
97
  last_char == "\n"
32
98
  end
33
99
 
100
+ # The last character of this file
34
101
  def last_char
35
102
  last_line[-1] if last_line
36
103
  end
37
104
 
105
+ # The last line of this file
38
106
  def last_line
39
107
  IO.readlines(path)[-1]
40
108
  end
@@ -1,3 +1,3 @@
1
1
  module KatanaStamp
2
- VERSION = "0.1.1"
3
- end
2
+ VERSION = "1.0.0"
3
+ end
@@ -14,6 +14,41 @@ describe KatanaStamp do
14
14
  end
15
15
 
16
16
  let(:file_content) { File.read("app/models/test_model_one.rb") }
17
+
18
+ context "printing to console" do
19
+
20
+ it "explains file has been stamped if not already" do
21
+ @orig_stdout = $stdout
22
+ $stdout = StringIO.new
23
+ run_with_options
24
+ $stdout.rewind
25
+ $stdout.string.chomp.should include("app/models/test_model_one.rb stamped!")
26
+ $stdout = @orig_stdout
27
+ end
28
+
29
+ it "explains file has already been if stamped with the same name" do
30
+ @orig_stdout = $stdout
31
+ $stdout = StringIO.new
32
+ run_with_options
33
+ run_with_options
34
+ $stdout.rewind
35
+ $stdout.string.chomp.should include("app/models/test_model_one.rb already stamped!")
36
+ $stdout = @orig_stdout
37
+ end
38
+
39
+ it "warns file has already been if stamped with another name" do
40
+ @orig_stderr = $stderr
41
+ $stderr = StringIO.new
42
+ run_with_options
43
+ run_with_options(owner: "gavin")
44
+ $stderr.rewind
45
+ $stderr.string.chomp.should include("app/models/test_model_one.rb stamped by another owner!")
46
+ $stderr = @orig_stderr
47
+ end
48
+
49
+ end
50
+
51
+
17
52
 
18
53
  context "by default" do
19
54
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katana_stamp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-01-28 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70231788536560 !ruby/object:Gem::Requirement
16
+ requirement: &70120602990220 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70231788536560
24
+ version_requirements: *70120602990220
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &70231788536140 !ruby/object:Gem::Requirement
27
+ requirement: &70120602989800 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70231788536140
35
+ version_requirements: *70120602989800
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: guard-rspec
38
- requirement: &70231788535720 !ruby/object:Gem::Requirement
38
+ requirement: &70120602989380 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70231788535720
46
+ version_requirements: *70120602989380
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
- requirement: &70231788535300 !ruby/object:Gem::Requirement
49
+ requirement: &70120602988960 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70231788535300
57
+ version_requirements: *70120602988960
58
58
  description: Adds copyright comments to .rb files within a Ruby application
59
59
  email:
60
60
  - bodacious@katanacode.com
@@ -97,7 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
97
97
  version: '0'
98
98
  segments:
99
99
  - 0
100
- hash: -4026286511023334403
100
+ hash: -1893729203238284076
101
101
  required_rubygems_version: !ruby/object:Gem::Requirement
102
102
  none: false
103
103
  requirements:
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
106
  version: '0'
107
107
  segments:
108
108
  - 0
109
- hash: -4026286511023334403
109
+ hash: -1893729203238284076
110
110
  requirements: []
111
111
  rubyforge_project: katana_stamp
112
112
  rubygems_version: 1.8.15