prependfile 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in prependfile.gemspec
4
+ gemspec
@@ -0,0 +1,52 @@
1
+ PrependFile
2
+ =============
3
+
4
+ Due to the fact that most file systems do not support prepend operations, I decided to create a simple gem to do just that.
5
+
6
+
7
+ ##Usage
8
+
9
+ ### Command Line
10
+
11
+ Usage:
12
+ prependfile [options] target_file
13
+
14
+ Examples:
15
+
16
+ prependfile file.cpp --text="PUT THIS ON TOP"
17
+
18
+ prependfile file.cpp main.cpp test.cpp --text="This goes on all of them!"
19
+
20
+ prependfile *.cpp --file=banner.txt
21
+
22
+ prependfile *.rb (this will put you into STDIN)
23
+
24
+ prependfile *.rb < banner.txt
25
+
26
+ where [options] are:
27
+ --text, -t <s>: Text to prepend to file
28
+ --file, -f <s>: Contents of file to prepend to the dest file
29
+ --version, -v: Print version and exit
30
+ --help, -h: Show this message
31
+
32
+
33
+ ### Ruby Code
34
+
35
+ **General**
36
+
37
+ require 'prependfile'
38
+
39
+ PrependFile.prepend(<file_to_append>, <text_to_prepend>)
40
+
41
+ `<file_to_append>` and `<text_to_prepend>` can be a filename(String), File Object, or IO Object
42
+
43
+
44
+ **Examples**
45
+
46
+ PrependFile.prepend("sample.txt", "I want this text to be on the top!")
47
+
48
+ PrependFile.prepend("sample.txt", File.new("file_that_contains_header.txt")
49
+
50
+ PrependFile.prepend(File.new("sample.txt"), "I want this text to be on the top!")
51
+
52
+ To see all examples/cases, see the spec file.
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env ruby
2
+ f = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__
3
+ require File.join(File.dirname(f), '..', 'lib', 'prependfile')
4
+ begin
5
+ require 'trollop'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'trollop'
9
+ end
10
+
11
+ opts = Trollop::options do
12
+ version "PrependFile (c) 2011 Mike Lewis"
13
+ banner <<-EOS
14
+ prependfile prepends text to FILEs
15
+
16
+ Usage:
17
+ prependfile [options] target_file
18
+
19
+ Examples:
20
+
21
+ prependfile file.cpp --text="PUT THIS ON TOP"
22
+
23
+ prependfile file.cpp main.cpp test.cpp --text="This goes on all of them!"
24
+
25
+ prependfile *.cpp --file=banner.txt
26
+
27
+ prependfile *.rb (this will put you into STDIN)
28
+
29
+ prependfile *.rb < banner.txt
30
+
31
+ where [options] are:
32
+ EOS
33
+
34
+ opt :text, "Text to prepend to file", :type => String
35
+ opt :file, "Contents of file to prepend to the dest file", :type => String
36
+ end
37
+
38
+ Trollop::die "need to give a file to prepend to" if ARGV.empty?
39
+
40
+
41
+ prepend_content = if opts[:text]
42
+ [*opts[:text]]
43
+ elsif opts[:file]
44
+ Trollop::die :file, "must exist" unless File.file?(opts[:file]) && File.exist?(opts[:file])
45
+ File.readlines(opts[:file])
46
+ else
47
+ lines = []
48
+ while(line=$stdin.gets)
49
+ lines << line
50
+ end
51
+ lines
52
+ end.join
53
+
54
+ unless prepend_content
55
+ Trollop::die "need to use either the --text or --file option if no text is given via STDIN"
56
+ end
57
+
58
+ ARGV.each{|f| PrependFile.prepend(f, prepend_content)}
@@ -0,0 +1,41 @@
1
+ require 'fileutils'
2
+ require 'tempfile'
3
+
4
+ module PrependFile
5
+ class << self
6
+ def prepend(old_file, data)
7
+ temp_file = Tempfile.open('prependfile')
8
+ begin
9
+ #prepend data
10
+ write_to_temp(temp_file, data)
11
+ #write old file to temp
12
+ write_to_temp(temp_file, old_file)
13
+ #move temp file to original location
14
+ move_temp_to_original(temp_file, old_file)
15
+ ensure
16
+ temp_file.close
17
+ temp_file.unlink
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def move_temp_to_original(temp, original)
24
+ original = original.path unless original.is_a?(String)
25
+ FileUtils.mv(temp.path, original)
26
+ end
27
+
28
+ def write_to_temp(temp_file, data)
29
+ if data.is_a?(IO) || data.is_a?(File)
30
+ temp_file.write data.read
31
+ elsif data.is_a?(String)
32
+ if File.file?(data)
33
+ temp_file.write File.read(data)
34
+ else
35
+ temp_file.puts data.to_s
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,3 @@
1
+ module PrependFile
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "prependfile/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "prependfile"
7
+ s.version = PrependFile::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Mike Lewis"]
10
+ s.email = ["ft.mikelewis@gmail.com"]
11
+ s.homepage = "http://github.com/mikelewis/prependfile"
12
+ s.summary = %q{Prepend any file with text (think licenses, copyright, ownership etc)}
13
+ s.description = %q{Prepend any file with text (think licenses, copyright, ownership etc)}
14
+
15
+ s.rubyforge_project = "prependfile"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_runtime_dependency('trollop')
23
+
24
+ s.add_development_dependency('rspec')
25
+
26
+ end
@@ -0,0 +1,82 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "prependfile binary" do
4
+ before do
5
+ @sample_files_with_content = {
6
+ "sample_files/sample.txt" => File.read("sample_files/sample.txt"),
7
+ "sample_files/other_sample.txt" => File.read("sample_files/other_sample.txt")
8
+ }
9
+ end
10
+
11
+ after do
12
+ @sample_files_with_content.each do |path, contents|
13
+ File.open(path, 'w'){|f| f.write contents}
14
+ end
15
+ end
16
+
17
+ it "should accept --text argument" do
18
+ file_path = "sample_files/sample.txt"
19
+ run_bin "#{abs_sample_file_path("sample.txt").join} --text='hi guys'"
20
+ File.read(file_path).should eq("hi guys\n#{@sample_files_with_content[file_path]}")
21
+ end
22
+
23
+ it "should accept -t argument" do
24
+ file_path = "sample_files/sample.txt"
25
+ run_bin "#{abs_sample_file_path("sample.txt").join} -t 'hi guys'"
26
+ File.read(file_path).should eq("hi guys\n#{@sample_files_with_content[file_path]}")
27
+
28
+ end
29
+
30
+ it "should accept --file argument" do
31
+ file_path = "sample_files/sample.txt"
32
+ run_bin "#{abs_sample_file_path("sample.txt").join} --file=#{abs_sample_file_path("prepend_data").join}"
33
+ File.read(file_path).should eq("#{File.read('sample_files/prepend_data')}#{@sample_files_with_content[file_path]}")
34
+ end
35
+
36
+ it "should accept -f argument" do
37
+ file_path = "sample_files/sample.txt"
38
+ run_bin "#{abs_sample_file_path("sample.txt").join} -f #{abs_sample_file_path("prepend_data").join}"
39
+ File.read(file_path).should eq("#{File.read('sample_files/prepend_data')}#{@sample_files_with_content[file_path]}")
40
+
41
+ end
42
+
43
+ it "should accept STDIN" do
44
+ file_path = "sample_files/sample.txt"
45
+ run_bin "#{abs_sample_file_path("sample.txt").join} < #{abs_sample_file_path("prepend_data").join}"
46
+ File.read(file_path).should eq("#{File.read('sample_files/prepend_data')}#{@sample_files_with_content[file_path]}")
47
+ end
48
+
49
+ it "should accept multiple files as input" do
50
+ run_bin "#{abs_sample_file_path("sample.txt","other_sample.txt").join(" ")} --text='hi guys'"
51
+ @sample_files_with_content.each do |path, content|
52
+ File.read(path).should eq("hi guys\n#{content}")
53
+ end
54
+ end
55
+
56
+ it "should not accept zero files" do
57
+ run_bin(" 2> /dev/null")
58
+ $?.success?.should eq(false)
59
+ end
60
+
61
+ it "should not accept zero files and give a stderror message" do
62
+ run_bin(" 2>&1").should match(/need to give a file to prepend to/)
63
+ end
64
+
65
+ it "should not accept a none existant --file" do
66
+ run_bin("--file=blahblah 2> /dev/null")
67
+ $?.success?.should eq(false)
68
+ end
69
+
70
+ it "should not accept a none existant --file and give a stderror message" do
71
+ run_bin("--file=blahblah 2>&1").should match(/Error: need to give a file to prepend to/)
72
+ end
73
+
74
+ it "should not accept a directory for --file" do
75
+ run_bin("--file=/dev 2> /dev/null")
76
+ $?.success?.should eq(false)
77
+ end
78
+
79
+ it "should not accept a none existant --file and give a stderror message" do
80
+ run_bin("--file=/dev 2>&1").should match(/Error: need to give a file to prepend to/)
81
+ end
82
+ end
@@ -0,0 +1,115 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe PrependFile do
4
+ context "Dest File is a location" do
5
+ before do
6
+ @file_name = "sample_files/sample.txt"
7
+ @original_contents = File.read(@file_name)
8
+ end
9
+
10
+ after do
11
+ File.open(@file_name, 'w') {|f| f.write @original_contents}
12
+ end
13
+
14
+ it "should prepend text to a file given a file name" do
15
+ PrependFile.prepend(@file_name, "This is text")
16
+ File.read(@file_name).should eq("This is text\n#{@original_contents}")
17
+ end
18
+
19
+ it "should prepend a file from a given file location to a file given a file name" do
20
+ PrependFile.prepend(@file_name, "sample_files/prepend_data" )
21
+ File.read(@file_name).should eq("#{File.read('sample_files/prepend_data')}#{@original_contents}")
22
+ end
23
+
24
+ it "should prepend a file from a given file object to a file given a file name" do
25
+ PrependFile.prepend(@file_name, File.new("sample_files/prepend_data"))
26
+ File.read(@file_name).should eq("#{File.read('sample_files/prepend_data')}#{@original_contents}")
27
+ end
28
+
29
+ it "should prepend a file from a given IO object to a file given a file name" do
30
+ PrependFile.prepend(@file_name, File.open("sample_files/prepend_data", 'r'))
31
+ File.read(@file_name).should eq("#{File.read('sample_files/prepend_data')}#{@original_contents}")
32
+ end
33
+ end
34
+
35
+ context "Dest File is a File Object" do
36
+ before do
37
+ @file_name = "sample_files/sample.txt"
38
+ @file = File.new @file_name
39
+ @original_contents = @file.read
40
+ @file.rewind
41
+ end
42
+
43
+ after do
44
+ File.open(@file_name, 'w') {|f| f.write @original_contents}
45
+ end
46
+
47
+ it "should reach the end of the given source file" do
48
+ PrependFile.prepend(@file, "This is text")
49
+ @file.eof?.should eq(true)
50
+ end
51
+
52
+ it "should prepend text to a file object" do
53
+ PrependFile.prepend(@file, "This is text")
54
+ File.read(@file_name).should eq("This is text\n#{@original_contents}")
55
+ end
56
+
57
+ it "should prepend a file from a given file location to a file given a file name" do
58
+ PrependFile.prepend(@file, "sample_files/prepend_data")
59
+ File.read(@file_name).should eq("#{File.read('sample_files/prepend_data')}#{@original_contents}")
60
+ end
61
+
62
+ it "should prepend a file from a given file object to a file given a file name" do
63
+ PrependFile.prepend(@file, File.new("sample_files/prepend_data"))
64
+ File.read(@file_name).should eq("#{File.read('sample_files/prepend_data')}#{@original_contents}")
65
+ end
66
+
67
+ it "should prepend a file from a given IO object to a file given a file name" do
68
+ PrependFile.prepend(@file, File.open("sample_files/prepend_data", 'r'))
69
+ File.read(@file_name).should eq("#{File.read('sample_files/prepend_data')}#{@original_contents}")
70
+ end
71
+
72
+ end
73
+
74
+
75
+ context "Dest File is a IO Object" do
76
+ before do
77
+ @file_name = "sample_files/sample.txt"
78
+ @file = File.open @file_name
79
+ @original_contents = @file.read
80
+ @file.rewind
81
+ end
82
+
83
+ after do
84
+ File.open(@file_name, 'w') {|f| f.write @original_contents}
85
+ end
86
+
87
+ it "should reach the end of the given source file" do
88
+ PrependFile.prepend(@file, "This is text")
89
+ @file.eof?.should eq(true)
90
+ end
91
+
92
+ it "should prepend text to a file object" do
93
+ PrependFile.prepend(@file, "This is text")
94
+ File.read(@file_name).should eq("This is text\n#{@original_contents}")
95
+ end
96
+
97
+ it "should prepend a file from a given file location to a file given a file name" do
98
+ PrependFile.prepend(@file, "sample_files/prepend_data")
99
+ File.read(@file_name).should eq("#{File.read('sample_files/prepend_data')}#{@original_contents}")
100
+ end
101
+
102
+ it "should prepend a file from a given file object to a file given a file name" do
103
+ PrependFile.prepend(@file, File.new("sample_files/prepend_data"))
104
+ File.read(@file_name).should eq("#{File.read('sample_files/prepend_data')}#{@original_contents}")
105
+ end
106
+
107
+ it "should prepend a file from a given IO object to a file given a file name" do
108
+ PrependFile.prepend(@file, File.open("sample_files/prepend_data", 'r'))
109
+ File.read(@file_name).should eq("#{File.read('sample_files/prepend_data')}#{@original_contents}")
110
+ end
111
+
112
+
113
+ end
114
+
115
+ end
@@ -0,0 +1,3 @@
1
+ This is a test document
2
+
3
+ Please handle WITH LOTS OF LOVE AND CARE :)
@@ -0,0 +1,5 @@
1
+ THIS SHOULD GO ON TOP
2
+
3
+ Yea
4
+
5
+ Ok
@@ -0,0 +1,5 @@
1
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam ac congue orci. Donec consequat pellentesque nibh, at ullamcorper ligula feugiat id. Sed urna lorem, iaculis at lacinia ac, ultrices nec lacus. Suspendisse dapibus accumsan orci id gravida. Nulla ligula sem, porttitor quis tincidunt ut, commodo sed dui. Nam ac dolor id mauris fermentum tincidunt. Nullam tortor lacus, interdum a adipiscing a, volutpat vel diam. Vestibulum dapibus dignissim eros in sodales. Mauris scelerisque dignissim erat quis fringilla. Suspendisse aliquet lorem mauris. In hac habitasse platea dictumst. Phasellus sit amet ipsum augue, id mollis eros. Vivamus et augue sem, non ultricies elit. Suspendisse potenti. Suspendisse elementum felis ac diam imperdiet gravida.
2
+
3
+ Sed euismod, turpis sed tristique scelerisque, justo nisl vestibulum lorem, id ultricies velit ante vitae neque. Phasellus ut mi nulla. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aenean scelerisque lacinia vulputate. Nulla facilisi. Maecenas facilisis varius elementum. In hac habitasse platea dictumst. Donec egestas eros sit amet eros pulvinar volutpat. Fusce eget lorem id magna varius iaculis. In hac habitasse platea dictumst. Phasellus rutrum posuere dolor nec aliquam. Maecenas lobortis risus ut nunc ultrices volutpat. Praesent neque orci, fringilla sagittis molestie eget, hendrerit sed nisl.
4
+
5
+
@@ -0,0 +1,10 @@
1
+ f = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__
2
+ require File.join(File.dirname(f), '..', 'lib', 'prependfile')
3
+
4
+ def run_bin(args)
5
+ `../bin/prependfile #{args}`
6
+ end
7
+
8
+ def abs_sample_file_path(*files)
9
+ files.map{|f| "../spec/sample_files/#{f}"}
10
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prependfile
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - Mike Lewis
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-17 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: trollop
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rspec
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id002
48
+ description: Prepend any file with text (think licenses, copyright, ownership etc)
49
+ email:
50
+ - ft.mikelewis@gmail.com
51
+ executables:
52
+ - prependfile
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - .gitignore
59
+ - Gemfile
60
+ - README.md
61
+ - Rakefile
62
+ - bin/prependfile
63
+ - lib/prependfile.rb
64
+ - lib/prependfile/version.rb
65
+ - prependfile.gemspec
66
+ - spec/bin_spec.rb
67
+ - spec/prependfile_spec.rb
68
+ - spec/sample_files/other_sample.txt
69
+ - spec/sample_files/prepend_data
70
+ - spec/sample_files/sample.txt
71
+ - spec/spec_helper.rb
72
+ homepage: http://github.com/mikelewis/prependfile
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options: []
77
+
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ hash: 3
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ requirements: []
99
+
100
+ rubyforge_project: prependfile
101
+ rubygems_version: 1.7.1
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Prepend any file with text (think licenses, copyright, ownership etc)
105
+ test_files:
106
+ - spec/bin_spec.rb
107
+ - spec/prependfile_spec.rb
108
+ - spec/sample_files/other_sample.txt
109
+ - spec/sample_files/prepend_data
110
+ - spec/sample_files/sample.txt
111
+ - spec/spec_helper.rb