├── .gitignore ├── README.md ├── Spring_Generator.egg-info ├── PKG-INFO ├── SOURCES.txt ├── dependency_links.txt ├── entry_points.txt ├── requires.txt └── top_level.txt ├── build └── lib │ └── script │ ├── SpringCodeGeneratore.py │ ├── SpringCodeGeneratoreOldv.py │ └── __init__.py ├── dist └── Spring_Generator-1.0.0-py3.10.egg ├── script ├── SpringCodeGeneratore.py └── __init__.py └── setup.py /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Fouadbst01/SpringCodeGeneratore/f3cf6c6a6e6aebcf6bd6c51a83ce42efa1c9b59c/.gitignore -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Demonstration : 2 | 3 | https://user-images.githubusercontent.com/70094556/198661275-ffb9c4e5-73c4-41b9-bd17-a1ee464a91aa.mp4 4 | 5 | # Spring Boot Generator CLI 6 | 7 | ``` 8 | [+] AUTOR: Fouad El bssita 9 | [+] GITHUB: https://github.com/Fouadbst01 10 | ``` 11 | 12 | # Reaquirements : 13 | 14 | > Python 3.10 15 | 16 | # Getting the code : 17 | 18 | ``` 19 | git clone https://github.com/Fouadbst01/SpringCodeGeneratore.git 20 | cd SpringCodeGeneratore/ 21 | sudo python3 setup.py install 22 | ``` 23 | 24 | # Generate Spring Boot Project : 25 | 26 | ```console 27 | sg create name_of_project 28 | cd name_of_project 29 | ``` 30 | 31 | # Generate New entity : 32 | 33 | ```console 34 | sg entity entity_name 35 | ``` 36 | 37 | or 38 | 39 | ```console 40 | sg e entity_name 41 | ``` 42 | 43 | # Generate repository : 44 | 45 | ```console 46 | sg repositories 47 | ``` 48 | 49 | or 50 | 51 | ```console 52 | sg r 53 | ``` 54 | 55 | # Generate association : 56 | 57 | ```console 58 | sg association 59 | ``` 60 | 61 | or 62 | 63 | ```console 64 | sg a 65 | ``` 66 | 67 | # Generate Enumeration : 68 | 69 | ```console 70 | sg enum 71 | ``` 72 | 73 | # DTOs are generated automaticlly 74 | -------------------------------------------------------------------------------- /Spring_Generator.egg-info/PKG-INFO: -------------------------------------------------------------------------------- 1 | Metadata-Version: 2.1 2 | Name: Spring-Generator 3 | Version: 1.0.0 4 | Summary: Spring Boot CLI 5 | Author: EL BSSITA Fouad 6 | Author-email: Fouadelbssita@gmail.com 7 | 8 | generate Spring Project and code 9 | -------------------------------------------------------------------------------- /Spring_Generator.egg-info/SOURCES.txt: -------------------------------------------------------------------------------- 1 | README.md 2 | setup.py 3 | Spring_Generator.egg-info/PKG-INFO 4 | Spring_Generator.egg-info/SOURCES.txt 5 | Spring_Generator.egg-info/dependency_links.txt 6 | Spring_Generator.egg-info/entry_points.txt 7 | Spring_Generator.egg-info/requires.txt 8 | Spring_Generator.egg-info/top_level.txt 9 | script/SpringCodeGeneratore.py 10 | script/__init__.py -------------------------------------------------------------------------------- /Spring_Generator.egg-info/dependency_links.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Spring_Generator.egg-info/entry_points.txt: -------------------------------------------------------------------------------- 1 | [console_scripts] 2 | sg = script.SpringCodeGeneratore:main 3 | -------------------------------------------------------------------------------- /Spring_Generator.egg-info/requires.txt: -------------------------------------------------------------------------------- 1 | click 2 | questionary 3 | click_aliases 4 | -------------------------------------------------------------------------------- /Spring_Generator.egg-info/top_level.txt: -------------------------------------------------------------------------------- 1 | script 2 | -------------------------------------------------------------------------------- /build/lib/script/SpringCodeGeneratore.py: -------------------------------------------------------------------------------- 1 | from ast import alias 2 | from copyreg import constructor 3 | import click 4 | from click_aliases import ClickAliasedGroup 5 | import questionary 6 | from os import mkdir,getcwd,path,remove,scandir,walk 7 | import requests as req 8 | import zipfile 9 | 10 | class bcolors: 11 | HEADER = '\033[95m' 12 | OKBLUE = '\033[94m' 13 | OKCYAN = '\033[96m' 14 | OKGREEN = '\033[92m' 15 | WARNING = '\033[93m' 16 | FAIL = '\033[91m' 17 | ENDC = '\033[0m' 18 | BOLD = '\033[1m' 19 | UNDERLINE = '\033[4m' 20 | 21 | 22 | @click.group(cls=ClickAliasedGroup) 23 | def sg(): 24 | """ 25 | [+] AUTOR: Fouad El bssita\n 26 | [+] GITHUB: https://github.com/fouadbst01\n 27 | Spring Generator CLI 28 | """ 29 | pass 30 | 31 | @sg.command(aliases=['c']) 32 | @click.argument("project_name") 33 | def create(project_name): 34 | """create new Spring Boot project""" 35 | if(path.isdir(path.join(getcwd(),project_name))): 36 | raise SystemExit(f"{bcolors.FAIL}Eroor: Project name already exists") 37 | package_name = questionary.text( 38 | 'package name ? (com.exemple)',default='com.exemple' 39 | ).ask() 40 | 41 | artifact_id = questionary.text( 42 | f'artifact_id ? ({project_name})',default=project_name 43 | ).ask() 44 | 45 | packaging= questionary.select( 46 | 'Select packaging ? (JAR)', 47 | choices=[ 48 | "jar", 49 | "war", 50 | ], 51 | default="jar").ask() 52 | 53 | dbpack= questionary.select( 54 | 'Select pckages', 55 | choices=[ 56 | "mysql", 57 | "h2", 58 | #"MongoDB", 59 | ]).ask() 60 | 61 | db_name = questionary.text( 62 | 'database name ?' 63 | ).ask() 64 | 65 | 66 | depe= questionary.checkbox( 67 | 'select dependencies', 68 | choices=[ 69 | "lombok", 70 | "web", 71 | "data-jpa" 72 | ], 73 | ).ask() 74 | 75 | depe = ",".join(depe) 76 | 77 | package = depe+","+dbpack 78 | 79 | 80 | 81 | url = "https://start.spring.io/starter.zip?type=maven-project&language=java&bootVersion=2.7.5&baseDir="+project_name+"&groupId="+package_name+"&artifactId="+artifact_id+"&name="+project_name+"&packageName="+package_name+"."+project_name+"&packaging="+packaging+"&javaVersion=1.8&dependencies="+package 82 | 83 | file = None 84 | try : 85 | file = req.get(url, allow_redirects=True) 86 | except req.exceptions.HTTPError as errh: 87 | print (f"{bcolors.FAIL}Http Error:",errh) 88 | SystemExit(-1) 89 | except req.exceptions.ConnectionError as errc: 90 | print (f"{bcolors.FAIL}Error Connecting:",errc) 91 | SystemExit(-1) 92 | except req.exceptions.Timeout as errt: 93 | print (f"{bcolors.FAIL}Timeout Error:",errt) 94 | SystemExit(-1) 95 | except req.exceptions.RequestException as err: 96 | print (f"{bcolors.FAIL}OOps: Something Else",err) 97 | SystemExit(-1) 98 | 99 | if(file !=None): 100 | pack_dir_name = package_name.split('.') 101 | project_file_name = project_name+".zip" 102 | open(project_file_name, 'wb').write(file.content) 103 | with zipfile.ZipFile(project_file_name, 'r') as zip_ref: 104 | zip_ref.extractall() 105 | 106 | remove(path.join(getcwd(),project_file_name)) 107 | 108 | 109 | mypath=path.join(getcwd(),project_name,"src","main","java",*pack_dir_name,project_name) 110 | 111 | listdir = ["entities","dtos","enums","repositories","services","web","mappers"] 112 | for dir in listdir: 113 | mkdir(path.join(mypath,dir)) 114 | f= open( path.join(getcwd(),project_name,"src","main","resources","application.properties"),"w") 115 | if(dbpack == "mysql"): 116 | f.write("spring.datasource.url=jdbc:mysql://localhost:3306/"+db_name+"?createDatabaseIfNotExist=true\n") 117 | f.write("spring.datasource.username=root\n") 118 | f.write("spring.datasource.password=\n") 119 | #f.write("spring.jpa.show-sql=true") 120 | f.write("spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect\n") 121 | f.write("spring.jpa.hibernate.ddl-auto=create\n") 122 | f.write("server.port=8083") 123 | if(dbpack == "h2"): 124 | f.write("spring.datasource.url=jdbc:h2:mem:"+db_name) 125 | f.write("\nspring.h2.console.enabled=true") 126 | f.close() 127 | 128 | 129 | @sg.command(aliases=['e']) 130 | @click.argument("entity_name") 131 | def entity(entity_name): 132 | """generate entity classe""" 133 | my_path = getBasePath() 134 | entities_path = path.join(my_path,"entities") 135 | dtos_path = path.join(my_path,"dtos") 136 | 137 | entity_name = entity_name.capitalize() 138 | 139 | entity_file_path = path.join(entities_path,entity_name+".java") 140 | dto_file_path = path.join(dtos_path,entity_name+"DTO"+".java") 141 | 142 | if(path.exists(entity_file_path)): 143 | print (f"{bcolors.FAIL}Entity alerady existe !!") 144 | SystemExit(-1) 145 | 146 | entity_file = open(entity_file_path, "w") 147 | 148 | dto_file = open(dto_file_path,"w") 149 | 150 | package = getBase() 151 | 152 | dto_file.write( 153 | "package "+ package+".dtos;\n\n" 154 | +"import lombok.Data;\n" 155 | +"import java.util.Date;\n" 156 | +"\n\n@Data\n" 157 | +"public class "+entity_name+"DTO"+" {\n" 158 | ) 159 | 160 | entity_file.write("package "+package+".entities;\n\n"+"import lombok.AllArgsConstructor;\n" 161 | +"import lombok.Data;\n" 162 | +"import lombok.NoArgsConstructor;\n" 163 | +"import javax.persistence.Entity;\n" 164 | +"import javax.persistence.GeneratedValue;\n" 165 | +"import java.util.Date;\n" 166 | +"import javax.persistence.GenerationType;\n" 167 | +"import javax.persistence.Id;\n" 168 | +"import javax.persistence.*;\nimport java.util.List;" 169 | +"\n\n@Data\n"+ 170 | "@AllArgsConstructor\n@NoArgsConstructor\n"+ 171 | "@Entity\n"+ 172 | "public class "+entity_name+"{\n") 173 | 174 | while(1): 175 | filed_name = questionary.text( 176 | 'filed name : ' 177 | ).ask() 178 | 179 | if filed_name=="": 180 | break 181 | filed_type = questionary.select( 182 | 'fild type ?', 183 | choices=[ 184 | "id","String", "int","short","float","double","long","Date" 185 | ]).ask() 186 | 187 | if(filed_type =='id'): 188 | id_type = questionary.select( 189 | 'id type ?', 190 | 191 | choices=[ 192 | "long","String" 193 | ]).ask() 194 | 195 | entity_file.write("\t@Id") 196 | if(id_type=="long"): 197 | entity_file.write("@GeneratedValue(strategy = GenerationType.IDENTITY)\n") 198 | else: 199 | entity_file.write("\n") 200 | entity_file.write("\tprivate "+id_type+" "+filed_name+";\n") 201 | dto_file.write("\tprivate "+id_type+" "+filed_name+";\n") 202 | continue 203 | 204 | if(filed_type=='Date'): 205 | entity_file.write("\t@Temporal(TemporalType.DATE)\n") 206 | entity_file.write("\tprivate Date "+filed_name+";\n") 207 | dto_file.write("private Date "+filed_name+";\n") 208 | continue 209 | 210 | entity_file.write("\tprivate "+filed_type+" "+filed_name+";\n") 211 | dto_file.write("\tprivate "+filed_type+" "+filed_name+";\n") 212 | 213 | entity_file.write("}") 214 | dto_file.write("}") 215 | entity_file.close() 216 | dto_file.close() 217 | 218 | @sg.command(aliases=['r']) 219 | def repositories(): 220 | """generate repositories""" 221 | my_path = getBasePath() 222 | repositories_path = path.join(my_path,"repositories") 223 | entities_path = path.join(my_path,"entities") 224 | 225 | package = getBase() 226 | 227 | for subdir, dirs, files in walk(entities_path): 228 | for file in files: 229 | entity_name=file.partition('.')[0] 230 | f = open(path.join(entities_path,file), 'r') 231 | idType="" 232 | flag=False 233 | while True: 234 | str=f.readline() 235 | if not str: 236 | break 237 | if('@Id' in str): 238 | flag=True 239 | idType=f.readline().split(" ")[1] 240 | break 241 | if(flag and not path.exists(path.join(repositories_path,entity_name+"Repository.java"))): 242 | f = open(path.join(repositories_path,entity_name+"Repository.java"), "w") 243 | f.write("package "+package+".repositories;\n\n"+ 244 | "import "+package+".entities."+entity_name+";\n" 245 | +"import org.springframework.data.jpa.repository.JpaRepository;\n" 246 | +"import org.springframework.stereotype.Repository;\n\n" 247 | +"@Repository\n" 248 | +"public interface "+entity_name+"Repository extends JpaRepository<" 249 | +entity_name 250 | +","+idType+"> {\n}") 251 | print(f"{bcolors.OKGREEN}generate "+entity_name+"Repository.") 252 | else: 253 | print(f"{bcolors.FAIL}Faild to generate "+entity_name+"Repository : id dosent existe") 254 | f.close() 255 | 256 | 257 | @sg.command() 258 | @click.argument("enum_name") 259 | def enum(enum_name): 260 | """generate enumeration class""" 261 | enum_name = enum_name.capitalize() 262 | str="public enum "+enum_name.capitalize()+" {\n"; 263 | values = [] 264 | while(1): 265 | value = questionary.text( 266 | 'Enter Value : ' 267 | ).ask() 268 | 269 | if value==None or value=="": 270 | break 271 | 272 | values.append(value.upper()) 273 | 274 | str=str+",".join(values)+"\n}" 275 | 276 | my_path = getBasePath() 277 | repositories_path = path.join(my_path,"enums") 278 | 279 | with open(path.join(repositories_path,enum_name+".java"),"w") as f: 280 | f.write(str) 281 | f.close() 282 | 283 | @sg.command(aliases=['a']) 284 | def association(): 285 | """generate association between tow classes""" 286 | my_path = getBasePath() 287 | 288 | entities_path = path.join(my_path,"entities") 289 | list_entity=[] 290 | for subdir, dirs, files in walk(entities_path): 291 | for file in files: 292 | list_entity.append(file.split(".")[0]) 293 | 294 | 295 | first_entity = questionary.select( 296 | 'first entity :', 297 | choices= 298 | list_entity 299 | ).ask() 300 | 301 | association_type = questionary.select( 302 | 'first entity :', 303 | choices=["ManyToMany","ManyToOne","OneToOne","OneToMany"]).ask() 304 | 305 | 306 | second_entity = questionary.select( 307 | 'second entity', 308 | choices= 309 | list_entity 310 | ).ask() 311 | 312 | AddDependency(association_type,first_entity,second_entity) 313 | 314 | def AddDependency(association_type,entity1,entity2): 315 | match association_type: 316 | case "OneToMany": 317 | dto1="\t"+"List<"+entity2+"DTO>"+" list"+entity2+"DTO;\n" 318 | str1='\t@OneToMany(mappedBy = "'+entity1.lower()+'",fetch = FetchType.LAZY)\n'+"\t"+"List<"+entity2+">"+" list"+entity2+";\n" 319 | dto2="\t"+entity1+"DTO "+entity1.lower()+"DTO;\n" 320 | str2="\t@ManyToOne\n"+"\t"+entity1+" "+entity1.lower()+";\n" 321 | write(str1,entity1,dto1) 322 | write(str2,entity2,dto2) 323 | case "ManyToOne": 324 | dto2="\t"+"List<"+entity1+"DTO>"+" list"+entity1+"DTO;\n" 325 | str2='\t@OneToMany(mappedBy = "'+entity2.lower()+'",fetch = FetchType.LAZY)\n'+"\t"+"List<"+entity1+">"+"list"+entity1+";\n" 326 | dto1="\t"+entity2+"DTO "+" "+entity2.lower()+"DTO;\n" 327 | str1="\t@ManyToOne\n\t"+entity2+" "+entity2.lower()+";\n" 328 | write(str1,entity1,dto1) 329 | write(str2,entity2,dto2) 330 | case "OneToOne": 331 | dto1="\t"+entity2+"DTO "+entity2.lower()+"DTO"+";\n" 332 | str1="\t@OneToOne\n"+"\t"+entity2+" "+entity2.lower()+";\n" 333 | dto2="\t"+entity1+"DTO "+entity1.lower()+"DTO"+";\n" 334 | str2="\t@OneToOne\n"+"\t"+entity1+" "+entity1.lower()+";\n" 335 | write(str1,entity1,dto1) 336 | write(str2,entity2,dto2) 337 | case "ManyToMany": 338 | dto1="\tList<"+entity1+"DTO> list"+entity1+"DTO"+";\n" 339 | str1="\t@ManyToMany\n"+"\t"+"List<"+entity1+"> list"+entity1+";\n" 340 | dto2="\tList<"+entity2+"DTO> list"+entity2+"DTO"+";\n" 341 | str2="\t@ManyToMany\n"+"\t"+"List<"+entity2+"> list"+entity2+";\n" 342 | write(str1,entity1,dto1) 343 | write(str2,entity2,dto2) 344 | 345 | def write(str,entity,dto): 346 | my_path = getBasePath() 347 | entities_path = path.join(my_path,"entities") 348 | dtos_path = path.join(my_path,"dtos") 349 | write_in_end(path.join(entities_path,entity),str) 350 | write_in_end(path.join(dtos_path,entity+"DTO"),dto,True) 351 | 352 | 353 | def write_in_end(path,str,flag=False): 354 | 355 | with open(path+".java", 'r+') as file: 356 | lines = file.readlines() 357 | file.seek(0,0) 358 | for i in range(len(lines)): 359 | if(i==2 and flag): 360 | file.write("\nimport java.util.List;\n") 361 | if(i==len(lines)-1): 362 | file.write(str) 363 | file.write(lines[i]) 364 | file.close() 365 | 366 | def getBasePath(): 367 | 368 | dir_name = path.basename(getcwd()) 369 | 370 | my_path = path.join(getcwd(),"src","main","java") 371 | if(path.exists(my_path)): 372 | while True: 373 | list = [f.path for f in scandir(my_path) if f.is_dir()] 374 | tmp_dir_name = path.basename(list[0]) 375 | my_path = path.join(my_path,tmp_dir_name) 376 | if(tmp_dir_name == dir_name): 377 | break; 378 | return my_path; 379 | else: 380 | print (f"{bcolors.FAIL}OOps: access to project folder to execute this commande 'cd MyProgect'") 381 | SystemExit(-1) 382 | 383 | 384 | def getBase(): 385 | 386 | dir_name = path.basename(getcwd()) 387 | 388 | my_path = path.join(getcwd(),"src","main","java") 389 | 390 | package_list = [] 391 | if(path.exists(my_path)): 392 | while True: 393 | list = [f.path for f in scandir(my_path) if f.is_dir()] 394 | tmp_dir_name = path.basename(list[0]) 395 | package_list.append(tmp_dir_name) 396 | my_path = path.join(my_path,tmp_dir_name) 397 | if(tmp_dir_name == dir_name): 398 | break; 399 | return ".".join(package_list); 400 | else: 401 | print (f"{bcolors.FAIL}OOps: access to project folder to execute this commande 'cd MyProgect'") 402 | SystemExit(-1) 403 | 404 | 405 | 406 | def main(): 407 | sg() 408 | -------------------------------------------------------------------------------- /build/lib/script/SpringCodeGeneratoreOldv.py: -------------------------------------------------------------------------------- 1 | import curses 2 | from struct import pack 3 | import sys 4 | import zipfile 5 | 6 | from pyparsing import Char 7 | from os.path import exists 8 | from os import mkdir, getcwd, walk, scandir,remove 9 | import platform 10 | import requests as req 11 | 12 | 13 | class1 = ["id","String", "int", "double","Long","Date"] 14 | class2 = ["String","Long"] 15 | class3 = ["ManyToMany","ManyToOne","OneToOne","OneToMany"] 16 | 17 | classes =class1 18 | 19 | mypath="" 20 | 21 | def choseType(stdscr): 22 | attributes = {} 23 | curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK) 24 | attributes['normal'] = curses.color_pair(1) 25 | 26 | curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_WHITE) 27 | attributes['highlighted'] = curses.color_pair(2) 28 | 29 | c = 0 30 | option = 0 31 | while c != 10: 32 | stdscr.erase() 33 | for i in range(len(classes)): 34 | if i == option: 35 | attr = attributes['highlighted'] 36 | else: 37 | attr = attributes['normal'] 38 | stdscr.addstr("> ") 39 | stdscr.addstr(classes[i] + '\n', attr) 40 | c = stdscr.getch() 41 | if c == curses.KEY_UP and option > 0: 42 | option -= 1 43 | elif c == curses.KEY_DOWN and option < len(classes) - 1: 44 | option += 1 45 | 46 | stdscr.addstr(classes[option]) 47 | return classes[option]; 48 | 49 | def start(args): 50 | match args: 51 | case 'c': 52 | return addEntity(sys.argv[2]) 53 | case 'r': 54 | return GenerateRepositorise() 55 | case 'i': 56 | return init(sys.argv[2]) 57 | case 'a': 58 | return addAssociation() 59 | case 'e': 60 | return generateEnum(sys.argv[2]) 61 | 62 | def basePackage(arg): 63 | sep=separator() 64 | cont=1 65 | if(arg==""): 66 | mpath = getcwd()+sep+"src"+sep+"main"+sep+"java"+sep 67 | else: 68 | mpath = getcwd()+sep+arg+sep+"src"+sep+"main"+sep+"java"+sep 69 | flag=True 70 | while flag: 71 | list = [f.path for f in scandir(mpath) if f.is_dir()] 72 | if(cont<4 and len(list)!=0): 73 | mpath=list[0] 74 | else: 75 | flag=False 76 | cont=cont+1 77 | return mpath 78 | 79 | def addEntity(name): 80 | global mypath 81 | mypath = basePackage("") 82 | sepa=separator() 83 | list= mypath.split(sepa) 84 | pname=list[len(list)-1] 85 | if(exists(name+".java")): 86 | print("Entity alerady existe !!") 87 | return 88 | if(not exists(mypath+sepa+"entities")): 89 | mkdir(mypath+sepa+"entities") 90 | print(mypath) 91 | f = open(mypath+sepa+"entities"+sepa+name+".java", "w") 92 | 93 | fdto = open(mypath+sepa+"dtos"+sepa+name+"DTO"+".java","w") 94 | 95 | fdto.write( 96 | "package com.enset."+pname+".dtos;\n\n" 97 | +"import lombok.Data;\n" 98 | +"import java.util.Date;\n" 99 | +"\n\n@Data\n" 100 | +"public class "+name+"DTO {\n" 101 | ) 102 | f.write("package com.enset."+pname+".entities;\n\n"+"import lombok.AllArgsConstructor;\n" 103 | +"import lombok.Data;\n" 104 | +"import lombok.NoArgsConstructor;\n" 105 | +"import javax.persistence.Entity;\n" 106 | +"import javax.persistence.GeneratedValue;\n" 107 | +"import java.util.Date;\n" 108 | +"import javax.persistence.GenerationType;\n" 109 | +"import javax.persistence.Id;\n" 110 | +"import javax.persistence.*;\n import java.util.List;" 111 | +"\n\n@Data\n"+ 112 | "@AllArgsConstructor\n@NoArgsConstructor\n"+ 113 | "@Entity\n"+ 114 | "public class "+name+"{\n") 115 | while(1): 116 | print("filed name > ",end="") 117 | filedname= input() 118 | if filedname=="": 119 | break 120 | print("fild type > ",end="") 121 | filedtype=curses.wrapper(choseType) 122 | if(filedtype=='id'): 123 | global classes 124 | classes=class2 125 | print("id type :") 126 | idType= curses.wrapper(choseType) 127 | f.write("\t@Id") 128 | if(idType=="Long"): 129 | f.write("@GeneratedValue(strategy = GenerationType.IDENTITY)\n") 130 | else: 131 | f.write("\n") 132 | f.write("\tprivate "+idType+" "+filedname+";\n") 133 | fdto.write("\tprivate "+idType+" "+filedname+";\n") 134 | classes=class1 135 | continue 136 | if(filedtype=='Date'): 137 | f.write("\t@Temporal(TemporalType.DATE)\n") 138 | f.write("\tprivate Date "+filedname+";\n") 139 | fdto.write("private Date "+filedname+";\n") 140 | continue 141 | print(filedtype) 142 | f.write("\tprivate "+filedtype+" "+filedname+";\n") 143 | fdto.write("\tprivate "+filedtype+" "+filedname+";\n") 144 | f.write("}") 145 | fdto.write("}") 146 | f.close() 147 | fdto.close() 148 | addMapper(name,name+"DTO") 149 | 150 | def generateProjectStructure(): 151 | sepa=separator() 152 | try: 153 | if(not exists(mypath+sepa+"entities")): 154 | print(mypath+sepa+"entities") 155 | mkdir(mypath+sepa+"entities") 156 | if(not exists(mypath+sepa+"dtos")): 157 | mkdir(mypath+sepa+"dtos") 158 | if(not exists(mypath+sepa+"enums")): 159 | mkdir(mypath+sepa+"enums") 160 | if(not exists(mypath+sepa+"repositories")): 161 | mkdir(mypath+sepa+"repositories") 162 | if(not exists(mypath+sepa+"services")): 163 | mkdir(mypath+sepa+"services") 164 | if(not exists(mypath+sepa+"web")): 165 | mkdir(mypath+sepa+"web") 166 | if(not exists(mypath+sepa+"dtos")): 167 | mkdir(mypath+sepa+"dtos") 168 | if(not exists(mypath+sepa+"mappers")): 169 | mkdir(mypath+sepa+"mappers") 170 | with open(mypath+sepa+"mappers"+sepa+"MapperImpl.java","w") as f: 171 | list= mypath.split(sepa) 172 | pname=list[len(list)-1] 173 | f.write("package com.enset."+pname+".mappers;\n\n" 174 | +"import org.springframework.beans.BeanUtils;\nimport org.springframework.stereotype.Service;\n" 175 | +"@Service\npublic class MapperImpl{\n}") 176 | f.close() 177 | except OSError as err: 178 | print("alerady generated !!") 179 | 180 | def GenerateRepositorise(): 181 | mypath = basePackage("") 182 | sepa=separator() 183 | list= mypath.split(sepa) 184 | pname=list[len(list)-1] 185 | for subdir, dirs, files in walk(mypath+sepa+"entities"): 186 | for file in files: 187 | entityName=file.partition('.')[0] 188 | f = open(mypath+sepa+'entities'+sepa+entityName+".java", 'r') 189 | idType="" 190 | flag=False 191 | while True: 192 | str=f.readline() 193 | if('@Id' in str): 194 | flag=True 195 | idType=f.readline().split(" ")[1] 196 | break 197 | if(flag and not exists(mypath+sepa+"repositories"+sepa+entityName+"Repository.java")): 198 | f = open(mypath+sepa+"repositories"+sepa+entityName+"Repository.java", "w") 199 | f.write("package com.enset."+pname+".repositories;\n\n"+ 200 | "import com.enset."+pname+".entities."+entityName+";\n" 201 | +"import org.springframework.data.jpa.repository.JpaRepository;\n" 202 | +"import org.springframework.stereotype.Repository;\n\n" 203 | +"@Repository\n" 204 | +"public interface "+entityName+"Repository extends JpaRepository<" 205 | +entityName 206 | +","+idType+"> {\n}") 207 | else: 208 | print("id dosent existe") 209 | f.close() 210 | 211 | def init(arg): 212 | sepa=separator() 213 | global mypath 214 | 215 | 216 | file = req.get(url, allow_redirects=True) 217 | open(arg+".zip", 'wb').write(file.content) 218 | with zipfile.ZipFile(arg+".zip", 'r') as zip_ref: 219 | zip_ref.extractall() 220 | remove(arg+".zip") 221 | mypath = basePackage(arg) 222 | print("enter DB name > ") 223 | dbname=input() 224 | f= open("."+sepa+arg+sepa+"src"+sepa+"main"+sepa+"resources"+sepa+"application.properties","w") 225 | f.write("spring.datasource.url=jdbc:mysql://localhost:3306/"+dbname+"?createDatabaseIfNotExist=true\n") 226 | f.write("spring.datasource.username=root\n") 227 | f.write("spring.datasource.password=\n") 228 | #f.write("spring.jpa.show-sql=true") 229 | f.write("spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect\n") 230 | f.write("spring.jpa.hibernate.ddl-auto=create\n") 231 | f.write("server.port=8083") 232 | f.close() 233 | generateProjectStructure() 234 | 235 | def addAssociation(): 236 | sepa=separator() 237 | mypath = basePackage("") 238 | 239 | mypath=mypath+sepa+"entities" 240 | global classes 241 | listE=list() 242 | for subdir, dirs, files in walk(mypath): 243 | for file in files: 244 | listE.append(file.split(".")[0]) 245 | 246 | print("chose first entity > ",end="") 247 | classes=listE 248 | entityOne=curses.wrapper(choseType) 249 | print(entityOne) 250 | classes=class3 251 | chosen=curses.wrapper(choseType) 252 | classes=listE 253 | print("chose first entity > ",end="") 254 | entityTow=curses.wrapper(choseType) 255 | print(entityTow) 256 | AddDependency(chosen,entityOne,entityTow) 257 | 258 | 259 | def AddDependency(chosen,entity1,entity2): 260 | match chosen: 261 | case "OneToMany": 262 | dto1="\t"+"List<"+entity2+"DTO>"+" list"+entity2+"DTO;\n" 263 | str1='\t@OneToMany(mappedBy = "'+entity1.lower()+'",fetch = FetchType.LAZY)\n'+"\t"+"List<"+entity2+">"+" list"+entity2+";\n" 264 | dto2="\t"+entity1+"DTO "+entity1.lower()+"DTO;\n" 265 | str2="\t@ManyToOne\n"+"\t"+entity1+" "+entity1.lower()+";\n" 266 | write(str1,entity1,dto1) 267 | write(str2,entity2,dto2) 268 | case "ManyToOne": 269 | dto2="\t"+"List<"+entity1+"DTO>"+" list"+entity1+"DTO;\n" 270 | str2='\t@OneToMany(mappedBy = "'+entity2.lower()+'",fetch = FetchType.LAZY)\n'+"\t"+"List<"+entity1+">"+"list"+entity1+";\n" 271 | dto1="\t"+entity2+"DTO "+" "+entity2.lower()+"DTO;\n" 272 | str1="\t@ManyToOne\n\t"+entity2+" "+entity2.lower()+";\n" 273 | write(str1,entity1,dto1) 274 | write(str2,entity2,dto2) 275 | case "OneToOne": 276 | dto1="\t"+entity2+"DTO "+entity2.lower()+"DTO"+";\n" 277 | str1="\t@OneToOne\n"+"\t"+entity2+" "+entity2.lower()+";\n" 278 | dto2="\t"+entity1+"DTO "+entity1.lower()+"DTO"+";\n" 279 | str2="\t@OneToOne\n"+"\t"+entity1+" "+entity1.lower()+";\n" 280 | write(str1,entity1,dto1) 281 | write(str2,entity2,dto2) 282 | case "ManyToMany": 283 | dto1="\tList<"+entity1+"DTO> list"+entity1+"DTO"+";\n" 284 | str1="\t@ManyToMany\n"+"\t"+"List<"+entity1+"> list"+entity1+";\n" 285 | dto2="\tList<"+entity2+"DTO> list"+entity2+"DTO"+";\n" 286 | str2="\t@ManyToMany\n"+"\t"+"List<"+entity2+"> list"+entity2+";\n" 287 | write(str1,entity1,dto1) 288 | write(str2,entity2,dto2) 289 | 290 | def write(str,entity,dto): 291 | mypath = basePackage("") 292 | sepa=separator() 293 | list= mypath.split(sepa) 294 | pname=list[len(list)-1] 295 | write_in_end(mypath+sepa+"entities",entity,str) 296 | write_in_end(mypath+sepa+"dtos",entity+"DTO",dto,True) 297 | 298 | 299 | def write_in_end(path,entity,str,flag=False): 300 | sepa=separator() 301 | with open(path+sepa+entity+".java", 'r+') as file: 302 | lines = file.readlines() 303 | file.seek(0,0) 304 | for i in range(len(lines)): 305 | if(i==2 and flag): 306 | file.write("\nimport java.util.List;\n") 307 | if(i==len(lines)-1): 308 | file.write(str) 309 | file.write(lines[i]) 310 | file.close() 311 | def separator(): 312 | if(platform.system()=='Windows'): 313 | return '\\' 314 | else: 315 | return '/' 316 | def addMapper(entity1,entity2): 317 | sepa=separator() 318 | mypath=basePackage("") 319 | list= mypath.split(sepa) 320 | pname=list[len(list)-1] 321 | str="\tpublic "+entity2+" from"+entity1+"("+entity1+" "+entity1.lower()+"){\n\t\t"\ 322 | +entity2+" "+entity2.lower()+" = new "+entity2+"();" \ 323 | +"\n\t\tBeanUtils.copyProperties("+entity1.lower()+", "+entity2.lower()+");" \ 324 | "\n\t\treturn "+entity2.lower()+";\n\t}\n" 325 | str2="\tpublic "+entity1+" from"+entity2+"("+entity2+" "+entity2.lower()+"){\n \t\t"\ 326 | +entity1+" "+entity1.lower()+" = new "+entity1+"();" \ 327 | +"\n\t\tBeanUtils.copyProperties("+entity2.lower()+", "+entity1.lower()+");" \ 328 | "\n\t\treturn "+entity1.lower()+";\n\t}\n" 329 | with open(mypath+sepa+"mappers"+sepa+"MapperImpl.java","r+") as f: 330 | flag=False 331 | text = f.readlines() 332 | f.seek(0,0) 333 | for i in range(len(text)): 334 | if(i==1): 335 | f.write("import com.enset."+pname+".entities."+entity1+";\n") 336 | f.write("import com.enset."+pname+".dtos."+entity2+";\n") 337 | if(flag): 338 | f.write(str) 339 | f.write(str2) 340 | flag=False 341 | if("class" in text[i]): 342 | flag=True 343 | f.write(text[i]) 344 | f.close() 345 | 346 | def generateEnum(arg): 347 | str="public enum "+arg+" {\n"; 348 | str2="" 349 | while(1): 350 | print("Enter Value1 >",end="") 351 | varName=input() 352 | if varName=="": 353 | break 354 | str2=str2+varName+" ," 355 | str2=str2[0:len(str)-1] 356 | str=str+str2+"\n}" 357 | sepa=separator() 358 | with open(mypath+sepa+"enums"+sepa+arg+".java","w") as f: 359 | f.write(str) 360 | f.close() 361 | 362 | def main(): 363 | global arg 364 | arg = Char(sys.argv[1]) 365 | start(arg) 366 | 367 | if __name__ == "__main__": 368 | main() 369 | 370 | -------------------------------------------------------------------------------- /build/lib/script/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Fouadbst01/SpringCodeGeneratore/f3cf6c6a6e6aebcf6bd6c51a83ce42efa1c9b59c/build/lib/script/__init__.py -------------------------------------------------------------------------------- /dist/Spring_Generator-1.0.0-py3.10.egg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Fouadbst01/SpringCodeGeneratore/f3cf6c6a6e6aebcf6bd6c51a83ce42efa1c9b59c/dist/Spring_Generator-1.0.0-py3.10.egg -------------------------------------------------------------------------------- /script/SpringCodeGeneratore.py: -------------------------------------------------------------------------------- 1 | from ast import alias 2 | from copyreg import constructor 3 | import click 4 | from click_aliases import ClickAliasedGroup 5 | import questionary 6 | from os import mkdir,getcwd,path,remove,scandir,walk 7 | import requests as req 8 | import zipfile 9 | 10 | class bcolors: 11 | HEADER = '\033[95m' 12 | OKBLUE = '\033[94m' 13 | OKCYAN = '\033[96m' 14 | OKGREEN = '\033[92m' 15 | WARNING = '\033[93m' 16 | FAIL = '\033[91m' 17 | ENDC = '\033[0m' 18 | BOLD = '\033[1m' 19 | UNDERLINE = '\033[4m' 20 | 21 | 22 | @click.group(cls=ClickAliasedGroup) 23 | def sg(): 24 | """ 25 | [+] AUTOR: Fouad El bssita\n 26 | [+] GITHUB: https://github.com/fouadbst01\n 27 | Spring Generator CLI 28 | """ 29 | pass 30 | 31 | @sg.command(aliases=['c']) 32 | @click.argument("project_name") 33 | def create(project_name): 34 | """create new Spring Boot project""" 35 | if(path.isdir(path.join(getcwd(),project_name))): 36 | raise SystemExit(f"{bcolors.FAIL}Eroor: Project name already exists") 37 | package_name = questionary.text( 38 | 'package name ? (com.exemple)',default='com.exemple' 39 | ).ask() 40 | 41 | artifact_id = questionary.text( 42 | f'artifact_id ? ({project_name})',default=project_name 43 | ).ask() 44 | 45 | packaging= questionary.select( 46 | 'Select packaging ? (JAR)', 47 | choices=[ 48 | "jar", 49 | "war", 50 | ], 51 | default="jar").ask() 52 | 53 | dbpack= questionary.select( 54 | 'Select pckages', 55 | choices=[ 56 | "mysql", 57 | "h2", 58 | #"MongoDB", 59 | ]).ask() 60 | 61 | db_name = questionary.text( 62 | 'database name ?' 63 | ).ask() 64 | 65 | 66 | depe= questionary.checkbox( 67 | 'select dependencies', 68 | choices=[ 69 | "lombok", 70 | "web", 71 | "data-jpa" 72 | ], 73 | ).ask() 74 | 75 | depe = ",".join(depe) 76 | 77 | package = depe+","+dbpack 78 | 79 | 80 | 81 | url = "https://start.spring.io/starter.zip?type=maven-project&language=java&bootVersion=2.7.5&baseDir="+project_name+"&groupId="+package_name+"&artifactId="+artifact_id+"&name="+project_name+"&packageName="+package_name+"."+project_name+"&packaging="+packaging+"&javaVersion=1.8&dependencies="+package 82 | 83 | file = None 84 | try : 85 | file = req.get(url, allow_redirects=True) 86 | except req.exceptions.HTTPError as errh: 87 | print (f"{bcolors.FAIL}Http Error:",errh) 88 | SystemExit(-1) 89 | except req.exceptions.ConnectionError as errc: 90 | print (f"{bcolors.FAIL}Error Connecting:",errc) 91 | SystemExit(-1) 92 | except req.exceptions.Timeout as errt: 93 | print (f"{bcolors.FAIL}Timeout Error:",errt) 94 | SystemExit(-1) 95 | except req.exceptions.RequestException as err: 96 | print (f"{bcolors.FAIL}OOps: Something Else",err) 97 | SystemExit(-1) 98 | 99 | if(file !=None): 100 | pack_dir_name = package_name.split('.') 101 | project_file_name = project_name+".zip" 102 | open(project_file_name, 'wb').write(file.content) 103 | with zipfile.ZipFile(project_file_name, 'r') as zip_ref: 104 | zip_ref.extractall() 105 | 106 | remove(path.join(getcwd(),project_file_name)) 107 | 108 | 109 | mypath=path.join(getcwd(),project_name,"src","main","java",*pack_dir_name,project_name) 110 | 111 | listdir = ["entities","dtos","enums","repositories","services","web","mappers"] 112 | for dir in listdir: 113 | mkdir(path.join(mypath,dir)) 114 | f= open( path.join(getcwd(),project_name,"src","main","resources","application.properties"),"w") 115 | if(dbpack == "mysql"): 116 | f.write("spring.datasource.url=jdbc:mysql://localhost:3306/"+db_name+"?createDatabaseIfNotExist=true\n") 117 | f.write("spring.datasource.username=root\n") 118 | f.write("spring.datasource.password=\n") 119 | #f.write("spring.jpa.show-sql=true") 120 | f.write("spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect\n") 121 | f.write("spring.jpa.hibernate.ddl-auto=create\n") 122 | f.write("server.port=8083") 123 | if(dbpack == "h2"): 124 | f.write("spring.datasource.url=jdbc:h2:mem:"+db_name) 125 | f.write("\nspring.h2.console.enabled=true") 126 | f.close() 127 | 128 | 129 | @sg.command(aliases=['e']) 130 | @click.argument("entity_name") 131 | def entity(entity_name): 132 | """generate entity classe""" 133 | my_path = getBasePath() 134 | entities_path = path.join(my_path,"entities") 135 | dtos_path = path.join(my_path,"dtos") 136 | 137 | entity_name = entity_name.capitalize() 138 | 139 | entity_file_path = path.join(entities_path,entity_name+".java") 140 | dto_file_path = path.join(dtos_path,entity_name+"DTO"+".java") 141 | 142 | if(path.exists(entity_file_path)): 143 | print (f"{bcolors.FAIL}Entity alerady existe !!") 144 | SystemExit(-1) 145 | 146 | entity_file = open(entity_file_path, "w") 147 | 148 | dto_file = open(dto_file_path,"w") 149 | 150 | package = getBase() 151 | 152 | dto_file.write( 153 | "package "+ package+".dtos;\n\n" 154 | +"import lombok.Data;\n" 155 | +"import java.util.Date;\n" 156 | +"\n\n@Data\n" 157 | +"public class "+entity_name+"DTO"+" {\n" 158 | ) 159 | 160 | entity_file.write("package "+package+".entities;\n\n"+"import lombok.AllArgsConstructor;\n" 161 | +"import lombok.Data;\n" 162 | +"import lombok.NoArgsConstructor;\n" 163 | +"import javax.persistence.Entity;\n" 164 | +"import javax.persistence.GeneratedValue;\n" 165 | +"import java.util.Date;\n" 166 | +"import javax.persistence.GenerationType;\n" 167 | +"import javax.persistence.Id;\n" 168 | +"import javax.persistence.*;\nimport java.util.List;" 169 | +"\n\n@Data\n"+ 170 | "@AllArgsConstructor\n@NoArgsConstructor\n"+ 171 | "@Entity\n"+ 172 | "public class "+entity_name+"{\n") 173 | 174 | while(1): 175 | filed_name = questionary.text( 176 | 'filed name : ' 177 | ).ask() 178 | 179 | if filed_name=="": 180 | break 181 | filed_type = questionary.select( 182 | 'fild type ?', 183 | choices=[ 184 | "id","String", "int","short","float","double","long","Date" 185 | ]).ask() 186 | 187 | if(filed_type =='id'): 188 | id_type = questionary.select( 189 | 'id type ?', 190 | 191 | choices=[ 192 | "long","String" 193 | ]).ask() 194 | 195 | entity_file.write("\t@Id") 196 | if(id_type=="long"): 197 | entity_file.write("@GeneratedValue(strategy = GenerationType.IDENTITY)\n") 198 | else: 199 | entity_file.write("\n") 200 | entity_file.write("\tprivate "+id_type+" "+filed_name+";\n") 201 | dto_file.write("\tprivate "+id_type+" "+filed_name+";\n") 202 | continue 203 | 204 | if(filed_type=='Date'): 205 | entity_file.write("\t@Temporal(TemporalType.DATE)\n") 206 | entity_file.write("\tprivate Date "+filed_name+";\n") 207 | dto_file.write("private Date "+filed_name+";\n") 208 | continue 209 | 210 | entity_file.write("\tprivate "+filed_type+" "+filed_name+";\n") 211 | dto_file.write("\tprivate "+filed_type+" "+filed_name+";\n") 212 | 213 | entity_file.write("}") 214 | dto_file.write("}") 215 | entity_file.close() 216 | dto_file.close() 217 | 218 | @sg.command(aliases=['r']) 219 | def repositories(): 220 | """generate repositories""" 221 | my_path = getBasePath() 222 | repositories_path = path.join(my_path,"repositories") 223 | entities_path = path.join(my_path,"entities") 224 | 225 | package = getBase() 226 | 227 | for subdir, dirs, files in walk(entities_path): 228 | for file in files: 229 | entity_name=file.partition('.')[0] 230 | f = open(path.join(entities_path,file), 'r') 231 | idType="" 232 | flag=False 233 | while True: 234 | str=f.readline() 235 | if not str: 236 | break 237 | if('@Id' in str): 238 | flag=True 239 | idType=f.readline().split(" ")[1] 240 | break 241 | if(flag and not path.exists(path.join(repositories_path,entity_name+"Repository.java"))): 242 | f = open(path.join(repositories_path,entity_name+"Repository.java"), "w") 243 | f.write("package "+package+".repositories;\n\n"+ 244 | "import "+package+".entities."+entity_name+";\n" 245 | +"import org.springframework.data.jpa.repository.JpaRepository;\n" 246 | +"import org.springframework.stereotype.Repository;\n\n" 247 | +"@Repository\n" 248 | +"public interface "+entity_name+"Repository extends JpaRepository<" 249 | +entity_name 250 | +","+idType+"> {\n}") 251 | print(f"{bcolors.OKGREEN}generate "+entity_name+"Repository.") 252 | else: 253 | print(f"{bcolors.FAIL}Faild to generate "+entity_name+"Repository : id dosent existe") 254 | f.close() 255 | 256 | 257 | @sg.command() 258 | @click.argument("enum_name") 259 | def enum(enum_name): 260 | """generate enumeration class""" 261 | enum_name = enum_name.capitalize() 262 | str="public enum "+enum_name.capitalize()+" {\n"; 263 | values = [] 264 | while(1): 265 | value = questionary.text( 266 | 'Enter Value : ' 267 | ).ask() 268 | 269 | if value==None or value=="": 270 | break 271 | 272 | values.append(value.upper()) 273 | 274 | str=str+",".join(values)+"\n}" 275 | 276 | my_path = getBasePath() 277 | repositories_path = path.join(my_path,"enums") 278 | 279 | with open(path.join(repositories_path,enum_name+".java"),"w") as f: 280 | f.write(str) 281 | f.close() 282 | 283 | @sg.command(aliases=['a']) 284 | def association(): 285 | """generate association between tow classes""" 286 | my_path = getBasePath() 287 | 288 | entities_path = path.join(my_path,"entities") 289 | list_entity=[] 290 | for subdir, dirs, files in walk(entities_path): 291 | for file in files: 292 | list_entity.append(file.split(".")[0]) 293 | 294 | 295 | first_entity = questionary.select( 296 | 'first entity :', 297 | choices= 298 | list_entity 299 | ).ask() 300 | 301 | association_type = questionary.select( 302 | 'first entity :', 303 | choices=["ManyToMany","ManyToOne","OneToOne","OneToMany"]).ask() 304 | 305 | 306 | second_entity = questionary.select( 307 | 'second entity', 308 | choices= 309 | list_entity 310 | ).ask() 311 | 312 | AddDependency(association_type,first_entity,second_entity) 313 | 314 | def AddDependency(association_type,entity1,entity2): 315 | match association_type: 316 | case "OneToMany": 317 | dto1="\t"+"List<"+entity2+"DTO>"+" list"+entity2+"DTO;\n" 318 | str1='\t@OneToMany(mappedBy = "'+entity1.lower()+'",fetch = FetchType.LAZY)\n'+"\t"+"List<"+entity2+">"+" list"+entity2+";\n" 319 | dto2="\t"+entity1+"DTO "+entity1.lower()+"DTO;\n" 320 | str2="\t@ManyToOne\n"+"\t"+entity1+" "+entity1.lower()+";\n" 321 | write(str1,entity1,dto1) 322 | write(str2,entity2,dto2) 323 | case "ManyToOne": 324 | dto2="\t"+"List<"+entity1+"DTO>"+" list"+entity1+"DTO;\n" 325 | str2='\t@OneToMany(mappedBy = "'+entity2.lower()+'",fetch = FetchType.LAZY)\n'+"\t"+"List<"+entity1+">"+"list"+entity1+";\n" 326 | dto1="\t"+entity2+"DTO "+" "+entity2.lower()+"DTO;\n" 327 | str1="\t@ManyToOne\n\t"+entity2+" "+entity2.lower()+";\n" 328 | write(str1,entity1,dto1) 329 | write(str2,entity2,dto2) 330 | case "OneToOne": 331 | dto1="\t"+entity2+"DTO "+entity2.lower()+"DTO"+";\n" 332 | str1="\t@OneToOne\n"+"\t"+entity2+" "+entity2.lower()+";\n" 333 | dto2="\t"+entity1+"DTO "+entity1.lower()+"DTO"+";\n" 334 | str2="\t@OneToOne\n"+"\t"+entity1+" "+entity1.lower()+";\n" 335 | write(str1,entity1,dto1) 336 | write(str2,entity2,dto2) 337 | case "ManyToMany": 338 | dto1="\tList<"+entity1+"DTO> list"+entity1+"DTO"+";\n" 339 | str1="\t@ManyToMany\n"+"\t"+"List<"+entity1+"> list"+entity1+";\n" 340 | dto2="\tList<"+entity2+"DTO> list"+entity2+"DTO"+";\n" 341 | str2="\t@ManyToMany\n"+"\t"+"List<"+entity2+"> list"+entity2+";\n" 342 | write(str1,entity1,dto1) 343 | write(str2,entity2,dto2) 344 | 345 | def write(str,entity,dto): 346 | my_path = getBasePath() 347 | entities_path = path.join(my_path,"entities") 348 | dtos_path = path.join(my_path,"dtos") 349 | write_in_end(path.join(entities_path,entity),str) 350 | write_in_end(path.join(dtos_path,entity+"DTO"),dto,True) 351 | 352 | 353 | def write_in_end(path,str,flag=False): 354 | 355 | with open(path+".java", 'r+') as file: 356 | lines = file.readlines() 357 | file.seek(0,0) 358 | for i in range(len(lines)): 359 | if(i==2 and flag): 360 | file.write("\nimport java.util.List;\n") 361 | if(i==len(lines)-1): 362 | file.write(str) 363 | file.write(lines[i]) 364 | file.close() 365 | 366 | def getBasePath(): 367 | 368 | dir_name = path.basename(getcwd()) 369 | 370 | my_path = path.join(getcwd(),"src","main","java") 371 | if(path.exists(my_path)): 372 | while True: 373 | list = [f.path for f in scandir(my_path) if f.is_dir()] 374 | tmp_dir_name = path.basename(list[0]) 375 | my_path = path.join(my_path,tmp_dir_name) 376 | if(tmp_dir_name == dir_name): 377 | break; 378 | return my_path; 379 | else: 380 | print (f"{bcolors.FAIL}OOps: access to project folder to execute this commande 'cd MyProgect'") 381 | SystemExit(-1) 382 | 383 | 384 | def getBase(): 385 | 386 | dir_name = path.basename(getcwd()) 387 | 388 | my_path = path.join(getcwd(),"src","main","java") 389 | 390 | package_list = [] 391 | if(path.exists(my_path)): 392 | while True: 393 | list = [f.path for f in scandir(my_path) if f.is_dir()] 394 | tmp_dir_name = path.basename(list[0]) 395 | package_list.append(tmp_dir_name) 396 | my_path = path.join(my_path,tmp_dir_name) 397 | if(tmp_dir_name == dir_name): 398 | break; 399 | return ".".join(package_list); 400 | else: 401 | print (f"{bcolors.FAIL}OOps: access to project folder to execute this commande 'cd MyProgect'") 402 | SystemExit(-1) 403 | 404 | 405 | 406 | def main(): 407 | sg() 408 | -------------------------------------------------------------------------------- /script/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Fouadbst01/SpringCodeGeneratore/f3cf6c6a6e6aebcf6bd6c51a83ce42efa1c9b59c/script/__init__.py -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | setup( 4 | name='Spring_Generator', 5 | version='1.0.0', 6 | description="Spring Boot CLI", 7 | long_description='generate Spring Project and code', 8 | author='EL BSSITA Fouad', 9 | author_email='Fouadelbssita@gmail.com', 10 | #license='MIT', # or any license you think is relevant 11 | packages=['script'], 12 | #zip_safe=False, 13 | install_requires=[ 14 | 'click', 15 | 'questionary', 16 | 'click_aliases' 17 | ], 18 | entry_points=""" 19 | [console_scripts] 20 | sg = script.SpringCodeGeneratore:main 21 | """, 22 | ) 23 | # sg = script.SpringCodeGeneratore:main 24 | --------------------------------------------------------------------------------