class Viperaptor::CLI::Application

Public Instance Methods

gen(module_name = nil, template_name = nil) click to toggle source
# File lib/viperaptor/cli/gen_command.rb, line 30
def gen(module_name = nil, template_name = nil)
  Rambafile.use(options[:rambafile])

  does_rambafile_exist = Rambafile.exist

  unless does_rambafile_exist
    puts("Rambafile not found! Run `viperaptor setup` in the working directory instead!".red)
    return
  end

  setup_username_command = Viperaptor::CLI::SetupUsernameCommand.new
  setup_username_command.setup_username

  rambafile = Rambafile.rambafile

  if module_name == nil
    prompt = TTY::Prompt.new
    module_name = prompt.ask("Enter the name of module?") do |q|
      q.required true
      q.validate /^[A-Z]\w*$/
    end
  end

  templates = rambafile[TEMPLATES_KEY] || []
  templates_as_set = Set.new(templates.map { |t| t["name"] })
  templates = templates.concat(TemplateHelper.global_templates.select { |t| !templates_as_set.include?(t) }.map { |name| { "name" => name } }).sort_by { |t| t["name"] }

  filter = rambafile[TEMPLATES_FILTER_KEY] || ""

  if !filter.kind_of?(Array)
    filter = [filter]
  end

  filter = filter.map { |f| f.strip }.select { |f| !f.empty? }
  negative_filters = filter.select { |f| f.start_with?("!") }.map { |f| f[1...] }.map { |f| Regexp.new(f, Regexp::IGNORECASE) }
  positive_filters = filter.select { |f| !f.start_with?("!") }.map { |f| Regexp.new(f, Regexp::IGNORECASE) }
  original_templates = templates

  if original_templates.count == 0
    puts("No any templates found. Add templates catalog or templates to Rambafile.".red)
    exit
  end

  templates = templates.select do |t|
    if filter.count == 0
      next true
    end

    negative_filters.none? { |f| f.match(t["name"]) } && (positive_filters.count == 0 || positive_filters.any? { |f| f.match(t["name"]) })
  end

  if templates.count == 0
    puts("No templates found. Try to modify *templates_filter*.".red)
    exit
  end

  if template_name == nil
    prompt = TTY::Prompt.new
    choices = templates.map { |t| t["name"] }
    index = 1
    history = Viperaptor::UserPreferences.obtain_templates_history
    if history.count > 0
      matching = choices.find_index(history[0])
      if matching != nil
        index = matching + 1
      end
    end
    template_name = prompt.select("Select template?", choices, per_page: choices.count, default: index)
    Viperaptor::UserPreferences.add_template_to_history(template_name)
  end

  code_module = CodeModule.new(module_name, rambafile, options)

  module_validator = ModuleValidator.new
  module_validator.validate(code_module)

  module_info = ModuleInfoGenerator.new(code_module)
  template = ModuleTemplate.new(template_name, module_info.scope)

  parameters = GenCommandTableParametersFormatter.prepare_parameters_for_displaying(code_module, template_name)
  PrintTable.print_values(
    values: parameters,
    title: "Summary for gen #{module_name}",
  )

  DependencyChecker.check_all_required_dependencies_has_in_podfile(template.dependencies, code_module.podfile_path)
  DependencyChecker.check_all_required_dependencies_has_in_cartfile(template.dependencies, code_module.cartfile_path)

  project = XcodeprojHelper.obtain_project(code_module.xcodeproj_path)
  module_group_already_exists = XcodeprojHelper.module_with_group_path_already_exists(project, code_module.project_group_path)

  if module_group_already_exists
    replace_exists_module = yes?("#{module_name} module already exists. Replace? (yes/no)")

    unless replace_exists_module
      return
    end
  end

  generator = Viperaptor::ModuleGenerator.new
  generator.generate_module(module_name, code_module, template)
