prepend_code 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in prepend_code.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 ryooo321
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # prepend_code
2
+
3
+ prepend_code will prepend context on your files.
4
+
5
+ prepend_code mainly supports:
6
+
7
+ * It handles recursively to the directory.
8
+
9
+ ## Supported Ruby versions and implementations
10
+ I've tested on:(maybe move on 1.8.7, 1.9.2 too)
11
+
12
+ * Ruby 1.9.3
13
+
14
+ ## Install
15
+
16
+ $ gem install prepend_code
17
+
18
+ ## Usage
19
+
20
+ ### on your rails project directory
21
+ $ prepend_code
22
+
23
+ ### view all options by --help.
24
+ $ prepend_code --help
25
+
26
+ ### specify context.
27
+ $ prepend_code -t "# Copyright (c) 2013 ryooo321."
28
+ # default is # coding: utf-8
29
+
30
+ ### specify target directory.
31
+ $ prepend_code -d ./lib
32
+ # default is ./app
33
+
34
+ ### specify target file extension.
35
+ $ prepend_code -e .erb
36
+ # default is .rb
37
+
38
+ ## For dev
39
+
40
+ ### run rspec
41
+ $ rake spec
42
+
43
+ ### set gem version
44
+ $ rake version:write MAJOR=0 MINOR=1 PATCH=0
45
+
46
+ ### build gem
47
+ $ rake build
48
+
49
+ ### release gem to RubyGems.org
50
+ $ rake release
51
+
52
+ ### Other
53
+
54
+ ## Contributing to Lapidary
55
+
56
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
57
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
58
+ * Fork the project.
59
+ * Start a feature/bugfix branch.
60
+ * Commit and push until you are happy with your contribution.
61
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
62
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
63
+
64
+ ## Copyright
65
+
66
+ Copyright (c) 2013 ryooo321. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ # rspec
4
+ require "rspec/core"
5
+ require "rspec/core/rake_task"
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.pattern = FileList["spec/**/*_spec.rb"]
8
+ spec.rspec_opts = ["-c", "-fs"]
9
+ end
data/bin/prepend_code ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'prepend_code'
4
+
5
+ ret = PrependCode::Application.run!(*ARGV)
6
+
7
+ exit ret
@@ -0,0 +1,3 @@
1
+ module PrependCode
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,64 @@
1
+ require "prepend_code/version"
2
+ begin
3
+ require "pry"
4
+ rescue LoadError
5
+ end
6
+
7
+ module PrependCode
8
+ module Application
9
+ def self.run!(*arguments)
10
+ dir_base = './app'
11
+ ext = '.rb'
12
+ code = '# coding: utf-8'
13
+ opts = OptionParser.new
14
+ opts.on("-d target directory. default: ./app."){|v| dir_base = v }
15
+ opts.on("-e target file extension. default: .rb"){|v| ext = v }
16
+ opts.on("-t prepend context. default: # coding: utf-8"){|v| code = v }
17
+ opts.parse!(arguments)
18
+
19
+ file_paths = find_file_paths(dir_base, ext)
20
+
21
+ count = 0
22
+ file_paths.each do |file_path|
23
+ result = prepend_on_file!(file_path, code)
24
+ count += 1 if result
25
+ end
26
+ puts sprintf('%s files has been matched.', file_paths.count)
27
+ puts sprintf('%s files has been saved.', count)
28
+ return 1
29
+ end
30
+
31
+ def self.find_file_paths(dir_name, ext)
32
+ ret = []
33
+ dirs = Dir.glob(dir_name)
34
+ dirs.each {|d|
35
+ next unless FileTest.directory?(d)
36
+
37
+ sub_dirs = Dir.glob("#{d}/**")
38
+ sub_dirs.each {|d|
39
+ if FileTest.directory?(d)
40
+ ret += find_file_paths(d, ext)
41
+ elsif FileTest.file?(d)
42
+ if d =~ /^*#{ext}$/
43
+ ret << d
44
+ end
45
+ end
46
+ }
47
+ }
48
+ return ret
49
+ end
50
+
51
+ def self.prepend_on_file!(file_path, code)
52
+ f = File.open(file_path, "r+")
53
+ lines = f.readlines
54
+ f.close
55
+
56
+ return false if lines && lines[0] == code + "\n"
57
+ lines = [sprintf('%s%s', code, "\n")] + lines
58
+ output = File.new(file_path, "w")
59
+ lines.each { |line| output.write line }
60
+ output.close
61
+ return true
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'prepend_code/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "prepend_code"
8
+ gem.version = PrependCode::VERSION
9
+ gem.authors = ["ryooo321"]
10
+ gem.email = ["ryooo.321@gmail.com"]
11
+ gem.description = %q{Prepend context on all app ruby files. coding directive, copy right and so on.}
12
+ gem.summary = %q{Prepend context on all app ruby files.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_runtime_dependency "OptionParser"
20
+ gem.add_development_dependency "pry"
21
+ gem.add_development_dependency "rspec"
22
+ end
@@ -0,0 +1,100 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'rubygems'
3
+ require 'rspec'
4
+ require 'prepend_code'
5
+
6
+ describe PrependCode::Application do
7
+ context 'prepend_on_file!' do
8
+ context 'file first line is different from prepend_context' do
9
+ before {
10
+ @file_path = './spec/test/test.rb'
11
+ output = File.new(@file_path, "w")
12
+ lines = ["original_1\n", "original_2\n"]
13
+ lines.each { |line| output.write line }
14
+ output.close
15
+ @result = PrependCode::Application.prepend_on_file!(@file_path, 'prepend_context')
16
+ }
17
+ describe 'return ' do
18
+ subject { @result }
19
+ it { should be_true }
20
+ end
21
+
22
+ describe 'file lines' do
23
+ subject {
24
+ f = File.open(@file_path, "r+")
25
+ lines = f.readlines
26
+ f.close
27
+ lines
28
+ }
29
+ it { should == ["prepend_context\n", "original_1\n", "original_2\n"] }
30
+ end
31
+ end
32
+
33
+ context 'file first line is same with prepend_context' do
34
+ before {
35
+ @file_path = './spec/test/test.rb'
36
+ output = File.new(@file_path, "w")
37
+ lines = ["prepend_context\n", "original_1\n", "original_2\n"]
38
+ lines.each { |line| output.write line }
39
+ output.close
40
+ @result = PrependCode::Application.prepend_on_file!(@file_path, 'prepend_context')
41
+ }
42
+ describe 'return ' do
43
+ subject { @result }
44
+ it { should be_false }
45
+ end
46
+
47
+ describe 'file lines' do
48
+ subject {
49
+ f = File.open(@file_path, "r+")
50
+ lines = f.readlines
51
+ f.close
52
+ lines
53
+ }
54
+ it { should == ["prepend_context\n", "original_1\n", "original_2\n"] }
55
+ end
56
+ end
57
+ end
58
+
59
+ context 'find_file_paths' do
60
+ context 'search sub directory too, and unmatch if extension is wrong.' do
61
+ before {
62
+ @file_path1 = './spec/test/test.rb'
63
+ output = File.new(@file_path1, "w")
64
+ output.close
65
+ @file_path2 = './spec/test/sub/test2.rb'
66
+ output = File.new(@file_path2, "w")
67
+ output.close
68
+ @file_path3 = './spec/test/sub/test3.txt'
69
+ output = File.new(@file_path3, "w")
70
+ output.close
71
+ @result = PrependCode::Application.find_file_paths('./spec/test', '.rb')
72
+ }
73
+ describe 'return ' do
74
+ subject { @result }
75
+ it { should == [@file_path2, @file_path1] }
76
+ end
77
+ end
78
+ end
79
+
80
+ context 'run!' do
81
+ context 'search sub directory too, and unmatch if extension is wrong.' do
82
+ before {
83
+ @file_path1 = './spec/test/test.rb'
84
+ output = File.new(@file_path1, "w")
85
+ output.close
86
+ @file_path2 = './spec/test/sub/test2.rb'
87
+ output = File.new(@file_path2, "w")
88
+ output.close
89
+ @file_path3 = './spec/test/sub/test3.txt'
90
+ output = File.new(@file_path3, "w")
91
+ output.close
92
+ @result = PrependCode::Application.find_file_paths('./spec/test', '.rb')
93
+ }
94
+ describe 'return ' do
95
+ subject { @result }
96
+ it { should == [@file_path2, @file_path1] }
97
+ end
98
+ end
99
+ end
100
+ end
File without changes
File without changes
data/spec/test/test.rb ADDED
File without changes
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prepend_code
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ryooo321
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: OptionParser
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: pry
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Prepend context on all app ruby files. coding directive, copy right and
63
+ so on.
64
+ email:
65
+ - ryooo.321@gmail.com
66
+ executables:
67
+ - prepend_code
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - .gitignore
72
+ - Gemfile
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - bin/prepend_code
77
+ - lib/prepend_code.rb
78
+ - lib/prepend_code/version.rb
79
+ - prepend_code.gemspec
80
+ - spec/lib/prepenc_code_spec.rb
81
+ - spec/test/sub/test2.rb
82
+ - spec/test/sub/test3.txt
83
+ - spec/test/test.rb
84
+ homepage: ''
85
+ licenses: []
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 1.8.24
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Prepend context on all app ruby files.
108
+ test_files:
109
+ - spec/lib/prepenc_code_spec.rb
110
+ - spec/test/sub/test2.rb
111
+ - spec/test/sub/test3.txt
112
+ - spec/test/test.rb
113
+ has_rdoc: