Class: Gem::Commands::EditCommand

Inherits:
Gem::Command
  • Object
show all
Includes:
VersionOption
Defined in:
lib/gemedit/system_commands.rb,
lib/gemedit/edit_command.rb,
lib/gemedit/git_options.rb

Constant Summary collapse

OPTIONS =
{
  :version => Gem::Requirement.default,
  :verbose => false,
  :dryrun => false,
  :git => false,
  :editor => ENV['GEMEDITOR'] || ENV['VISUAL'] || ENV['EDITOR'] || 'vi'
}

Instance Method Summary collapse

Constructor Details

#initializeEditCommand

Returns a new instance of EditCommand.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gemedit/edit_command.rb', line 16

def initialize
  super 'edit', 'Open an installed gem in your favorite editor', OPTIONS

  add_version_option

  add_option('-e', '--editor EDITOR', String,
             'The editor to use to open the gems',
             'GEMEDITOR, VISUAL and EDITOR environment variables',
             'are used to find the default',
             "Default: #{OPTIONS[:editor]}") do |editor, options|
    options[:editor] = editor
  end

  add_option('-g', '--git',
             'Create a git repository in gem directory and do an',
             'initial commit to help keep track of subsequent',
             'changes.',
             "Default: #{OPTIONS[:git]}") do |git, options|
    if(git?)
      options[:git] = git
    else
      puts "It doesn't appear that git is installed. If you're sure it's installed please check your $PATH"
    end
  end

  add_option('-d', '--[no-]dry-run',
             'Shows what command would be run without running it',
             'Turns on verbose logging', "Default: #{OPTIONS[:dryrun]}") do |dryrun, options|
    Gem.configuration.verbose ||= true if dryrun
    options[:dryrun] = dryrun
  end
end

Instance Method Details

#argumentsObject

:nodoc:



49
50
51
# File 'lib/gemedit/edit_command.rb', line 49

def arguments # :nodoc:
  "GEMNAME       name of gem to open in your favorite editor"
end

#carry_out_choice(choice) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gemedit/git_options.rb', line 38

def carry_out_choice(choice)
  case choice[0]
    when /View status*/ then puts git_status_full; true
    when /Permanantly remove*/ then
      `rm -rf .git`
      git_init
      git_commit('Gemedit generated commit')
      false
    when /Perform a commit and edit gem anyway*/ then
      git_commit
      false
    when /Skip commit and edit gem anyway*/ then
      false
    when /Get out of here*/ then
      Process.exit!(0)
  end
end

#cd(directory) ⇒ Object



2
3
# File 'lib/gemedit/system_commands.rb', line 2

def cd(directory)
end

#choicesObject



56
57
58
59
60
61
62
# File 'lib/gemedit/git_options.rb', line 56

def choices
  ['View status',
   'Permanantly remove repository (currently is effectively the same as option 3)',
   'Perform a commit and edit gem anyway',
   'Skip commit and edit gem anyway',
   'Get out of here']
end

#defaults_strObject

:nodoc:



53
54
55
# File 'lib/gemedit/edit_command.rb', line 53

def defaults_str # :nodoc:
  "--version '#{OPTIONS[:version]}' --editor #{OPTIONS[:editor]} --no-dry-run"
end

#executeObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gemedit/edit_command.rb', line 61

def execute
  version = options[:version] || OPTIONS[:version]

  gem_specs = get_all_gem_names.map { |gem_name|
    if spec = Gem.source_index.find_name(gem_name, version).last
      say "Found gem for '#{gem_name}' with version #{version}" if Gem.configuration.verbose
    else
      say "No gem found for '#{gem_name}' with version #{version}" if Gem.configuration.verbose
    end
    spec
  }.compact

  if gem_specs.size > 0
    say "Opening the following gems with #{options[:editor]}:" if Gem.configuration.verbose
    paths = gem_specs.map do |spec|
      say "  #{spec.full_name} #{spec.full_gem_path}" if Gem.configuration.verbose
      %Q{"#{spec.full_gem_path}"}
    end
    if options[:git]
      paths.each do |path|
        path.gsub!(/"/, '')
        Dir.chdir("#{path}") do
          if File.exists?('.git')
            if(git_status.empty?)
              alert(notify_of_clean_repo, "Press 'Enter' to continue")
            else
              alert(warn_of_unclean_repo)
              while(carry_out_choice(choose_from_list('Which action would you like to pursue?: ', choices)))
                #alert_warning(warn_of_unclean_repo, "Press 'Enter' to continue")
              end
            end
          else
            say "Initializing empty git repository in #{path}" if Gem.configuration.verbose
            system('git init')
            say "Creating initial commit" if Gem.configuration.verbose
            system("git add . && git commit -m 'Gemedit generated commit'")
          end
        end
      end
    end
    cmd = "#{options[:editor]} #{paths.join(' ')}"
    say "Running `#{cmd}`" if Gem.configuration.verbose
    exec cmd unless options[:dryrun]
  else
    say "No gems found for #{get_all_gem_names.join(', ')}"
    raise Gem::SystemExitException, 1
  end
end

#git?Boolean

Returns:

  • (Boolean)


2
3
4
# File 'lib/gemedit/git_options.rb', line 2

def git?
  system("which git")
end

#git_initObject



14
15
16
# File 'lib/gemedit/git_options.rb', line 14

def git_init
  system('git init')
end

#git_statusObject



6
7
8
# File 'lib/gemedit/git_options.rb', line 6

def git_status
  `git status -s`
end

#git_status_fullObject



10
11
12
# File 'lib/gemedit/git_options.rb', line 10

def git_status_full
  `git status`
end

#notify_of_clean_repoObject



18
19
20
21
22
23
24
25
26
# File 'lib/gemedit/git_options.rb', line 18

def notify_of_clean_repo
  <<-eos
\e[32mThere seems to already be a git repository for this gem.
Since there are no staged or unstaged comments, this just
serves as a heads-up.\e[0m
eos

  #p method(:say)
end

#usageObject

:nodoc:



57
58
59
# File 'lib/gemedit/edit_command.rb', line 57

def usage # :nodoc:
  "#{program_name} [options] GEMNAME [GEMNAME ...]"
end

#warn_of_unclean_repoObject



28
29
30
31
32
33
34
35
36
# File 'lib/gemedit/git_options.rb', line 28

def warn_of_unclean_repo
  warning = <<-eos
\e[31mThere seems to be an existing git repository for this gem
and there are also staged and/or unstaged changes. Since
I will not overwrite existing git repositories and since changes
to files in this gem will be hard to track, you may want to
consider addressing this before actually editing the gem.\e[0m
eos
end

#which(command) ⇒ Object



5
6
# File 'lib/gemedit/system_commands.rb', line 5

def which(command)
end