Class: Fastlane::AutoComplete

Inherits:
Object
  • Object
show all
Defined in:
fastlane/lib/fastlane/auto_complete.rb

Overview

Enable tab auto completion

Class Method Summary collapse

Class Method Details

.execute(args, options) ⇒ Object

This method copies the tab auto completion scripts to the user’s home folder, while optionally adding custom commands for which to enable auto complete

Parameters:

  • options (Array)

    An array of all options (e.g. –custom fl)



9
10
11
12
13
14
15
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'fastlane/lib/fastlane/auto_complete.rb', line 9

def self.execute(args, options)
  shell = ENV['SHELL']

  if shell.end_with?("fish")
    fish_completions_dir = "~/.config/fish/completions"

    if UI.interactive?
      confirm = UI.confirm("This will copy a fish script into #{fish_completions_dir} that provides the command tab completion. If the directory does not exist it will be created. Sound good?")
      return unless confirm
    end

    fish_completions_dir = File.expand_path(fish_completions_dir)
    FileUtils.mkdir_p(fish_completions_dir)

    completion_script_path = File.join(Fastlane::ROOT, 'lib', 'assets', 'completions', 'completion.fish')
    final_completion_script_path = File.join(fish_completions_dir, 'fastlane.fish')

    FileUtils.cp(completion_script_path, final_completion_script_path)

    UI.success("Copied! You can now use tab completion for lanes")
  else
    fastlane_conf_dir = "~/.fastlane"

    if UI.interactive?
      confirm = UI.confirm("This will copy a shell script into #{fastlane_conf_dir} that provides the command tab completion. Sound good?")
      return unless confirm
    end

    # create the ~/.fastlane directory
    fastlane_conf_dir = File.expand_path(fastlane_conf_dir)
    FileUtils.mkdir_p(fastlane_conf_dir)

    # then copy all of the completions files into it from the gem
    completion_script_path = File.join(Fastlane::ROOT, 'lib', 'assets', 'completions')
    FileUtils.cp_r(completion_script_path, fastlane_conf_dir)

    custom_commands = options.custom.to_s.split(',')

    Fastlane::SHELLS.each do |shell_name|
      open("#{fastlane_conf_dir}/completions/completion.#{shell_name}", 'a') do |file|
        default_line_prefix = Helper.bundler? ? "bundle exec " : ""

        file.puts(self.get_auto_complete_line(shell_name, "#{default_line_prefix}fastlane"))

        custom_commands.each do |command|
          auto_complete_line = self.get_auto_complete_line(shell_name, command)

          next if auto_complete_line.nil?

          file.puts(auto_complete_line)
        end
      end
    end

    UI.success("Copied! To use auto complete for fastlane, add the following line to your favorite rc file (e.g. ~/.bashrc)")
    UI.important("  . ~/.fastlane/completions/completion.sh")
    UI.success("Don't forget to source that file in your current shell! 🐚")
  end
end

.get_auto_complete_line(shell, command) ⇒ Object

Helper to get the auto complete register script line



70
71
72
73
74
75
76
77
78
79
80
# File 'fastlane/lib/fastlane/auto_complete.rb', line 70

def self.get_auto_complete_line(shell, command)
  if shell == :bash
    prefix = "complete -F"
  elsif shell == :zsh
    prefix = "compctl -K"
  else
    return nil
  end

  return "#{prefix} _fastlane_complete #{command}"
end