-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenModels.py
More file actions
executable file
·156 lines (132 loc) · 6.07 KB
/
Copy pathgenModels.py
File metadata and controls
executable file
·156 lines (132 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/python3
###
# Laravel API Model and Form Generation Application
# Copyright (C) 2020 Thomas Grothe, grothedev@gmail.com
# This file is part of Laravel API Model and Form Generation Application.
# Laravel API Model and Form Generation Application is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Laravel API Model and Form Generation Application is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Laravel API Model and Form Generation Application. If not, see <https://www.gnu.org/licenses/>.
###
import os
import sys
import subprocess
prev=""
props=[] #array of properties of model
rprops=False #reading properties of model
columnTypes=[] #array of datatypes of the properties, in same order as props array
model="" #the name of the model currently being constructed
models=[]
ver=`./artisan --version | cut -d' ' -f3 | cut -d'.' -f1` #check if laravel version >7 to use app/Models/
#runs the artisan commands to generate the files for the current model and properties and modifies the files as necessary.
#note that global variables are used
function makeModel {
if [[ $((ver)) -gt 7 ]]; then
model_filename="app/Models/${model}.php"
else
model_filename="app/${model}.php"
fi
#check if model already exists
if [[ -f ${model_filename} ]]; then
return
fi
#have artisan make the model file
./artisan make:model --migration "$model"
echo "artisan made model "${model}
#get the fields ready to add to the model definition file
fields=''
for p in ${props[@]}
do
fields+="'"$p"', "
done
#insert fillable values into model definition file
#sed -i "s!{!{\n protected \$fillable = [$fields];!g" ${model_filename}
sed -i "s!}! protected \$fillable = [$fields];\n\n}!g" ${model_filename}
#make the controller
./genController.sh ${model}
#add to static global PHP const?
#insert fields into database migration file
for mf in `ls database/migrations/*`; do
model_plural=`./pluralize ${model} | tail -n 1`
if [[ `echo $mf | sed 's/_//g' | grep -i ${model_plural}` ]]; then #this is migration file we want to modify
#sed -i "s/id();/id(); \/\/TODO: fill in these fields: ${fields}/g" $mf
for (( i=0; i<${#props[*]}; i++ )); do
if [[ ${columnTypes[$i]} != "" ]]; then
if [[ ${columnTypes[$i]:0:1} =~ ^[A-Z] ]]; then #the column type is another model, because starts with a capital letter
fieldLC=`echo ${props[$i]} | tr '[:upper:]' '[:lower:]'`
fieldPlural=`./pluralize ${fieldLC} | tail -n 1`
datatypeLC=`echo ${columnTypes[$i]} | tr '[:upper:]' '[:lower:]'`
datatypePlural=`./pluralize ${datatypeLC} | tail -n 1`
#TODO is this ever a pivot table?
sed -i "s/id();/id();\n \$table->integer(\'${fieldLC}\')->unsigned()->nullable()->default(null);/g" $mf
sed -i "s/default(null);/default(null);\n \$table->foreign(\'${fieldLC}\')->references(\'id\')->on(\'${datatypePlural}\')->onDelete(\'cascade\');/g" $mf #this is not ideal but it will work for the time being
#insert the model relation function
sed -i "s!}! public function ${fieldLC}(){\n return \$this->hasOne('App\\Models\\${columnTypes[$i]}');\n }\n\n}!g" ${model_filename}
else #column type is a normal database primitive
sed -i "s/id();/id();\n \$table->${columnTypes[$i]}(\'${props[$i]}\');/g" $mf
fi
else
sed -i "s/id();/id();\n \/\/TODO: ${props[$i]}/g" $mf
fi
done
break
fi
done
}
#parses the property and column type from the given string which should follow the standard as defined in the model-definition file
function addProperty {
if [[ -z $1 ]]; then return -1; fi
props+=(`echo ${1} | cut -d':' -f1`)
if [[ ${1} == *":"* ]]; then #has a column type
columnTypes+=(`echo ${1} | cut -d':' -f2`)
else
columnTypes+=('')
fi
}
#Begin
while read m; do
if [[ -z ${m} || ${m:0:1} == "#" ]]; then #this line is a comment or blank
continue;
fi
m=`echo ${m} | cut -d# -f1 | cut -d' ' -f1` #remove trailing comment and whitespace
if [[ ${m:0:1} != [a-zA-Z] && ${m:0:1} != "-" ]]; then #malformed model definition file, model should only begin with a letter
echo "this model definition file is malformed, exiting."
echo " \'${m}\' "
exit -1
fi
if [ $rprops ]; then
if [ ${m:0:1} == "-" ]; then #we are reading props of a model, append another
addProperty ${m:1}
else #this is the start of a definition of a new model. write the previous one to application
if [[ -z $model ]]; then
model=$m
models+=(${m})
continue;
fi
echo "making model: "${model}
makeModel
props=()
columnTypes=()
model=$m #new model name
models+=(${m})
fi
elif [ ${m:0:1} == "-" ]; then
rprops=true
addProperty ${m:1}
else
model=$m
fi
done < models.txt
makeModel #one more model to make
./genSiteController.sh ${models[@]} #the main site controller for functions not specific to models
./genViewModeList.sh #a page for meta-info on each model
#What is left to do after this script runs:
# relationships between eloquent models
#
# TODO what else can reasonably be included in models.txt to extend automatic code generation?