Skip to content

Commit bc7cae1

Browse files
authored
Merge pull request #150 from tongjingran/buildCopy2
optimize the file copy strategy of `goc build` and fix bugs
2 parents 96a326b + cc35564 commit bc7cae1

File tree

12 files changed

+128
-15
lines changed

12 files changed

+128
-15
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ require (
99
github.com/julienschmidt/httprouter v1.2.0
1010
github.com/mattn/go-runewidth v0.0.9 // indirect
1111
github.com/olekukonko/tablewriter v0.0.4
12-
github.com/otiai10/copy v1.0.2
1312
github.com/qiniu/api.v7/v7 v7.5.0
1413
github.com/sirupsen/logrus v1.6.0
1514
github.com/spf13/cobra v1.0.0
1615
github.com/spf13/pflag v1.0.5
1716
github.com/spf13/viper v1.6.2
1817
github.com/stretchr/testify v1.5.1
18+
github.com/tongjingran/copy v1.3.3
1919
golang.org/x/mod v0.3.0
2020
golang.org/x/net v0.0.0-20200625001655-4c5254603344
2121
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d

go.sum

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4er
325325
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
326326
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
327327
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
328+
github.com/golang/mock v1.3.1 h1:qGJ6qTW+x6xX/my+8YUVl4WNpX9B7+/l2tRsHGZ7f2s=
328329
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
329330
github.com/golang/protobuf v0.0.0-20161109072736-4bd1920723d7/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
330331
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -578,8 +579,12 @@ github.com/otiai10/copy v1.0.2 h1:DDNipYy6RkIkjMwy+AWzgKiNTyj2RUI9yEMeETEpVyc=
578579
github.com/otiai10/copy v1.0.2/go.mod h1:c7RpqBkwMom4bYTSkLSym4VSJz/XtncWRAj/J4PEIMY=
579580
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95 h1:+OLn68pqasWca0z5ryit9KGfp3sUsW4Lqg32iRMJyzs=
580581
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
582+
github.com/otiai10/curr v1.0.0 h1:TJIWdbX0B+kpNagQrjgq8bCMrbhiuX73M2XwgtDMoOI=
583+
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
581584
github.com/otiai10/mint v1.3.0 h1:Ady6MKVezQwHBkGzLFbrsywyp09Ah7rkmfjV3Bcr5uc=
582585
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
586+
github.com/otiai10/mint v1.3.2 h1:VYWnrP5fXmz1MXvjuUvcBrXSjGE6xjON+axB/UrpO3E=
587+
github.com/otiai10/mint v1.3.2/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
583588
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
584589
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
585590
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
@@ -714,6 +719,8 @@ github.com/tektoncd/plumbing/pipelinerun-logs v0.0.0-20191206114338-712d544c2c21
714719
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
715720
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
716721
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
722+
github.com/tongjingran/copy v1.3.3 h1:Oha32tKfDrLTSXGaxW+jaTSmGc+hiHFRf/STZ4x9b4g=
723+
github.com/tongjingran/copy v1.3.3/go.mod h1:Njma1OR5OuzB8pLAmQSzonHXzba+DDiPVmMSonpSpy4=
717724
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
718725
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
719726
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
@@ -739,7 +746,6 @@ github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMx
739746
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
740747
github.com/xlab/handysort v0.0.0-20150421192137-fb3537ed64a1/go.mod h1:QcJo0QPSfTONNIgpN5RA8prR7fF8nkF6cTWTcNerRO8=
741748
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
742-
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
743749
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
744750
github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43/go.mod h1:aX5oPXxHm3bOH+xeAttToC8pqch2ScQN/JoXYupl6xs=
745751
github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50/go.mod h1:NUSPSUX/bi6SeDMUh6brw0nXpxHnc96TguQh0+r/ssA=
@@ -962,8 +968,6 @@ golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapK
962968
golang.org/x/tools v0.0.0-20200214144324-88be01311a71/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
963969
golang.org/x/tools v0.0.0-20200303214625-2b0b585e22fe h1:Kh3iY7o/2bMfQXZdwLdL9jDMU1k9HoVn0P1mGCfoFLc=
964970
golang.org/x/tools v0.0.0-20200303214625-2b0b585e22fe/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw=
965-
golang.org/x/tools v0.0.0-20200329025819-fd4102a86c65 h1:1KSbntBked74wYsKq0jzXYy7ZwcjAUtrl7EmPE97Iiw=
966-
golang.org/x/tools v0.0.0-20200329025819-fd4102a86c65/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8=
967971
golang.org/x/tools v0.0.0-20200730221956-1ac65761fe2c h1:c5JjBOQWM2pRemVbog00sS+oAdi8tTR+NNRFDwUOrTQ=
968972
golang.org/x/tools v0.0.0-20200730221956-1ac65761fe2c/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
969973
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

pkg/build/build.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,11 @@ func (b *Build) determineOutputDir(outputDir string) (string, error) {
133133
targetName := ""
134134
for _, pkg := range b.Pkgs {
135135
if pkg.Name == "main" {
136-
_, file := filepath.Split(pkg.Target)
137-
targetName = file
136+
if pkg.Target != "" {
137+
targetName = filepath.Base(pkg.Target)
138+
} else {
139+
targetName = filepath.Base(pkg.Dir)
140+
}
138141
break
139142
}
140143
}

pkg/build/gomodules.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
"io/ioutil"
2121
"path/filepath"
2222

23-
"github.com/otiai10/copy"
2423
log "github.com/sirupsen/logrus"
24+
"github.com/tongjingran/copy"
2525
"golang.org/x/mod/modfile"
2626
)
2727

@@ -31,7 +31,7 @@ func (b *Build) cpGoModulesProject() {
3131
dst := b.TmpDir
3232
src := v.Module.Dir
3333

34-
if err := copy.Copy(src, dst); err != nil {
34+
if err := copy.Copy(src, dst, copy.Options{Skip: skipCopy}); err != nil {
3535
log.Errorf("Failed to Copy the folder from %v to %v, the error is: %v ", src, dst, err)
3636
}
3737
break

pkg/build/legacy.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ package build
1919
import (
2020
"os"
2121
"path/filepath"
22+
"strings"
2223

2324
log "github.com/sirupsen/logrus"
2425

25-
"github.com/otiai10/copy"
2626
"github.com/qiniu/goc/pkg/cover"
27+
"github.com/tongjingran/copy"
2728
)
2829

2930
func (b *Build) cpLegacyProject() {
@@ -37,7 +38,7 @@ func (b *Build) cpLegacyProject() {
3738
continue
3839
}
3940

40-
if err := copy.Copy(src, dst); err != nil {
41+
if err := copy.Copy(src, dst, copy.Options{Skip: skipCopy}); err != nil {
4142
log.Errorf("Failed to Copy the folder from %v to %v, the error is: %v ", src, dst, err)
4243
}
4344

@@ -66,7 +67,7 @@ func (b *Build) cpDepPackages(pkg *cover.Package, visited map[string]bool) {
6667

6768
dst := filepath.Join(b.TmpDir, "src", dep)
6869

69-
if err := copy.Copy(src, dst); err != nil {
70+
if err := copy.Copy(src, dst, copy.Options{Skip: skipCopy}); err != nil {
7071
log.Errorf("Failed to Copy the folder from %v to %v, the error is: %v ", src, dst, err)
7172
}
7273

@@ -80,7 +81,7 @@ func (b *Build) cpNonStandardLegacy() {
8081
dst := b.TmpDir
8182
src := v.Dir
8283

83-
if err := copy.Copy(src, dst); err != nil {
84+
if err := copy.Copy(src, dst, copy.Options{Skip: skipCopy}); err != nil {
8485
log.Printf("Failed to Copy the folder from %v to %v, the error is: %v ", src, dst, err)
8586
}
8687
break
@@ -89,3 +90,17 @@ func (b *Build) cpNonStandardLegacy() {
8990
}
9091
}
9192
}
93+
94+
// skipCopy skip copy .git dir and irregular files
95+
func skipCopy(src string, info os.FileInfo) (bool, error) {
96+
irregularModeType := os.ModeNamedPipe | os.ModeSocket | os.ModeDevice | os.ModeCharDevice | os.ModeIrregular
97+
if strings.HasSuffix(src, "/.git") {
98+
log.Infof("Skip .git dir [%s]", src)
99+
return true, nil
100+
}
101+
if info.Mode()&irregularModeType != 0 {
102+
log.Warnf("Skip file [%s], the file mode is [%s]", src, info.Mode().String())
103+
return true, nil
104+
}
105+
return false, nil
106+
}

pkg/build/legacy_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package build
1818

1919
import (
20+
"os"
2021
"path/filepath"
2122
"strings"
2223
"testing"
24+
"time"
2325

2426
"github.com/qiniu/goc/pkg/cover"
2527
"github.com/stretchr/testify/assert"
@@ -69,3 +71,63 @@ func TestDepPackagesCopyWithInvalidDir(t *testing.T) {
6971
})
7072
assert.Equal(t, strings.Contains(output, "Failed to Copy"), true)
7173
}
74+
75+
type MockFile struct {
76+
name string
77+
size int64
78+
mode os.FileMode
79+
modTime time.Time
80+
isDir bool
81+
}
82+
83+
func (m MockFile) Name() string {
84+
return m.name
85+
}
86+
87+
func (m MockFile) Size() int64 {
88+
return m.size
89+
}
90+
91+
func (m MockFile) Mode() os.FileMode {
92+
return m.mode
93+
}
94+
95+
func (m MockFile) ModTime() time.Time {
96+
return m.modTime
97+
}
98+
99+
func (m MockFile) IsDir() bool {
100+
return m.isDir
101+
}
102+
103+
func (m MockFile) Sys() interface{} {
104+
return nil
105+
}
106+
107+
// skipCopy verify
108+
func TestSkipCopy(t *testing.T) {
109+
testCases := map[string]struct {
110+
inputSrc string
111+
inputInfo MockFile
112+
expected bool
113+
}{
114+
"src with /.git suffix": {inputSrc: "/test/.git", inputInfo: MockFile{mode: 0}, expected: true},
115+
"src with ./git suffix": {inputSrc: "/test.git", inputInfo: MockFile{mode: 0}, expected: false},
116+
"src with /.gita suffix": {inputSrc: "/test/.gita", inputInfo: MockFile{mode: 0}, expected: false},
117+
"src with /.git in middle": {inputSrc: "/test/.git/test", inputInfo: MockFile{mode: 0}, expected: false},
118+
"irregular file": {inputSrc: "/test", inputInfo: MockFile{mode: os.ModeIrregular}, expected: true},
119+
"dir file": {inputSrc: "/test", inputInfo: MockFile{isDir: true, mode: os.ModeDir}, expected: false},
120+
"temporary file": {inputSrc: "/test", inputInfo: MockFile{mode: os.ModeTemporary}, expected: false},
121+
"symlink file": {inputSrc: "/test", inputInfo: MockFile{mode: os.ModeSymlink}, expected: false},
122+
"device file": {inputSrc: "/test", inputInfo: MockFile{mode: os.ModeDevice}, expected: true},
123+
"named pipe file": {inputSrc: "/test", inputInfo: MockFile{mode: os.ModeNamedPipe}, expected: true},
124+
"socket file": {inputSrc: "/test", inputInfo: MockFile{mode: os.ModeSocket}, expected: true},
125+
}
126+
for name, tc := range testCases {
127+
t.Run(name, func(t *testing.T) {
128+
output, err := skipCopy(tc.inputSrc, tc.inputInfo)
129+
assert.NoError(t, err)
130+
assert.Equal(t, output, tc.expected)
131+
})
132+
}
133+
}

pkg/build/tmpfolder.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ func (b *Build) mvProjectsToTmp() error {
115115
} else if b.IsMod == false && b.Root == "" {
116116
b.TmpWorkingDir = b.TmpDir
117117
b.cpNonStandardLegacy()
118-
} else {
119-
return fmt.Errorf("unknown project type: %w", ErrShouldNotReached)
120118
}
121119

122120
log.Infof("New workingdir in tmp directory in: %v", b.TmpWorkingDir)

pkg/cover/cover_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import (
2727

2828
log "github.com/sirupsen/logrus"
2929

30-
"github.com/otiai10/copy"
3130
"github.com/stretchr/testify/assert"
31+
"github.com/tongjingran/copy"
3232
)
3333

3434
func testCoverage() (c *Coverage) {

tests/build.bats

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,18 @@ setup() {
9898
info build5 output: $output
9999
[ "$status" -eq 0 ]
100100

101+
wait $profile_pid
102+
}
103+
104+
@test "test goc build on reference other package project" {
105+
cd samples/reference_other_package_project/app
106+
107+
wait_profile_backend "build6" &
108+
profile_pid=$!
109+
110+
run gocc build --debug --debugcisyncfile ci-sync.bak;
111+
info build5 output: $output
112+
[ "$status" -eq 0 ]
113+
101114
wait $profile_pid
102115
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
import "example.com/reference_other_package_project/foo"
4+
5+
func main() {
6+
foo.PrintFoo()
7+
return
8+
}

0 commit comments

Comments
 (0)