Skip to content

Commit 6f54282

Browse files
committed
Track build folders with git and render configs
1 parent a829ad5 commit 6f54282

File tree

16 files changed

+791
-406
lines changed

16 files changed

+791
-406
lines changed

codeplain_REST_api.py

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
from typing import Optional
2+
13
import requests
24

5+
from plain2code_state import RunState
6+
37

48
class FunctionalRequirementTooComplex(Exception):
59
def __init__(self, message, proposed_breakdown=None):
@@ -36,6 +40,14 @@ class LinkMustHaveTextSpecified(Exception):
3640
pass
3741

3842

43+
class NoRenderFound(Exception):
44+
pass
45+
46+
47+
class MultipleRendersFound(Exception):
48+
pass
49+
50+
3951
class CodeplainAPI:
4052

4153
def __init__(self, api_key):
@@ -49,7 +61,13 @@ def api_url(self):
4961
def api_url(self, value):
5062
self._api_url = value
5163

52-
def post_request(self, endpoint_url, headers, payload):
64+
def _extend_payload_with_run_state(self, payload: dict, run_state: RunState):
65+
run_state.increment_call_count()
66+
payload["render_state"] = run_state.to_dict()
67+
68+
def post_request(self, endpoint_url, headers, payload, run_state: Optional[RunState]):
69+
if run_state is not None:
70+
self._extend_payload_with_run_state(payload, run_state)
5371
response = requests.post(endpoint_url, headers=headers, json=payload)
5472

5573
try:
@@ -83,6 +101,12 @@ def post_request(self, endpoint_url, headers, payload):
83101
if response_json["error_code"] == "LinkMustHaveTextSpecified":
84102
raise LinkMustHaveTextSpecified(response_json["message"])
85103

104+
if response_json["error_code"] == "NoRenderFound":
105+
raise NoRenderFound(response_json["message"])
106+
107+
if response_json["error_code"] == "MultipleRendersFound":
108+
raise MultipleRendersFound(response_json["message"])
109+
86110
response.raise_for_status()
87111

88112
return response_json
@@ -106,9 +130,16 @@ def get_plain_source_tree(self, plain_source, loaded_templates):
106130

107131
payload = {"plain_source": plain_source, "loaded_templates": loaded_templates}
108132

109-
return self.post_request(endpoint_url, headers, payload)
133+
return self.post_request(endpoint_url, headers, payload, None)
110134

111-
def render_functional_requirement(self, frid, plain_source_tree, linked_resources, existing_files_content):
135+
def render_functional_requirement(
136+
self,
137+
frid,
138+
plain_source_tree,
139+
linked_resources,
140+
existing_files_content,
141+
run_state: RunState,
142+
):
112143
"""
113144
Renders the content of a functional requirement based on the provided ID,
114145
plain source tree, and existing files' content.
@@ -138,9 +169,17 @@ def render_functional_requirement(self, frid, plain_source_tree, linked_resource
138169
"existing_files_content": existing_files_content,
139170
}
140171

141-
return self.post_request(endpoint_url, headers, payload)
172+
return self.post_request(endpoint_url, headers, payload, run_state)
142173

143-
def fix_unittests_issue(self, frid, plain_source_tree, linked_resources, existing_files_content, unittests_issue):
174+
def fix_unittests_issue(
175+
self,
176+
frid,
177+
plain_source_tree,
178+
linked_resources,
179+
existing_files_content,
180+
unittests_issue,
181+
run_state: RunState,
182+
):
144183
endpoint_url = f"{self.api_url}/fix_unittests_issue"
145184
headers = {"X-API-Key": self.api_key, "Content-Type": "application/json"}
146185

@@ -152,9 +191,9 @@ def fix_unittests_issue(self, frid, plain_source_tree, linked_resources, existin
152191
"unittests_issue": unittests_issue,
153192
}
154193

155-
return self.post_request(endpoint_url, headers, payload)
194+
return self.post_request(endpoint_url, headers, payload, run_state)
156195

157-
def refactor_source_files_if_needed(self, frid, files_to_check, existing_files_content):
196+
def refactor_source_files_if_needed(self, frid, files_to_check, existing_files_content, run_state: RunState):
158197
endpoint_url = f"{self.api_url}/refactor_source_files_if_needed"
159198
headers = {"X-API-Key": self.api_key, "Content-Type": "application/json"}
160199

@@ -164,10 +203,16 @@ def refactor_source_files_if_needed(self, frid, files_to_check, existing_files_c
164203
"existing_files_content": existing_files_content,
165204
}
166205

167-
return self.post_request(endpoint_url, headers, payload)
206+
return self.post_request(endpoint_url, headers, payload, run_state)
168207

169208
def render_conformance_tests(
170-
self, frid, functional_requirement_id, plain_source_tree, linked_resources, existing_files_content
209+
self,
210+
frid,
211+
functional_requirement_id,
212+
plain_source_tree,
213+
linked_resources,
214+
existing_files_content,
215+
run_state: RunState,
171216
):
172217
endpoint_url = f"{self.api_url}/render_conformance_tests"
173218
headers = {"X-API-Key": self.api_key, "Content-Type": "application/json"}
@@ -180,9 +225,15 @@ def render_conformance_tests(
180225
"existing_files_content": existing_files_content,
181226
}
182227

183-
return self.post_request(endpoint_url, headers, payload)
228+
return self.post_request(endpoint_url, headers, payload, run_state)
184229

185-
def generate_folder_name_from_functional_requirement(self, frid, functional_requirement, existing_folder_names):
230+
def generate_folder_name_from_functional_requirement(
231+
self,
232+
frid,
233+
functional_requirement,
234+
existing_folder_names,
235+
run_state: RunState,
236+
):
186237
endpoint_url = f"{self.api_url}/generate_folder_name_from_functional_requirement"
187238
headers = {"X-API-Key": self.api_key, "Content-Type": "application/json"}
188239

@@ -192,7 +243,7 @@ def generate_folder_name_from_functional_requirement(self, frid, functional_requ
192243
"existing_folder_names": existing_folder_names,
193244
}
194245

195-
return self.post_request(endpoint_url, headers, payload)
246+
return self.post_request(endpoint_url, headers, payload, run_state)
196247

197248
def fix_conformance_tests_issue(
198249
self,
@@ -206,6 +257,7 @@ def fix_conformance_tests_issue(
206257
acceptance_tests,
207258
conformance_tests_issue,
208259
implementation_fix_count,
260+
run_state: RunState,
209261
):
210262
endpoint_url = f"{self.api_url}/fix_conformance_tests_issue"
211263
headers = {"X-API-Key": self.api_key, "Content-Type": "application/json"}
@@ -225,7 +277,7 @@ def fix_conformance_tests_issue(
225277
if acceptance_tests is not None:
226278
payload["acceptance_tests"] = acceptance_tests
227279

228-
return self.post_request(endpoint_url, headers, payload)
280+
return self.post_request(endpoint_url, headers, payload, run_state)
229281

230282
def render_acceptance_tests(
231283
self,
@@ -235,6 +287,7 @@ def render_acceptance_tests(
235287
existing_files_content,
236288
conformance_tests_files,
237289
acceptance_test,
290+
run_state: RunState,
238291
):
239292
"""
240293
Renders acceptance tests based on the provided parameters.
@@ -267,4 +320,4 @@ def render_acceptance_tests(
267320
"acceptance_test": acceptance_test,
268321
}
269322

