-
Notifications
You must be signed in to change notification settings - Fork 2
/
swagger-codegen.py
87 lines (70 loc) · 3.96 KB
/
swagger-codegen.py
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
import requests
import os
import json
import shutil
try:
# 1. Retrieves schema's names from swagger.json
with requests.Session() as session:
response = session.get('https://signer-lac.azurewebsites.net/swagger/api/swagger.json')
response_json = json.loads(response.content)
schemas = response_json['components']['schemas']
schemaNames = list(schemas.keys())
# 2. Creates a Map<'NamespaceANamespaceBClassName', { namespace: 'namespacea.namespaceb', name: 'ClassName' }
# NOTE:
# If a class is represented as NamespaceA.NamespaceB.ClassName then the class generated by swagger-codegen-3.0.20
# will be NamespaceANamespaceBClassName
classNames = {}
for name in schemaNames:
if '.' in name:
classNamespaceAndName = name.split('.')
generatedClassName = "".join(classNamespaceAndName)
if (not generatedClassName in classNames.keys()):
classNames[generatedClassName] = {}
classNames[generatedClassName]['namespace'] = [n.lower() for n in classNamespaceAndName[:-1]]
classNames[generatedClassName]['name'] = classNamespaceAndName[-1]
# 3. Delete all old generated files inside of folders.
dirname = os.path.dirname(__file__) + '\\src\\main\\java\\com\\lacunasoftware\\signer'
for className in classNames:
package = dirname + '\\' + classNames[className]['namespace'][0]
if os.path.exists(package):
shutil.rmtree(package)
# 4. Iterates on the list of generated files.
javaFileClassList = [f.name for f in os.scandir(dirname) if f.is_file()]
for javaFileClass in javaFileClassList:
with open(dirname + '\\' + javaFileClass, 'rb') as file:
fileData = file.read()
# 4.1. Change imported packages to meet the new folder's structure
for className in classNames:
oldImport = bytes('import com.lacunasoftware.signer.' + className, 'utf-8')
newImport = bytes('import com.lacunasoftware.signer.' + '.'.join(classNames[className]['namespace']) + '.' + classNames[className]['name'], 'utf-8')
fileData = fileData.replace(oldImport, newImport)
oldClassReferences = bytes(className, 'utf-8')
newClassReferences = bytes(classNames[className]['name'], 'utf-8')
fileData = fileData.replace(oldClassReferences, newClassReferences)
# 4.2. If this file itself belongs to another package, changes it on code.
javaClass = javaFileClass[:-5] # Removes .java
if javaClass in classNames.keys():
oldNamespace = bytes('package com.lacunasoftware.signer', 'utf-8')
newNamespace = bytes('package com.lacunasoftware.signer.' + '.'.join(classNames[javaClass]['namespace']), 'utf-8')
fileData = fileData.replace(oldNamespace, newNamespace)
# 4.3. Phisically save the changes to the file.
with open(dirname + '\\' + javaFileClass, 'wb') as file:
file.write(fileData)
# 4.4. If this file itself belongs to another package, moves physically it's new location.
if javaClass in classNames.keys():
folder = dirname + '\\' + '\\'.join(classNames[javaClass]['namespace'])
if not os.path.exists(folder):
os.mkdir(folder)
os.rename(dirname + '\\' + javaFileClass, folder + '\\' + classNames[javaClass]['name'] + '.java')
# 5. Discard useless files and undo git changes
with open('swagger-discard.json', 'rb') as file:
discard = json.loads(file.read())
for folderToDiscard in discard['folders']:
shutil.rmtree(os.path.dirname(__file__) + folderToDiscard)
for fileToDiscard in discard['files']:
os.remove(os.path.dirname(__file__) + fileToDiscard)
for changeToDiscard in discard['discardChanges']:
os.system('git checkout ' + os.path.dirname(__file__) + changeToDiscard)
except:
# If something went wrong, all the unstaged changes.
os.system('git checkout .')