Repository: goplus/gop
Pull request #1072 (2021-12-26 06:14:15)

goprun


Patch:
diff --git a/cmd/goprun/goprun.go b/cmd/goprun/goprun.go
index f486d7fc..1c7ce9d1 100644
--- a/cmd/goprun/goprun.go
+++ b/cmd/goprun/goprun.go
@@ -6,21 +6,34 @@ import (
 	"os"
 	"os/exec"
 
+	"github.com/goplus/gop/cmd/internal/gopproj"
 	"github.com/goplus/gop/x/gopmod"
 )
 
 func main() {
 	if len(os.Args) < 2 {
-		fmt.Fprint(os.Stderr, "Usage: gorun <file.go> [switch ...]\n\n")
+		fmt.Fprint(os.Stderr, "Usage: goprun package [arguments ...]\n\n")
 		return
 	}
-	ctx := gopmod.New("")
-	goProj, err := ctx.OpenFiles(0, os.Args[1])
+	proj, args, err := gopproj.ParseOne(os.Args[1:]...)
+	if err != nil {
+		log.Fatalln(err)
+	}
+	var ctx = gopmod.New("")
+	var goProj *gopmod.Project
+	switch v := proj.(type) {
+	case *gopproj.FilesProj:
+		goProj, err = ctx.OpenFiles(0, v.Files...)
+	case *gopproj.DirProj:
+		goProj, err = ctx.OpenDir(0, v.Dir)
+	case *gopproj.PkgPathProj:
+		panic("TODO: package path project")
+	}
 	if err != nil {
 		fmt.Fprint(os.Stderr, "OpenProject failed:", err)
 		return
 	}
-	goProj.ExecArgs = os.Args[2:]
+	goProj.ExecArgs = args
 	cmd := ctx.GoCommand("run", goProj)
 	cmd.Stdin = os.Stdin
 	cmd.Stdout = os.Stdout
diff --git a/cmd/make.go b/cmd/make.go
index 8887024b..4d5623e7 100644
--- a/cmd/make.go
+++ b/cmd/make.go
@@ -386,7 +386,7 @@ func releaseNewVersion(tag string) {
 	fmt.Printf("\nReleasing new version: %s\n\n", version)
 
 	// Cache new version
-	if err := os.WriteFile(versionFile, []byte(version), 0666); err != nil {
+	if err := os.WriteFile(versionFile, []byte(version), 0644); err != nil {
 		log.Fatalf("Error: cache new version with error: %v\n", err)
 	}
 
diff --git a/env/path.go b/env/path.go
index b59c3781..d52cf90a 100644
--- a/env/path.go
+++ b/env/path.go
@@ -105,12 +105,6 @@ func isDirExists(path string) bool {
 	return err == nil && st.IsDir()
 }
 
-// A valid gop root has go.mod, go.sum, builtin/
 func isValidGopRoot(path string) bool {
-	if !isFileExists(filepath.Join(path, "go.mod")) ||
-		!isFileExists(filepath.Join(path, "go.sum")) ||
-		!isDirExists(filepath.Join(path, "builtin")) {
-		return false
-	}
-	return true
+	return isDirExists(filepath.Join(path, "cmd/gop")) && isFileExists(filepath.Join(path, "go.mod"))
 }
diff --git a/x/gopmod/gofile.go b/x/gopmod/gofile.go
index 35923ab2..9cbd6eed 100644
--- a/x/gopmod/gofile.go
+++ b/x/gopmod/gofile.go
@@ -56,7 +56,7 @@ func (p *goFile) GenGo(outFile, modFile string) error {
 		if err != nil {
 			return err
 		}
-		return os.WriteFile(outFile, code, 0666)
+		return os.WriteFile(outFile, code, 0644)
 	}
 	return nil
 }
diff --git a/x/gopmod/gopmod.go b/x/gopmod/gopmod.go
index 79ae1a7c..08e9baaf 100644
--- a/x/gopmod/gopmod.go
+++ b/x/gopmod/gopmod.go
@@ -173,14 +173,14 @@ func genGomodFile(modfile string) {
 		gopRoot = filepath.ToSlash(gopRoot)
 	}
 	fmt.Fprintf(&buf, gomodFormat, GOPVERSION, gopRoot)
-	err = os.WriteFile(modfile, buf.Bytes(), 0666)
+	err = os.WriteFile(modfile, buf.Bytes(), 0644)
 	if err != nil {
 		log.Panicln(err)
 	}
 }
 
 func genDummyProject(dir string) {
-	err := os.WriteFile(dir+"/dummy.go", []byte(dummyGoFile), 0666)
+	err := os.WriteFile(dir+"/dummy.go", []byte(dummyGoFile), 0644)
 	if err != nil {
 		log.Panicln(err)
 	}
diff --git a/x/mod/modfetch/file.go b/x/mod/modfetch/file.go
index 1e368da4..f20b9dde 100644
--- a/x/mod/modfetch/file.go
+++ b/x/mod/modfetch/file.go
@@ -20,13 +20,13 @@ import (
 	"os"
 )
 
-// Edit creates the named file with mode 0666 (before umask),
+// Edit creates the named file with mode 0644 (before umask),
 // but does not truncate existing contents.
 //
 // If Edit succeeds, methods on the returned File can be used for I/O.
 // The associated file descriptor has mode O_RDWR and the file is write-locked.
 func Edit(name string) (*os.File, error) {
-	return os.OpenFile(name, os.O_RDWR|os.O_CREATE, 0666)
+	return os.OpenFile(name, os.O_RDWR|os.O_CREATE, 0644)
 }
 
 // Transform invokes t with the result of reading the named file, with its lock


Test patch:
diff --git a/cmd/make_test.go b/cmd/make_test.go
index e8e9f5e3..58242bf6 100644
--- a/cmd/make_test.go
+++ b/cmd/make_test.go
@@ -292,7 +292,7 @@ func TestInstallInNonGitRepo(t *testing.T) {
 	t.Run("install with VERSION file", func(t *testing.T) {
 		version := "v1.0.98"
 		// Create VERSION file
-		if err := os.WriteFile(versionFile, []byte(version), 0666); err != nil {
+		if err := os.WriteFile(versionFile, []byte(version), 0644); err != nil {
 			t.Fatal(err)
 		}
 
diff --git a/env/path_test.go b/env/path_test.go
index f20a30e1..38162da6 100644
--- a/env/path_test.go
+++ b/env/path_test.go
@@ -42,7 +42,7 @@ func findGoModFile(dir string) (modfile string, noCacheFile bool, err error) {
 //   valid_goproot/
 //	   go.mod
 //	   go.sum
-//     builtin/
+//     cmd/gop/
 func makeTestDir(t *testing.T) (root string, src string, gopRoot string) {
 	root, _ = filepath.EvalSymlinks(t.TempDir())
 	src = filepath.Join(root, "src")
@@ -54,7 +54,7 @@ func makeTestDir(t *testing.T) (root string, src string, gopRoot string) {
 
 func makeValidGopRoot(root string) {
 	os.Mkdir(root, 0755)
-	os.Mkdir(filepath.Join(root, "builtin"), 0755)
+	os.MkdirAll(filepath.Join(root, "cmd/gop"), 0755)
 	ioutil.WriteFile(filepath.Join(root, "go.mod"), []byte(""), 0644)
 	ioutil.WriteFile(filepath.Join(root, "go.sum"), []byte(""), 0644)
 }
diff --git a/x/format/gopstyledir_test.go b/x/format/gopstyledir_test.go
index b87e9c03..d9acaba4 100644
--- a/x/format/gopstyledir_test.go
+++ b/x/format/gopstyledir_test.go
@@ -77,7 +77,7 @@ func diffBytes(t *testing.T, outfile string, dst, src []byte) {
 		d := dst[i]
 		s := src[i]
 		if d != s {
-			os.WriteFile(outfile, dst, 0666)
+			os.WriteFile(outfile, dst, 0644)
 			t.Errorf("dst:%d: %s\n", line, dst[offs:])
 			t.Errorf("src:%d: %s\n", line, src[offs:])
 			return