Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to automatically generated code #74

Merged
merged 1 commit into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions extractor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#!/usr/bin/env python3

import json
import os

import requests
import pyperclip
import json


DEBUG = True
REPLACE = True
WEB = True
URL = "https://raw.githubusercontent.com/rockneurotiko/telegram_api_json/master/exports/tg_api.json"
# URL = "https://raw.githack.com/rockneurotiko/telegram_api_json/master/exports/tg_api_pretty.json"
Expand Down Expand Up @@ -127,6 +131,24 @@ def get_definition():
return definition_from_file()


def maybe_replace(text):
if not REPLACE:
return

new_text = ""
with open("lib/ex_gram.ex", "r") as f:
current = f.read()
start = current[:current.find("# START AUTO GENERATED")].strip()
end_ind = current.find("# END AUTO GENERATED") + 20
end = current[end_ind:]
new_text = start + text + end

if new_text:
with open("lib/ex_gram.ex", "w") as f:
f.write(new_text)

os.system("mix format lib/ex_gram.ex")

def main():
definition = get_definition()

Expand All @@ -139,9 +161,9 @@ def main():
generics_str = "\n\n ".join(generics)

text = """
# ----------METHODS-----------
# START AUTO GENERATED

# AUTO GENERATED
# ----------METHODS-----------

# Methods

Expand All @@ -165,10 +187,14 @@ def main():
{}

# {} generics
end""".format(methods_str, len(methods), models_str, len(models), generics_str, len(generics))
end

# END AUTO GENERATED
""".format(methods_str, len(methods), models_str, len(models), generics_str, len(generics))

print(text)
pyperclip.copy(text)
maybe_replace(text)

if __name__ == "__main__":
main()
6 changes: 4 additions & 2 deletions lib/ex_gram.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ defmodule ExGram do
ExGram.Encoder.EngineCompiler.compile(engine)
end

# ----------METHODS-----------
# START AUTO GENERATED

# AUTO GENERATED
# ----------METHODS-----------

# Methods

Expand Down Expand Up @@ -2129,4 +2129,6 @@ defmodule ExGram do

# 5 generics
end

# END AUTO GENERATED
end