270-
return self.post_request(endpoint_url, headers, payload)
323+
return self.post_request(endpoint_url, headers, payload, run_state)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
verbose: true
2+
debug: true
3+
unittests-script: ../../test_scripts/run_unittests_golang.sh
4+
conformance-tests-script: ../../test_scripts/run_conformance_tests_golang.sh

examples/example_hello_world_golang/run.sh

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,24 @@
11
# Store command line arguments
22
VERBOSE=0
3-
API_ENDPOINT=""
3+
CONFIG_FILE="config.yaml"
44

5-
# Parse command line arguments
6-
while [[ $# -gt 0 ]]; do
7-
case $1 in
8-
-v|--verbose)
9-
VERBOSE=1
10-
shift # Remove --verbose from processing
11-
;;
12-
--render-range)
13-
shift
14-
RENDER_RANGE=$1
15-
shift
16-
;;
17-
--render-from)
18-
shift
19-
RENDER_FROM=$1
20-
shift
21-
;;
22-
--api)
23-
API_ENDPOINT="$2"
24-
shift # Remove --api from processing
25-
shift # Remove the API endpoint value from processing
26-
;;
27-
*)
28-
# Unknown option
29-
echo "Unknown option: $1"
30-
exit 1
31-
;;
32-
esac
33-
done
5+
# Check if verbose is set in config.yaml and set VERBOSE accordingly
6+
if grep -q "verbose: true" "$CONFIG_FILE" 2>/dev/null; then
7+
VERBOSE=1
8+
fi
349

3510
if [ $VERBOSE -eq 1 ]; then
3611
echo "Running Go lang hello world example in verbose mode."
3712
fi
3813

39-
# Construct the command with optional parameters
40-
CMD="python ../../plain2code.py hello_world_golang.plain --unittests-script=../../test_scripts/run_unittests_golang.sh --conformance-tests-script=../../test_scripts/run_conformance_tests_golang.sh ${VERBOSE:+-v} ${RENDER_RANGE:+"--render-range=$RENDER_RANGE"} ${RENDER_FROM:+"--render-from=$RENDER_FROM"} ${API_ENDPOINT:+"--api $API_ENDPOINT"}"
41-
42-
# Removing all the conformance tests before rendering the hello world example.
43-
rm -rf conformance_tests
14+
# Check if render-range and render-from exist in config.yaml
15+
if ! (grep -q "render-range:" $CONFIG_FILE || grep -q "render-from:" $CONFIG_FILE); then
16+
echo "Removing conformance tests folder"
17+
rm -rf conformance_tests
18+
fi
4419

4520
# Execute the command
46-
$CMD
21+
python ../../plain2code.py hello_world_golang.plain
4722

