Class: LicenseHeader::TagLicense
- Inherits:
-
Object
- Object
- LicenseHeader::TagLicense
- Defined in:
- lib/license_header/tag_license.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_license(file) ⇒ Object
- #dry_run? ⇒ Boolean
- #has_copyright?(file) ⇒ Boolean
-
#initialize(options = {}) ⇒ TagLicense
constructor
A new instance of TagLicense.
- #license_check ⇒ Object
- #tag_directory_recursively(directory) ⇒ Object
- #tempfile ⇒ Object
- #whitelisted?(file) ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ TagLicense
Returns a new instance of TagLicense.
21 22 23 24 25 |
# File 'lib/license_header/tag_license.rb', line 21 def initialize( = {}) @dry_run = [:dry_run] @license_content = [:license_content] || LICENSE @extensions = [:extension] || ['rb'] end |
Class Method Details
.run! ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/license_header/tag_license.rb', line 81 def self.run! = { :target => '.' } = OptionParser.new do |opts| opts. = "Usager: license_header [options]" opts.separator "" opts.separator "Options:" opts.on("-t", "--target=val", String, "specify the target directory, default to current directory") do |v| [:target] = v end opts.on("-l", "--license=val", String, "specify the header to apply to the source code") do |v| [:license_content] = File.read(v) if File.exist?(v) end opts.on("--extensions=[x,y,z]", Array, "rb,cs") do |v| [:extensions] = v end opts.on("-d", "--dry-run", "List the files to changes") do |v| [:dry_run] = v end opts.on_tail end begin .parse! if [:license_content].nil? puts "Missing the license header file" puts exit else tag = TagLicense.new() modified = tag.tag_directory_recursively([:target]) modified.each { |f| puts "Modified: #{f}" } end rescue OptionParser::InvalidOption puts "Invalid option" puts end end |
Instance Method Details
#add_license(file) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/license_header/tag_license.rb', line 48 def add_license(file) temp = tempfile File.open(temp, 'w+') do |fd| fd.write(@license_content) fd.write(File.read(file)) end FileUtils.cp(temp.path, file) FileUtils.rm_rf(temp.path) end |
#dry_run? ⇒ Boolean
77 78 79 |
# File 'lib/license_header/tag_license.rb', line 77 def dry_run? @dry_run end |
#has_copyright?(file) ⇒ Boolean
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/license_header/tag_license.rb', line 32 def has_copyright?(file) fd = File.open(file) line = fd.gets fd.close if line =~ license_check return true else return false end end |
#license_check ⇒ Object
59 60 61 |
# File 'lib/license_header/tag_license.rb', line 59 def license_check /#{@license_check ||= @license_content.split("\n").shift}$/ end |
#tag_directory_recursively(directory) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/license_header/tag_license.rb', line 63 def tag_directory_recursively(directory) modified_files = [] Dir[File.join(File.(directory), '**/*')].each do |f| if !File.directory?(f) && whitelisted?(f) && !has_copyright?(f) modified_files << f add_license(f) unless dry_run? end end modified_files end |
#tempfile ⇒ Object
44 45 46 |
# File 'lib/license_header/tag_license.rb', line 44 def tempfile Tempfile.new(SecureRandom.hex(30)) end |
#whitelisted?(file) ⇒ Boolean
27 28 29 30 |
# File 'lib/license_header/tag_license.rb', line 27 def whitelisted?(file) extension = File.basename(file).split('.').pop @extensions.include?(extension) end |