license-header 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 29550a5c7febb9d4ad0981160d12585c7702dbe0
4
+ data.tar.gz: e7b7f7a145af8a3586a00a98026421bb9d10af88
5
+ SHA512:
6
+ metadata.gz: ce5a0fe189cc7b10acb0d6751181a4d6ff28adfadc953beda9381f7ba856525dc04ec61ac1e5394b5da2025727526fc307d0fc8b6bc801d27497116c81affee4
7
+ data.tar.gz: d4c8d7a696d6308c4cfb5a503ab0857079ebf5e03d95b4ab7b7b1c656fc9e885e8528b548decb85011da416754b095f6e217b71806611f78bc6a299ab98ca8e5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in license-header.gemspec
4
+ gemspec
@@ -0,0 +1,14 @@
1
+ # Copyright 2014 Pier-Hugues Pellerin
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
@@ -0,0 +1,24 @@
1
+ # License::Header
2
+
3
+ Simple way to add license header to your ruby source file.
4
+
5
+ ## Caveats
6
+
7
+ Only work for ruby file and doesnt support updating yet.
8
+
9
+
10
+ ```ruby
11
+ license-header -l LICENSE.txt -t director
12
+ ```
13
+ Dry run with -d
14
+ ```ruby
15
+ license-header -l LICENSE.txt -t director -d
16
+ ```
17
+
18
+ ## Contributing
19
+
20
+ 1. Fork it ( https://github.com/ph/license-header/fork )
21
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
22
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
23
+ 4. Push to the branch (`git push origin my-new-feature`)
24
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/license_header/tag_license'
4
+ LicenseHeader::TagLicense.run!
@@ -0,0 +1,126 @@
1
+ # Copyright 2014 Pier-Hugues Pellerin
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ require 'optparse'
16
+ require 'tempfile'
17
+ require 'securerandom'
18
+
19
+ module LicenseHeader
20
+ class TagLicense
21
+ def initialize(options = {})
22
+ @dry_run = options[:dry_run]
23
+ @license_content = options[:license_content] || LICENSE
24
+ @extensions = options[:extension] || ['rb']
25
+ end
26
+
27
+ def whitelisted?(file)
28
+ extension = File.basename(file).split('.').pop
29
+ @extensions.include?(extension)
30
+ end
31
+
32
+ def has_copyright?(file)
33
+ fd = File.open(file)
34
+ line = fd.gets
35
+ fd.close
36
+
37
+ if line =~ license_check
38
+ return true
39
+ else
40
+ return false
41
+ end
42
+ end
43
+
44
+ def tempfile
45
+ Tempfile.new(SecureRandom.hex(30))
46
+ end
47
+
48
+ def add_license(file)
49
+ temp = tempfile
50
+ File.open(temp, 'w+') do |fd|
51
+ fd.write(@license_content)
52
+ fd.write(File.read(file))
53
+ end
54
+
55
+ FileUtils.cp(temp.path, file)
56
+ FileUtils.rm_rf(temp.path)
57
+ end
58
+
59
+ def license_check
60
+ /#{@license_check ||= @license_content.split("\n").shift}$/
61
+ end
62
+
63
+ def tag_directory_recursively(directory)
64
+ modified_files = []
65
+
66
+
67
+ Dir[File.join(File.expand_path(directory), '**/*')].each do |f|
68
+ if !File.directory?(f) && whitelisted?(f) && !has_copyright?(f)
69
+ modified_files << f
70
+ add_license(f) unless dry_run?
71
+ end
72
+ end
73
+
74
+ modified_files
75
+ end
76
+
77
+ def dry_run?
78
+ @dry_run
79
+ end
80
+
81
+ def self.run!
82
+ options = { :target => '.' }
83
+
84
+ options_parser = OptionParser.new do |opts|
85
+ opts.banner = "Usager: license_header [options]"
86
+ opts.separator ""
87
+ opts.separator "Options:"
88
+
89
+ opts.on("-t", "--target=val", String, "specify the target directory, default to current directory") do |v|
90
+ options[:target] = v
91
+ end
92
+
93
+ opts.on("-l", "--license=val", String, "specify the header to apply to the source code") do |v|
94
+ options[:license_content] = File.read(v) if File.exist?(v)
95
+ end
96
+
97
+ opts.on("--extensions=[x,y,z]", Array, "rb,cs") do |v|
98
+ options[:extensions] = v
99
+ end
100
+
101
+ opts.on("-d", "--dry-run", "List the files to changes") do |v|
102
+ options[:dry_run] = v
103
+ end
104
+
105
+ opts.on_tail
106
+ end
107
+
108
+ begin
109
+ options_parser.parse!
110
+
111
+ if options[:license_content].nil?
112
+ puts "Missing the license header file"
113
+ puts options_parser
114
+ exit
115
+ else
116
+ tag = TagLicense.new(options)
117
+ modified = tag.tag_directory_recursively(options[:target])
118
+ modified.each { |f| puts "Modified: #{f}" }
119
+ end
120
+ rescue OptionParser::InvalidOption
121
+ puts "Invalid option"
122
+ puts options_parser
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,17 @@
1
+ # Copyright 2014 Pier-Hugues Pellerin
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ module LicenseHeader
16
+ VERSION = "0.0.1"
17
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'license_header/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "license-header"
8
+ spec.version = LicenseHeader::VERSION
9
+ spec.authors = ["Pier-Hugues Pellerin"]
10
+ spec.email = ["ph@heykimo.com"]
11
+ spec.summary = "Simple way to tag a directory with a specific opensource license"
12
+ spec.description = "Simple way to tag a directory with a specific opensource license"
13
+ spec.homepage = ""
14
+ spec.license = "Apache License 2.0"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.1.0"
24
+ spec.add_development_dependency "stud", "~> 0.0.18"
25
+ end
@@ -0,0 +1,16 @@
1
+ # Copyright 2014 Pier-Hugues Pellerin
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ def Testing
16
+ end
@@ -0,0 +1 @@
1
+ $:.unshift(File.expand_path("#{__FILE__}/../../lib"))
@@ -0,0 +1,73 @@
1
+ # Copyright 2014 Pier-Hugues Pellerin
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ require 'spec_helper'
16
+ require 'license_header/tag_license'
17
+ require 'fileutils'
18
+ require 'stud/temporary'
19
+
20
+ describe LicenseHeader::TagLicense do
21
+ let(:current_license) { File.read(__FILE__).split("\n").slice(0, 14).join("\n") }
22
+ let(:file_with_copyright) { File.join(File.dirname(__FILE__), 'fixtures/file_with_copyright.rb') }
23
+ let(:unmatched_file) { File.join(File.dirname(__FILE__), 'fixtures/unmatched.md') }
24
+
25
+ subject { LicenseHeader::TagLicense.new(:dry_run => true, :license_content => current_license) }
26
+
27
+ context '#tagged?' do
28
+ it "return false if the file doesn't content the copyright header" do
29
+ Stud::Temporary.file do |f|
30
+ expect(subject.has_copyright?(f)).to eq(false)
31
+ end
32
+ end
33
+
34
+ it 'return true if the file has the copyright header' do
35
+ expect(subject.has_copyright?(file_with_copyright)).to eq(true)
36
+ end
37
+ end
38
+
39
+ it "should return tagged file" do
40
+ tmp_file = temporary_file('code.rb', 'w')
41
+ files = [tmp_file, file_with_copyright]
42
+
43
+ expect(Dir).to receive(:[]).with(File.expand_path('spec/**/*')).and_return(files)
44
+ expect(subject.tag_directory_recursively("spec")).to eq([tmp_file])
45
+
46
+ FileUtils.rm_rf(tmp_file)
47
+ end
48
+
49
+ it "should add the license at the beginning of the file" do
50
+ tmp = Stud::Temporary.file
51
+
52
+ File.open(tmp, 'w+') do |f|
53
+ f.write('LGTM')
54
+ end
55
+
56
+ subject.add_license(tmp)
57
+ content = File.read(tmp)
58
+ splitted = content.split("\n")
59
+
60
+ expect(splitted.first).to match(/^# Copyright/)
61
+ expect(splitted.last).to match('LGTM')
62
+
63
+ FileUtils.rm_rf(tmp.path)
64
+
65
+ end
66
+ end
67
+
68
+ def temporary_file(suffix, *args)
69
+ root = ENV["TMP"] || ENV["TMPDIR"] || ENV["TEMP"] || "/tmp"
70
+ path = File.join(root, "#{SecureRandom.hex(30)}-#{suffix}")
71
+ File.open(path, *args) {}
72
+ path
73
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: license-header
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pier-Hugues Pellerin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.1.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: stud
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.0.18
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.0.18
69
+ description: Simple way to tag a directory with a specific opensource license
70
+ email:
71
+ - ph@heykimo.com
72
+ executables:
73
+ - license_header
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - bin/license_header
82
+ - lib/license_header/tag_license.rb
83
+ - lib/license_header/version.rb
84
+ - license-header.gemspec
85
+ - spec/fixtures/file_with_copyright.rb
86
+ - spec/spec_helper.rb
87
+ - spec/tag_license_spec.rb
88
+ homepage: ''
89
+ licenses:
90
+ - Apache License 2.0
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Simple way to tag a directory with a specific opensource license
112
+ test_files:
113
+ - spec/fixtures/file_with_copyright.rb
114
+ - spec/spec_helper.rb
115
+ - spec/tag_license_spec.rb