github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/cmd/fns/initialization/base/work.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  	"golang.org/x/mod/modfile"
    25  	"os"
    26  	"path/filepath"
    27  )
    28  
    29  func NewWorkFile(goVersion string, path string, dir string) (f *WorkFile, err error) {
    30  	if !filepath.IsAbs(dir) {
    31  		dir, err = filepath.Abs(dir)
    32  		if err != nil {
    33  			err = errors.Warning("fns: new work file failed").WithCause(err).WithMeta("dir", dir)
    34  			return
    35  		}
    36  	}
    37  	f = &WorkFile{
    38  		path:      path,
    39  		modDir:    filepath.ToSlash(dir),
    40  		filename:  filepath.ToSlash(filepath.Join(filepath.Dir(dir), "go.work")),
    41  		goVersion: goVersion,
    42  	}
    43  	return
    44  }
    45  
    46  type WorkFile struct {
    47  	path      string
    48  	modDir    string
    49  	filename  string
    50  	goVersion string
    51  }
    52  
    53  func (f *WorkFile) Name() (name string) {
    54  	name = f.filename
    55  	return
    56  }
    57  
    58  func (f *WorkFile) Write(ctx context.Context) (err error) {
    59  	var p []byte
    60  	if files.ExistFile(f.filename) {
    61  		p, err = os.ReadFile(f.filename)
    62  		if err != nil {
    63  			err = errors.Warning("fns: work file write failed").WithCause(err).WithMeta("filename", f.filename)
    64  			return
    65  		}
    66  	}
    67  	wf, parseErr := modfile.ParseWork(f.filename, p, nil)
    68  	if parseErr != nil {
    69  		err = errors.Warning("fns: work file write failed").WithCause(parseErr).WithMeta("filename", f.filename)
    70  		return
    71  	}
    72  	if wf.Go == nil {
    73  		versionErr := wf.AddGoStmt(f.goVersion)
    74  		if versionErr != nil {
    75  			err = errors.Warning("fns: work file write failed").WithCause(versionErr).WithMeta("filename", f.filename)
    76  			return
    77  		}
    78  	}
    79  
    80  	modDir := filepath.ToSlash(f.modDir)
    81  	workDir := filepath.ToSlash(filepath.Dir(modDir))
    82  
    83  	wf.AddNewUse(f.modDir[len(workDir)+1:], f.path)
    84  	b := modfile.Format(wf.Syntax)
    85  	writeErr := os.WriteFile(f.filename, b, 0644)
    86  	if writeErr != nil {
    87  		err = errors.Warning("fns: work file write failed").WithCause(writeErr).WithMeta("filename", f.filename)
    88  		return
    89  	}
    90  	return
    91  }