Skip to content

Commit

Permalink
refactor: py/js entry func name (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Jun 7, 2024
1 parent 739a832 commit e1d895c
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 24 deletions.
22 changes: 13 additions & 9 deletions scripts/build-declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

const fs = require("fs");

function main() {
const TOOL_ENTRY_FUNC = "run";

function main(isTool = true) {
const scriptfile = process.argv[2];
const contents = fs.readFileSync(process.argv[2], "utf8");
const functions = extractFunctions(contents);
const functions = extractFunctions(contents, isTool);
let declarations = functions.map(({ funcName, jsdoc }) => {
const { description, params } = parseJsDoc(jsdoc, funcName);
const declaration = buildDeclaration(funcName, description, params);
return declaration;
});
const name = getBasename(scriptfile);
if (declarations.length > 0) {
declarations = declarations.slice(0, 1);
declarations[0].name = name;
if (isTool) {
const name = getBasename(scriptfile);
if (declarations.length > 0) {
declarations = declarations.slice(0, 1);
declarations[0].name = name;
}
}
console.log(JSON.stringify(declarations, null, 2));
}
Expand All @@ -23,7 +27,7 @@ function main() {
* @param {string} contents
* @param {bool} isTool
*/
function extractFunctions(contents, isTool = true) {
function extractFunctions(contents, isTool) {
const output = [];
const lines = contents.split("\n");
let isInComment = false;
Expand All @@ -45,9 +49,9 @@ function extractFunctions(contents, isTool = true) {
continue;
}
if (isTool) {
if (/function main/.test(line)) {
if (new RegExp(`function ${TOOL_ENTRY_FUNC}`).test(line)) {
output.push({
funcName: "main",
funcName: TOOL_ENTRY_FUNC,
jsdoc,
});
}
Expand Down
18 changes: 11 additions & 7 deletions scripts/build-declarations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import sys
from collections import OrderedDict

TOOL_ENTRY_FUNC = "run"

def main():
def main(is_tool = True):
scriptfile = sys.argv[1]
with open(scriptfile, "r", encoding="utf-8") as f:
contents = f.read()

functions = extract_functions(contents)
functions = extract_functions(contents, is_tool)
declarations = []
for function in functions:
func_name, docstring, func_args = function
Expand All @@ -22,15 +23,16 @@ def main():
build_declaration(func_name, description, params, func_args)
)

name = os.path.splitext(os.path.basename(scriptfile))[0]
if declarations:
declarations = declarations[0:1]
declarations[0]["name"] = name
if is_tool:
name = os.path.splitext(os.path.basename(scriptfile))[0]
if declarations:
declarations = declarations[0:1]
declarations[0]["name"] = name

print(json.dumps(declarations, indent=2))


def extract_functions(contents: str):
def extract_functions(contents: str, is_tool: bool):
tree = ast.parse(contents)
output = []
for node in ast.walk(tree):
Expand All @@ -39,6 +41,8 @@ def extract_functions(contents: str):
func_name = node.name
if func_name.startswith("_"):
continue
if is_tool and func_name != TOOL_ENTRY_FUNC:
continue
docstring = ast.get_docstring(node) or ""
func_args = OrderedDict()
for arg in node.args.args:
Expand Down
2 changes: 1 addition & 1 deletion scripts/create-tool.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ create_js() {
* @typedef {Object} Args${properties}
* @param {Args} args
*/
exports.main = function main(args) {
exports.run = function run(args) {
console.log(args);
}
EOF
Expand Down
4 changes: 2 additions & 2 deletions scripts/run-tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ try {
process.exit(1);
}

const { main } = loadFunc(funcName);
main(args);
const { run } = loadFunc(funcName);
run(args);
2 changes: 1 addition & 1 deletion scripts/run-tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ def load_env(file_path):
sys.exit(1)

module = load_func(func_name)
module.main(**args)
module.run(**args)
2 changes: 1 addition & 1 deletion tools/demo_tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
* @property {string[]} [array_optional] - Define a optional string array property
* @param {Args} args
*/
exports.main = function main(args) {
exports.run = function run(args) {
console.log(args);
}
2 changes: 1 addition & 1 deletion tools/demo_tool.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List, Literal, Optional


def main(
def run(
boolean: bool,
string: str,
string_enum: Literal["foo", "bar"],
Expand Down
2 changes: 1 addition & 1 deletion tools/may_execute_js_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
* @property {string} code - Javascript code to execute, such as `console.log("hello world")`
* @param {Args} args
*/
exports.main = function main({ code }) {
exports.run = function run({ code }) {
eval(code);
}
2 changes: 1 addition & 1 deletion tools/may_execute_py_code.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def main(code: str):
def run(code: str):
"""Runs the python code.
Args:
code: Python code to execute, such as `print("hello world")`
Expand Down

0 comments on commit e1d895c

Please sign in to comment.