github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/pkg/platform/runtime/buildscript/file.go (about)

     1  package buildscript
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/ActiveState/cli/pkg/localcommit"
     9  	"github.com/ActiveState/cli/pkg/platform/model/buildplanner"
    10  	"github.com/go-openapi/strfmt"
    11  
    12  	"github.com/ActiveState/cli/internal/constants"
    13  	"github.com/ActiveState/cli/internal/errs"
    14  	"github.com/ActiveState/cli/internal/fileutils"
    15  	"github.com/ActiveState/cli/internal/logging"
    16  	"github.com/ActiveState/cli/pkg/platform/authentication"
    17  	"github.com/ActiveState/cli/pkg/platform/runtime/buildexpression"
    18  )
    19  
    20  // projecter is a union between project.Project and setup.Targeter
    21  type projecter interface {
    22  	ProjectDir() string
    23  	Owner() string
    24  	Name() string
    25  }
    26  
    27  var ErrBuildscriptNotExist = errors.New("Build script does not exist")
    28  
    29  func ScriptFromProject(proj projecter) (*Script, error) {
    30  	path := filepath.Join(proj.ProjectDir(), constants.BuildScriptFileName)
    31  	return ScriptFromFile(path)
    32  }
    33  
    34  func ScriptFromFile(path string) (*Script, error) {
    35  	data, err := fileutils.ReadFile(path)
    36  	if err != nil {
    37  		if errors.Is(err, os.ErrNotExist) {
    38  			return nil, errs.Pack(err, ErrBuildscriptNotExist)
    39  		}
    40  		return nil, errs.Wrap(err, "Could not read build script from file")
    41  	}
    42  	return New(data)
    43  }
    44  
    45  func Initialize(path string, auth *authentication.Auth) error {
    46  	scriptPath := filepath.Join(path, constants.BuildScriptFileName)
    47  	script, err := ScriptFromFile(scriptPath)
    48  	if err == nil {
    49  		return nil // nothing to do, buildscript already exists
    50  	}
    51  	if !errors.Is(err, os.ErrNotExist) {
    52  		return errs.Wrap(err, "Could not read build script from file")
    53  	}
    54  
    55  	logging.Debug("Build script does not exist. Creating one.")
    56  	commitId, err := localcommit.Get(path)
    57  	if err != nil {
    58  		return errs.Wrap(err, "Unable to get the local commit ID")
    59  	}
    60  	buildplanner := buildplanner.NewBuildPlannerModel(auth)
    61  	expr, atTime, err := buildplanner.GetBuildExpressionAndTime(commitId.String())
    62  	if err != nil {
    63  		return errs.Wrap(err, "Unable to get the remote build expression and time")
    64  	}
    65  	script, err = NewFromBuildExpression(atTime, expr)
    66  	if err != nil {
    67  		return errs.Wrap(err, "Unable to convert build expression to build script")
    68  	}
    69  
    70  	logging.Debug("Initializing build script at %s", scriptPath)
    71  	err = fileutils.WriteFile(scriptPath, []byte(script.String()))
    72  	if err != nil {
    73  		return errs.Wrap(err, "Unable to write build script")
    74  	}
    75  
    76  	return nil
    77  }
    78  
    79  func Update(proj projecter, atTime *strfmt.DateTime, newExpr *buildexpression.BuildExpression) error {
    80  	script, err := ScriptFromProject(proj)
    81  	if err != nil {
    82  		return errs.Wrap(err, "Could not read build script")
    83  	}
    84  
    85  	newScript, err := NewFromBuildExpression(atTime, newExpr)
    86  	if err != nil {
    87  		return errs.Wrap(err, "Could not construct new build script to write")
    88  	}
    89  
    90  	if script != nil && script.Equals(newScript) {
    91  		return nil // no changes to write
    92  	}
    93  
    94  	logging.Debug("Writing build script")
    95  	if err := fileutils.WriteFile(filepath.Join(proj.ProjectDir(), constants.BuildScriptFileName), []byte(newScript.String())); err != nil {
    96  		return errs.Wrap(err, "Could not write build script to file")
    97  	}
    98  	return nil
    99  }