Commit 5e81227e authored by Marin Usalj's avatar Marin Usalj

Add command for manipulating pod path config

parent 23abea78
......@@ -14,6 +14,7 @@ module Pod
require 'cocoapods/command/list'
require 'cocoapods/command/outdated'
require 'cocoapods/command/podfile_info'
require 'cocoapods/command/config'
require 'cocoapods/command/project'
require 'cocoapods/command/push'
require 'cocoapods/command/repo'
......@@ -87,7 +88,7 @@ module Pod
#-------------------------------------------------------------------------#
include Config::Mixin
include Pod::Config::Mixin
private
......
module Pod
class Command
# This command was made in first place for supporting local repos.
# Command uses file ~/.config/cocoapods
#
# Sample usage:
# pod config --local ObjectiveSugar ~/code/OSS/ObjectiveSugar
# pod config --global ObjectiveRecord ~/code/OSS/ObjectiveRecord
#
# pod config --delete --local ObjectiveSugar
# pod config --delete --global ObjectiveSugar
#
# For both storing and deleting, --local is default and it can be ommitted
# pod config Kiwi ~/code/OSS/Kiwi
# pod config --delete Kiwi
#
class Config < Command
CONFIG_FILE_PATH = File.expand_path('~/.config/cocoapods')
LOCAL_OVERRIDES = 'PER_PROJECT_REPO_OVERRIDES'
GLOBAL_OVERRIDES = 'GLOBAL_REPO_OVERRIDES'
self.summary = 'Something like `bundle config` ... but better.'
self.description = <<-DESC
Use `pod config` when you're developing a pod that uses another pod of yours.
This way you will reference it locally without modifying a Podfile.
DESC
self.arguments = '[pod name] [--local, --global, --delete] [path]'
def initialize(argv)
@global = argv.flag?('global')
@local = argv.flag?('local') || !@global
@should_delete = argv.flag?('delete')
@pod_name = argv.shift_argument
@pod_path = argv.shift_argument
super
end
def self.options
[['--local' , 'Uses the local pod for the current project only'],
['--global', 'Uses the local pod everywhere'],
['--delete', 'Removes the local pod from configuration']]
end
def run
help! unless args_are_valid?
update_config
end
private
def args_are_valid?
valid = !!@pod_name
valid &= !!@pod_path unless @should_delete
valid
end
def update_config
if @should_delete
@local ? delete_local_config : delete_global_config
else
@local ? store_local_config : store_global_config
end
write_config_to_file
end
def store_global_config
config_hash[GLOBAL_OVERRIDES][@pod_name] = @pod_path
end
def store_local_config
config_hash[LOCAL_OVERRIDES][project_name] ||= {}
config_hash[LOCAL_OVERRIDES][project_name][@pod_name] = @pod_path
end
def delete_local_config
config_hash[LOCAL_OVERRIDES][project_name].delete(@pod_name)
end
def delete_global_config
config_hash[GLOBAL_OVERRIDES].delete(@pod_name)
end
def config_hash
@config_hash ||= load_config
end
def load_config
FileUtils.touch(CONFIG_FILE_PATH) unless File.exists? CONFIG_FILE_PATH
config = YAML.load(File.open(CONFIG_FILE_PATH)) || {}
config[LOCAL_OVERRIDES] ||= {}
config[GLOBAL_OVERRIDES] ||= {}
config
end
def project_name
`basename #{Dir.pwd}`.chomp
end
def write_config_to_file
File.write(CONFIG_FILE_PATH, config_hash.delete_blank.to_yaml)
end
end
end
end
class Hash
def delete_blank
delete_if{|k, v| v.empty? or v.instance_of?(Hash) && v.delete_blank.empty?}
end
end
......@@ -170,7 +170,7 @@ module Pod
end
end
# Loads or returns a previously initialized for the Pod of the given
# Loads or returns a previously initialized Set for the Pod of the given
# dependency.
#
# @param [Dependency] dependency
......
require File.expand_path('../../../spec_helper', __FILE__)
require 'yaml'
module Pod
describe Command::Config do
extend SpecHelper::TemporaryRepos
LOCAL_OVERRIDES = 'PER_PROJECT_REPO_OVERRIDES'
GLOBAL_OVERRIDES = 'GLOBAL_REPO_OVERRIDES'
pod_name = 'ObjectiveSugar'
pod_path = '~/code/OSS/ObjectiveSugar'
project_name = 'SampleProject'
before do
Config.instance = nil
def Dir.pwd; '~/code/OSS/SampleProject'; end
@config_file_path = temporary_directory + "mock_config"
Command::Config.send(:remove_const, 'CONFIG_FILE_PATH')
Command::Config.const_set("CONFIG_FILE_PATH", @config_file_path)
end
it "writes local repos for each project" do
run_command('config', "--local", pod_name, pod_path)
yaml = YAML.load(File.open(@config_file_path))
yaml[LOCAL_OVERRIDES][project_name][pod_name].should.equal pod_path
end
it "writes global repos without specifying project" do
run_command('config', "--global", pod_name, pod_path)
yaml = YAML.load(File.open(@config_file_path))
yaml[GLOBAL_OVERRIDES][pod_name].should.equal pod_path
end
it "defaults to local scope" do
run_command('config', pod_name, pod_path)
yaml = YAML.load(File.open(@config_file_path))
yaml[LOCAL_OVERRIDES][project_name][pod_name].should.equal pod_path
end
it "raises help! if invalid args are provided" do
[
lambda { run_command("config", 'ObjectiveSugar') },
lambda { run_command("config", "--local", 'ObjectiveSugar') },
lambda { run_command("config", "--global", 'ObjectiveSugar') },
lambda { run_command("config", '~/code/OSS/ObjectiveSugar') },
]
.each { |invalid| invalid.should.raise CLAide::Help }
end
it "deletes local configuration by default" do
run_command('config', "--global", pod_name, pod_path)
run_command('config', "--local", pod_name, pod_path)
run_command('config', "--delete", pod_name)
yaml = YAML.load(File.open(@config_file_path))
yaml.should.not.has_key? LOCAL_OVERRIDES
yaml[GLOBAL_OVERRIDES][pod_name].should.equal pod_path
end
it "deletes global configuration" do
run_command('config', "--global", pod_name, pod_path)
run_command('config', "--global", "--delete", pod_name)
yaml = YAML.load(File.open(@config_file_path))
yaml.should.not.has_key? GLOBAL_OVERRIDES
end
end
end
# ===================
# Config file format
# ===================
#
# ---
# LOCAL_OVERRIDES:
# SampleApp:
# ARAnalytics: ~/code/ARAnalytics
#
# GLOBAL_OVERRIDES:
# ObjectiveRecord: ~/code/OSS/ObjectiveRecord
# ObjectiveSugar: ~/code/OSS/ObjectiveSugar
#
......@@ -3,6 +3,7 @@ require File.expand_path('../../spec_helper', __FILE__)
module Pod
describe Command do
it "returns the proper command class" do
Command.parse(%w{ config }).should.be.instance_of Command::Config
Command.parse(%w{ help }).should.be.instance_of Command::Help
Command.parse(%w{ install }).should.be.instance_of Command::Install
Command.parse(%w{ list }).should.be.instance_of Command::List
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment