github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/cmd/fns/initialization/base/modules.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 base
    19  
    20  import (
    21  	"context"
    22  	"github.com/aacfactory/errors"
    23  	"github.com/aacfactory/fns/cmd/generates/files"
    24  	"os"
    25  	"path/filepath"
    26  	"strings"
    27  )
    28  
    29  func NewModulesFile(path string, dir string) (mf *ModulesFile, err error) {
    30  	if !filepath.IsAbs(dir) {
    31  		dir, err = filepath.Abs(dir)
    32  		if err != nil {
    33  			err = errors.Warning("fns: new modules file failed").WithCause(err).WithMeta("dir", dir)
    34  			return
    35  		}
    36  	}
    37  	dir = filepath.ToSlash(filepath.Join(dir, "modules"))
    38  	mf = &ModulesFile{
    39  		path: path,
    40  		dir:  dir,
    41  	}
    42  	return
    43  }
    44  
    45  type ModulesFile struct {
    46  	path string
    47  	dir  string
    48  }
    49  
    50  func (f *ModulesFile) Name() (name string) {
    51  	name = f.dir
    52  	return
    53  }
    54  
    55  func (f *ModulesFile) Write(ctx context.Context) (err error) {
    56  	if !files.ExistFile(f.dir) {
    57  		mdErr := os.MkdirAll(f.dir, 0644)
    58  		if mdErr != nil {
    59  			err = errors.Warning("fns: modules file write failed").WithCause(mdErr).WithMeta("dir", f.dir)
    60  			return
    61  		}
    62  	}
    63  	err = f.writeServices(ctx)
    64  	if err != nil {
    65  		return
    66  	}
    67  	err = f.writeExamples(ctx)
    68  	if err != nil {
    69  		return
    70  	}
    71  	return
    72  }
    73  
    74  func (f *ModulesFile) writeServices(_ context.Context) (err error) {
    75  	// services
    76  	const (
    77  		exports = `package modules
    78  
    79  import (
    80  	"github.com/aacfactory/fns/services"
    81  )
    82  
    83  func Services() (v []services.Service) {
    84  	v = append(
    85  		dependencies(),
    86  		endpoints()...,
    87  	)
    88  	return
    89  }
    90  
    91  func dependencies() (v []services.Service) {
    92  	v = []services.Service{
    93  		// add dependencies here
    94  	}
    95  	return
    96  }`
    97  	)
    98  	servicesFilename := filepath.ToSlash(filepath.Join(f.dir, "services.go"))
    99  	writeErr := os.WriteFile(servicesFilename, []byte(exports), 0644)
   100  	if writeErr != nil {
   101  		err = errors.Warning("fns: modules file write failed").WithCause(writeErr).WithMeta("filename", servicesFilename)
   102  		return
   103  	}
   104  	// fns
   105  	const (
   106  		fns = `// NOTE: this file has been automatically generated, DON'T EDIT IT!!!
   107  
   108  package modules
   109  
   110  import (
   111  	"#path#/modules/examples"
   112  	"github.com/aacfactory/fns/services"
   113  )
   114  
   115  func endpoints() (v []services.Service) {
   116  	v = []services.Service{
   117  		
   118  	}
   119  	return
   120  }
   121  `
   122  	)
   123  	fnsFilename := filepath.ToSlash(filepath.Join(f.dir, "fns.go"))
   124  	writeErr = os.WriteFile(fnsFilename, []byte(strings.ReplaceAll(fns, "#path#", f.path)), 0644)
   125  	if writeErr != nil {
   126  		err = errors.Warning("fns: modules file write failed").WithCause(writeErr).WithMeta("filename", servicesFilename)
   127  		return
   128  	}
   129  	return
   130  }
   131  
   132  func (f *ModulesFile) writeExamples(_ context.Context) (err error) {
   133  	dir := filepath.ToSlash(filepath.Join(f.dir, "examples"))
   134  	if !files.ExistFile(dir) {
   135  		mdErr := os.MkdirAll(dir, 0600)
   136  		if mdErr != nil {
   137  			err = errors.Warning("fns: new modules file failed").WithCause(mdErr).WithMeta("dir", dir)
   138  			return
   139  		}
   140  	}
   141  	// doc
   142  	const (
   143  		doc = `// Package examples
   144  // @service examples
   145  // @title Examples
   146  // @description Example service
   147  package examples`
   148  	)
   149  	err = os.WriteFile(filepath.ToSlash(filepath.Join(dir, "doc.go")), []byte(doc), 0644)
   150  	if err != nil {
   151  		err = errors.Warning("fns: modules file write failed").WithCause(err).WithMeta("filename", filepath.ToSlash(filepath.Join(dir, "doc.go")))
   152  		return
   153  	}
   154  	// hello
   155  	const (
   156  		hello = `package examples
   157  
   158  import (
   159  	"github.com/aacfactory/fns/context"
   160  	"fmt"
   161  	"github.com/aacfactory/errors"
   162  )
   163  
   164  // HelloParam
   165  // @title Hello function param
   166  // @description Hello function param
   167  type HelloParam struct {
   168  	// World
   169  	// @title Name
   170  	// @description Name
   171  	// @validate-message-i18n >>>
   172  	// zh: 世界是必要的
   173  	// en: world is required
   174  	// <<<
   175  	World string ` + "`" + `json:"world" validate:"required" validate-message:"world_required"` + "`" + `
   176  }
   177  
   178  // HelloResults
   179  // @title Hello Results
   180  // @description Hello Results
   181  type HelloResults []string
   182  
   183  // hello
   184  // @fn hello
   185  // @readonly
   186  // @barrier
   187  // @errors >>>
   188  // examples_hello_failed
   189  // zh: 错误
   190  // en: failed
   191  // <<<
   192  // @title Hello
   193  // @description >>>
   194  // Hello
   195  // <<<
   196  func hello(ctx context.Context, param HelloParam) (result HelloResults, err error) {
   197  	if param.World == "error" {
   198  		err = errors.ServiceError("examples_hello_failed")
   199  		return
   200  	}
   201  	result = HelloResults{fmt.Sprintf("hello %s!", param.World)}
   202  	return
   203  }
   204  `
   205  	)
   206  
   207  	err = os.WriteFile(filepath.ToSlash(filepath.Join(dir, "hello.go")), []byte(hello), 0644)
   208  	if err != nil {
   209  		err = errors.Warning("fns: modules file write failed").WithCause(err).WithMeta("filename", filepath.ToSlash(filepath.Join(dir, "hello.go")))
   210  		return
   211  	}
   212  
   213  	return
   214  }