Skip to content

Commit

Permalink
Merge pull request #3602 from ActiveState/mitchell/dx-3159
Browse files Browse the repository at this point in the history
Added "--portable" flag to `state checkout` for copying runtime files instead of linking them.
  • Loading branch information
mitchell-as authored Nov 22, 2024
2 parents d2ec9cd + 8b4a5af commit d523079
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 10 deletions.
5 changes: 5 additions & 0 deletions cmd/state/internal/cmdtree/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func newCheckoutCommand(prime *primer.Values) *captain.Command {
Description: locale.Tl("flag_state_checkout_runtime-path_description", "Path to store the runtime files"),
Value: &params.RuntimePath,
},
{
Name: "portable",
Description: locale.Tl("flag_state_checkout_portable_description", "Copy files to their runtime path instead of linking to them"),
Value: &params.Portable,
},
{
Name: "no-clone",
Description: locale.Tl("flag_state_checkout_no_clone_description", "Do not clone the github repository associated with this project (if any)"),
Expand Down
1 change: 1 addition & 0 deletions internal/assets/contents/activestate.yaml.cache.tpl
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# It is recommended that you do not commit this file as it contains configuration that is specific to your machine
cache: {{ .Cache }}
portable: {{ .Portable }}
7 changes: 4 additions & 3 deletions internal/runbits/checkout/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func New(repo git.Repository, prime primeable) *Checkout {
return &Checkout{repo, prime}
}

func (r *Checkout) Run(ns *project.Namespaced, branchName, cachePath, targetPath string, noClone, bareCheckout bool) (_ string, rerr error) {
func (r *Checkout) Run(ns *project.Namespaced, branchName, cachePath, targetPath string, noClone, bareCheckout, portable bool) (_ string, rerr error) {
defer r.rationalizeError(&rerr)

path, err := r.pathToUse(ns, targetPath)
Expand Down Expand Up @@ -94,7 +94,7 @@ func (r *Checkout) Run(ns *project.Namespaced, branchName, cachePath, targetPath
return "", errNoCommitID
}

if err := CreateProjectFiles(path, cachePath, owner, proj, branchName, commitID.String(), language); err != nil {
if err := CreateProjectFiles(path, cachePath, owner, proj, branchName, commitID.String(), language, portable); err != nil {
return "", errs.Wrap(err, "Could not create project files")
}

Expand Down Expand Up @@ -182,7 +182,7 @@ func (r *Checkout) fetchProject(
return owner, proj, commitID, branchName, language, pj.RepoURL, nil
}

func CreateProjectFiles(checkoutPath, cachePath, owner, name, branch, commitID, language string) error {
func CreateProjectFiles(checkoutPath, cachePath, owner, name, branch, commitID, language string, portable bool) error {
configFile := filepath.Join(checkoutPath, constants.ConfigFileName)
if !fileutils.FileExists(configFile) {
_, err := projectfile.Create(&projectfile.CreateParams{
Expand All @@ -192,6 +192,7 @@ func CreateProjectFiles(checkoutPath, cachePath, owner, name, branch, commitID,
Directory: checkoutPath,
Language: language,
Cache: cachePath,
Portable: portable,
})
if err != nil {
if osutils.IsAccessDeniedError(err) {
Expand Down
3 changes: 3 additions & 0 deletions internal/runbits/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ func Update(
u.RawQuery = q.Encode()
rtOpts = append(rtOpts, runtime.WithBuildProgressUrl(u.String()))
}
if proj.IsPortable() {
rtOpts = append(rtOpts, runtime.WithPortable())
}

if err := rt.Update(buildPlan, rtHash, rtOpts...); err != nil {
return nil, locale.WrapError(err, "err_packages_update_runtime_install")
Expand Down
2 changes: 1 addition & 1 deletion internal/runners/activate/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (r *Activate) Run(params *ActivateParams) (rerr error) {
}

// Perform fresh checkout
pathToUse, err := r.activateCheckout.Run(params.Namespace, params.Branch, "", params.PreferredPath, false, false)
pathToUse, err := r.activateCheckout.Run(params.Namespace, params.Branch, "", params.PreferredPath, false, false, false)
if err != nil {
return locale.WrapError(err, "err_activate_pathtouse", "Could not figure out what path to use.")
}
Expand Down
3 changes: 2 additions & 1 deletion internal/runners/checkout/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Params struct {
RuntimePath string
NoClone bool
Force bool
Portable bool
}

type primeable interface {
Expand Down Expand Up @@ -121,7 +122,7 @@ func (u *Checkout) Run(params *Params) (rerr error) {

u.out.Notice(locale.Tr("checking_out", ns.String()))

projectDir, err := u.checkout.Run(ns, params.Branch, params.RuntimePath, params.PreferredPath, params.NoClone, archive != nil)
projectDir, err := u.checkout.Run(ns, params.Branch, params.RuntimePath, params.PreferredPath, params.NoClone, archive != nil, params.Portable)
if err != nil {
return errs.Wrap(err, "Checkout failed")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/runners/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (d *Deploy) install(params *Params, commitID strfmt.UUID) (rerr error) {

if err := checkout.CreateProjectFiles(
params.Path, params.Path, params.Namespace.Owner, params.Namespace.Project,
constants.DefaultBranchName, commitID.String(), "",
constants.DefaultBranchName, commitID.String(), "", true,
); err != nil {
return errs.Wrap(err, "Could not create project files")
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ func (p *Project) Lock() string { return p.projectfile.Lock }
// Cache returns the cache information for this project
func (p *Project) Cache() string { return p.projectfile.Cache }

func (p *Project) IsPortable() bool { return p.projectfile.Portable }

// Namespace returns project namespace
func (p *Project) Namespace() *Namespaced {
return &Namespaced{Owner: p.projectfile.Owner(), Project: p.projectfile.Name()}
Expand Down
9 changes: 6 additions & 3 deletions pkg/projectfile/projectfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type Project struct {
Jobs Jobs `yaml:"jobs,omitempty"`
Private bool `yaml:"private,omitempty"`
Cache string `yaml:"cache,omitempty"`
Portable bool `yaml:"portable,omitempty"`
path string // "private"
parsedURL projectURL // parsed url data
parsedChannel string
Expand Down Expand Up @@ -920,6 +921,7 @@ type CreateParams struct {
path string
ProjectURL string
Cache string
Portable bool
}

// Create will create a new activestate.yaml with a projectURL for the given details
Expand Down Expand Up @@ -1019,7 +1021,7 @@ func createCustom(params *CreateParams, lang language.Language) (*Project, error
}

if params.Cache != "" {
createErr := createHostFile(params.Directory, params.Cache)
createErr := createHostFile(params.Directory, params.Cache, params.Portable)
if createErr != nil {
return nil, errs.Wrap(createErr, "Could not create cache file")
}
Expand All @@ -1028,14 +1030,15 @@ func createCustom(params *CreateParams, lang language.Language) (*Project, error
return Parse(params.path)
}

func createHostFile(filePath, cachePath string) error {
func createHostFile(filePath, cachePath string, portable bool) error {
user, err := user.Current()
if err != nil {
return errs.Wrap(err, "Could not get current user")
}

data := map[string]interface{}{
"Cache": cachePath,
"Cache": cachePath,
"Portable": portable,
}

tplName := "activestate.yaml.cache.tpl"
Expand Down
4 changes: 4 additions & 0 deletions pkg/runtime/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func WithPreferredLibcVersion(version string) SetOpt {
return func(opts *Opts) { opts.PreferredLibcVersion = version }
}

func WithPortable() SetOpt {
return func(opts *Opts) { opts.Portable = true }
}

func WithArchive(dir string, platformID strfmt.UUID, ext string) SetOpt {
return func(opts *Opts) {
opts.FromArchive = &fromArchive{dir, platformID, ext}
Expand Down
3 changes: 2 additions & 1 deletion pkg/runtime/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Opts struct {
EventHandlers []events.HandlerFunc
BuildlogFilePath string
BuildProgressUrl string
Portable bool

FromArchive *fromArchive

Expand Down Expand Up @@ -467,7 +468,7 @@ func (s *setup) install(id strfmt.UUID) (rerr error) {
return errs.Wrap(err, "Could not get env")
}

if envDef.NeedsTransforms() || !s.supportsHardLinks {
if envDef.NeedsTransforms() || !s.supportsHardLinks || s.opts.Portable {
if err := s.depot.DeployViaCopy(id, envDef.InstallDir, s.path); err != nil {
return errs.Wrap(err, "Could not deploy artifact via copy")
}
Expand Down

0 comments on commit d523079

Please sign in to comment.