licence_injector 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.
- checksums.yaml +7 -0
- data/bin/licence_injector +86 -0
- data/lib/LicenceInjector.rb +119 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7a2d0ade97b6f04f72393ed9b973b8dbd3c268b7
|
4
|
+
data.tar.gz: 48ab7cf802630e2f43f576dbc08eeaa9066a2826
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f6888bbf88f5d993ef828c78452db4f8c40efcfffe67242f3c469711f3d4097570a6dcd2210911cfec7e32e812929e17655e5a8f927a434e3d6ef8e7bb7c5a1b
|
7
|
+
data.tar.gz: 830d258291126c58013524194c56089e4a00892ac80fab03f3633d9444c9039c6d6b453d2c3a56bf52f2271477007d216a2e727137cba8cae0fc4d6c3d43b63b
|
@@ -0,0 +1,86 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require_relative '../lib/LicenceInjector.rb'
|
5
|
+
|
6
|
+
commands = [:inject, :replace]
|
7
|
+
options = {:list=>false, :old_licence=>"", :overwrite=>false}
|
8
|
+
|
9
|
+
option_parser = OptionParser.new do |opts|
|
10
|
+
executable_name = File.basename($PROGRAM_NAME)
|
11
|
+
opts.banner = "\tInject licence into src files, or replace current licence with new licence. Requires <command> - either 'inject' or 'replace'
|
12
|
+
|
13
|
+
Usage: #{executable_name} <command> [options]"
|
14
|
+
|
15
|
+
opts.on("--list", "output changed files as a list") do
|
16
|
+
options[:list] = true
|
17
|
+
end
|
18
|
+
|
19
|
+
opts.on("-o OLDLICENCE", "--old_licence OLDLICENCE", "the path to the file containing the old licence - used only with the replace command") do |old_licence|
|
20
|
+
options[:old_licence] = old_licence
|
21
|
+
end
|
22
|
+
|
23
|
+
opts.on("-l LICENCE", "--licence LICENCE", "the path to the file containing the licence") do |licence|
|
24
|
+
options[:licence] = licence
|
25
|
+
end
|
26
|
+
|
27
|
+
opts.on("-s SRCPATH", "--src_path SRCPATH", "path to the folder containing the src files to be processed. SRCPATH will be searched and all of its sub-folders") do |src_path|
|
28
|
+
options[:src_path] = src_path.gsub("\\", "/")
|
29
|
+
end
|
30
|
+
|
31
|
+
opts.on("-e EXTENSIONS", "--extensions EXTENSIONS", Array, "the list of file extensions to be searched for, comma separated. E.g. cpp,h,java") do |extensions|
|
32
|
+
options[:extensions] = extensions
|
33
|
+
end
|
34
|
+
|
35
|
+
opts.on("--overwrite", "makes the changes to the source files as opposed to the default of just reporting the number of src file discovered") do
|
36
|
+
options[:overwrite] = true
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
option_parser.parse!
|
41
|
+
|
42
|
+
def file_doesnt_exist file
|
43
|
+
!File.exist? file
|
44
|
+
end
|
45
|
+
|
46
|
+
if ARGV.empty?
|
47
|
+
puts "\tError: you must supply a command, path to licence file, path to src and some extensions"
|
48
|
+
puts
|
49
|
+
puts option_parser.help
|
50
|
+
STDERR.puts "ERROR: There are missing arguments"
|
51
|
+
exit 1
|
52
|
+
end
|
53
|
+
|
54
|
+
commands = [:inject, :replace]
|
55
|
+
command = ARGV.shift.to_sym
|
56
|
+
|
57
|
+
if options[:licence].nil? || options[:src_path].nil? || options[:extensions].nil?
|
58
|
+
STDERR.puts "ERROR: There are missing arguments"
|
59
|
+
raise OptionParser::MissingArgument
|
60
|
+
end
|
61
|
+
|
62
|
+
if file_doesnt_exist(options[:licence])
|
63
|
+
STDERR.puts "ERROR: The licence file #{options[:licence]} doesn't exist"
|
64
|
+
raise OptionParser::InvalidArgument
|
65
|
+
end
|
66
|
+
|
67
|
+
unless commands.include? command
|
68
|
+
STDERR.puts "ERROR: There are missing arguments"
|
69
|
+
raise OptionParser::MissingArgument
|
70
|
+
end
|
71
|
+
|
72
|
+
if command == :replace
|
73
|
+
if options[:old_licence].empty?
|
74
|
+
STDERR.puts "ERROR: Need an old licence when using 'replace'"
|
75
|
+
raise OptionParser::MissingArgument
|
76
|
+
end
|
77
|
+
if file_doesnt_exist(options[:old_licence])
|
78
|
+
STDERR.puts "ERROR: The old licence file #{options[:old_licence]} doesn't exist"
|
79
|
+
raise OptionParser::InvalidArgument
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
#puts options.inspect
|
84
|
+
|
85
|
+
licence_injector = LicenceInjector.new command, options[:licence], options[:src_path], options[:extensions], options[:old_licence], options[:list], options[:overwrite]
|
86
|
+
licence_injector.inject_licence
|
@@ -0,0 +1,119 @@
|
|
1
|
+
class LicenceInjector
|
2
|
+
|
3
|
+
attr_accessor :licence_file_path, :old_licence_file_path, :src_path, :extensions
|
4
|
+
attr_reader :changed_files_count
|
5
|
+
|
6
|
+
def initialize(command, licence_file_path, src_path, file_extensions, old_licence_file_path = "", list = false, overwrite = true)
|
7
|
+
@licence_file_path = licence_file_path
|
8
|
+
@old_licence_file_path = old_licence_file_path
|
9
|
+
@src_path = src_path
|
10
|
+
@extensions = file_extensions
|
11
|
+
@changed_files_count = 0
|
12
|
+
@command = command
|
13
|
+
@list = list
|
14
|
+
@overwrite = overwrite
|
15
|
+
end
|
16
|
+
|
17
|
+
def read_file file_path
|
18
|
+
File.open file_path, "r" do | file |
|
19
|
+
file.read
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def inject_licence
|
24
|
+
@changed_files_count = 0
|
25
|
+
@extensions.each do | extension |
|
26
|
+
unless @list
|
27
|
+
puts "\tprocessing #{extension} files in #{@src_path}"
|
28
|
+
print "\t"
|
29
|
+
end
|
30
|
+
Dir.glob(@src_path + "/**/*.#{extension}") do |file| #find src files in current folder and all subfolders
|
31
|
+
case
|
32
|
+
when @command == :inject
|
33
|
+
inject_licence_into_file file
|
34
|
+
when @command == :replace
|
35
|
+
replace_old_licence file
|
36
|
+
end
|
37
|
+
end
|
38
|
+
unless @list
|
39
|
+
puts
|
40
|
+
end
|
41
|
+
end
|
42
|
+
unless @list
|
43
|
+
puts
|
44
|
+
end
|
45
|
+
unless @list
|
46
|
+
if @overwrite
|
47
|
+
puts "There were #{@changed_files_count} changes made"
|
48
|
+
else
|
49
|
+
puts "#{@changed_files_count} file(s) were found. Use the --overwrite flag to make changes to the files"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def replace_old_licence file
|
55
|
+
src = read_file file
|
56
|
+
|
57
|
+
src = src.sub(read_file(@licence_file_path), read_file(@old_licence_file_path))
|
58
|
+
|
59
|
+
if @overwrite
|
60
|
+
begin
|
61
|
+
output = File.new(file, "w")
|
62
|
+
output.write src
|
63
|
+
output.close
|
64
|
+
rescue
|
65
|
+
puts
|
66
|
+
STDERR.puts "ERROR: There was a problem writing to '#{file}"
|
67
|
+
exit 1
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
if @list
|
72
|
+
puts File.absolute_path(file)
|
73
|
+
else
|
74
|
+
print "."
|
75
|
+
end
|
76
|
+
@changed_files_count += 1
|
77
|
+
end
|
78
|
+
|
79
|
+
def inject_licence_into_file file
|
80
|
+
# possible refactor here - using an array to read the src so we can easily check the
|
81
|
+
# first line for special content. If we used a string we could reuse the read file method
|
82
|
+
# but lose the nice array methods... Instinct is to use array
|
83
|
+
old_content = []
|
84
|
+
newcontent = []
|
85
|
+
File.open(file, "r") do | f |
|
86
|
+
old_content = f.readlines
|
87
|
+
end
|
88
|
+
|
89
|
+
#some files need to have their first lines maintained such as xml files and files using a shebang
|
90
|
+
unless old_content.first.nil? # empty file
|
91
|
+
if old_content.first.start_with?("#!") || old_content.first.start_with?("<?xml")
|
92
|
+
newcontent.push old_content.first
|
93
|
+
old_content.shift
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
newcontent.push read_file @licence_file_path
|
98
|
+
newcontent = newcontent + old_content
|
99
|
+
|
100
|
+
if @overwrite
|
101
|
+
begin
|
102
|
+
output = File.new(file, "w")
|
103
|
+
newcontent.each { |line| output.write line }
|
104
|
+
output.close
|
105
|
+
rescue
|
106
|
+
puts
|
107
|
+
STDERR.puts "ERROR: There was a problem writing to '#{file}"
|
108
|
+
exit 1
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
if @list
|
113
|
+
puts File.absolute_path(file)
|
114
|
+
else
|
115
|
+
print "."
|
116
|
+
end
|
117
|
+
@changed_files_count += 1
|
118
|
+
end
|
119
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: licence_injector
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Barry Drinkwater
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: licence_injector injects text from the given licence file into each source
|
14
|
+
file found at the given source path whose extension is one of those in the given
|
15
|
+
list of extensions. licence_injector also allows a previously injected licence to
|
16
|
+
be replaced with a new licence
|
17
|
+
email:
|
18
|
+
- barry@penrillian.com
|
19
|
+
executables:
|
20
|
+
- licence_injector
|
21
|
+
extensions: []
|
22
|
+
extra_rdoc_files: []
|
23
|
+
files:
|
24
|
+
- lib/LicenceInjector.rb
|
25
|
+
- bin/licence_injector
|
26
|
+
homepage: http://www.penrillian.com
|
27
|
+
licenses:
|
28
|
+
- BSD Clause 2
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 2.0.3
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: Injects licence text into source files
|
50
|
+
test_files: []
|