-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproc3_produce.go
218 lines (182 loc) · 6.81 KB
/
proc3_produce.go
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// MIT License
//
// Copyright (c) 2021 @gxlb
// Url:
// https://github.com/gxlb
// https://gitee.com/gxlb
// AUTHORS:
// Ally Dale <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package gogp
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
)
// gen .go file from .gp and .gpg
func (this *gopgProcessor) procStep3Produce() (err error) {
//normal process
gpPath := this.getGpFullPath("")
gpgDir := filepath.Dir(this.gpgPath)
gpName := strings.TrimSuffix(filepath.Base(gpPath), gpExt)
codePath := this.getProductFilePath(gpgDir, gpName, this.getCodeFileSuffix(this.section))
this.loadCodeFile(codePath) //load code file, ignore error
if this.gpPath != gpPath { //load gp file if needed
if err = this.loadGpFile(gpPath); err != nil {
return
}
}
replacedGp := ""
if replacedGp, err = this.doGpReplace(this.gpPath, this.gpContent, this.section, 0, false); err != nil {
return
}
if err = this.saveCodeFile(replacedGp); err != nil { //save code to file
return
}
return
}
func (this *gopgProcessor) doPredefReplace(gpPath, content, section string, nDepth int) (rep string) {
pathIdentify := fmt.Sprintf("%s|%s", relateGoPath(gpPath), relateGoPath(filepath.Dir(this.gpgPath))) //gp file+gpg path=unique
this.replaces.clear()
for _content, needReplace, i := content, true, 0; needReplace && i < 3; _content, i = rep, i+1 {
needReplace = false
rep = gogpExpPretreatAll.ReplaceAllStringFunc(_content, func(src string) (_rep string) {
//[]string{"", "IGNORE", "REQ", "REQP", "REQN", "REQGPG", "REQCONTENT", "GPGCFG", "ONCE", "REPSRC", "REPDST", "COMMENT"}
elem := gogpExpPretreatAll.FindAllStringSubmatch(src, -1)[0]
ignore, req, reqp, reqn, reqgpg, reqcontent, gpgcfg, once, repsrc, repdst, comment :=
elem[1], elem[2], elem[3], elem[4], elem[5], elem[6], elem[7], elem[8], elem[9], elem[10], elem[11]
if reqgpg != "" && reqn == "" { //section name is config from gpg file
reqn = this.getGpgCfg(section, reqgpg, true)
}
if !optSilence && i > 1 {
fmt.Printf("##src=[%#v]\n i=%d ignore=[%s] req=[%s] reqp=[%s] reqn=[%s] reqgpg=[%s] gpgcfg=[%s] once=[%s] repsrc=[%s] repdst=[%s]\n",
src, i, ignore, req, reqp, reqn, reqgpg, gpgcfg, once, repsrc, repdst)
}
needReplace = true
switch {
case comment != "":
_rep = ""
case ignore != "":
_rep = "\n\n"
case reqp != "":
if reqcontent == "" {
//require process
if r, _, err := this.procRequireReplacement(src, section, nDepth+1); err == nil {
_rep = r
} else {
fmt.Println(err)
}
req, reqn, reqcontent = req, reqn, reqcontent //never use
} else {
_rep = reqcontent
}
case gpgcfg != "":
_rep = this.getGpgCfg(section, gpgcfg, true)
case once != "":
if _, ok := onceMap[pathIdentify]; ok { //check if has processed this file
_rep = "\n\n"
if debug {
fmt.Printf("[gogp debug]: %s GOGP_ONCE(%s:%s) ignore [%#v]\n", this.step, pathIdentify, section, once)
}
} else {
_rep = fmt.Sprintf("\n\n%s\n\n", once)
if debug {
fmt.Printf("[gogp debug]: %s GOGP_ONCE(%s:%s) ok [%#v]\n", this.step, pathIdentify, section, once)
}
}
case repsrc != "":
_rep = ""
this.replaces.insert(repdst, repsrc, true)
if debug {
fmt.Printf("[debug]%s %s %s replace [%s] -> [%s]\n", gpPath, section, src, repsrc, repdst)
}
default:
fmt.Printf("[gogp error]: %s invalid predef statement [%#v]\n", this.step, src)
}
return
})
}
if this.step == gogpStepPRODUCE { //prevent gen #GOGP_ONCE code twice when gen code
onceMap[pathIdentify] = true //record processed gp file
}
return
}
func (this *gopgProcessor) doGpReplace(gpPath, content, section string, nDepth int, second bool) (replacedGp string, err error) {
_path := fmt.Sprintf("%s|%s", relateGoPath(gpPath), relateGoPath(filepath.Dir(this.gpgPath))) //gp file+gpg path=unique
replacedGp = content
this.replaces.clear()
if this.step == gogpStepPRODUCE {
replacedGp = this.step3PretreatGpCodeSelector(replacedGp, section)
}
replacedGp = this.doPredefReplace(gpPath, replacedGp, section, nDepth)
//replaces keys that need be replacing
if this.replaces.Len() > 0 {
replacedGp, _ = this.replaces.doReplacing(replacedGp, this.gpgPath, true)
this.replaces.clear()
}
//gen code file content
this.buildMatches(section, gpPath, false, second)
replist := this.getReplist(second)
norep := 0
replacedGp, norep = replist.doReplacing(replacedGp, this.gpgPath, false)
this.nNoReplaceMathNum += norep
replacedGp = gogpExpEmptyLine.ReplaceAllString(replacedGp, "\n") //avoid multi empty lines
//remove more empty line
replacedGp = goFmt(replacedGp, this.gpgPath)
if this.nNoReplaceMathNum > 0 { //report error
s := fmt.Sprintf("[gogp error]: [%s:%s %s depth=%d] not every gp have been replaced\n", relateGoPath(this.gpgPath), relateGoPath(_path), replist.sectionName, nDepth)
fmt.Printf("----**result is:\n%s\n----**end\n", replacedGp)
err = fmt.Errorf(s)
}
return
}
func (this *gopgProcessor) saveCodeFile(body string) (err error) {
if optRemoveProductsOnly { //remove products only
this.nCodeFile++
this.remove(this.codePath)
return
}
if optForceUpdate || !strings.HasSuffix(this.codeContent, body) { //body change then save it,else skip it
var fout *os.File
if fout, err = os.OpenFile(this.codePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm); err != nil {
return
}
defer fout.Close()
wt := bufio.NewWriter(fout)
wt.WriteString(this.fileHead(this.gpPath, this.gpgPath, this.section))
wt.WriteByte('\n')
wt.WriteString(body)
if err = wt.Flush(); err != nil {
return
}
this.nCodeFile++
if !optSilence {
fmt.Printf(">>[gogp][%s] ok\n", relateGoPath(this.codePath))
}
} else {
this.nSkipCodeFile++
if !optSilence {
fmt.Printf(">>[gogp][%s] skip\n", relateGoPath(this.codePath))
}
}
return
}