Class: Fastlane::PluginInfoCollector

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

Instance Method Summary collapse

Constructor Details

#initialize(ui = PluginGeneratorUI.new) ⇒ PluginInfoCollector

Returns a new instance of PluginInfoCollector.



3
4
5
# File 'fastlane/lib/fastlane/plugins/plugin_info_collector.rb', line 3

def initialize(ui = PluginGeneratorUI.new)
  @ui = ui
end

Instance Method Details

#author_valid?(author) ⇒ Boolean

Returns:



115
116
117
# File 'fastlane/lib/fastlane/plugins/plugin_info_collector.rb', line 115

def author_valid?(author)
  !author.to_s.strip.empty?
end

#collect_author(initial_author = nil) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'fastlane/lib/fastlane/plugins/plugin_info_collector.rb', line 102

def collect_author(initial_author = nil)
  return initial_author if author_valid?(initial_author)
  author = nil
  loop do
    author = @ui.input("What is the plugin author's name?")
    break if author_valid?(author)

    @ui.message('An author name is required.')
  end

  author
end

#collect_detailsObject

Summary



155
156
157
# File 'fastlane/lib/fastlane/plugins/plugin_info_collector.rb', line 155

def collect_details
  return @ui.input("Please enter a detailed description of this fastlane plugin:").to_s
end

#collect_email(initial_email = nil) ⇒ Object



128
129
130
# File 'fastlane/lib/fastlane/plugins/plugin_info_collector.rb', line 128

def collect_email(initial_email = nil)
  return initial_email || @ui.input("What is the plugin author's email address?")
end

#collect_info(initial_name = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'fastlane/lib/fastlane/plugins/plugin_info_collector.rb', line 7

def collect_info(initial_name = nil)
  plugin_name = collect_plugin_name(initial_name)
  author = collect_author(detect_author)
  email = collect_email(detect_email)
  summary = collect_summary
  details = collect_details

  PluginInfo.new(plugin_name, author, email, summary, details)
end

#collect_plugin_name(initial_name = nil) ⇒ Object

Plugin name



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
# File 'fastlane/lib/fastlane/plugins/plugin_info_collector.rb', line 21

def collect_plugin_name(initial_name = nil)
  plugin_name = initial_name
  first_try = true

  loop do
    if !first_try || plugin_name.to_s.empty?
      plugin_name = @ui.input("What would you like to be the name of your plugin?")
    end
    first_try = false

    unless plugin_name_valid?(plugin_name)
      fixed_name = fix_plugin_name(plugin_name)

      if plugin_name_valid?(fixed_name)
        plugin_name = fixed_name if @ui.confirm("\nWould '#{fixed_name}' be okay to use for your plugin name?")
      end
    end

    break if plugin_name_valid?(plugin_name)

    gem_name = PluginManager.to_gem_name(plugin_name)

    if gem_name_taken?(gem_name)
      # Gem name is already taken on RubyGems
      @ui.message("\nThe gem name '#{gem_name}' is already taken on RubyGems, please choose a different plugin name.")
    else
      # That's a naming error
      @ui.message("\nPlugin names can only contain lower case letters, numbers, and underscores")
      @ui.message("and should not contain 'fastlane' or 'plugin'.")
    end
  end

  plugin_name
end

#collect_summaryObject

Summary



136
137
138
139
140
141
142
143
144
145
146
# File 'fastlane/lib/fastlane/plugins/plugin_info_collector.rb', line 136

def collect_summary
  summary = nil
  loop do
    summary = @ui.input("Please enter a short summary of this fastlane plugin:")
    break if summary_valid?(summary)

    @ui.message('A summary is required.')
  end

  summary
end

#detect_authorObject

Author



97
98
99
100
# File 'fastlane/lib/fastlane/plugins/plugin_info_collector.rb', line 97

def detect_author
  git_name = Helper.backticks('git config --get user.name', print: FastlaneCore::Globals.verbose?).strip
  return git_name.empty? ? nil : git_name
end

#detect_emailObject

Email



123
124
125
126
# File 'fastlane/lib/fastlane/plugins/plugin_info_collector.rb', line 123

def detect_email
  git_email = Helper.backticks('git config --get user.email', print: FastlaneCore::Globals.verbose?).strip
  return git_email.empty? ? nil : git_email
end

#fix_plugin_name(name) ⇒ Object

Applies a series of replacement rules to turn the requested plugin name into one that is acceptable, returning that suggestion



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'fastlane/lib/fastlane/plugins/plugin_info_collector.rb', line 79

def fix_plugin_name(name)
  name = name.to_s.downcase
  fixes = {
    /[\- ]/ => '_', # dashes and spaces become underscores
    /[^a-z0-9_]/ => '', # anything other than lower case letters, numbers and underscores is removed
    /fastlane[_]?/ => '', # 'fastlane' or 'fastlane_' is removed
    /plugin[_]?/ => '' # 'plugin' or 'plugin_' is removed
  }
  fixes.each do |regex, replacement|
    name = name.gsub(regex, replacement)
  end
  name
end

#gem_name_taken?(name) ⇒ Boolean

Checks if the gem name is still free on RubyGems

Returns:



67
68
69
70
71
72
73
74
75
# File 'fastlane/lib/fastlane/plugins/plugin_info_collector.rb', line 67

def gem_name_taken?(name)
  require 'open-uri'
  require 'json'
  url = "https://rubygems.org/api/v1/gems/#{name}.json"
  response = JSON.parse(open(url).read)
  return !!response['version']
rescue
  false
end

#plugin_name_valid?(name) ⇒ Boolean

Returns:



56
57
58
59
60
61
62
63
64
# File 'fastlane/lib/fastlane/plugins/plugin_info_collector.rb', line 56

def plugin_name_valid?(name)
  # Only lower case letters, numbers and underscores allowed
  /^[a-z0-9_]+$/ =~ name &&
    # Does not contain the words 'fastlane' or 'plugin' since those will become
    # part of the gem name
    [/fastlane/, /plugin/].none? { |regex| regex =~ name } &&
    # Gem name isn't taken on RubyGems yet
    !gem_name_taken?(PluginManager.to_gem_name(name))
end

#summary_valid?(summary) ⇒ Boolean

Returns:



148
149
150
# File 'fastlane/lib/fastlane/plugins/plugin_info_collector.rb', line 148

def summary_valid?(summary)
  !summary.to_s.strip.empty?
end