Class: Dependency::DependencyReplacer

Inherits:
Object
  • Object
show all
Defined in:
lib/dependencyswapper/DependencyReplacer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DependencyReplacer

Returns a new instance of DependencyReplacer.



17
18
19
20
21
22
23
# File 'lib/dependencyswapper/DependencyReplacer.rb', line 17

def initialize(options)
	@dependency_name = options.fetch(:dependency_name)
	@podfile_path = "Podfile"
	if !File.exist?(@podfile_path)
		abort("Cannot find the podfile. Make sure to run depswapper where your Podfile exists!")
	end
end

Instance Attribute Details

#dependency_nameObject (readonly)

Returns the value of attribute dependency_name.



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

def dependency_name
  @dependency_name
end

Class Method Details

.perform(options) ⇒ Object



13
14
15
# File 'lib/dependencyswapper/DependencyReplacer.rb', line 13

def self.perform(options)
	new(options).perform
end

Instance Method Details

#devObject



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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/dependencyswapper/DependencyReplacer.rb', line 73

def dev
	graph = Dependency::Graph.new({
		 :podfilelock_path => @podfile_path + ".lock"
	})
	pods = graph.generate()

	#tag variable to store the version
	tag = ""

	pods.each { |pod|
		if pod.name.eql? @dependency_name
			tag = pod.version
		end
	}

	file_lines = ''
	IO.readlines(@podfile_path).each do |line|
			 file_lines += line unless line.include? "'" + dependency_name + "'"
			 if line.include? "'" + dependency_name + "'"
			 		 			 	# We will look in the cocoapods public/private repos to map each Framework with its git repository.
			 	directories = Dir.glob("#{Dir.home}/.cocoapods/repos/**/" + dependency_name + ".podspec.json")
			 	file = File.read(directories.last)
			dependency_replacements = JSON.parse(file)

			 	remote_url = dependency_replacements["homepage"]
			 	if remote_url.to_s.empty?
						puts "You are missing the dependency mapping for " + dependency_name + "."
			else
				url_extension = File.extname(remote_url)
				if url_extension.to_s.empty?
					remote_url = remote_url + ".git"
				end
				unless File.directory?("dev-#{dependency_name}")
					if tag.length > 0
						`git clone --branch #{tag} #{remote_url} dev-#{dependency_name}`
					else
						`git clone #{remote_url} dev-#{dependency_name}`
					end
				end
			 		file_lines += "pod '" + @dependency_name + "', :path => './dev-" + dependency_name + "/'\n"
			 	end
			 end
	end

	File.open(@podfile_path, 'w') do |file|
 			file.puts file_lines
	end
	development_dir = Dir.pwd + "/dev-" + dependency_name
	puts "The development pod was cloned in ".green + development_dir.green
	system("pod install")
end

#runObject



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
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dependencyswapper/DependencyReplacer.rb', line 25

def run
	graph = Dependency::Graph.new({
		 			 	:podfilelock_path => @podfile_path + ".lock"
	})
	pods = graph.generate()

	#tag variable to store the version
	tag = ""

	pods.each { |pod|
		if pod.name.eql? @dependency_name
			tag = pod.version
		end
	}

	file_lines = ''

	IO.readlines(@podfile_path).each do |line|
			 file_lines += line unless line.include? "'" + dependency_name + "'"
			 if line.include? "'" + dependency_name + "'"
			 	# We will look in the cocoapods public/private repos to map each Framework with its git repository.
			 	directories = Dir.glob("#{Dir.home}/.cocoapods/repos/**/" + dependency_name + ".podspec.json")
			 	file = File.read(directories.last)
			dependency_replacements = JSON.parse(file)

			 	remote_url = dependency_replacements["homepage"]
			 	if remote_url.to_s.empty?
						puts "You are missing the dependency mapping for " + dependency_name + "."
			else
				url_extension = File.extname(remote_url)
				if url_extension.to_s.empty?
					remote_url = remote_url + ".git"
				end

				if tag.length > 0
					file_lines += "pod '" + @dependency_name + "', :git => '" + remote_url + "', :tag => '" + tag + "'\n"
			 		else
			 			file_lines += "pod '" + @dependency_name + "', :git => '" + remote_url + "'\n"
			 		end
			 	end
			 end
	end
	File.open(@podfile_path, 'w') do |file|
 			file.puts file_lines
	end
	system("pod install")
end