#!/usr/bin/env ruby require "cocoapods" require "podmode" require "fileutils" module Podmode def self.run() action = ARGV[0] action_method = action_method(action) if action_method.nil? help() else action_method.call() end end def self.help() puts "Specify one of the following actions:\n\n" actions().each do |action_method, desc| puts " #{String(action_method.name).ljust(10)}#{desc}" end end def self.version() puts ::Podmode::VERSION end # OPTIONS: def self.list() podmodes = available_podmodes() if podmodes.size() == 0 puts " - No podmodes configured - " else podmodes.each do |podmode| puts "- #{podmode[:name]}" puts " path: #{podmode[:path]}" puts " contents:" podmode[:contents].lines.each { |l| puts (' ' * 10) + l } puts "\n" end end end def self.create() name = ARGV[1] if name.nil? puts "Required name attribute is missing. [podmode create NAME]" return end name = name.strip path = podmode_filepath(name) if File.file?(path) if Prompt::bool('Podmode file already exists, edit it?') edit() end return end puts "Creating podmode for '#{name}'" edit_podmode(name) puts "Podmode created for '#{name}'" print_contents(path) end def self.edit() name = ARGV[1] if name.nil? puts "Required name attribute is missing. [podmode edit NAME]" return end name = name.strip path = podmode_filepath(name) if File.file?(path) == false if Prompt::bool('Podmode file does not exist, create it?') create() end return end puts "Editing podmode for '#{name}'" print_contents(path) edit_podmode(name) puts "Podmode edited for '#{name}'" print_contents(path) end def self.remove() name = ARGV[1] if name.nil? puts "Required name attribute is missing. [podmode remove NAME]" return end name = name.strip path = podmode_filepath(name) if File.file?(path) == false puts "No podmode file found for #{name}, nothing removed." end File.delete(path) puts "Podmode removed for #{name}." end # --------------- private def self.actions() return {method(:list) => "Lists currently configured podmodes", method(:create) => "Creates a new podmode; [podmode create NAME]", method(:edit) => "Edits an existing podmode; [podmode edit NAME]", method(:remove) => "Removes an existing podmode; [podmode remove NAME]", method(:help) => "Shows the available podmode actions", method(:version) => "Prints the current version of Podmode"} end def self.action_method(action_name) return if action_name.nil? action_name = action_name.strip actions().each do |action_method, desc| if String(action_method.name) == action_name return action_method end end puts "Unknown action '#{action_name}'" return nil end def self.available_podmodes() dir = podmodes_dir() podmodes = [] if File.directory?(dir) Dir["#{dir}/*.podmode"].each do |podmode| podmodes.push({name: File.basename(podmode, ".*"), path: podmode, contents: File.read(podmode)}) end end return podmodes end def self.podmode_filepath(name) dir = podmodes_dir() FileUtils::mkdir_p dir return File.join(dir, "#{name.downcase}.podmode") end def self.edit_podmode(name) puts "- Available modes:" modes = ['auto', 'local', 'branch', 'tag'] modes.each_with_index {|mode, ind| puts " [#{ind}] #{mode}"} mode = Prompt::index(modes, 'Select a mode') content = [] content.push("#{name.upcase}_PODMODE=#{mode}") case mode when 'local' path = Prompt::prompt('Specify the local path: ') fullpath = File.expand_path(path.strip) content.push("#{name.upcase}_PODMODE_PATH=\"#{fullpath}\"") when 'branch' branch = Prompt::prompt('Specify the branch to check out: ') content.push("#{name.upcase}_PODMODE_BRANCH=#{branch.strip}") when 'tag' tag = Prompt::prompt('Specify the tag to check out: ') content.push("#{name.upcase}_PODMODE_TAG=#{tag.strip}") end File.open(podmode_filepath(name), 'w') { |f| f.write(content.join("\n")) } end def self.print_contents(path) Console::separator() puts "Current contents:" File.read(path).lines.each { |l| puts (' ' * 4) + l } Console::separator() end end Podmode::run()