Skip to content

Commit

Permalink
generate devcontainer feature source code from lsp-bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
nohzafk committed Jan 5, 2024
1 parent 187c689 commit 91df369
Show file tree
Hide file tree
Showing 143 changed files with 8,550 additions and 20 deletions.
7 changes: 5 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
]
},
"extensions": [
"mads-hartmann.bash-ide-vscode"
"mads-hartmann.bash-ide-vscode",
"ms-python.python",
"ms-python.vscode-pylance",
"ms-python.black-formatter"
]
}
},
Expand All @@ -22,5 +25,5 @@
"ghcr.io/devcontainers/features/python:1": {}
},
"remoteUser": "node",
"updateContentCommand": "npm install -g @devcontainers/cli; pipx install cookiecutter"
"updateContentCommand": "bash .devcontainer/install-deps.sh"
}
5 changes: 5 additions & 0 deletions .devcontainer/install-deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

npm install -g @devcontainers/cli
pipx install cookiecutter
pipx inject cookiecutter requests
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
90 changes: 90 additions & 0 deletions _generator/gen_langserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import requests
import tarfile
import os
import json

source_tar_gz_path = "/tmp/source.tar.gz"


def download_and_extract(url, extract_path="."):
"""
Download a tar.gz file from a URL and extract it to a specified path.
"""
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(source_tar_gz_path, "wb") as f:
f.write(response.raw.read())

# Extract the tar.gz file
with tarfile.open(source_tar_gz_path) as tar:
tar.extractall(path=extract_path)
else:
raise Exception(f"Failed to download file: Status code {response.status_code}")


def main():
repo = "manateelazycat/lsp-bridge"
branch = "master"
url = f"https://github.com/{repo}/archive/refs/heads/{branch}.tar.gz"
extract_path = "/tmp/extracted"

# Download and extract the tar.gz
if not os.path.exists(source_tar_gz_path):
download_and_extract(url, extract_path)

# Path to the 'langserver' directory
langserver_path = os.path.join(extract_path, f"lsp-bridge-{branch}", "langserver")

with open("langserver.json", "r") as f:
langserver_list = json.load(f)

langaserver_map = {item["langserver"]: item for item in langserver_list}

# Iterate over JSON files in the 'langserver' directory
for root, dirs, files in os.walk(langserver_path):
for file in files:
if file.endswith(".json"):
json_file = os.path.join(root, file)
try:
process_json(langaserver_map, json_file)
except Exception as e:
print(e)

langserver_list = [
langaserver_map[item] for item in sorted(list(langaserver_map.keys()))
]

with open("langserver.json", "w") as f:
json.dump(langserver_list, f, indent=4)

print("\nlangserver.json updated\n")


def process_json(langaserver_map, json_file):
"""
Function to process a JSON file.
"""
# Process the JSON data as needed
with open(json_file, "r") as f:
data = json.load(f)

name = data["name"]
# add if not exist to generate the file
if name not in langaserver_map or not langaserver_map[name]["packages"]:
langaserver_map[name] = {
"langserver": name,
"packages": "",
"langserver_binary": data["command"][0],
}
elif langaserver_map[name]["langserver_binary"] != data["command"][0]:
print(f"Upstream changed LSP server for {name}! Need manually update")
print(
" "
+ langaserver_map[name]["langserver_binary"]
+ " -> "
+ data["command"][0]
)


if __name__ == "__main__":
main()
31 changes: 17 additions & 14 deletions _generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,38 @@
from cookiecutter.main import cookiecutter

# Read the JSON file
with open('langserver.json', 'r') as file:
with open("langserver.json", "r") as file:
data = json.load(file)

template_src_dir = "src_template"
template_test_dir = "test_template"

for entry in data:
langserver = entry['langserver']
if not entry["packages"]:
continue

langserver = entry["langserver"]
target_src_dir = f"../src/{langserver}"
target_test_dir = f"../test/{langserver}"

if os.path.exists(target_src_dir):
shutil.rmtree(target_src_dir)
if os.path.exists(target_src_dir):
shutil.rmtree(target_src_dir)

if os.path.exists(target_test_dir):
if os.path.exists(target_test_dir):
shutil.rmtree(target_test_dir)

cookiecutter(
'src_template',
no_input=True,
"src_template",
no_input=True,
output_dir=f"{target_src_dir}/../",
extra_context=entry
)
extra_context=entry,
)
print(f"Generated {target_src_dir}")

cookiecutter(
'test_template',
no_input=True,
"test_template",
no_input=True,
output_dir=f"{target_test_dir}/../",
extra_context=entry
extra_context=entry,
)
print(f"Generated {target_test_dir}")
print(f"Generated {target_test_dir}")
Loading

0 comments on commit 91df369

Please sign in to comment.