stamper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7260804a0ee8714a348bba3207f9ed391591c74d
4
+ data.tar.gz: 8597342b0aca99fa0c1b2dde2593d5a06642fa6d
5
+ SHA512:
6
+ metadata.gz: 99262c5f8b66aa88d89790a594ba42ac3a14f6c42216ab0ccbb578d3c4af60664e076ea00701fd95aae645310f2eeb41cd1a575c17692e1d48685be75b40c4f2
7
+ data.tar.gz: f9530d1daa881c2bc7646691368a0fd6336ffe9f5292724e161312fd075b4a919274b4bdaec47c67a7d30d63d21af076f851d29901f39e3edfd0f3d8072574f4
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .config
4
+ .yardoc
5
+ Gemfile.lock
6
+ InstalledFiles
7
+ _yardoc
8
+ coverage
9
+ doc/
10
+ lib/bundler/man
11
+ pkg
12
+ rdoc
13
+ spec/reports
14
+ test/tmp
15
+ test/version_tmp
16
+ tmp
17
+ *.bundle
18
+ *.so
19
+ *.o
20
+ *.a
21
+ mkmf.log
22
+ vendor/bundle
data/.rubocop.yml ADDED
@@ -0,0 +1,35 @@
1
+ AllCops:
2
+ Include:
3
+ - config.ru
4
+ - bin
5
+ - lib
6
+ - Gemfile
7
+ - Guardfile
8
+ - Rakefile
9
+ Exclude:
10
+ - vendor/**/*
11
+ - pkg/**/*
12
+
13
+ ClassLength:
14
+ Enabled: false
15
+
16
+ Documentation:
17
+ Enabled: false
18
+
19
+ LineLength:
20
+ Enabled: false
21
+
22
+ MethodLength:
23
+ Enabled: false
24
+
25
+ RegexpLiteral:
26
+ Enabled: false
27
+
28
+ AlignHash:
29
+ Enabled: false
30
+
31
+ SpecialGlobalVars:
32
+ Enabled: false
33
+
34
+ CyclomaticComplexity:
35
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in stamper.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Renier Morales
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,74 @@
1
+ # Stamper
2
+
3
+ Prepends a blurb of text to any files you specify while respecting a list of
4
+ includes and exclude patterns for maximum flexibility.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'stamper'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ sudo gem install stamper
19
+
20
+ ## Usage
21
+
22
+ ### CLI
23
+
24
+ $ stamper --help
25
+ Usage: stamper -s STAMPFILE [options]
26
+
27
+ Prepends a blurb of text to any files you specify while respecting
28
+ a list of includes and exclude patterns for maximum flexibility.
29
+
30
+ Required:
31
+ -s, --stamp STAMPFILE Read the stamp from the specified file.
32
+
33
+ Options:
34
+ -p, --path DIRECTORY Directory to scan for files. Defaults to current directory.
35
+ -i, --include 'REGEXP' Only stamp files that match this pattern. Can be used multiple times.
36
+ Defaults to [".*\\.rb$"].
37
+ -e, --exclude 'REGEXP' Do not stamp files that match this pattern. Can be used multiple times.
38
+ Evaluated after includes. Defaults to ["^vendor/"].
39
+ -r, --respect 'REGEXP' If the first line in the file matches this pattern,
40
+ place stamp under that line.
41
+ Can be used multiple times. Defaults to ["^#", "^<!"].
42
+ -d, --dry-run Report which files need stamping, but don't make any changes.
43
+ -q, --quiet Do not print any output.
44
+ -h, --help Show this message.
45
+ --version Show version.
46
+
47
+ **Note**: The `--respect` option is nice for telling _Stamper_ to leave any first
48
+ magic-encoding lines untouched and unmoved. The stamp will be placed under the
49
+ matching line. Will only respect the first line, however, if matched.
50
+
51
+ **Hint**: You may want to try a few runs with the `--dry-run` option on to see what its doing,
52
+ until you get all the options you want right.
53
+
54
+ ### Programmatic
55
+
56
+ require 'stamper'
57
+
58
+ Stamper.stamp(
59
+ stamp: IO.read('copyright.txt'),
60
+ files: Stamper::DEFAULTS[:files], # This is a glob. Will be fed into Dir.glob()
61
+ includes: Stamper::DEFAULTS[:includes], # Defaults to ['.*\.rb$']
62
+ excludes: Stamper::DEFAULTS[:excludes], # Defaults to ['^vendor/']
63
+ respect_first_marks: Stamper::DEFAULTS[:respect_first_marks], # Defaults to ['^#', '^<!'']
64
+ dryrun: false,
65
+ quiet: true
66
+ )
67
+
68
+ ## Contributing
69
+
70
+ 1. Fork it ( https://github.com/renier/stamper/fork )
71
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
72
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
73
+ 4. Push to the branch (`git push origin my-new-feature`)
74
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rubocop/rake_task'
3
+
4
+ task :rubocop do
5
+ RuboCop::RakeTask.new
6
+ end
data/bin/stamper ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'stamper/cli'
3
+
4
+ exit(Stamper.CLI(ARGV) ? 0 : 1)
data/lib/stamper.rb ADDED
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env ruby
2
+ require 'colorize'
3
+ require 'stamper/version'
4
+
5
+ module Stamper
6
+ DEFAULTS = {
7
+ files: '**/*',
8
+ includes: ['.*\.rb$'],
9
+ excludes: ['^vendor/'],
10
+ respect_first_marks: ['^#', '^<!'],
11
+ dryrun: false,
12
+ quiet: false
13
+ }
14
+
15
+ def self.stamp(opts)
16
+ opts = DEFAULTS.merge(opts)
17
+
18
+ stamp, files = opts[:stamp], Dir.glob(opts[:files])
19
+ includes, excludes = opts[:includes], opts[:excludes]
20
+ respect_first_marks, dryrun = opts[:respect_first_marks], opts[:dryrun]
21
+ quiet = opts[:quiet]
22
+
23
+ if dryrun
24
+ puts "Checking files that need stamping...\n\n" unless quiet
25
+ else
26
+ puts "Stamping files...\n\n" unless quiet
27
+ end
28
+
29
+ stamped = 0
30
+ # For each file that matches pattern(s)
31
+ files.each do |file|
32
+ next unless includes.any? { |include| Regexp.new(include).match(file) }
33
+ next if excludes.any? { |exclude| Regexp.new(exclude).match(file) }
34
+
35
+ # Check the header of the file. Match on first lines or shifted by one line.
36
+ # If match, do nothing, else stamp file (or report only -- use colorize).
37
+ contents = IO.read(file)
38
+ next if contents.size < 1
39
+ next if contents.start_with?(stamp)
40
+ contents = contents.split("\n")
41
+ next if contents[1..-1].join("\n").start_with?(stamp)
42
+
43
+ if dryrun
44
+ puts file unless quiet
45
+ stamped += 1
46
+ next
47
+ end
48
+
49
+ if respect_first_marks.any? { |mark| Regexp.new(mark).match(contents.first) }
50
+ shifted = contents.shift
51
+ contents = shifted + "\n" + stamp + contents.join("\n")
52
+ else
53
+ contents = stamp + contents.join("\n")
54
+ end
55
+
56
+ IO.write(file, contents)
57
+ stamped += 1
58
+ puts file unless quiet
59
+ end
60
+
61
+ print "\nFinished. ".green
62
+ if dryrun
63
+ puts "#{stamped} files need stamping.".send(stamped > 0 ? :red : :green) unless quiet
64
+ else
65
+ puts "#{stamped} files were stamped.".green unless quiet
66
+ end
67
+
68
+ dryrun && stamped > 0 ? false : true
69
+ end
70
+ end
@@ -0,0 +1,109 @@
1
+ require 'optparse'
2
+ require 'ostruct'
3
+ require 'logger'
4
+ require 'stamper'
5
+
6
+ module Stamper
7
+ def self.CLI(args) # rubocop:disable MethodName
8
+ options = OpenStruct.new
9
+
10
+ optparse = OptionParser.new do |opts|
11
+ opts.banner = 'Usage: stamper -s STAMPFILE [options]'
12
+ opts.separator ''
13
+ opts.separator "Prepends a blurb of text to any files you specify while respecting\n"\
14
+ 'a list of includes and exclude patterns for maximum flexibility.'
15
+ opts.separator ''
16
+
17
+ opts.separator 'Required:'
18
+ opts.on('-s', '--stamp STAMPFILE', 'Read the stamp from the specified file.') do |stamp|
19
+ options.stamp = stamp
20
+ end
21
+
22
+ opts.separator ''
23
+ opts.separator 'Options:'
24
+
25
+ opts.on(
26
+ '-p',
27
+ '--path DIRECTORY',
28
+ 'Directory to scan for files. Defaults to current directory.'
29
+ ) do |path|
30
+ options.path = path
31
+ end
32
+
33
+ opts.on(
34
+ '-i',
35
+ '--include \'REGEXP\'',
36
+ "Only stamp files that match this pattern. Can be used multiple times.\n"\
37
+ "\t\t\t\t\tDefaults to #{Stamper::DEFAULTS[:includes]}."
38
+ ) do |include|
39
+ options.includes ||= []
40
+ options.includes << include
41
+ end
42
+
43
+ opts.on(
44
+ '-e',
45
+ '--exclude \'REGEXP\'',
46
+ "Do not stamp files that match this pattern. Can be used multiple times.\n"\
47
+ "\t\t\t\t\tEvaluated after includes. Defaults to #{Stamper::DEFAULTS[:excludes]}."
48
+ ) do |exclude|
49
+ options.excludes ||= []
50
+ options.excludes << exclude
51
+ end
52
+
53
+ opts.on(
54
+ '-r',
55
+ '--respect \'REGEXP\'',
56
+ "If the first line in the file matches this pattern,\n"\
57
+ "\t\t\t\t\tplace stamp under that line. \n"\
58
+ "\t\t\t\t\tCan be used multiple times. Defaults to #{Stamper::DEFAULTS[:respect_first_marks]}."
59
+ ) do |mark|
60
+ options.marks ||= []
61
+ options.marks << mark
62
+ end
63
+
64
+ opts.on(
65
+ '-d',
66
+ '--dry-run',
67
+ 'Report which files need stamping, but don\'t make any changes.'
68
+ ) do |dryrun|
69
+ options.dryrun = dryrun
70
+ end
71
+
72
+ opts.on(
73
+ '-q',
74
+ '--quiet',
75
+ 'Do not print any output.'
76
+ ) do |dryrun|
77
+ options.dryrun = dryrun
78
+ end
79
+
80
+ opts.on_tail('-h', '--help', 'Show this message.') do
81
+ puts opts
82
+ exit
83
+ end
84
+
85
+ opts.on_tail('--version', 'Show version.') do
86
+ puts VERSION
87
+ exit
88
+ end
89
+ end
90
+
91
+ optparse.parse!(args)
92
+
93
+ if options.stamp.nil?
94
+ puts "No stamp file specified.\n\n"
95
+ puts optparse
96
+ exit
97
+ end
98
+
99
+ Stamper.stamp(
100
+ stamp: IO.read(options.stamp),
101
+ files: options.path ? options.path + '/**/*' : Stamper::DEFAULTS[:files],
102
+ includes: options.includes ? options.includes : Stamper::DEFAULTS[:includes],
103
+ excludes: options.excludes ? options.excludes : Stamper::DEFAULTS[:excludes],
104
+ respect_first_marks: options.marks ? options.marks : Stamper::DEFAULTS[:respect_first_marks],
105
+ dryrun: options.dryrun,
106
+ quiet: options.quiet
107
+ )
108
+ end
109
+ end
@@ -0,0 +1,3 @@
1
+ module Stamper
2
+ VERSION = '0.1.0'
3
+ end
data/stamper.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'stamper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'stamper'
8
+ spec.version = Stamper::VERSION
9
+ spec.authors = ['Renier Morales']
10
+ spec.email = ['renier@morales-rodriguez.net']
11
+ spec.summary = %q{You know. For file stamping.}
12
+ spec.description = %q{Prepends a blurb of text to any files you specify while respecting a list of includes and exclude patterns for maximum flexibility.}
13
+ spec.homepage = 'https://github.com/renier/stamper'
14
+ spec.license = 'MIT'
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_dependency 'colorize', '~> 0.7.3'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.6'
24
+ spec.add_development_dependency 'rake', '~> 10.3.0'
25
+ spec.add_development_dependency 'rubocop', '~> 0.23.0'
26
+ spec.add_development_dependency 'pry', '~> 0.10.0'
27
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stamper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Renier Morales
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.7.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.7.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 10.3.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 10.3.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.23.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.23.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.10.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.10.0
83
+ description: Prepends a blurb of text to any files you specify while respecting a
84
+ list of includes and exclude patterns for maximum flexibility.
85
+ email:
86
+ - renier@morales-rodriguez.net
87
+ executables:
88
+ - stamper
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rubocop.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/stamper
99
+ - lib/stamper.rb
100
+ - lib/stamper/cli.rb
101
+ - lib/stamper/version.rb
102
+ - stamper.gemspec
103
+ homepage: https://github.com/renier/stamper
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.2.2
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: You know. For file stamping.
127
+ test_files: []