Skip to content

Commit

Permalink
Merge pull request #3 from skyscrapers/2-adding-modules-documentation
Browse files Browse the repository at this point in the history
2 adding modules documentation
  • Loading branch information
routel-nolan authored Dec 6, 2024
2 parents a6c7d02 + d540353 commit 14d99d0
Showing 1 changed file with 75 additions and 3 deletions.
78 changes: 75 additions & 3 deletions gen-wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,70 @@
import argparse
import subprocess

TEMP_DIR = 'temp_wiki'
MODULES_NAME = []

def check_next_module_name(curent_name, next_name):
curent = curent_name.split('-')
second = next_name.split('-')
if curent[0] == second[0]:
return True
return False

def compare_directory_names(directory_names):
matching_directories = []
for i in range(len(directory_names)):
for j in range(i+1, len(directory_names)):
if check_next_module_name(directory_names[i], directory_names[j]):
matching_directories.append((directory_names[i], directory_names[j]))
return matching_directories

def process_files(module_dir, directory):
files = os.listdir(f"{module_dir}/{directory}")
for file in files:
if file.endswith('.md'):
return f"{module_dir}/{directory}/{file}"

def remove_module_from_list(module_list, module_to_remove):
clean_least = [i for i in module_list if i not in module_to_remove]
return clean_least

def create_str_for_multiple_modules(paths, module_name, module_full_name):
content = ""
content += f"## {module_name.capitalize()}\n\n"
for path in paths:
with open(path, 'r') as f:
tmp = f.read()
if tmp.strip():
content += f"### {module_full_name.capitalize()}\n\n"
content += tmp
MODULES_NAME.append(module_name)
return content

def create_modules_documentation(output_dir):
modules_dir = 'terraform/modules'
markdown_content = ""
paths = []
directory_names = [d for d in os.listdir(modules_dir) if os.path.isdir(os.path.join(modules_dir, d))]
multiple_directory = compare_directory_names(directory_names)
if multiple_directory is not None:
for module in multiple_directory:
paths.append(process_files(modules_dir, module[0]))
paths.append(process_files(modules_dir, module[1]))
markdown_content += create_str_for_multiple_modules(paths, multiple_directory[0][0].split('-')[0], multiple_directory[0][0])
directory_names = remove_module_from_list(directory_names, multiple_directory[0])
for directory in directory_names:
tmp = process_files(modules_dir, directory)
if tmp != None:
markdown_content += f"## {directory.capitalize()}\n\n"
markdown_content += tmp
MODULES_NAME.append(directory)

if markdown_content.strip() != f"#TMP\n\n":
output_file = os.path.join(output_dir, f"modules-documentation.md")
os.makedirs(output_dir, exist_ok=True)
with open(output_file, 'w') as f:
f.write(markdown_content)
print(f"Markdown file generated for modules: {output_file}")

def load_config(config_file):
with open(config_file, 'r') as f:
Expand Down Expand Up @@ -68,6 +131,10 @@ def download_bucket(directory):
except Exception as e:
print(f"Error: {e}")
return None

def create_link_to_module_doc(module_name):
content = f"Module documentation [here](https://github.com/skyscrapers/{os.getenv('REPO_NAME')}/wiki/modules-documentation#{module_name})\n"
return content

def process_directory(directory, config):
json_content = download_bucket(directory)
Expand All @@ -87,7 +154,10 @@ def process_directory(directory, config):

if markdown_content:
directory = directory.split('/', 1)[1]
markdown_content = f"## {directory.capitalize()}\n\n" + markdown_content
markdown_content = f"## {directory.capitalize()}\n\n"
if directory in MODULES_NAME:
markdown_content += create_link_to_module_doc(directory)
markdown_content += markdown_content

return markdown_content

Expand Down Expand Up @@ -143,7 +213,9 @@ def copy_wiki(md_files):
directories = [d for d in os.listdir(os.getcwd()) if os.path.isdir(os.path.join(os.getcwd(), d))]
for directory in directories:
process_environment(directory, config, args.output_dir)

md_files = list_md_files(args.output_dir)
os.chdir("../..")
create_modules_documentation(args.output_dir)
md_files += list_md_files(args.output_dir)
copy_wiki(md_files)

0 comments on commit 14d99d0

Please sign in to comment.