Class: Gena::Application

Inherits:
Thor
  • Object
show all
Defined in:
lib/cli/cli.rb,
lib/cli/init.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.class_for_commandObject

class_for_command - hash to store custom classes (actually Plugin subclasses) for each command registered with gena



11
12
13
# File 'lib/cli/cli.rb', line 11

def class_for_command
  @class_for_command ||= Hash.new
end

.class_for_command=(commands) ⇒ Object



15
16
17
# File 'lib/cli/cli.rb', line 15

def class_for_command=(commands)
  @class_for_command = commands
end

.finishObject



93
94
95
96
# File 'lib/cli/cli.rb', line 93

def finish
  XcodeUtils.shared.save_project
  Application.new.check_gena_version
end

.help(shell, subcommand = false) ⇒ Object

Override help to forward



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
# File 'lib/cli/cli.rb', line 25

def help(shell, subcommand = false)

  #List plugin commands separately from Gena general commands
  plugins = []
  class_for_command.each do |command, klass|
    plugins += klass.printable_commands(false)
  end

  plugins.uniq!

  list = printable_commands(true, subcommand)
  Thor::Util.thor_classes_in(self).each do |klass|
    list += klass.printable_commands(false)
  end

  list -= plugins

  # Remove this line to disable alphabetical sorting
  # list.sort! { |a, b| a[0] <=> b[0] }

  # Add this line to remove the help-command itself from the output
  # list.reject! {|l| l[0].split[1] == 'help'}

  if defined?(@package_name) && @package_name
    shell.say "#{@package_name} commands:"
  else
    shell.say "General commands:"
  end

  shell.print_table(list, :indent => 2, :truncate => true)
  shell.say
  class_options_help(shell)

  shell.say "Plugins:"
  shell.print_table(plugins, :indent => 2, :truncate => true)
end

.plugin_classesObject



19
20
21
# File 'lib/cli/cli.rb', line 19

def plugin_classes
  class_for_command.values.uniq
end

.start(given_args = ARGV, config = {}) ⇒ Object

Override start to do custom dispatch (looking for plugin for unknown command)



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
# File 'lib/cli/cli.rb', line 65

def start(given_args = ARGV, config = {})

  config[:shell] ||= Thor::Base.shell.new

  command_name = normalize_command_name(retrieve_command_name(given_args.dup))
  clazz = command_name ? class_for_command[command_name] : nil

  if command_name && clazz
    if check_dependencies(command_name, clazz.dependencies)
      clazz.dispatch(nil, given_args.dup, nil, config)
    end
  else
    dispatch(nil, given_args.dup, nil, config)
  end

  finish

rescue Thor::Error => e
  config[:debug] || ENV["THOR_DEBUG"] == "1" ? (raise e) : config[:shell].error(e.message)
  exit(1) if exit_on_failure?
rescue Errno::EPIPE
  # This happens if a thor command is piped to something like `head`,
  # which closes the pipe when it's done reading. This will also
  # mean that if the pipe is closed, further unnecessary
  # computation will not occur.
  exit(0)
end

Instance Method Details

#initObject



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
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
# File 'lib/cli/init.rb', line 28

def init

  xcode_project_name = `find . -name *.xcodeproj`
  xcode_project_name.strip!

  xcode_project_name = ask_with_default("Enter path for #{set_color('project', Color::YELLOW)} or ENTER to continue (#{xcode_project_name}):", xcode_project_name)

  xcode_project = Xcodeproj::Project.open(xcode_project_name)


  main_target = nil
  test_target = nil
  xcode_project.native_targets.each do |target|
    if target.product_type == 'com.apple.product-type.application'
      main_target = target
    elsif target.product_type == 'com.apple.product-type.bundle.unit-test'
      test_target = target
    end
  end

  hash = Hash.new
  hash[:plugins_url] = [
      'https://github.com/alexgarbarev/gena-plugins.git'
  ]

  unless main_target
    say "Can't find application target in your Xcode project. Please create application target and try again", Color::RED
    abort
  end

  default_build_configuration = main_target.build_configuration_list.default_configuration_name || 'Debug'
  info_plist_value = main_target.build_configuration_list.get_setting('INFOPLIST_FILE')[default_build_configuration]
  if info_plist_value['$(SRCROOT)/']
    info_plist_value['$(SRCROOT)/'] = ''
  end

  hash['company'] = xcode_project.root_object.attributes['ORGANIZATIONNAME'].to_s
  hash['prefix'] = xcode_project.root_object.attributes['CLASSPREFIX'].to_s
  hash['project_name'] = xcode_project.root_object.name
  hash['info_plist'] = info_plist_value

  if main_target
    sources_path = common_path_in_target(main_target, 'main.m')
    sources_path = relative_to_current_dir(sources_path)

    hash['project_target'] = main_target.name
    hash['sources_dir'] = ask_with_default("Enter path for #{set_color('sources', Color::YELLOW)} or ENTER to continue (#{sources_path}):", sources_path)
 end

  if test_target
    tests_path = common_path_in_target(test_target, "#{sources_path}/")
    tests_path = relative_to_current_dir(tests_path)

    hash['test_target'] = test_target.name
    hash['tests_dir'] = ask_with_default("Enter path for #{set_color('tests', Color::YELLOW)} or ENTER to continue (#{tests_path}):", tests_path)
  else
    say "Can't find target for UnitTests. You setup it later using 'test_target' and 'tests_dir' key (similar to sources)", Color::YELLOW
    hash['test_target'] = ''
    hash['tests_dir'] = ''
  end

  language = file_exists_in_target?(main_target, 'main.m') ? "objc" : "swift"
  hash['language'] = ask_with_default("Enter main #{set_color('language', Color::YELLOW)} or ENTER to continue (#{language}):", language)

  say '===============================================================', Color::YELLOW
  print_table(hash)
  say '===============================================================', Color::YELLOW

  File.open('gena.plist', 'w') { |f| f.write hash.to_plist }


  say "'gena.plist' created..", Color::GREEN
end

#reconfigure(command) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cli/init.rb', line 6

def reconfigure(command)

  $config = Gena::Config.new
  $config.load_plist_config

  data = $config.data[GENA_PLUGINS_CONFIG_KEY] || Hash.new

  Application.plugin_classes.each do |klass|

    plugin_config_name = klass.plugin_config_name
    if plugin_config_name == command
      say "Setting defaults for '#{command}'..", Color::GREEN
      data[plugin_config_name] = klass.plugin_config_defaults
      $config.data[GENA_PLUGINS_CONFIG_KEY] = data
      $config.save!
      return
    end
  end
  say "Can't find plugin with name '#{command}'", Color::RED
end