Skip to content

Commit adc9701

Browse files
authored
Merge pull request #46 from JuliaComputing/tan/apimodule
bump CSTParser ver, allow specifying codegen loc
2 parents 7465ae9 + 89ffd17 commit adc9701

File tree

3 files changed

+70
-50
lines changed

3 files changed

+70
-50
lines changed

gen/genapialiases.jl

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ function emit_alias(fn_expr::CSTParser.EXPR, api_decoration::String)
2929

3030
(fn_name == undec_fn_name) && (return nothing)
3131
fn_sig = CSTParser.get_sig(fn_expr)
32-
fn_args = filter(x->((x.typ === CSTParser.BinaryOpCall) || (x.typ === CSTParser.IDENTIFIER)), fn_sig.args[3:end])
33-
fn_kwargs_container = filter(x->(x.typ === CSTParser.Parameters), fn_sig.args[3:end])
34-
fn_kwargs = isempty(fn_kwargs_container) ? [] : filter(x->(x.typ === CSTParser.Kw), fn_kwargs_container[1].args)
32+
fn_args = filter(x->(CSTParser.isbinarysyntax(x) || CSTParser.isidentifier(x)), fn_sig.args[2:end])
33+
fn_kwargs_container = filter(x->CSTParser.isparameters(x), fn_sig.args[2:end])
34+
fn_kwargs = isempty(fn_kwargs_container) ? [] : filter(x->CSTParser.iskwarg(x), fn_kwargs_container[1].args)
3535

36-
arg_pairs = map(x->CSTParser.str_value(CSTParser.get_name(x))=>((x.typ === CSTParser.IDENTIFIER) ? "" : CSTParser.str_value(x.args[3])), fn_args)
37-
kwarg_pairs = map(x->CSTParser.str_value(CSTParser.get_name(x.args[1]))=>CSTParser.str_value(x.args[3]), fn_kwargs)
36+
arg_pairs = map(x->CSTParser.str_value(CSTParser.get_name(x))=>(CSTParser.isidentifier(x) ? "" : CSTParser.str_value(x.args[2])), fn_args)
37+
kwarg_pairs = map(x->CSTParser.str_value(CSTParser.get_name(x.args[1]))=>CSTParser.str_value(x.args[2]), fn_kwargs)
3838

3939
KuberAPIAlias(undec_fn_name, fn_name, arg_pairs, kwarg_pairs)
4040
end
@@ -48,13 +48,13 @@ function kuberapi(file::String)
4848

4949
x, ps = CSTParser.parse(ParseState(String(readchomp(file))))
5050

51-
while !ps.done
51+
while !ps.done && (CSTParser.kindof(ps.nt) !== CSTParser.Tokens.ENDMARKER)
5252
if CSTParser.defines_struct(x)
5353
structsig = CSTParser.get_sig(x)
54-
if structsig.typ === CSTParser.BinaryOpCall
55-
op = structsig.args[2]
54+
if CSTParser.isbinarysyntax(structsig)
55+
op = structsig.head
5656
if CSTParser.is_issubt(op)
57-
subtyp = CSTParser.str_value(CSTParser.get_name(structsig.args[3]))
57+
subtyp = CSTParser.str_value(CSTParser.get_name(structsig.args[2]))
5858
if subtyp == "SwaggerApi"
5959
typename = CSTParser.str_value(CSTParser.get_name(structsig.args[1]))
6060
if endswith(typename, "Api")
@@ -67,7 +67,7 @@ function kuberapi(file::String)
6767
# Note: We are guaranteed to receive the struct definition before methods
6868
# because of the sequence in which Swagger code is generated and also
6969
# because of the Julia restriction of type being defined before use
70-
fn_expr = ((x.typ === CSTParser.MacroCall) && CSTParser.defines_function(x.args[3])) ? x.args[3] : CSTParser.defines_function(x) ? x : nothing
70+
fn_expr = (CSTParser.ismacrocall(x) && CSTParser.defines_function(x.args[4])) ? x.args[4] : CSTParser.defines_function(x) ? x : nothing
7171
if fn_expr !== nothing
7272
fn_name = CSTParser.str_value(CSTParser.get_name(fn_expr))
7373
if !startswith(fn_name, "_swaggerinternal_")
@@ -126,8 +126,12 @@ function gen_aliases(folder::String, output::String)
126126
end
127127

