-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add generate client command (#266)
- Loading branch information
1 parent
6e937a8
commit b885fb1
Showing
17 changed files
with
904 additions
and
389 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import json | ||
import logging | ||
import pathlib | ||
import platform | ||
import re | ||
|
||
import algokit_client_generator | ||
import click | ||
|
||
from algokit.core import proc | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def snake_case(s: str) -> str: | ||
s = s.replace("-", " ") | ||
s = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1_\2", s) | ||
s = re.sub(r"([a-z\d])([A-Z])", r"\1_\2", s) | ||
return re.sub(r"[-\s]", "_", s).lower() | ||
|
||
|
||
def format_client_name(output: pathlib.Path, application_file: pathlib.Path) -> pathlib.Path: | ||
client_name = str(output).replace("%parent_dir%", snake_case(application_file.parent.name)) | ||
|
||
if "%name%" in str(output): | ||
application_json = json.loads(application_file.read_text()) | ||
client_name = str(output).replace("%name%", snake_case(application_json["contract"]["name"])) | ||
|
||
return pathlib.Path(client_name) | ||
|
||
|
||
def generate_client_by_language(app_spec: pathlib.Path, output: pathlib.Path, language: str) -> None: | ||
if language.lower() == "python": | ||
logger.info(f"Generating Python client code for application specified in {app_spec} and writing to {output}") | ||
algokit_client_generator.generate_client(app_spec, pathlib.Path(output)) | ||
|
||
elif language.lower() == "typescript": | ||
is_windows = platform.system() == "Windows" | ||
npx = "npx" if not is_windows else "npx.cmd" | ||
cmd = [ | ||
npx, | ||
"--yes", | ||
"@algorandfoundation/[email protected]", | ||
"generate", | ||
"-a", | ||
str(app_spec), | ||
"-o", | ||
str(output), | ||
] | ||
try: | ||
proc.run( | ||
cmd, | ||
bad_return_code_error_message=f"Failed to run {' '.join(cmd)} for {app_spec}.", | ||
) | ||
except OSError as e: | ||
raise click.ClickException("Typescript generator requires Node.js and npx to be installed.") from e | ||
logger.info( | ||
f"Generating TypeScript client code for application specified in {app_spec} and writing to {output}" | ||
) | ||
|
||
|
||
def generate_recursive_clients(app_spec: pathlib.Path, output: pathlib.Path, language: str) -> None: | ||
if app_spec.is_dir(): | ||
for child in app_spec.iterdir(): | ||
if child.is_dir(): | ||
generate_recursive_clients(app_spec=child, output=output, language=language) | ||
elif child.name.lower() == "application.json": | ||
formatted_output = format_client_name(output=output, application_file=child) | ||
generate_client_by_language(app_spec=child, output=formatted_output, language=language) | ||
else: | ||
formatted_output = format_client_name(output=output, application_file=app_spec) | ||
generate_client_by_language(app_spec=app_spec, output=formatted_output, language=language) | ||
|
||
|
||
@click.group("generate") | ||
def generate_group() -> None: | ||
"""Generate code for an Algorand project.""" | ||
|
||
|
||
@generate_group.command("client") | ||
@click.option( | ||
"app_spec", | ||
"--appspec", | ||
"-a", | ||
type=click.Path(exists=True, dir_okay=True, resolve_path=True), | ||
default="./application.json", | ||
help="Path to an application specification file or a directory to recursively search for application.json", | ||
) | ||
@click.option( | ||
"output", | ||
"--output", | ||
"-o", | ||
type=click.Path(exists=False, dir_okay=False, resolve_path=True), | ||
default="./client_generated.py", | ||
help="Path to the output file. The following tokens can be used to substitute into the output path:" | ||
" %name%, %parent_dir% ", | ||
) | ||
@click.option( | ||
"--language", | ||
default=None, | ||
type=click.Choice(["python", "typescript"]), | ||
help="Programming language of the generated client code", | ||
) | ||
def generate_client(app_spec: str, output: str, language: str | None) -> None: | ||
""" | ||
Create a typed ApplicationClient from an ARC-32 application.json | ||
""" | ||
output_path = pathlib.Path(output) | ||
app_spec_path = pathlib.Path(app_spec) | ||
if language is None: | ||
extension = output_path.suffix | ||
if extension == ".ts": | ||
language = "typescript" | ||
elif extension == ".py": | ||
language = "python" | ||
else: | ||
raise click.ClickException( | ||
"Could not determine language from file extension, Please use the --language option to specify a " | ||
"target language" | ||
) | ||
|
||
generate_recursive_clients(app_spec=app_spec_path, output=output_path, language=language) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
{ | ||
"hints": { | ||
"hello(string)string": { | ||
"call_config": { | ||
"no_op": "CALL" | ||
} | ||
}, | ||
"hello_world_check(string)void": { | ||
"call_config": { | ||
"no_op": "CALL" | ||
} | ||
} | ||
}, | ||
"source": { | ||
"approval": "I3ByYWdtYSB2ZXJzaW9uIDgKaW50Y2Jsb2NrIDAgMQp0eG4gTnVtQXBwQXJncwppbnRjXzAgLy8gMAo9PQpibnogbWFpbl9sNgp0eG5hIEFwcGxpY2F0aW9uQXJncyAwCnB1c2hieXRlcyAweDAyYmVjZTExIC8vICJoZWxsbyhzdHJpbmcpc3RyaW5nIgo9PQpibnogbWFpbl9sNQp0eG5hIEFwcGxpY2F0aW9uQXJncyAwCnB1c2hieXRlcyAweGJmOWMxZWRmIC8vICJoZWxsb193b3JsZF9jaGVjayhzdHJpbmcpdm9pZCIKPT0KYm56IG1haW5fbDQKZXJyCm1haW5fbDQ6CnR4biBPbkNvbXBsZXRpb24KaW50Y18wIC8vIE5vT3AKPT0KdHhuIEFwcGxpY2F0aW9uSUQKaW50Y18wIC8vIDAKIT0KJiYKYXNzZXJ0CnR4bmEgQXBwbGljYXRpb25BcmdzIDE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LyBDaGVjayBhcHAgaXMgdXBkYXRhYmxlCmFzc2VydApyZXRzdWIKCi8vIGRlbGV0ZQpkZWxldGVfMToKcHJvdG8gMCAwCnR4biBTZW5kZXIKZ2xvYmFsIENyZWF0b3JBZGRyZXNzCj09Ci8vIHVuYXV0aG9yaXplZAphc3NlcnQKcHVzaGludCBUTVBMX0RFTEVUQUJMRSAvLyBUTVBMX0RFTEVUQUJMRQovLyBDaGVjayBhcHAgaXMgZGVsZXRhYmxlCmFzc2VydApyZXRzdWIKCi8vIGhlbGxvCmhlbGxvXzI6CnByb3RvIDEgMQpwdXNoYnl0ZXMgMHggLy8gIiIKcHVzaGJ5dGVzIDB4NDg2NTZjNmM2ZjJjMjAgLy8gIkhlbGxvLCAiCmZyYW1lX2RpZyAtMQpleHRyYWN0IDIgMApjb25jYXQKZnJhbWVfYnVyeSAwCmZyYW1lX2RpZyAwCmxlbgppdG9iCmV4dHJhY3QgNiAwCmZyYW1lX2RpZyAwCmNvbmNhdApmcmFtZV9idXJ5IDAKcmV0c3ViCgovLyBoZWxsb193b3JsZF9jaGVjawpoZWxsb3dvcmxkY2hlY2tfMzoKcHJvdG8gMSAwCmZyYW1lX2RpZyAtMQpleHRyYWN0IDIgMApwdXNoYnl0ZXMgMHg1NzZmNzI2YzY0IC8vICJXb3JsZCIKPT0KYXNzZXJ0CnJldHN1Yg==", | ||
"clear": "I3ByYWdtYSB2ZXJzaW9uIDgKcHVzaGludCAwIC8vIDAKcmV0dXJu" | ||
}, | ||
"state": { | ||
"global": { | ||
"num_byte_slices": 0, | ||
"num_uints": 0 | ||
}, | ||
"local": { | ||
"num_byte_slices": 0, | ||
"num_uints": 0 | ||
} | ||
}, | ||
"schema": { | ||
"global": { | ||
"declared": {}, | ||
"reserved": {} | ||
}, | ||
"local": { | ||
"declared": {}, | ||
"reserved": {} | ||
} | ||
}, | ||
"contract": { | ||
"name": "HelloWorldApp", | ||
"methods": [ | ||
{ | ||
"name": "hello", | ||
"args": [ | ||
{ | ||
"type": "string", | ||
"name": "name" | ||
} | ||
], | ||
"returns": { | ||
"type": "string" | ||
}, | ||
"desc": "Returns Hello, {name}" | ||
}, | ||
{ | ||
"name": "hello_world_check", | ||
"args": [ | ||
{ | ||
"type": "string", | ||
"name": "name" | ||
} | ||
], | ||
"returns": { | ||
"type": "void" | ||
}, | ||
"desc": "Asserts {name} is \"World\"" | ||
} | ||
], | ||
"networks": {} | ||
}, | ||
"bare_call_config": { | ||
"delete_application": "CALL", | ||
"no_op": "CREATE", | ||
"update_application": "CALL" | ||
} | ||
} |
Oops, something went wrong.
b885fb1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage Report