github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/cmd/generates/modules/deploys.go (about) 1 /* 2 * Copyright 2023 Wang Min Xiang 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 package modules 19 20 import ( 21 "bytes" 22 "context" 23 "fmt" 24 "github.com/aacfactory/errors" 25 "github.com/aacfactory/gcg" 26 "os" 27 "path/filepath" 28 ) 29 30 func NewDeploysFile(dir string, services Services) (file CodeFileWriter) { 31 file = &DeploysFile{ 32 filename: filepath.ToSlash(filepath.Join(dir, "fns.go")), 33 services: services, 34 } 35 return 36 } 37 38 type DeploysFile struct { 39 filename string 40 services Services 41 } 42 43 func (s *DeploysFile) Name() (name string) { 44 name = s.filename 45 return 46 } 47 48 func (s *DeploysFile) Write(ctx context.Context) (err error) { 49 if s.filename == "" { 50 return 51 } 52 if ctx.Err() != nil { 53 err = errors.Warning("modules: services write failed"). 54 WithMeta("kind", "services").WithMeta("file", s.Name()). 55 WithCause(ctx.Err()) 56 return 57 } 58 59 file := gcg.NewFileWithoutNote("modules") 60 file.FileComments("NOTE: this file has been automatically generated, DON'T EDIT IT!!!\n") 61 62 fn := gcg.Func() 63 fn.Name("endpoints") 64 fn.AddResult("v", gcg.Token("[]services.Service", gcg.NewPackage("github.com/aacfactory/fns/services"))) 65 body := gcg.Statements() 66 if s.services != nil && s.services.Len() > 0 { 67 body.Token("v = []services.Service{").Line() 68 for _, service := range s.services { 69 body.Tab().Token(fmt.Sprintf("%s.Service()", service.PathIdent), gcg.NewPackage(service.Path)).Symbol(",").Line() 70 } 71 body.Token("}").Line() 72 } 73 body.Return() 74 fn.Body(body) 75 file.AddCode(fn.Build()) 76 77 buf := bytes.NewBuffer([]byte{}) 78 79 renderErr := file.Render(buf) 80 if renderErr != nil { 81 err = errors.Warning("modules: services code file write failed"). 82 WithMeta("kind", "services").WithMeta("file", s.Name()). 83 WithCause(renderErr) 84 return 85 } 86 87 writer, openErr := os.OpenFile(s.Name(), os.O_CREATE|os.O_TRUNC|os.O_RDWR|os.O_SYNC, 0644) 88 if openErr != nil { 89 err = errors.Warning("modules: services code file write failed"). 90 WithMeta("kind", "services").WithMeta("file", s.Name()). 91 WithCause(openErr) 92 return 93 } 94 95 n := 0 96 bodyLen := buf.Len() 97 content := buf.Bytes() 98 for n < bodyLen { 99 nn, writeErr := writer.Write(content[n:]) 100 if writeErr != nil { 101 err = errors.Warning("modules: services code file write failed"). 102 WithMeta("kind", "services").WithMeta("file", s.Name()). 103 WithCause(writeErr) 104 return 105 } 106 n += nn 107 } 108 syncErr := writer.Sync() 109 if syncErr != nil { 110 err = errors.Warning("modules: services code file write failed"). 111 WithMeta("kind", "services").WithMeta("file", s.Name()). 112 WithCause(syncErr) 113 return 114 } 115 closeErr := writer.Close() 116 if closeErr != nil { 117 err = errors.Warning("modules: services code file write failed"). 118 WithMeta("kind", "services").WithMeta("file", s.Name()). 119 WithCause(closeErr) 120 return 121 } 122 return 123 }