def generate(self, output): if not os.path.exists(output): os.makedirs(output) template_path = self.template_path() template_loader = jinja2.FileSystemLoader(searchpath=template_path) template_env = jinja2.Environment(loader=template_loader) PrintColor.log(Fg.FOCUS.value + "STARTED TEMPLATE GENERATION") for (directory, __, files) in os.walk(template_path): current_template_directory = directory.replace(template_path, "") if len(current_template_directory) != 0: self.create_dir_from_template( output + current_template_directory, output) for (template_file) in files: template_file_path = directory + "/" + template_file template_file_path = template_file_path.replace( template_path + "/", "") template_current = template_env.get_template( template_file_path) if re.match(FOR_PATTERN, template_file): group = re.search(FOR_PATTERN, template_file) item_tag = group.group(1) items_tag = group.group(2) item_file = group.group(3) array = self.arguments.arguments.get(items_tag) if isinstance(array, list): items = array else: items = array.split(' ') for item in items: temp_arguments = self.arguments.arguments.copy() temp_arguments.__setitem__(item_tag, item) output_file_path = output + \ jinja2.Template(current_template_directory).render( self.arguments.arguments).replace(".", "/") + "/" + \ jinja2.Template(item_file).render(temp_arguments) template_current.stream(temp_arguments).dump( output_file_path) print(Fg.NOTICE.value + "FILE " + Fg.RESET.value + output_file_path.replace("//", "/").replace( output, "")) else: output_file_path = output + \ jinja2.Template(current_template_directory).render(self.arguments.arguments).replace(".", "/") + "/" + \ jinja2.Template(template_file).render(self.arguments.arguments) template_current.stream( self.arguments.arguments).dump(output_file_path) print(Fg.NOTICE.value + "FILE " + Fg.RESET.value + output_file_path.replace("//", "/").replace( output, "")) PrintColor.log(Style.BOLD.value + Fg.SUCCESS.value + "FILES GENERATED")
def confirm(self): print() PrintColor.log(Style.BOLD.value + Fg.FOCUS.value + '[' + self.template + ']') self.arguments.print_colored() confirm_input = input("confirm [y/n] ? ") if confirm_input == "y" or confirm_input == "Y" or confirm_input == "": return True return False
def choose(self): template_dirs = os.listdir(self.TEMPLATES_REPOSITORY) index = 1 for template_dir in template_dirs: print(index, ":", template_dir) index += 1 self.template = self.get_input_template(template_dirs) print() PrintColor.log(Style.BOLD.value + Fg.FOCUS.value + '[' + self.template + ']')
def get_input_template(self, template_dirs): input_string = input("template : ") if not input_string.isnumeric(): PrintColor.log(Fg.FAIL.value + input_string + " is not a correct template index") return self.get_input_template(template_dirs) template_index = int(input_string) if template_index < 1 or template_index > len(template_dirs): PrintColor.log(Fg.FAIL.value + str(template_index) + " is not a correct template index") return self.get_input_template(template_dirs) else: return template_dirs[template_index - 1]
def get_input_value(self, arg, argument, default_value): if default_value is None: value = input(argument + " : ") if len(value) == 0: PrintColor.log(Fg.FAIL.value + argument + " should not be empty and has no default value") return self.get_input_value(arg, argument, default_value) else: value = input(argument + " [" + default_value + "] : ") if len(value) == 0: value = default_value regex = arg.get("format") if regex is not None: if not re.match(regex, value): PrintColor.log(Fg.FAIL.value + argument + "should match expression " + regex) return self.get_input_value(arg, argument, default_value) return value
def process(template, output): json_data = open(template.path() + "/template.json").read() tasks = json.loads(json_data).get("postBuildTasks") if tasks is None: return for task in tasks: working_directory = output + "/" + Template( task.get("workingDirectory")).render( template.arguments.arguments) task_path = template.path() + "/post_build_tasks/" + task.get( "script") status = Popen([task_path, working_directory]).wait() if status == 0: PrintColor.log("TASK [" + task.get("task") + "] : " + Fg.NOTICE.value + "SUCCESS") else: PrintColor.log("TASK [" + task.get("task") + "] : " + Fg.FAIL.value + "FAILURE") PrintColor.log(Style.BOLD.value + Fg.SUCCESS.value + "POST BUILD TASKS COMPLETED")
def print_colored(self): for key in self.arguments.keys(): if self.arguments.get(key) is None: PrintColor.log(Fg.WARNING.value + key + " has no value") PrintColor.log(Fg.NOTICE.value + key + Fg.RESET.value + " : " + self.arguments.get(key))
def process(self, output): self.generate(output) post_build_task = PostBuildTask() post_build_task.process(self, output) PrintColor.log(Style.BOLD.value + Fg.SUCCESS.value + "SUCCESS")