insert_after 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/README.md +114 -0
- data/Rakefile +40 -0
- data/exe/insert_after +5 -0
- data/insert_after.gemspec +37 -0
- data/lib/insert_after/version.rb +3 -0
- data/lib/insert_after.rb +67 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f7b32c8d75b05bb77620742256241e80ffa08ae9dd2aab62c77ff89162958aa3
|
4
|
+
data.tar.gz: a005eeca4d06274644788acc9ef772ab17fcde223ab177a4c73f4f8c2885a0fc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 22c3845706c9672bc8e1786610de92337c20a03ec9ae9597c10b10f0abf12ce1ff087f45bfc176136453c2c1c8c9b3799d893879ee48900c7bd8b332aa83c0c1
|
7
|
+
data.tar.gz: 0bb1f80568534d1bb4715a5838e87f0abc8caaaaadd31fb2a174d881ea7c400386f96000151ee85202e040e08cac3f79322e3a118bf3d9863e4f1dff0b7db66f
|
data/CHANGELOG.md
ADDED
data/README.md
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
# `Insert_after` [![Gem Version](https://badge.fury.io/rb/insert_after.svg)](https://badge.fury.io/rb/insert_after)
|
2
|
+
|
3
|
+
Inserts a line into a file after a line matching a regular expression.
|
4
|
+
String comparisons are case-insensitive.
|
5
|
+
Works on very large files because it reads the file line by line instead of reading the entire file into memory.
|
6
|
+
|
7
|
+
Can be used on the command line or in a Ruby program.
|
8
|
+
|
9
|
+
|
10
|
+
## Command-line Installation
|
11
|
+
|
12
|
+
Either add this line to your application’s `Gemfile`:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'insert_after'
|
16
|
+
```
|
17
|
+
|
18
|
+
## Installation For Use In a Ruby Program
|
19
|
+
|
20
|
+
Add the following to your application’s `.gemspec`:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
spec.add_dependency 'insert_after'
|
24
|
+
```
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
```shell
|
29
|
+
$ bundle
|
30
|
+
```
|
31
|
+
|
32
|
+
|
33
|
+
## Command-line Usage
|
34
|
+
|
35
|
+
```shell
|
36
|
+
# Inserts 'Inserted 1' after the first line containing 'line' into demo/my_file.txt:
|
37
|
+
$ insert_after line 'Inserted 1' demo/my_file.txt
|
38
|
+
|
39
|
+
# Inserts an empty line after the first line containing 'line 1' into demo/my_file.txt:
|
40
|
+
$ insert_after 'line 1' '' demo/my_file.txt
|
41
|
+
|
42
|
+
# Inserts 'Inserted 2' after the first line starting with 'line 2' into demo/my_file.txt:
|
43
|
+
$ insert_after '^line 2' 'Inserted 2' demo/my_file.txt
|
44
|
+
|
45
|
+
# Inserts 'Inserted 3' after the first line containing an 'e' followed by a '2' into demo/my_file.txt:
|
46
|
+
$ insert_after 'e.*2' 'Inserted 3' demo/my_file.txt
|
47
|
+
```
|
48
|
+
|
49
|
+
|
50
|
+
## Ruby Program Usage
|
51
|
+
|
52
|
+
The `demo/demo.rb` program is an example of how to use `insert_after` in a Ruby program:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
require 'insert_after'
|
56
|
+
|
57
|
+
insert_after 'line 2', 'New line', 'my_file.txt'
|
58
|
+
```
|
59
|
+
|
60
|
+
|
61
|
+
## Development
|
62
|
+
|
63
|
+
After checking out this git repository, install dependencies by typing:
|
64
|
+
|
65
|
+
```shell
|
66
|
+
$ bin/setup
|
67
|
+
```
|
68
|
+
|
69
|
+
You should do the above before running Visual Studio Code.
|
70
|
+
|
71
|
+
|
72
|
+
### Run the Tests
|
73
|
+
|
74
|
+
```shell
|
75
|
+
$ bundle exec rspec
|
76
|
+
```
|
77
|
+
|
78
|
+
|
79
|
+
### Interactive Session
|
80
|
+
|
81
|
+
The following will allow you to experiment:
|
82
|
+
|
83
|
+
```shell
|
84
|
+
$ bin/console
|
85
|
+
```
|
86
|
+
|
87
|
+
|
88
|
+
### Local Installation
|
89
|
+
|
90
|
+
To install this gem onto your local machine, type:
|
91
|
+
|
92
|
+
```shell
|
93
|
+
$ bundle exec rake install
|
94
|
+
```
|
95
|
+
|
96
|
+
|
97
|
+
### To Release A New Version
|
98
|
+
|
99
|
+
To create a git tag for the new version, push git commits and tags,
|
100
|
+
and push the new version of the gem to https://rubygems.org, type:
|
101
|
+
|
102
|
+
```shell
|
103
|
+
$ bundle exec rake release
|
104
|
+
```
|
105
|
+
|
106
|
+
|
107
|
+
## Contributing
|
108
|
+
|
109
|
+
Bug reports and pull requests are welcome at https://github.com/mslinn/insert_after.
|
110
|
+
|
111
|
+
|
112
|
+
## License
|
113
|
+
|
114
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << 'test'
|
6
|
+
t.libs << 'lib'
|
7
|
+
t.test_files = FileList['test/**/*_test.rb']
|
8
|
+
end
|
9
|
+
|
10
|
+
desc 'Bump patch version'
|
11
|
+
task :patch do
|
12
|
+
system 'gem bump --tag'
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Bump minor version'
|
16
|
+
task :minor do
|
17
|
+
system 'gem bump --version minor --tag'
|
18
|
+
end
|
19
|
+
|
20
|
+
desc 'Bump major version'
|
21
|
+
task :major do
|
22
|
+
system 'gem bump --version major --tag'
|
23
|
+
end
|
24
|
+
|
25
|
+
task publish: [:build] do
|
26
|
+
$VERBOSE = nil
|
27
|
+
load 'insert_after/version.rb'
|
28
|
+
system "gem push pkg/insert_after-#{InsertAfter::VERSION}.gem"
|
29
|
+
end
|
30
|
+
|
31
|
+
desc 'Bump patch version, create git tag, build the gem and release to geminabox (default)'
|
32
|
+
task release_patch: %i[test patch publish]
|
33
|
+
|
34
|
+
desc 'Bump minor version, create git tag, build the gem and release to geminabox'
|
35
|
+
task release_minor: %i[test minor publish]
|
36
|
+
|
37
|
+
desc 'Bump major version, create git tag, build the gem and release to geminabox'
|
38
|
+
task release_major: %i[test major publish]
|
39
|
+
|
40
|
+
task default: :test
|
data/exe/insert_after
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require_relative 'lib/insert_after/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
host = 'https://github.com/mslinn/insert_after'
|
5
|
+
|
6
|
+
spec.authors = ['Mike Slinn']
|
7
|
+
spec.bindir = 'exe'
|
8
|
+
spec.description = <<~END_DESC
|
9
|
+
Inserts a line into a file after a line matching a regular expression.
|
10
|
+
String comparisons are case-insensitive.
|
11
|
+
Works on very large files because it reads the file line by line instead of reading the entire file into memory.
|
12
|
+
|
13
|
+
Can be used on the command line or in a Ruby program.
|
14
|
+
END_DESC
|
15
|
+
spec.email = ['mslinn@mslinn.com']
|
16
|
+
spec.executables = ['insert_after']
|
17
|
+
spec.files = Dir['LICENSE.*', 'Rakefile', '{exe,lib}/**/*', '*.gemspec', '*.md']
|
18
|
+
spec.homepage = 'https://github.com/mslinn/insert_after'
|
19
|
+
spec.license = 'MIT'
|
20
|
+
spec.metadata = {
|
21
|
+
'allowed_push_host' => 'https://rubygems.org',
|
22
|
+
'bug_tracker_uri' => "#{host}/issues",
|
23
|
+
'changelog_uri' => "#{host}/CHANGELOG.md",
|
24
|
+
'homepage_uri' => spec.homepage,
|
25
|
+
'source_code_uri' => host,
|
26
|
+
}
|
27
|
+
spec.name = 'insert_after'
|
28
|
+
spec.post_install_message = <<~END_MESSAGE
|
29
|
+
|
30
|
+
Thanks for installing #{spec.name}!
|
31
|
+
|
32
|
+
END_MESSAGE
|
33
|
+
spec.require_paths = ['lib']
|
34
|
+
spec.required_ruby_version = '>= 2.6.0'
|
35
|
+
spec.summary = 'Inserts a line into a file after a line matching a regular expression.'
|
36
|
+
spec.version = InsertAfter::VERSION
|
37
|
+
end
|
data/lib/insert_after.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'tempfile'
|
3
|
+
require_relative 'insert_after/version'
|
4
|
+
|
5
|
+
module InsertAfter
|
6
|
+
def self.help(msg = nil)
|
7
|
+
puts "Error: #{msg}\n\n" if msg
|
8
|
+
puts <<~END_MSG
|
9
|
+
Inserts a line into a file after a line matching a regular expression.
|
10
|
+
String comparisons are case-insensitive.
|
11
|
+
Works on very large files because it reads the file line by line instead of reading the entire file into memory.
|
12
|
+
|
13
|
+
Can be used on the command line or in a Ruby program.
|
14
|
+
|
15
|
+
Command line usage:
|
16
|
+
# Inserts 'Inserted 1' after the first line containing 'line' into demo/my_file.txt:
|
17
|
+
$ insert_after line 'Inserted 1' demo/my_file.txt
|
18
|
+
|
19
|
+
# Inserts an empty line after the first line containing 'line 1' into demo/my_file.txt:
|
20
|
+
$ insert_after 'line 1' '' demo/my_file.txt
|
21
|
+
|
22
|
+
# Inserts 'Inserted 2' after the first line starting with 'line 2' into demo/my_file.txt:
|
23
|
+
$ insert_after '^line 2' 'Inserted 2' demo/my_file.txt
|
24
|
+
|
25
|
+
# Inserts 'Inserted 3' after the first line containing an 'e' followed by a '2' into demo/my_file.txt:
|
26
|
+
$ insert_after 'e.*2' 'Inserted 3' demo/my_file.txt
|
27
|
+
|
28
|
+
Ruby usage:
|
29
|
+
require 'insert_after'
|
30
|
+
InsertAfter.insert_after 'line 2' 'New line' 'demo/my_file.txt'
|
31
|
+
END_MSG
|
32
|
+
exit 1
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.insert_after(regex, new_line, filename)
|
36
|
+
InsertAfter.help "#{filename} does not exist" unless File.exist? filename
|
37
|
+
|
38
|
+
original_file = File.new(filename)
|
39
|
+
temporary_file = Tempfile.new(filename)
|
40
|
+
begin
|
41
|
+
original_file.each do |line|
|
42
|
+
temporary_file << line
|
43
|
+
temporary_file << "#{new_line}\n" if line.downcase.match?(/#{regex}/)
|
44
|
+
end
|
45
|
+
original_file.close
|
46
|
+
temporary_file.close
|
47
|
+
FileUtils.mv(temporary_file.path, filename)
|
48
|
+
ensure
|
49
|
+
original_file.close unless original_file.closed?
|
50
|
+
temporary_file.close unless temporary_file.closed?
|
51
|
+
temporary_file.unlink if File.exist? temporary_file.path
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.cmd(argv = ARGV)
|
56
|
+
InsertAfter.help if argv.empty?
|
57
|
+
InsertAfter.help 'The new line to insert was not provided' if argv.length == 1
|
58
|
+
InsertAfter.help 'No file name was provided' if argv.length == 2
|
59
|
+
InsertAfter.help 'Too many arguments' if argv.length > 3
|
60
|
+
regex = argv[0].downcase
|
61
|
+
new_line = argv[1]
|
62
|
+
filename = argv[2]
|
63
|
+
InsertAfter.insert_after(regex, new_line, filename)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
InsertAfter.cmd(ARGV) if __FILE__ == $PROGRAM_NAME
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: insert_after
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Slinn
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-11-10 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |
|
14
|
+
Inserts a line into a file after a line matching a regular expression.
|
15
|
+
String comparisons are case-insensitive.
|
16
|
+
Works on very large files because it reads the file line by line instead of reading the entire file into memory.
|
17
|
+
|
18
|
+
Can be used on the command line or in a Ruby program.
|
19
|
+
email:
|
20
|
+
- mslinn@mslinn.com
|
21
|
+
executables:
|
22
|
+
- insert_after
|
23
|
+
extensions: []
|
24
|
+
extra_rdoc_files: []
|
25
|
+
files:
|
26
|
+
- CHANGELOG.md
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- exe/insert_after
|
30
|
+
- insert_after.gemspec
|
31
|
+
- lib/insert_after.rb
|
32
|
+
- lib/insert_after/version.rb
|
33
|
+
homepage: https://github.com/mslinn/insert_after
|
34
|
+
licenses:
|
35
|
+
- MIT
|
36
|
+
metadata:
|
37
|
+
allowed_push_host: https://rubygems.org
|
38
|
+
bug_tracker_uri: https://github.com/mslinn/insert_after/issues
|
39
|
+
changelog_uri: https://github.com/mslinn/insert_after/CHANGELOG.md
|
40
|
+
homepage_uri: https://github.com/mslinn/insert_after
|
41
|
+
source_code_uri: https://github.com/mslinn/insert_after
|
42
|
+
post_install_message: |2+
|
43
|
+
|
44
|
+
Thanks for installing insert_after!
|
45
|
+
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.6.0
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubygems_version: 3.3.7
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: Inserts a line into a file after a line matching a regular expression.
|
64
|
+
test_files: []
|
65
|
+
...
|