github.com/goplus/gop@v1.2.6/build_install_run.go (about)

     1  /*
     2   * Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved.
     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  package gop
    18  
    19  import (
    20  	"log"
    21  	"os"
    22  
    23  	"github.com/goplus/gop/x/gocmd"
    24  	"github.com/qiniu/x/errors"
    25  )
    26  
    27  func genFlags(flags []GenFlags) GenFlags {
    28  	if flags != nil {
    29  		return flags[0]
    30  	}
    31  	return 0
    32  }
    33  
    34  // -----------------------------------------------------------------------------
    35  
    36  // InstallDir installs a Go+ package directory.
    37  func InstallDir(dir string, conf *Config, install *gocmd.InstallConfig, flags ...GenFlags) (err error) {
    38  	_, _, err = GenGoEx(dir, conf, false, genFlags(flags))
    39  	if err != nil {
    40  		return errors.NewWith(err, `GenGo(dir, conf, false)`, -2, "gop.GenGo", dir, conf, false)
    41  	}
    42  	return gocmd.Install(dir, install)
    43  }
    44  
    45  // InstallPkgPath installs a Go+ package.
    46  func InstallPkgPath(workDir, pkgPath string, conf *Config, install *gocmd.InstallConfig, flags ...GenFlags) (err error) {
    47  	localDir, recursively, err := GenGoPkgPathEx(workDir, pkgPath, conf, true, genFlags(flags))
    48  	if err != nil {
    49  		return errors.NewWith(err, `GenGoPkgPath(workDir, pkgPath, conf, true)`, -2, "gop.GenGoPkgPath", workDir, pkgPath, conf, true)
    50  	}
    51  	old := chdir(localDir)
    52  	defer os.Chdir(old)
    53  	return gocmd.Install(cwdParam(recursively), install)
    54  }
    55  
    56  func cwdParam(recursively bool) string {
    57  	if recursively {
    58  		return "./..."
    59  	}
    60  	return "."
    61  }
    62  
    63  // InstallFiles installs specified Go+ files.
    64  func InstallFiles(files []string, conf *Config, install *gocmd.InstallConfig) (err error) {
    65  	files, err = GenGoFiles("", files, conf)
    66  	if err != nil {
    67  		return errors.NewWith(err, `GenGoFiles("", files, conf)`, -2, "gop.GenGoFiles", "", files, conf)
    68  	}
    69  	return gocmd.InstallFiles(files, install)
    70  }
    71  
    72  func chdir(dir string) string {
    73  	old, err := os.Getwd()
    74  	if err != nil {
    75  		log.Panicln(err)
    76  	}
    77  	err = os.Chdir(dir)
    78  	if err != nil {
    79  		log.Panicln(err)
    80  	}
    81  	return old
    82  }
    83  
    84  // -----------------------------------------------------------------------------
    85  
    86  // BuildDir builds a Go+ package directory.
    87  func BuildDir(dir string, conf *Config, build *gocmd.BuildConfig, flags ...GenFlags) (err error) {
    88  	_, _, err = GenGoEx(dir, conf, false, genFlags(flags))
    89  	if err != nil {
    90  		return errors.NewWith(err, `GenGo(dir, conf, false)`, -2, "gop.GenGo", dir, conf, false)
    91  	}
    92  	return gocmd.Build(dir, build)
    93  }
    94  
    95  // BuildPkgPath builds a Go+ package.
    96  func BuildPkgPath(workDir, pkgPath string, conf *Config, build *gocmd.BuildConfig, flags ...GenFlags) (err error) {
    97  	localDir, recursively, err := GenGoPkgPathEx(workDir, pkgPath, conf, false, genFlags(flags))
    98  	if err != nil {
    99  		return errors.NewWith(err, `GenGoPkgPath(workDir, pkgPath, conf, false)`, -2, "gop.GenGoPkgPath", workDir, pkgPath, conf, false)
   100  	}
   101  	old, mod := chdirAndMod(localDir)
   102  	defer restoreDirAndMod(old, mod)
   103  	return gocmd.Build(cwdParam(recursively), build)
   104  }
   105  
   106  // BuildFiles builds specified Go+ files.
   107  func BuildFiles(files []string, conf *Config, build *gocmd.BuildConfig) (err error) {
   108  	files, err = GenGoFiles("", files, conf)
   109  	if err != nil {
   110  		return errors.NewWith(err, `GenGoFiles("", files, conf)`, -2, "gop.GenGoFiles", "", files, conf)
   111  	}
   112  	return gocmd.BuildFiles(files, build)
   113  }
   114  
   115  func chdirAndMod(dir string) (old string, mod os.FileMode) {
   116  	mod = 0755
   117  	if info, err := os.Stat(dir); err == nil {
   118  		mod = info.Mode().Perm()
   119  	}
   120  	os.Chmod(dir, 0777)
   121  	old = chdir(dir)
   122  	return
   123  }
   124  
   125  func restoreDirAndMod(old string, mod os.FileMode) {
   126  	os.Chmod(".", mod)
   127  	os.Chdir(old)
   128  }
   129  
   130  // -----------------------------------------------------------------------------
   131  
   132  // If no go.mod and used Go+, use GOPROOT as buildDir.
   133  func getBuildDir(conf *Config) string {
   134  	if conf != nil && conf.GopDeps != nil && *conf.GopDeps != 0 {
   135  		return conf.Gop.Root
   136  	}
   137  	return ""
   138  }
   139  
   140  // RunDir runs an application from a Go+ package directory.
   141  func RunDir(dir string, args []string, conf *Config, run *gocmd.RunConfig, flags ...GenFlags) (err error) {
   142  	_, _, err = GenGoEx(dir, conf, false, genFlags(flags))
   143  	if err != nil {
   144  		return errors.NewWith(err, `GenGo(dir, conf, false)`, -2, "gop.GenGo", dir, conf, false)
   145  	}
   146  	return gocmd.RunDir(getBuildDir(conf), dir, args, run)
   147  }
   148  
   149  // RunPkgPath runs an application from a Go+ package.
   150  func RunPkgPath(pkgPath string, args []string, chDir bool, conf *Config, run *gocmd.RunConfig, flags ...GenFlags) (err error) {
   151  	localDir, recursively, err := GenGoPkgPathEx("", pkgPath, conf, true, genFlags(flags))
   152  	if err != nil {
   153  		return errors.NewWith(err, `GenGoPkgPath("", pkgPath, conf, true)`, -2, "gop.GenGoPkgPath", "", pkgPath, conf, true)
   154  	}
   155  	if recursively {
   156  		return errors.NewWith(errors.New("can't use ... pattern for `gop run` command"), `recursively`, -1, "", recursively)
   157  	}
   158  	if chDir {
   159  		old := chdir(localDir)
   160  		defer os.Chdir(old)
   161  		localDir = "."
   162  	}
   163  	return gocmd.RunDir("", localDir, args, run)
   164  }
   165  
   166  // RunFiles runs an application from specified Go+ files.
   167  func RunFiles(autogen string, files []string, args []string, conf *Config, run *gocmd.RunConfig) (err error) {
   168  	files, err = GenGoFiles(autogen, files, conf)
   169  	if err != nil {
   170  		return errors.NewWith(err, `GenGoFiles(autogen, files, conf)`, -2, "gop.GenGoFiles", autogen, files, conf)
   171  	}
   172  	return gocmd.RunFiles(getBuildDir(conf), files, args, run)
   173  }
   174  
   175  // -----------------------------------------------------------------------------
   176  
   177  // TestDir tests a Go+ package directory.
   178  func TestDir(dir string, conf *Config, test *gocmd.TestConfig, flags ...GenFlags) (err error) {
   179  	_, _, err = GenGoEx(dir, conf, true, genFlags(flags))
   180  	if err != nil {
   181  		return errors.NewWith(err, `GenGo(dir, conf, true)`, -2, "gop.GenGo", dir, conf, true)
   182  	}
   183  	return gocmd.Test(dir, test)
   184  }
   185  
   186  // TestPkgPath tests a Go+ package.
   187  func TestPkgPath(workDir, pkgPath string, conf *Config, test *gocmd.TestConfig, flags ...GenFlags) (err error) {
   188  	localDir, recursively, err := GenGoPkgPathEx(workDir, pkgPath, conf, false, genFlags(flags))
   189  	if err != nil {
   190  		return errors.NewWith(err, `GenGoPkgPath(workDir, pkgPath, conf, false)`, -2, "gop.GenGoPkgPath", workDir, pkgPath, conf, false)
   191  	}
   192  	old, mod := chdirAndMod(localDir)
   193  	defer restoreDirAndMod(old, mod)
   194  	return gocmd.Test(cwdParam(recursively), test)
   195  }
   196  
   197  // TestFiles tests specified Go+ files.
   198  func TestFiles(files []string, conf *Config, test *gocmd.TestConfig) (err error) {
   199  	files, err = GenGoFiles("", files, conf)
   200  	if err != nil {
   201  		return errors.NewWith(err, `GenGoFiles("", files, conf)`, -2, "gop.GenGoFiles", "", files, conf)
   202  	}
   203  	return gocmd.TestFiles(files, test)
   204  }
   205  
   206  // -----------------------------------------------------------------------------