128128
function main()
129-
DIR = dirname(@__FILE__)
130-
gen_aliases(joinpath(DIR, "../src/ApiImpl/api"), joinpath(DIR, "../src/ApiImpl/apialiases.jl"))
129+
if length(ARGS) > 0
130+
output_path = ARGS[1]
131+
else
132+
output_path = joinpath(dirname(dirname(__FILE__)), "src", "ApiImpl")
133+
end
134+
gen_aliases(joinpath(output_path, "api"), joinpath(output_path, "apialiases.jl"))
131135
end
132136

133137
main()

gen/generate.sh

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#!/usr/bin/env bash
22

3-
if [ $# -ne 1 ]; then
4-
echo "Usage: generate.sh <path to kubernetes specifications folder>"
3+
if [ $# -lt 1 ] || [ $# -gt 2 ]; then
4+
echo "Usage: generate.sh <path to kubernetes specifications folder> [output path]"
5+
echo " - output path is optional, if not specified '../src/ApiImpl/api' folder relative to this script will be used"
56
echo "Ensure:"
67
echo " - 'julia' is in PATH or set in environment variable 'JULIA'."
78
echo " - package directory is writable."
8-
echo " - CSTParser v2.1.0 is installed."
9+
echo " - CSTParser v3.3.0 is installed."
910
echo "Note:"
10-
echo " - your current 'src/ApiImpl/api' folder in the package will be renamed to 'src/ApiImpl/api_bak'"
11-
echo " - existing 'src/ApiImpl/api_bak' folder if any will be deleted"
11+
echo " - the 'api' folder in the output path will be renamed to 'api_bak'"
12+
echo " - existing 'api_bak' folder if any in output folder will be deleted"
1213
echo "Ref:"
1314
echo " - API conventions: https://github.com/kubernetes/community/blob/master/contributors/devel/api-conventions.md"
1415
echo " - API: https://kubernetes.io/docs/concepts/overview/kubernetes-api/"
@@ -21,36 +22,42 @@ then
2122
fi
2223

2324
# ensure CSTParser is of right version
24-
cstpver=`${JULIA} -e 'using Pkg; println(Pkg.installed()["CSTParser"] == v"2.2.0")'`
25+
cstpver=`${JULIA} -e 'using Pkg, UUIDs; println(Pkg.dependencies()[UUID("00ebfdb7-1f24-5e51-bd34-a7502290713f")].version == v"3.3.0")'`
2526
if [ "$cstpver" != "true" ]
2627
then
27-
echo "CSTParser v2.2.0 is required"
28+
echo "CSTParser v3.3.0 is required"
2829
exit -1
2930
fi
3031

3132
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
32-
GENDIR="$( readlink -e "${DIR}/../" )"
33-
TEMPGENDIR=${GENDIR}/_tmp
3433
SPECDIR=$1
34+
if [ $# -eq 2 ]; then
35+
APIIMPLDIR=$2
36+
else
37+
GENDIR="$( cd "$( dirname "${DIR}" )" && pwd )"
38+
APIIMPLDIR=${GENDIR}/src/ApiImpl
39+
fi
40+
#TEMPGENDIR=${GENDIR}/_tmp
41+
TEMPGENDIR=`mktemp -d`
3542
#SPECFILE=root_swagger.json
3643
SPECFILE=swagger.json
3744
SWAGGERDIR=`${JULIA} -e 'import Swagger; print(normpath(joinpath(dirname(pathof(Swagger)), "..")))'`
3845

3946
echo "Swagger is at $SWAGGERDIR"
40-
echo "Generating into $TEMPGENDIR, moving into $GENDIR"
47+
echo "Generating into $TEMPGENDIR, moving into $APIIMPLDIR"
4148
mkdir -p ${TEMPGENDIR}
4249
${SWAGGERDIR}/plugin/generate.sh -i ${SPECDIR}/${SPECFILE} -o ${TEMPGENDIR} -c ${DIR}/config.json
43-
rm -rf ${GENDIR}/src/ApiImpl/api_bak
44-
mv ${GENDIR}/src/ApiImpl/api ${GENDIR}/src/ApiImpl/api_bak
45-
mkdir ${GENDIR}/src/ApiImpl/api
46-
mv ${TEMPGENDIR}/src/* ${GENDIR}/src/ApiImpl/api/
50+
rm -rf ${APIIMPLDIR}/api_bak
51+
mv ${APIIMPLDIR}/api ${APIIMPLDIR}/api_bak
52+
mkdir ${APIIMPLDIR}/api
53+
mv ${TEMPGENDIR}/src/* ${APIIMPLDIR}/api/
4754
rm ${TEMPGENDIR}/LICENSE
4855
rm -r ${TEMPGENDIR}
4956

50-
${JULIA} ${DIR}/genapialiases.jl
51-
${JULIA} ${DIR}/gentypealiases.jl
57+
${JULIA} ${DIR}/genapialiases.jl ${APIIMPLDIR}
58+
${JULIA} ${DIR}/gentypealiases.jl ${APIIMPLDIR}
5259

5360
# the following models are not generated correctly by Swagger, hand code them for now
54-
cp ${DIR}/model_IoK8sApimachineryPkgApisMetaV1Time.jl ${GENDIR}/src/ApiImpl/api/
55-
cp ${DIR}/model_IoK8sApimachineryPkgUtilIntstrIntOrString.jl ${GENDIR}/src/ApiImpl/api/
56-
cp ${DIR}/model_IoK8sApimachineryPkgApisMetaV1WatchEvent.jl ${GENDIR}/src/ApiImpl/api/
61+
cp ${DIR}/model_IoK8sApimachineryPkgApisMetaV1Time.jl ${APIIMPLDIR}/api/
62+
cp ${DIR}/model_IoK8sApimachineryPkgUtilIntstrIntOrString.jl ${APIIMPLDIR}/api/
63+
cp ${DIR}/model_IoK8sApimachineryPkgApisMetaV1WatchEvent.jl ${APIIMPLDIR}/api/

gen/gentypealiases.jl

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ const BaseTypes = ("String", "Float64", "Float32", "Int", "Int64", "Int32", "Int
1010
const ModelPrefixes = ("IoK8sApimachineryPkgApis", "IoK8sApimachineryPkg", "IoK8sApi", "IoK8sKubeAggregatorPkgApis", "IoK8sApiextensionsApiserverPkgApis")
1111

1212
function get_swagger_ctx_call_return_type(fn_body)
13-
all_calls = findall(x->(x.typ === CSTParser.BinaryOpCall), fn_body.args)
13+
all_calls = findall(x->CSTParser.isbinarysyntax(x), fn_body.args)
1414
for nidx in all_calls
1515
ncall = fn_body.args[nidx]
16-
fc = ncall.args[3]
16+
fc = ncall.args[2]
1717
if CSTParser.is_func_call(fc)
1818
binop = fc.args[1]
19-
if binop.typ === CSTParser.BinaryOpCall
20-
if (CSTParser.str_value(binop.args[1]) == "Swagger") && CSTParser.is_dot(binop.args[2]) && (binop.args[3].typ === CSTParser.Quotenode) && (CSTParser.str_value(binop.args[3].args[1]) == "Ctx")
21-
if (length(fc.args) > 6) && CSTParser.isidentifier(fc.args[7])
22-
return CSTParser.str_value(fc.args[7])
19+
if CSTParser.isbinarysyntax(binop)
20+
if (CSTParser.str_value(binop.args[1]) == "Swagger") && CSTParser.is_dot(binop.head) && (binop.args[2].head === :quotenode) && (CSTParser.str_value(binop.args[2].args[1]) == "Ctx")
21+
if (length(fc.args) > 3) && CSTParser.isidentifier(fc.args[4])
22+
return CSTParser.str_value(fc.args[4])
2323
end
2424
end
2525
end
@@ -32,7 +32,7 @@ end
3232
Adds Swagger API call return types to be aliased to undecorated names.
3333
"""
3434
function emit_alias(fn_expr::CSTParser.EXPR, api_decoration::String, aliases::KuberTypeAliasesSet)
35-
fn_body = CSTParser.get_body(fn_expr)
35+
fn_body = fn_expr.args[end]
3636
return_type = get_swagger_ctx_call_return_type(fn_body)
3737
((nothing === return_type) || (return_type in BaseTypes)) && return
3838
push!(get!(()->Set{String}(), aliases, api_decoration), return_type)
@@ -46,19 +46,19 @@ function kuberapitypes(file::String, aliases_set::KuberTypeAliasesSet)
4646
x, ps = CSTParser.parse(ParseState(String(readchomp(file))))
4747
api_decoration = ""
4848

49-
while !ps.done
49+
while !ps.done && (CSTParser.kindof(ps.nt) !== CSTParser.Tokens.ENDMARKER)
5050
structsig = nothing
5151
if CSTParser.defines_struct(x)
5252
structsig = CSTParser.get_sig(x)
53-
elseif x.typ == CSTParser.MacroCall && CSTParser.str_value(x[1]) == "@doc"
53+
elseif CSTParser.ismacrocall(x) && CSTParser.str_value(x[1]) == "@doc"
5454
structsig = CSTParser.get_sig(x[3])
5555
end
5656

5757
if structsig !== nothing
58-
if structsig.typ === CSTParser.BinaryOpCall
59-
op = structsig.args[2]
58+
if CSTParser.isbinarysyntax(structsig)
59+
op = structsig.head
6060
if CSTParser.is_issubt(op)
61-
subtyp = CSTParser.str_value(CSTParser.get_name(structsig.args[3]))
61+
subtyp = CSTParser.str_value(CSTParser.get_name(structsig.args[2]))
6262
if subtyp == "SwaggerApi"
6363
typename = CSTParser.str_value(CSTParser.get_name(structsig.args[1]))
6464
if endswith(typename, "Api")
@@ -71,7 +71,7 @@ function kuberapitypes(file::String, aliases_set::KuberTypeAliasesSet)
7171
# Note: We are guaranteed to receive the struct definition before methods
7272
# because of the sequence in which Swagger code is generated and also
7373
# because of the Julia restriction of type being defined before use
74-
fn_expr = ((x.typ === CSTParser.MacroCall) && CSTParser.defines_function(x.args[3])) ? x.args[3] : CSTParser.defines_function(x) ? x : nothing
74+
fn_expr = (CSTParser.ismacrocall(x) && CSTParser.defines_function(x.args[4])) ? x.args[4] : CSTParser.defines_function(x) ? x : nothing
7575
if fn_expr !== nothing
7676
emit_alias(fn_expr, api_decoration, aliases_set)
7777
end
@@ -110,16 +110,21 @@ end
110110
function kubermodeltypes(file::String, sorted_apis::Vector{String}, aliases_set::KuberTypeAliasesSet, unmapped::Set{String})
111111
x, ps = CSTParser.parse(ParseState(String(readchomp(file))))
112112

113-
while !ps.done
113+
while !ps.done && (CSTParser.kindof(ps.nt) !== CSTParser.Tokens.ENDMARKER)
114114
structsig = nothing
115115
if CSTParser.defines_struct(x)
116116
structsig = CSTParser.get_sig(x)
117-
elseif x.typ == CSTParser.MacroCall && CSTParser.str_value(x[1]) == "@doc"
118-
structsig = CSTParser.get_sig(x[3])
117+
elseif CSTParser.ismacrocall(x) && CSTParser.str_value(x[1]) == "@doc"
118+
for elem in x
119+
if CSTParser.defines_struct(elem)
120+
structsig = elem
121+
break
122+
end
123+
end
119124
end
120125

121126
if structsig !== nothing
122-
model_name = CSTParser.str_value(CSTParser.get_name(structsig.args[1]))
127+
model_name = CSTParser.str_value(CSTParser.get_name(structsig))
123128
if !startswith(model_name, "IoK8sKubernetes") && !(model_name == "IoK8sApimachineryPkgRuntimeRawExtension")
124129
api_idx = find_matching_api(sorted_apis, model_name)
125130
if api_idx !== nothing
@@ -201,8 +206,12 @@ function gen_aliases(folder::String, output::String)
201206
end
202207

203208
function main()
204-
DIR = dirname(@__FILE__)
205-
gen_aliases(joinpath(DIR, "../src/ApiImpl/api"), joinpath(DIR, "../src/ApiImpl/typealiases.jl"))
209+
if length(ARGS) > 0
210+
output_path = ARGS[1]
211+
else
212+
output_path = joinpath(dirname(dirname(__FILE__)), "src", "ApiImpl")
213+
end
214+
gen_aliases(joinpath(output_path, "api"), joinpath(output_path, "typealiases.jl"))
206215
end
207216

208217
main()

0 commit comments

Comments
 (0)