-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_private_cocoapods.py
More file actions
429 lines (345 loc) · 15.6 KB
/
generate_private_cocoapods.py
File metadata and controls
429 lines (345 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import os
import subprocess # just to call an arbitrary command e.g. 'ls'
import sys
import shutil
try:
# for Python2
from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog
except ImportError:
# for Python 3
from tkinter import filedialog
# Sample class for navigation
class cd:
"""Context manager for changing the current working directory"""
def __init__(self, newPath):
self.newPath = os.path.expanduser(newPath)
def __enter__(self):
self.savedPath = os.getcwd()
os.chdir(self.newPath)
def __exit__(self, etype, value, traceback):
os.chdir(self.savedPath)
## Helper functions
def get_user_input(message):
''' (str) -> str
Get the input from the user in the form of string depending on the python version
'''
if sys.version_info[0] < 3:
return raw_input(message)
else:
return str(input(message))
def run_command(command):
'''
(str) -> Bool
Returns True if the command is executed successfully
'''
retcode = subprocess.call(command)
if retcode == 0:
print("######################## Command success ################################")
return True
else:
print("#####################################################################")
print("\n\nSomething went wrong: " + str(retcode) + "\n\n")
print("#####################################################################")
response = get_user_input("\n\nDo you want to retry same step?(y/n) :")
if response == 'y':
return run_command(command)
return False
def get_directory_path():
''' () -> str
Asks user to select a directory and returns selected path
'''
# Manage python version
if sys.version_info[0] < 3:
root = Tk()
root.title('Select folder to clone your framework')
root.directory = tkFileDialog.askdirectory()
return root.directory
else:
return filedialog.askdirectory()
def get_command_list(command):
''' (str) -> [str]
Returns the list of strings for a given command
'''
return command.split(' ')
# Main functions
def generate_cocoapods():
''' () -> ()
Start of the process, call this function to start the cocoapod generation
'''
# Create pod spec repo
print("\n\n####### Creating pods #######\n\n")
create_pod_spec_repo()
# Create pod repo
create_pod_repo()
def check_if_directory_exists(directory_name, path):
''' (Str, Str) -> Bool
Checks if the directory exists at given path and returns True if already exists
'''
return os.path.isdir(path + '/' + directory_name)
def add_pod_spec_repo(directory_path):
print("#####################################################################")
print("--- Creating your local pod repo \n ")
pod_repo_add_command = ["pod", "repo", "add", pod_spec_repo_name, pod_spec_repo_url]
if run_command(pod_repo_add_command) == True:
# Validate pod spec repo
validate_pod_spec_repo(directory_path)
def create_pod_spec_repo():
''' () -> ()
Navigates to ~/.cocoapods/repos/ adds local repo with pod_spec_repo_name collected from user
'''
directory_path = "~/.cocoapods/repos/" # Cocoapods directory path
with cd(directory_path):
print("#####################################################################")
print("\n\n ------ Navigating to the cocoapods directory -------- \n\n ")
# we are in cocoapods directory
absolute_path = os.getcwd() # get absolute path for listing out
folder_exists = check_if_directory_exists(pod_spec_repo_name, absolute_path)
if folder_exists == True:
print("#####################################################################")
print("\n\nPodSpec with the name \"" + pod_spec_repo_name + "\" already exists\n\n")
print("Do you want to skip this step ?")
response = get_user_input("Press (y/n): ")
if response == 'y':
print("#####################################################################")
print("\n\nValidating podspec repo\n\n")
# Validate pod spec repo
validate_pod_spec_repo(directory_path)
else:
print("#####################################################################")
print("Deleting your folder " + pod_spec_repo_name)
# Delete the empty folder
shutil.rmtree(absolute_path + '/' + pod_spec_repo_name)
# Add pod spec repo
add_pod_spec_repo(directory_path)
else:
# Add pod spec repo
add_pod_spec_repo(directory_path)
def validate_pod_spec_repo(directory_path):
''' (str) -> ()
Validates the local pod created in directory_path
'''
print("--- Validating your repo \n")
# Navigate inside the locally created spec repo
directory_path = directory_path + '/' + pod_spec_repo_name
print(directory_path)
with cd(directory_path):
lint_cmd = ['pod', 'repo', 'lint', '.']
run_command(lint_cmd)
def clone_repo_at_path(folder_path):
# Clone the pod to the selected folder
print("#####################################################################")
print("\n\n Cloning your repo from \"" + custom_pod_repo_url + "\"\n\n")
cmd = ['git', 'clone', custom_pod_repo_url]
if run_command(cmd) == True:
folder_path = folder_path + '/' + custom_pod_name
with cd(folder_path):
create_pod_library(folder_path)
def create_pod_repo_at(folder_path):
# Navigate to pod folder
with cd(folder_path):
# Check if the folder already exists
folder_exists = check_if_directory_exists(custom_pod_name, folder_path)
if folder_exists == True:
print("#####################################################################")
print("\n\nFolder with the name \"" + custom_pod_name + "\" already exists at the current location \"" + folder_path + "\"\n\n")
response = get_user_input("Do you want to select different path? Press (y/n): ")
if response == 'y':
create_pod_repo()
else:
print("#####################################################################")
print("\n\n Do you want us to delete the existing folder with name \"" + custom_pod_name + "\" at \"" + folder_path + "\" and retry again?\n\n")
response = get_user_input("Press (y/n): ")
if response == 'y':
print("#####################################################################")
print("Deleting your folder " + custom_pod_name)
# Delete the empty folder
shutil.rmtree(folder_path + '/' + custom_pod_name)
clone_repo_at_path(folder_path)
else:
print("#####################################################################")
response = get_user_input("Retry at same location again? Press (y/n): ")
if response == 'y':
create_pod_repo_at(folder_path)
else: # Folder doesn't exist
clone_repo_at_path(folder_path)
def create_pod_repo():
#select the path to clone the repository
print("#####################################################################")
print("-- Select a folder to clone your directory")
folder_path = get_directory_path()
create_pod_repo_at(folder_path)
def create_pod_library(folder_path):
# Create pod library
print("#####################################################################")
print("\n\n Creating pod library \n\n")
print("#####################################################################")
cmd = ['pod', 'lib', 'create', custom_pod_name]
if run_command(cmd) == True:
move_files_from_parent_directory_to_root_directory(folder_path)
def move_files_from_parent_directory_to_root_directory(folder_path):
# Rename the folder for easy work-around
# adding _tmp_automate for now
path = folder_path + '/' + custom_pod_name # get the folder name
os.rename(path, path + '_tmp_automate') # rename it by adding _tmp_automate as suffix
# Navigate to Pod folder and delete .git file
updated_pod_name = custom_pod_name + '_tmp_automate'
cwd = folder_path + '/' + updated_pod_name
with cd(cwd):
remove_git_file_cmd = ['rm', '-rf', '.git'] # Remove the .git file as there will be two .git files in root and parent
run_command(remove_git_file_cmd)
# Move all the files to root folder
source = os.listdir(cwd)
destination = folder_path
for file in source:
if file == '.DS_Store':
continue
if file == '.git':
continue
file_path = cwd + '/' + file
if file_path != destination:
shutil.move(file_path, destination)
# Delete the empty folder
shutil.rmtree(cwd)
# Update the pod spec file
update_pod_spec_file(path + '.podspec' )
# Run the command
print("#####################################################################")
print("--- Linting your pod")
lint_command = ["pod", "lib", "lint", custom_pod_name + '.podspec']
if run_command(lint_command) == True:
#Wait for user input to continue
print("#####################################################################")
response = get_user_input('Do you want to continue the process (y/n): ')
install_pods_in_example_project(folder_path)
print("#####################################################################")
response = get_user_input('Do you want to continue the process (y/n): ')
push_code_to_remote()
push_to_spec_repo()
def push_code_to_remote():
print("--- Adding it to git repo")
cmd = ["git", "add", "."]
run_command(cmd)
cmd = ["git", "add", "--all"]
run_command(cmd)
commit_message = get_user_input('Enter you commit message: ')
cmd = ["git", "commit", "-m", commit_message]
run_command(cmd)
cmd = ["git", "push", "origin", "master"]
run_command(cmd)
tag_no = get_user_input('Enter you tag no: ')
cmd = ["git", "tag", tag_no]
run_command(cmd)
cmd = ["git", "push", "--tags"]
run_command(cmd)
def push_to_spec_repo():
print("#####################################################################")
print("--- Validating before pushing code to spec repo")
cmd = ["pod", "spec", "lint", custom_pod_name + '.podspec']
run_command(cmd)
print("#####################################################################")
print("--- Pushing code to spec repo")
cmd = ["pod", "repo", "push", pod_spec_repo_name, custom_pod_name + '.podspec']
if run_command(cmd) == False:
# Committing changes
directory_path = "~/.cocoapods/repos/" + pod_spec_repo_name # Cocoapods directory path
with cd(directory_path):
print("--- Adding the changes code to spec repo")
git_add_command = ["git", "add", "."]
run_command(git_add_command)
print("--- Committing changes code to spec repo")
git_commit_command = ["git", "commit", "-am", "\' Adding the changes \'"]
run_command(git_commit_command)
# Changes committed so try the same step again
push_to_spec_repo()
def install_pods_in_example_project(folder_path):
example_project_directory = folder_path + '/Example'
with cd(example_project_directory):
print("#####################################################################")
print("--- Installing your pods")
cmd = ["pod", "install"]
run_command(cmd)
def update_pod_spec_file(file_path):
'''
(str) -> ()
Reads the pod spec file and asks user for data where needed and updates the file
'''
# Open podspec file
f = open(file_path, 'r')
# Read all the contents
contents = f.readlines()
# Close the file
f.close()
# Buffer to store the updated content
updated_contents = []
# Loop through each line of the file and ask user for inputs when needed
for line_index in range(len(contents)):
# Get the line
line = contents[line_index]
# Strip white spaces on both sides
line = line.strip()
# Create the line as array for checking user imputs
array = line.split(" ")
# If empty line or just a new line just add it for better indentaion
if len(array) == 0:
updated_contents.append(line)
continue
# Access the value
val = array[0]
# Ask user for summary
if val == 's.summary':
print("#####################################################################")
summary = get_user_input('Enter summary: ')
line = val + ' = \'' + summary + '\''
updated_contents.append(line)
continue
# Version updating
if val == 's.version':
print("#####################################################################")
version_no = get_user_input('Enter Version: ')
line = val + ' = \'' + version_no + '\''
updated_contents.append(line)
continue
# Ask user for description of the pod
if val == 's.description':
print("#####################################################################")
description = get_user_input('Enter Description: ')
updated_contents.append(line)
updated_contents.append(description)
# updated_contents.append('DESC')
#line_index = line_index + 3
continue
# Home page url to be updated
if val == 's.homepage':
print("#####################################################################")
home_page_url = get_user_input('Enter bitbucket home page url: ')
line = val + ' = \'' + home_page_url + '\''
updated_contents.append(line)
continue
# Souce code url
if val == 's.source':
print("#####################################################################")
bitbucket_source_url = get_user_input('Enter bitbucket source url: ')
line = val + ' = { :git => \'' + bitbucket_source_url + '\', :tag => s.version.to_s }'
updated_contents.append(line)
continue
# End of the file reached so adding required lines
if val == 'end':
updated_contents.append('\n')
updated_contents.append('s.platform = :ios, \"9.0\"')
updated_contents.append('s.pod_target_xcconfig = { \'SWIFT_VERSION\' => \'4.0\' }')
updated_contents.append('`echo "4.0" > .swift-version`')
updated_contents.append(line)
continue
updated_contents.append(line)
# Open the same file again and add the lines with new line characters at the end
f = open(file_path, 'w')
for line in updated_contents:
f.write(line + '\n')
f.close()
# Fetch all the required data at one go
pod_spec_repo_name = get_user_input("Enter your Pod Spec Repo Name: ")
pod_spec_repo_url = get_user_input("Enter Pod Spec Repo URL: ")
custom_pod_repo_url = get_user_input("Enter Pod repo url: ")
custom_pod_name = get_user_input("Enter your custom Pod name : ")
generate_cocoapods()