4823
# Check if the plain2code command failed
4924
if [ $? -ne 0 ]; then
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
unittests-script: ../../test_scripts/run_unittests_python.sh
2+
conformance-tests-script: ../../test_scripts/run_conformance_tests_python.sh
3+
v: true # verbose flag, defaults to false if not set
4+
debug: true # debug flag, defaults to false if not set
5+
api: http://127.0.0.1:5000

examples/example_hello_world_python/run.sh

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,23 @@
1-
# Store command line arguments
1+
CONFIG_FILE="config.yaml"
22
VERBOSE=0
3-
API_ENDPOINT=""
43

5-
# Parse command line arguments
6-
while [[ $# -gt 0 ]]; do
7-
case $1 in
8-
-v|--verbose)
9-
VERBOSE=1
10-
shift # Remove --verbose from processing
11-
;;
12-
--render-range )
13-
shift
14-
RENDER_RANGE=$1
15-
shift
16-
;;
17-
--render-from)
18-
shift
19-
RENDER_FROM=$1
20-
shift
21-
;;
22-
--api)
23-
API_ENDPOINT="$2"
24-
shift # Remove --api from processing
25-
shift # Remove the API endpoint value from processing
26-
;;
27-
*)
28-
# Unknown option
29-
echo "Unknown option: $1"
30-
exit 1
31-
;;
32-
esac
33-
done
4+
# Check if verbose is set in config.yaml and set VERBOSE accordingly
5+
if grep -q "verbose: true" "$CONFIG_FILE" 2>/dev/null; then
6+
VERBOSE=1
7+
fi
348

359
if [ $VERBOSE -eq 1 ]; then
36-
echo "Running Python hello world example in verbose mode."
10+
echo "Running the hello world example for Python in verbose mode."
3711
fi
3812

39-
# Construct the command with optional parameters
40-
CMD="python ../../plain2code.py hello_world_python.plain --unittests-script=../../test_scripts/run_unittests_python.sh --conformance-tests-script=../../test_scripts/run_conformance_tests_python.sh ${VERBOSE:+-v} ${RENDER_RANGE:+"--render-range=$RENDER_RANGE"} ${RENDER_FROM:+"--render-from=$RENDER_FROM"} ${API_ENDPOINT:+"--api $API_ENDPOINT"}"
41-
42-
if [ -z "$RENDER_RANGE" ] || [ -z "$RENDER_FROM" ]; then
13+
# Check if render-range and render-from exist in config.yaml
14+
if ! (grep -q "render-range:" $CONFIG_FILE || grep -q "render-from:" $CONFIG_FILE); then
4315
echo "Removing conformance tests folder"
4416
rm -rf conformance_tests
4517
fi
4618

4719
# Execute the command
48-
$CMD
20+
python ../../plain2code.py hello_world_python.plain
4921

5022
# Check if the plain2code command failed
5123
if [ $? -ne 0 ]; then

examples/example_hello_world_react/run.sh

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,25 @@
1-
# Store command line arguments
21
VERBOSE=0
3-
API_ENDPOINT=""
4-
5-
# Parse command line arguments
6-
while [[ $# -gt 0 ]]; do
7-
case $1 in
8-
-v|--verbose)
9-
VERBOSE=1
10-
shift # Remove --verbose from processing
11-
;;
12-
--api)
13-
API_ENDPOINT="$2"
14-
shift # Remove --api from processing
15-
shift # Remove the API endpoint value from processing
16-
;;
17-
*)
18-
# Unknown option
19-
echo "Unknown option: $1"
20-
exit 1
21-
;;
22-
esac
23-
done
2+
CONFIG_FILE="config.yaml"
3+
4+
# Check if verbose is set in config.yaml and set VERBOSE accordingly
5+
if grep -q "verbose: true" "$CONFIG_FILE" 2>/dev/null; then
6+
VERBOSE=1
7+
fi
8+
249

2510
if [ $VERBOSE -eq 1 ]; then
2611
echo "Running the hello world example for React in verbose mode."
2712
fi
2813

29-
# Construct the command with optional parameters
30-
CMD="python ../../plain2code.py hello_world_react.plain --conformance-tests-script=../../test_scripts/run_conformance_tests_cypress.sh ${VERBOSE:+-v} ${RENDER_RANGE:+"--render-range=$RENDER_RANGE"} ${RENDER_FROM:+"--render-from=$RENDER_FROM"} ${API_ENDPOINT:+"--api $API_ENDPOINT"}"
31-
32-
# Removing all the conformance tests before rendering the hello world example.
33-
rm -rf conformance_tests
34-
rm -rf node_conformance_tests
14+
# Check if render-range and render-from exist in config.yaml
15+
if ! (grep -q "render-range:" $CONFIG_FILE || grep -q "render-from:" $CONFIG_FILE); then
16+
echo "Removing conformance tests folder"
17+
rm -rf conformance_tests
18+
rm -rf node_conformance_tests
19+
fi
3520

3621
# Execute the command
37-
$CMD
22+
python ../../plain2code.py hello_world_react.plain
3823

3924
# Check if the plain2code command failed
4025
if [ $? -ne 0 ]; then

0 commit comments

Comments
 (0)