reenrb 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2253d4b2aa304bfac8b7e4659fee9ca3b514a22d4a0bb323be9619883b9448c0
4
- data.tar.gz: 3f82b6afbbe84b70878d153b56f6f75a72c2570df3b2b292c0d42a4a0057e973
3
+ metadata.gz: 8f607120ef687c5ace8a02b12e3fb6306abc16aa45b6f906f3e853f995df9a51
4
+ data.tar.gz: 0d4b19262e64584508daf56c0ec2024603ff1a375d134fc000774d1e9233714e
5
5
  SHA512:
6
- metadata.gz: 331ad4055d5abc194ec73fb0be6ac7479d3d2d8c41fb37980edc9c772f998a7b469da386e6b8e9c9dfe594802f9f1e39daa0081f0e109a1278fa41b1fb666a24
7
- data.tar.gz: 39d3af74b266effdb9ad82c513412539edd75b2573e97a36f41850027822c4a22029fa0b1fe6b298de177f9d794c86e80c88349dcb05b2b19541c998a572e2c2
6
+ metadata.gz: 420f2980c0e6a22ddeb7dbfc9a6abc62a9c8845f19f0902ba975c13996e87c371da9dd003b1d70fbdca9a14ebfeae36e85bd883671bb8b9eec27aa622a1e4fe1
7
+ data.tar.gz: 971cade35548d6fbde58a07ebd3707f34e95da5933e37c37d4eef1f8f108a1714b789aa873ccbcd2331a0214c330bd93a83c4b4f52f295f04130802efff0a726
data/README.md CHANGED
@@ -14,26 +14,29 @@ Or add this line to your Ruby application's Gemfile for programmatic use:
14
14
  gem 'reenrb'
15
15
  ```
16
16
 
17
- And then execute:
18
-
19
- $ bundle install
20
-
21
- Or install it yourself as:
22
-
23
- $ gem install reenrb
24
-
25
17
  ## Usage
26
18
 
27
19
  ### Command line
28
20
 
29
21
  From command line, run `reen` with file list:
30
22
 
31
- reen [file ...]
23
+ reen files [options]
24
+
25
+ where `files` are a list of files or wildcard pattern (defaults to `*`; see examples)
26
+
27
+ Options include:
28
+
29
+ - `--help` or `-h`' to see options help
30
+ - `--editor EDITOR` or `-e EDITOR`' to set the editor (defaults to $VISUAL or $EDITOR otherwise) such as `emacs` or `vi`. For Visual Studio Code use `'code -w'` to block until editor finishes.
31
+ - `--review` or `-r` request to review and confirm changes
32
32
 
33
33
  Examples:
34
34
 
35
- reen *
36
- reen myfolder/**/*.mov
35
+ reen # reen all files (*)
36
+ reen **/* # reen all files in all subfolders
37
+ reen myfolder/**/*.mov # reen all mov files in subfolders
38
+ reen *.md --editor vi # reen all markdown files using vi
39
+ reen --editor 'code -w' # reen all markdown files using vscode
37
40
 
38
41
  ### Ruby application
39
42
 
@@ -45,13 +48,10 @@ require 'reenrb'
45
48
  glob = Dir.glob("*")
46
49
  reen = Reenrb::Reen.new(editor: nil)
47
50
 
48
- reen.execute(glob) do |tmpfile_path|
49
- lines = File.read(tmpfile_path).split("\n")
50
-
51
+ reen.execute(glob) do |file|
51
52
  # Rename LICENSE.txt -> LICENSE.md
52
- index = lines.index { |l| l.include? "LICENSE.txt" }
53
- lines[index] = lines[index].gsub("txt", "md")
54
- File.write(tmpfile_path, lines.join("\n"))
53
+ index = file.list.index { |l| l.include? "LICENSE.txt" }
54
+ file.list[index] = file.list[index].gsub("txt", "md")
55
55
  end
56
56
  ```
57
57
 
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require "bundler/gem_tasks"
4
4
  require "rake/testtask"
5
5
 
6
6
  Rake::TestTask.new(:spec) do |t|
7
- t.libs << "tspecest"
7
+ t.libs << "spec"
8
8
  t.libs << "lib"