end
setup() click to toggle source
# File lib/viperaptor/cli/setup_command.rb, line 13
def setup
  properties = {}

  setup_username_command = Viperaptor::CLI::SetupUsernameCommand.new
  setup_username_command.setup_username

  properties[COMPANY_KEY] = ask('The company name which will be used in the headers:')

  project_name = Pathname.new(Dir.getwd).basename.to_s
  is_right_project_name = yes?("The name of your project is #{project_name}. Do you want to use it? (yes/no)")

  properties[PROJECT_NAME_KEY] = is_right_project_name ? project_name : ask_non_empty_string('The project name:', 'Project name should not be empty')
  properties[PROJECT_PREFIX_KEY]  = ask('The project prefix (if any):')

  xcodeproj_path = ask_file_with_path('*.xcodeproj',
                                      '.xcodeproj file of the project')

  properties[XCODEPROJ_PATH_KEY] = xcodeproj_path
  project = Xcodeproj::Project.open(xcodeproj_path)

  targets_prompt = ''
  project.targets.each_with_index { |element, i| targets_prompt += ("#{i}. #{element.name}" + "\n") }
  project_target = ask_index("Select the appropriate target for adding your MODULES (type the index):\n" + targets_prompt,project.targets)
  include_tests = yes?('Are you using unit-tests in this project? (yes/no)')

  test_target = nil

  if include_tests
    test_target = ask_index("Select the appropriate target for adding your TESTS (type the index):\n" + targets_prompt,project.targets)
  end

  should_add_all_modules_by_one_path = yes?('Do you want to add all your modules by one path? (yes/no)')

  project_file_path = nil
  project_group_path = nil

  test_file_path = nil
  test_group_path = nil

  if should_add_all_modules_by_one_path || include_tests
    should_use_same_paths = false

    if should_add_all_modules_by_one_path
      should_use_same_paths = yes?('Do you want to use the same paths for your files both in Xcode and the filesystem? (yes/no)')
    end

    if should_use_same_paths
      if should_add_all_modules_by_one_path
        project_group_path = ask('The default path for creating new modules:')
        project_file_path = project_group_path
      end

      if include_tests
        test_group_path = ask('The default path for creating tests:')
        test_file_path = test_group_path
      end
    else
      if should_add_all_modules_by_one_path
        project_group_path = ask('The default path for creating new modules (in Xcode groups):')
        project_file_path = ask('The default path for creating new modules (in the filesystem):')
      end

      if include_tests
        test_group_path = ask('The default path for creating tests (in Xcode groups):')
        test_file_path = ask('The default path for creating tests (in the filesystem):')
      end
    end
  end

  using_pods = yes?('Are you using Cocoapods? (yes/no)')
  if using_pods
    properties[PODFILE_PATH_KEY] = ask_file_with_path('Podfile', 'Podfile')
  end

  using_carthage = yes?('Are you using Carthage? (yes/no)')
  if using_carthage
    properties[CARTFILE_PATH_KEY] = ask_file_with_path('Cartfile', 'Cartfile')
  end

  should_add_templates = yes?('Do you want to add some well known templates to the Rambafile? (yes/no)')
  if should_add_templates
    properties[TEMPLATES_KEY] = [
        '{name: rviper_controller}',
        '{name: mvvm_controller}',
        '{name: swifty_viper}'
    ]
  end

  properties[PROJECT_TARGET_KEY] = project_target.name if project_target
  properties[PROJECT_FILE_PATH_KEY] = project_file_path if project_file_path
  properties[PROJECT_GROUP_PATH_KEY] = project_group_path if project_group_path
  properties[TEST_TARGET_KEY] = test_target.name if test_target
  properties[TEST_FILE_PATH_KEY] = test_file_path if test_file_path
  properties[TEST_GROUP_PATH_KEY] = test_group_path if test_group_path

  PrintTable.print_values(
      values: properties,
      title: 'Summary for viperaptor setup'
  )

  Viperaptor::RambafileGenerator.create_rambafile(properties)
  if should_add_templates
    puts('Rambafile successfully created! Now run `viperaptor template install`.'.green)
  else
    puts('Rambafile successfully created!\n Go on and find some templates in our catalog using `viperaptor template list` command.\n Add any of them to the Rambafile and run `generamba template install`.'.green)
  end

end
version() click to toggle source
# File lib/viperaptor/cli/version_command.rb, line 9
def version
  options = {}
  options['Version'] = Viperaptor::VERSION.green
  options['Release date'] = Viperaptor::RELEASE_DATE.green
  options['Change notes'] = Viperaptor::RELEASE_LINK.green

  values = []

  options.each do |title, value|
    values.push("#{title}: #{value}")
  end

  output = values.join("\n")
  puts(output)
end