mmcopyrights 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,33 @@
1
+ == MM Copyrights
2
+
3
+ Add copyrights to your source files.
4
+
5
+ == Copyrights (I know this is a bit recursive)
6
+
7
+ Copyright © 2009 Micah Martin.
8
+ MM Copyrights and all included source files are distributed under terms of the GNU LGPL.
9
+
10
+ == Install
11
+
12
+ gem sources -a http://gems.github.com
13
+ sudo gem install slagyr-mmcopyrights
14
+
15
+ == Example
16
+
17
+ require 'mmcopyrights'
18
+ MM::Copyrights.process("lib", "rb", "#-", "©2009 Micah Martin\nAll rights reserved")
19
+
20
+ == Usage
21
+
22
+ MM::Copyrights.process(SOURCE_DIRECTORY, FILE_EXTENSION, COMMENT_PREFIX, COPYRIGHT_TEXT)
23
+
24
+ * SOURCE_DIRECTORY - A directory where you keep source files
25
+ * FILE_EXTENSION - To identify which files to process. A values of "rb" will process all files ending in ".rb".
26
+ * COMMENT_PREFIX - Be sure to use a unique comment prefix like "#-" for Ruby and "//-" for C, Java, C#, etc. This is used to identify existing copyright headers to remove or update them.
27
+ * COPYRIGHT_TEXT - The content of your copyright message (without comment prefix)
28
+
29
+ == Description
30
+
31
+ Running MM::Copyrights.process will add the COPYRIGHT_TEXT to all of the matching files in the SOURCE_DIRECTORY. Processing the files multiple times is harmless.
32
+
33
+ You can change the COPYRIGHT_TEXT and process files again. The old copyright headers will be replaces with the new content.
@@ -0,0 +1,87 @@
1
+ #- Copyright © 2009 Micah Martin.
2
+ #- MM Copyrights and all included source files are distributed under terms of the GNU LGPL.
3
+
4
+ module MM
5
+ module Copyrights
6
+
7
+ class <<self
8
+ attr_accessor :verbose
9
+ end
10
+
11
+ self.verbose = true
12
+
13
+ ENDL = $/
14
+
15
+ class SourceFile
16
+
17
+ attr_reader :copyright
18
+
19
+ def initialize(filename, comment_prefix)
20
+ @filename = filename
21
+ @comment_prefix = comment_prefix
22
+ @lines = IO.readlines(filename)
23
+ find_copyright
24
+ end
25
+
26
+ def has_copyright?
27
+ not @copyright.empty?
28
+ end
29
+
30
+ def find_copyright
31
+ @copyright = ''
32
+ @lines.each do |line|
33
+ break unless has_comment_prefix(line)
34
+ @copyright << line[@comment_prefix.length..-1]
35
+ end
36
+ @copyright.strip!
37
+ end
38
+
39
+ def has_comment_prefix(line)
40
+ line.match(@comment_prefix)
41
+ end
42
+
43
+ def remove_copyright
44
+ @lines.shift while has_comment_prefix(@lines[0])
45
+ end
46
+
47
+ def add_copyright(text)
48
+ copyright_header = text.split(/^/).inject([]) do |header,line|
49
+ header << "#@comment_prefix #{line}"
50
+ end
51
+ @lines.unshift ENDL unless @lines.first.strip.empty?
52
+ @lines = copyright_header + [ENDL] + @lines
53
+ end
54
+
55
+ def save!
56
+ File.open(@filename, "w"){ |f| f.print @lines }
57
+ end
58
+ end
59
+
60
+ def self.process(dir, ext, prefix, copyright)
61
+ Dir.glob(File.join(dir, "**", "*.#{ext}")).each do |filename|
62
+ source_file = SourceFile.new(filename, prefix)
63
+ if source_file.has_copyright?
64
+ if source_file.copyright != copyright
65
+ say "updated copyright: #{filename}"
66
+ source_file.remove_copyright
67
+ source_file.add_copyright(copyright)
68
+ source_file.save!
69
+ else
70
+ say "already has valid copyright: #{filename}"
71
+ end
72
+ else
73
+ say "missing copyright: #{filename}"
74
+ source_file.add_copyright(copyright)
75
+ source_file.save!
76
+ end
77
+ end
78
+ end
79
+
80
+ private
81
+
82
+ def self.say(thing)
83
+ puts thing if @verbose
84
+ end
85
+
86
+ end
87
+ end
@@ -0,0 +1,77 @@
1
+ #- Copyright © 2009 Micah Martin.
2
+ #- MM Copyrights and all included source files are distributed under terms of the GNU LGPL.
3
+
4
+ require 'rubygems'
5
+ require 'spec'
6
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/mmcopyrights")
7
+ require 'fileutils'
8
+
9
+ TEST_DATA = File.expand_path(File.dirname(__FILE__) + "/../test_data")
10
+ SAMPLE = File.expand_path(File.dirname(__FILE__) + "/../sample")
11
+
12
+ describe MM::Copyrights do
13
+
14
+ before(:each) do
15
+ MM::Copyrights.verbose = false
16
+ FileUtils.rm_rf(TEST_DATA) if File.exist?(TEST_DATA)
17
+ FileUtils.cp_r(SAMPLE, TEST_DATA)
18
+ end
19
+
20
+ def check_file(file, headers)
21
+ lines = IO.readlines(File.join(TEST_DATA, file))
22
+ headers.each do |header|
23
+ lines.shift.strip.should == header
24
+ end
25
+ original_lines = IO.readlines(File.join(SAMPLE, file))
26
+ lines.should == original_lines
27
+ end
28
+
29
+ it "should copyright all the ruby files" do
30
+ MM::Copyrights.process(TEST_DATA, "rb", "#-", "A simple copyright\nAll rights reserved")
31
+
32
+ check_file("root.rb", ["#- A simple copyright", "#- All rights reserved", ""])
33
+ check_file("one/one.rb", ["#- A simple copyright", "#- All rights reserved", ""])
34
+ check_file("two/two.rb", ["#- A simple copyright", "#- All rights reserved", ""])
35
+ check_file("one/one_child/one_child.rb", ["#- A simple copyright", "#- All rights reserved", ""])
36
+
37
+ check_file("root.java", [])
38
+ check_file("one/one.java", [])
39
+ check_file("two/two.java", [])
40
+ check_file("one/one_child/oneChild.java", [])
41
+ end
42
+
43
+ it "should copyright all the java files" do
44
+ MM::Copyrights.process(TEST_DATA, "java", "//-", "A java copyright\nAll rights reserved")
45
+
46
+ check_file("root.rb", [])
47
+ check_file("one/one.rb", [])
48
+ check_file("two/two.rb", [])
49
+ check_file("one/one_child/one_child.rb", [])
50
+
51
+ check_file("root.java", ["//- A java copyright", "//- All rights reserved", ""])
52
+ check_file("one/one.java", ["//- A java copyright", "//- All rights reserved", ""])
53
+ check_file("two/two.java", ["//- A java copyright", "//- All rights reserved", ""])
54
+ check_file("one/one_child/oneChild.java", ["//- A java copyright", "//- All rights reserved", ""])
55
+ end
56
+
57
+ it "should update existing copyrights" do
58
+ MM::Copyrights.process(TEST_DATA, "java", "//-", "Old copyright\nNo rights reserved")
59
+ MM::Copyrights.process(TEST_DATA, "java", "//-", "New copyright\nAll rights reserved")
60
+
61
+ check_file("root.java", ["//- New copyright", "//- All rights reserved", ""])
62
+ check_file("one/one.java", ["//- New copyright", "//- All rights reserved", ""])
63
+ check_file("two/two.java", ["//- New copyright", "//- All rights reserved", ""])
64
+ check_file("one/one_child/oneChild.java", ["//- New copyright", "//- All rights reserved", ""])
65
+ end
66
+
67
+ it "should not change valid copyright" do
68
+ MM::Copyrights.process(TEST_DATA, "rb", "#-", "A simple copyright\nAll rights reserved")
69
+ MM::Copyrights.process(TEST_DATA, "rb", "#-", "A simple copyright\nAll rights reserved")
70
+
71
+ check_file("root.rb", ["#- A simple copyright", "#- All rights reserved", ""])
72
+ check_file("one/one.rb", ["#- A simple copyright", "#- All rights reserved", ""])
73
+ check_file("two/two.rb", ["#- A simple copyright", "#- All rights reserved", ""])
74
+ check_file("one/one_child/one_child.rb", ["#- A simple copyright", "#- All rights reserved", ""])
75
+ end
76
+
77
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mmcopyrights
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 1
8
+ - 0
9
+ version: 1.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Micah Martin
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-06 00:49:53 -05:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Add copyright comments to all your source files
22
+ email: micah@8thlight.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.rdoc
29
+ files:
30
+ - README.rdoc
31
+ - lib/mmcopyrights.rb
32
+ - spec/mmcopyrights_spec.rb
33
+ has_rdoc: true
34
+ homepage: http://github.com/slagyr/mmcopyrights
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --inline-source
40
+ - --charset=UTF-8
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.6
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: MM Copyrights
64
+ test_files: []
65
+