9
9
  t.test_files = FileList["spec/**/spec_*.rb"]
10
10
  end
@@ -14,18 +14,18 @@ task :respec do
14
14
  end
15
15
 
16
16
  namespace :example do
17
- task :config do
17
+ task :helper do
18
18
  require_relative "spec/fixture_helper"
19
19
  end
20
20
 
21
21
  desc "Recreates the example fixture folder"
22
- task :recreate => :config do
22
+ task :recreate => :helper do
23
23
  FixtureHelper.recreate_example_dir
24
24
  puts "Example fixture recreated"
25
25
  end
26
26
 
27
27
  desc "Deletes the example fixture folder"
28
- task :remove => :config do
28
+ task :remove => :helper do
29
29
  FixtureHelper.remove_example_dirs
30
30
  puts "Example fixture removed"
31
31
  end
data/bin/reen CHANGED
@@ -1,22 +1,87 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- EDITOR_MSG = "Editor not set in $EDITOR or $VISUAL -- please set one of those environment variables"
5
-
6
- def exit_with_error(err)
7
- puts err
8
- exit(false)
9
- end
10
-
11
4
  $LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
12
5
  require "reenrb"
6
+ require "optparse"
7
+
8
+ # Reen command line application
9
+ class ReenCLI
10
+ EDITOR_MSG = "Editor not set in $EDITOR or $VISUAL -- please set one of those environment variables"
11
+ Options = Struct.new(:editor, :review) do
12
+ def initialize(editor: nil, review: false)
13
+ editor ||= ENV["VISUAL"] || ENV["EDITOR"] # rubocop:disable Style/FetchEnvVar
14
+ super(editor, review)
15
+ end
16
+ end
17
+
18
+ attr_reader :options, :files
19
+
20
+ def initialize(args)
21
+ @options = Options.new
22
+ optparse = OptionParser.new { |parser| setup_options(parser) }
23
+ optparse.parse!(args, into: @options)
24
+
25
+ exit_with_msg(EDITOR_MSG) unless options.editor
26
+
27
+ @files = args.empty? ? Dir.glob("*") : args
28
+ rescue OptionParser::InvalidOption => e
29
+ puts "#{e.message}\n\n"
30
+ exit_with_msg(optparse)
31
+ end
32
+
33
+ def setup_options(parser)
34
+ parser.banner = "Usage: reen files [options]"
35
+ parser.version = Reenrb::VERSION
13
36
 
14
- file_list = ARGV || Dir.glob("*")
15
- editor = ENV["VISUAL"] || ENV["EDITOR"] # rubocop:disable Style/FetchEnvVar
16
- exit_with_error(EDITOR_MSG) if editor.empty?
37
+ parser.on("-e", "--editor EDITOR", "Specify EDITOR to use")
38
+ parser.on("-r", "--review", "Require review and confirmation of changes")
17
39
 
18
- results = Reenrb::Reen.new(editor: editor).execute(file_list)
40
+ parser.on("-h", "--help", "Show help for options") do
41
+ exit_with_msg(parser)
42
+ end
43
+ end
19
44
 
20
- changes = results.change_requested
45
+ def exit_with_msg(message)
46
+ puts message
47
+ exit(false)
48
+ end
21
49
 
22
- puts changes.summarize
50
+ def review_changes
51
+ puts
52
+ puts @requests.change_requested.summarize
53
+ print "\nContinue? (y/n) "
54
+ confirmation = %w[y yes].include?($stdin.gets.chomp.downcase)
55
+ exit_with_msg("Nothing changed") unless confirmation
56
+ end
57
+
58
+ def check_inputs
59
+ @files = files.empty? ? Dir.glob("*") : files
60
+ exit_with_msg(EDITOR_MSG) unless options.editor
61
+ end
62
+
63
+ def user_review?
64
+ @options.review && @requests.changes_requested?
65
+ end
66
+
67
+ def call
68
+ @requests = Reenrb::Reen.new(editor: options.editor).request(files)
69
+ review_changes if user_review?
70
+
71
+ changes = @requests
72
+ .execute_all
73
+ .change_requested
74
+
75
+ if user_review? && changes.all_executed?
76
+ puts "Changes made"
77
+ else
78
+ puts changes.summarize
79
+ end
80
+ end
81
+ end
82
+
83
+ begin
84
+ ReenCLI.new(ARGV).call
85
+ rescue Reenrb::Error => e
86
+ puts "#{e.message}\n"
87
+ end
@@ -11,7 +11,7 @@ module Reenrb
11
11
  @list = changes_list
