example_file 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 127253a74db21db286783ea5a8eee2929f7dd85ab11a6d2fe2aadac42aabd56b
4
+ data.tar.gz: 1bedfb4962cf19fc156d97697a9924980864c56b5556ffff39feb945cc0ffa4e
5
+ SHA512:
6
+ metadata.gz: e5d2cd033e13c51a2905a7d12f9e530492f081c2edbd674463837965c1c5523751f72c27539797e1b4511762372d07ef959f9f4f2485d7fa923005be791321c1
7
+ data.tar.gz: 928d302a75284fcc145751762a2f1e56b35f68a992a3ae47a6abfb0955657a4c4133e3a6831f97281fb655e95a825e382b090019ae1336051ac1c1e4e75ef1c8
@@ -0,0 +1,35 @@
1
+ bundle_cache: &bundle_cache
2
+ bundle_cache:
3
+ folder: /usr/local/bundle
4
+ fingerprint_script:
5
+ - echo $CIRRUS_OS
6
+ - ruby -v
7
+ - cat Gemfile
8
+ - cat *.gemspec
9
+ install_script: bundle update
10
+
11
+ markdownlint_task:
12
+ container:
13
+ image: ruby:latest
14
+ <<: *bundle_cache
15
+ rubocop_script: bundle exec mdl .
16
+
17
+ rubocop_task:
18
+ container:
19
+ image: ruby:latest
20
+ <<: *bundle_cache
21
+ rubocop_script: bundle exec rubocop
22
+
23
+ test_task:
24
+ depends_on:
25
+ - rubocop
26
+ - markdownlint
27
+ container:
28
+ matrix:
29
+ image: ruby:2.5
30
+ image: ruby:2.6
31
+ image: ruby:2.7
32
+ <<: *bundle_cache
33
+ environment:
34
+ CODECOV_TOKEN: ENCRYPTED[de650f17db291399a09839a898ab3be8d0bb09a14b369f16f31a1831d6db8d826d4601ea7ad87eab99d005ad832846cc]
35
+ test_script: bundle exec rake
@@ -0,0 +1,18 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = tab
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
10
+ max_line_length = 100
11
+
12
+ [*.y{a,}ml]
13
+ indent_style = space
14
+ indent_size = 2
15
+
16
+ [*.md]
17
+ indent_style = space
18
+ indent_size = 4
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ /coverage/
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ all
4
+
5
+ rule 'MD007', indent: 4
6
+ rule 'MD024', allow_different_nesting: true
7
+ rule 'MD029', style: 'ordered'
8
+ rule 'MD030', ul_single: 3, ul_multi: 3, ol_single: 2, ol_multi: 2
data/.mdlrc ADDED
@@ -0,0 +1 @@
1
+ style ".mdl.style.rb"
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --warnings
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,58 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+
5
+ Layout/IndentationStyle:
6
+ EnforcedStyle: tabs
7
+ IndentationWidth: 2
8
+ Layout/IndentationWidth:
9
+ Width: 1
10
+ Layout/LineLength:
11
+ Max: 100
12
+ Layout/MultilineMethodCallIndentation:
13
+ EnforcedStyle: indented
14
+ Layout/ArgumentAlignment:
15
+ EnforcedStyle: with_fixed_indentation
16
+ Layout/EmptyLinesAroundAttributeAccessor:
17
+ Enabled: true
18
+ Layout/SpaceAroundMethodCallOperator:
19
+ Enabled: true
20
+
21
+ Lint/DeprecatedOpenSSLConstant:
22
+ Enabled: true
23
+ Lint/MixedRegexpCaptureTypes:
24
+ Enabled: true
25
+ Lint/RaiseException:
26
+ Enabled: true
27
+ Lint/StructNewOverride:
28
+ Enabled: true
29
+
30
+ Style/HashEachMethods:
31
+ Enabled: true
32
+ Style/HashTransformKeys:
33
+ Enabled: true
34
+ Style/HashTransformValues:
35
+ Enabled: true
36
+ Style/ExponentialNotation:
37
+ Enabled: true
38
+ Style/RedundantRegexpCharacterClass:
39
+ Enabled: true
40
+ Style/RedundantRegexpEscape:
41
+ Enabled: true
42
+ Style/SlicingWithRange:
43
+ Enabled: true
44
+
45
+ Metrics/BlockLength:
46
+ Exclude:
47
+ - '*.gemspec'
48
+ - spec/**/*.rb
49
+
50
+ AllCops:
51
+ TargetRubyVersion: 2.5
52
+
53
+ RSpec/NestedGroups:
54
+ Max: 6
55
+ RSpec/RepeatedExampleGroupBody:
56
+ Exclude:
57
+ ## Variables are generating from context description
58
+ - spec/example_file_spec.rb
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Alexander Popov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,96 @@
1
+ # ExampleFile
2
+
3
+ Class helper for example files. Usualy it's the alternative
4
+ to environment variables (and `.env` files).
5
+
6
+ You can have git-controlled example files and git-ignored real files.
7
+ For example, configuration, especially with sensitive data.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'example_file'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ ```shell
20
+ bundle install
21
+ ```
22
+
23
+ Or install it yourself as:
24
+
25
+ ```shell
26
+ gem install example_file
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ ```ruby
32
+ require 'example_file'
33
+
34
+ ExampleFile.new('some_file.example.conf').actualize_regular_file
35
+ ```
36
+
37
+ For `some_file.example.conf` example file there is `some_file.conf`
38
+ regular file.
39
+
40
+ Editor below is `$EDITOR` environment variable.
41
+ You should set it to your preferred editor, console or GUI
42
+ (console is more comfortable usually for such scenarios).
43
+
44
+ It'll act in these ways:
45
+
46
+ * If there is no regular file:
47
+ * create it with content from example and open the editor.
48
+ * If there is example file with file modified time after regular file:
49
+ * If there is no difference between their contents:
50
+ * touch regular file for updating its modified time.
51
+ * If there is a difference between their contents:
52
+ * ask for a further action:
53
+ * open the editor;
54
+ * just update regular without changes;
55
+ * or replace regular file with a content from new example file.
56
+ * If there is regular file with file modified time after example file:
57
+ * do nothing.
58
+
59
+ You should add example files to git control:
60
+
61
+ ```shell
62
+ git add some_file.example.conf
63
+ ```
64
+
65
+ And ignore regular ones in `.gitignore`:
66
+
67
+ ```gitignore
68
+ some_file.conf
69
+ ```
70
+
71
+ Recursively it'd be like:
72
+
73
+ ```gitignore
74
+ config/**/*
75
+ !config/**/*.example*
76
+ ```
77
+
78
+ ## Development
79
+
80
+ After checking out the repo, run `bundle install` to install dependencies.
81
+ Then, run `bundle exec rake spec` to run the tests.
82
+
83
+ To install this gem onto your local machine, run `bundle exec rake install`.
84
+ To release a new version, update the version number in `version.rb`,
85
+ and then run `bundle exec rake release`, which will create a git tag
86
+ for the version, push git commits and tags, and push the `.gem` file
87
+ to [rubygems.org](https://rubygems.org).
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/AlexWayfer/example_file).
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the
96
+ [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/example_file/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'example_file'
7
+ spec.version = ExampleFile::VERSION
8
+ spec.authors = ['Alexander Popov']
9
+ spec.email = ['alex.wayfer@gmail.com']
10
+
11
+ spec.summary = 'Class helper for example files'
12
+ spec.description = <<~DESC
13
+ Class helper for example files.
14
+ Usualy it's the alternative to environment variables (and `.env` files).
15
+ You can have git-controlled example files and git-ignored real files.
16
+ For example, configuration, especially with sensitive data.
17
+ DESC
18
+ spec.license = 'MIT'
19
+
20
+ spec.required_ruby_version = '>= 2.5'
21
+
22
+ source_code_uri = 'https://github.com/AlexWayfer/example_file'
23
+
24
+ spec.homepage = source_code_uri
25
+
26
+ spec.metadata['source_code_uri'] = source_code_uri
27
+
28
+ spec.metadata['homepage_uri'] = spec.homepage
29
+
30
+ # TODO: Put your gem's CHANGELOG.md URL here.
31
+ # spec.metadata["changelog_uri"] = ""
32
+
33
+ # Specify which files should be added to the gem when it is released.
34
+ # The `git ls-files -z` loads the files in the RubyGem that have been added
35
+ # into git.
36
+ spec.files = Dir.chdir(__dir__) do
37
+ `git ls-files -z`.split("\x0").reject do |file|
38
+ file.match(%r{^(test|spec|features)/})
39
+ end
40
+ end
41
+ spec.bindir = 'exe'
42
+ spec.executables = spec.files.grep(%r{^exe/}) { |file| File.basename(file) }
43
+ spec.require_paths = ['lib']
44
+
45
+ spec.add_runtime_dependency 'diffy', '~> 3.0'
46
+ spec.add_runtime_dependency 'highline', '~> 2.0'
47
+ spec.add_runtime_dependency 'paint', '~> 2.0'
48
+
49
+ spec.add_development_dependency 'codecov', '~> 0.1.0'
50
+ spec.add_development_dependency 'mdl', '~> 0.9.0'
51
+ spec.add_development_dependency 'pry-byebug', '~> 3.9'
52
+ spec.add_development_dependency 'rake', '~> 13.0'
53
+ spec.add_development_dependency 'rspec', '~> 3.9'
54
+ spec.add_development_dependency 'rubocop', '~> 0.85.0'
55
+ spec.add_development_dependency 'rubocop-performance', '~> 1.0'
56
+ spec.add_development_dependency 'rubocop-rspec', '~> 1.0'
57
+ spec.add_development_dependency 'simplecov', '~> 0.18.0'
58
+ end
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'example_file/version'
4
+
5
+ require 'diffy'
6
+ require 'highline'
7
+ require 'paint'
8
+
9
+ ## Class for example file
10
+ class ExampleFile
11
+ SUFFIX = '.example'
12
+ HIGHLINE = HighLine.new
13
+
14
+ class << self
15
+ def all(directory)
16
+ Dir[File.join(directory, '**', "*#{SUFFIX}*")]
17
+ .map { |file_name| new file_name }
18
+ end
19
+ end
20
+
21
+ def initialize(file_name)
22
+ @file_name = file_name
23
+ @regular_file_name = @file_name.sub SUFFIX, ''
24
+
25
+ @basename =
26
+ Paint[File.basename(@file_name), :green, :bold]
27
+ @regular_basename =
28
+ Paint[File.basename(@regular_file_name), :red, :bold]
29
+ end
30
+
31
+ def actualize_regular_file
32
+ return create_regular_file unless regular_file_exist?
33
+
34
+ return unless new?
35
+
36
+ return update_regular_file if diff.chomp.empty?
37
+
38
+ ask_question_and_make_actions
39
+ end
40
+
41
+ private
42
+
43
+ def new?
44
+ File.mtime(@file_name) > File.mtime(@regular_file_name)
45
+ end
46
+
47
+ def regular_file_exist?
48
+ File.exist? @regular_file_name
49
+ end
50
+
51
+ def create_regular_file
52
+ FileUtils.cp @file_name, @regular_file_name
53
+ edit_file @regular_file_name
54
+ end
55
+
56
+ def update_regular_file
57
+ FileUtils.touch @regular_file_name
58
+ end
59
+
60
+ def diff
61
+ @diff ||= Diffy::Diff
62
+ .new(@regular_file_name, @file_name, source: 'files', context: 3)
63
+ .to_s(:color)
64
+ end
65
+
66
+ CHOICES = {
67
+ yes: proc { edit_file @regular_file_name },
68
+ replace: proc { rewrite_regular_file },
69
+ no: proc do
70
+ update_regular_file
71
+ puts 'File modified time updated'
72
+ end
73
+ }.freeze
74
+
75
+ private_constant :CHOICES
76
+
77
+ def ask_question_and_make_actions
78
+ puts warning_with_diff
79
+
80
+ HIGHLINE.choose do |menu|
81
+ menu.layout = :one_line
82
+
83
+ menu.prompt = "Do you want to edit #{@regular_basename} ? "
84
+
85
+ CHOICES.each do |answer, block|
86
+ menu.choice(answer) { instance_exec(&block) }
87
+ end
88
+ end
89
+ end
90
+
91
+ def warning_with_diff
92
+ <<~WARN
93
+ #{@basename} was modified after #{@regular_basename}.
94
+
95
+ ```diff
96
+ #{diff}
97
+ ```
98
+
99
+ WARN
100
+ end
101
+
102
+ def edit_file(filename)
103
+ system "eval $EDITOR #{filename}"
104
+ end
105
+
106
+ def rewrite_regular_file
107
+ File.write @regular_file_name, File.read(@file_name)
108
+ end
109
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ExampleFile
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,231 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: example_file
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Popov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-06-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: diffy
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: highline
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: paint
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: codecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.1.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.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: mdl
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.9.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.9.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.9'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.9'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '13.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '13.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.9'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.9'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.85.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.85.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop-performance
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '1.0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '1.0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop-rspec
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '1.0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '1.0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: simplecov
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 0.18.0
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: 0.18.0
181
+ description: |
182
+ Class helper for example files.
183
+ Usualy it's the alternative to environment variables (and `.env` files).
184
+ You can have git-controlled example files and git-ignored real files.
185
+ For example, configuration, especially with sensitive data.
186
+ email:
187
+ - alex.wayfer@gmail.com
188
+ executables: []
189
+ extensions: []
190
+ extra_rdoc_files: []
191
+ files:
192
+ - ".cirrus.yml"
193
+ - ".editorconfig"
194
+ - ".gitignore"
195
+ - ".mdl.style.rb"
196
+ - ".mdlrc"
197
+ - ".rspec"
198
+ - ".rubocop.yml"
199
+ - Gemfile
200
+ - LICENSE.txt
201
+ - README.md
202
+ - Rakefile
203
+ - example_file.gemspec
204
+ - lib/example_file.rb
205
+ - lib/example_file/version.rb
206
+ homepage: https://github.com/AlexWayfer/example_file
207
+ licenses:
208
+ - MIT
209
+ metadata:
210
+ source_code_uri: https://github.com/AlexWayfer/example_file
211
+ homepage_uri: https://github.com/AlexWayfer/example_file
212
+ post_install_message:
213
+ rdoc_options: []
214
+ require_paths:
215
+ - lib
216
+ required_ruby_version: !ruby/object:Gem::Requirement
217
+ requirements:
218
+ - - ">="
219
+ - !ruby/object:Gem::Version
220
+ version: '2.5'
221
+ required_rubygems_version: !ruby/object:Gem::Requirement
222
+ requirements:
223
+ - - ">="
224
+ - !ruby/object:Gem::Version
225
+ version: '0'
226
+ requirements: []
227
+ rubygems_version: 3.1.2
228
+ signing_key:
229
+ specification_version: 4
230
+ summary: Class helper for example files
231
+ test_files: []