summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven <steven.vasilogianis@gmail.com>2021-06-20 13:17:01 -0400
committerSteven <steven.vasilogianis@gmail.com>2021-06-20 13:17:01 -0400
commit480bc6e9640324b55a2b4f6326a0f6f3d897eb6e (patch)
tree540eea175e35620a700cc9f4de83f2256f2c1741
parent4b977f97fdb65f3b7fba39386ead877b0b8a307b (diff)
Allow paramters to be overridden by shell variables, i.e.: cubby_width=200 ./shopencad.sh input.scad
-rwxr-xr-xshopenscad.sh32
1 files changed, 25 insertions, 7 deletions
diff --git a/shopenscad.sh b/shopenscad.sh
index dcdb8e6..459dee1 100755
--- a/shopenscad.sh
+++ b/shopenscad.sh
@@ -35,7 +35,6 @@ function chomp () {
35 strip_leading_spaces |strip_trailing_spaces 35 strip_leading_spaces |strip_trailing_spaces
36} 36}
37 37
38
39function parse_variable () { 38function parse_variable () {
40 #sed -r 's/^([a-zA-Z0-9_]+).*/\1/' 39 #sed -r 's/^([a-zA-Z0-9_]+).*/\1/'
41 sed -r 's/^([^=]+)=.*/\1/' 40 sed -r 's/^([^=]+)=.*/\1/'
@@ -47,7 +46,6 @@ function parse_value () {
47 sed -r 's/.*=\s*([^;]+?).*/\1/' 46 sed -r 's/.*=\s*([^;]+?).*/\1/'
48} 47}
49 48
50
51function val_to_filename () { 49function val_to_filename () {
52 echo "$1" |sed -r 's/"//g' |sed -r 's/ /_/g' 50 echo "$1" |sed -r 's/"//g' |sed -r 's/ /_/g'
53} 51}
@@ -60,26 +58,46 @@ function strip_after_hidden () {
60 echo "$1" |sed -n '/\/\* \[Hidden\] \*\//q;p' cubbies.scad 58 echo "$1" |sed -n '/\/\* \[Hidden\] \*\//q;p' cubbies.scad
61} 59}
62 60
61function strip_trailing () {
62 trailing="$1"
63 input="$2"
64 echo ${input%${trailing}}
65}
66
63function shopenscad () { 67function shopenscad () {
64 input_file="$1" 68 input_file="$1"
65 output_params="" 69 output_params=""
66 output_filename="" 70 output_filename=""
67 output_sh="" 71 all=""
72 vals_only=""
73 val_sep="-"
74
68 while IFS= read -r line; do 75 while IFS= read -r line; do
69 [ "$line" ] || continue; 76 [ "$line" ] || continue;
70 clean="$(echo -n "$line" |chomp)" 77 clean="$(echo -n "$line" |chomp)"
71 var=$(echo -n "$clean" |parse_variable |chomp) 78 var=$(echo -n "$clean" |parse_variable |chomp)
72 val=$(echo -n "$clean" |parse_value |chomp) 79
80 # use value provided on command line preferentially to any values in the .scad file
81 val=""
82 if [[ -n "${!var}" ]]; then
83 val=${!var}
84 else
85 val=$(echo -n "$clean" |parse_value |chomp)
86 fi
87
73 if [ "$var" ] && [ "$val" ]; then 88 if [ "$var" ] && [ "$val" ]; then
74 output_params+="-D '$var=$val' " 89 output_params+="-D '$var=$val' "
75 output_sh+="${var}=${val}\n"
76 output_filename+="$(val_to_filename "$val")-" 90 output_filename+="$(val_to_filename "$val")-"
91 vals_only+="$(val_to_filename "$val")${val_sep}"
92 all+="${var}=$(val_to_filename "$val")${val_sep}"
77 fi; 93 fi;
94
78 done < <(echo "$(cat "$input_file")" | 95 done < <(echo "$(cat "$input_file")" |
79 strip_comments |strip_after_hidden |strip_nonassignments |strip_trailing_semicolon) 96 strip_comments |strip_after_hidden |strip_nonassignments |strip_trailing_semicolon)
80 97
81 openscad_str="$openscad "$scad_file" "$output_params" -o $(strip_last_char "$output_filename").stl" 98 openscad_str="$openscad "$scad_file" "$output_params" -o $(strip_trailing "-" "${vals_only}").stl"
82 echo ${openscad_str} 99 echo "${openscad_str}"
100
83} 101}
84 102
85shopenscad "$scad_file" 103shopenscad "$scad_file"