12
12
  end
13
13
 
14
- def execute!
14
+ def execute_all
15
15
  @list.map(&:execute)
16
16
  self
17
17
  end
@@ -42,6 +42,14 @@ module Reenrb
42
42
  Changes.new(@list.select(&:executed?))
43
43
  end
44
44
 
45
+ def failed
46
+ Changes.new(@list.select(&:failed?))
47
+ end
48
+
49
+ def any?
50
+ !@list.empty?
51
+ end
52
+
45
53
  def count
46
54
  @list.size
47
55
  end
@@ -5,22 +5,37 @@ require "tempfile"
5
5
  module Reenrb
6
6
  # Manages a temporary file with requested changes
7
7
  class ChangesFile
8
+ INSTRUCTIONS = <<~COMMENTS
9
+ # Edit the names of any files/folders to rename or move them
10
+ # - Put a preceeding dash to delete a file or empty folder
11
+
12
+ COMMENTS
13
+
14
+ attr_accessor :list
15
+
8
16
  def initialize(requested_list)
9
17
  @list_file = Tempfile.new("reenrb-")
18
+ @list_file.write(INSTRUCTIONS)
10
19
  @list_file.write(requested_list.join("\n"))
11
20
  @list_file.close
12
21
  end
13
22
 
14
- def path
15
- @list_file.path
23
+ def allow_changes(editor, &block)
24
+ await_editor(editor) if editor
25
+ @list = File.read(path).split("\n").map(&:strip)
26
+ .reject { |line| line.start_with?("#") || line.empty? }
27
+
28
+ block&.call(self)
29
+ @list
16
30
  end
17
31
 
18
- def allow_changes(&block)
19
- block.call(self)
20
- File.read(path).split("\n")
32
+ private
33
+
34
+ def path
35
+ @list_file.path
21
36
  end
22
37
 
23
- def blocking_edit(editor)
38
+ def await_editor(editor)
24
39
  `#{editor} #{@list_file.path}`
25
40
  end
26
41
  end
data/lib/reenrb/reen.rb CHANGED
@@ -6,7 +6,7 @@ module Reenrb
6
6
  # Reenrb::Reen.new(editor: "code -w").call("spec/fixtures/example/*")
7
7
  # Reenrb::Reen.new(editor: nil).call("spec/fixtures/example/*") { ... }
8
8
  class Reen
9
- DEL_ERROR = "Do not delete any file/folder names"
9
+ DEL_ERROR = "Do not remove any file/folder names (no changes made)"
10
10
 
11
11
  attr_reader :changes
12
12
 
@@ -15,16 +15,8 @@ module Reenrb
15
15
  @options = options
16
16
  end
17
17
 
18
- def request(original_list, &block) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
19
- changed_list = ChangesFile.new(original_list).allow_changes do |file|
20
- file.blocking_edit(@editor) if @editor
21
-
22
- if block
23
- lines = File.read(file.path).split("\n")
24
- new_lines = block.call(lines) || lines
25
- File.write(file.path, new_lines.join("\n"))
26
- end
27
- end
18
+ def request(original_list, &block)
19
+ changed_list = ChangesFile.new(original_list).allow_changes(@editor, &block)
28
20
 
29
21
  raise(Error, DEL_ERROR) if changed_list.size != original_list.size
30
22
 
@@ -34,7 +26,7 @@ module Reenrb
34
26
 
35
27
  def execute(original_list, &block)
36
28
  @changes ||= request(original_list, &block)
37
- @changes = @changes.execute!
29
+ @changes = @changes.execute_all
38
30
  end
39
31
 
40
32
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Reenrb
4
- VERSION = "0.2.2"
4
+ VERSION = "0.3.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reenrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soumya Ray
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-06 00:00:00.000000000 Z
11
+ date: 2022-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest