Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Another try at handling *.inc.js cleanly #335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 21, 2015
70 changes: 49 additions & 21 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,21 @@ func NewBuildContext(installSuffix string, buildTags []string) *build.Context {
}
}

func Import(path string, mode build.ImportMode, installSuffix string, buildTags []string) (*build.Package, error) {
// Import returns details about the Go package named by the import path. If the
// path is a local import path naming a package that can be imported using
// a standard import path, the returned package will set p.ImportPath to
// that path.
//
// In the directory containing the package, .go and .inc.js files are
// considered part of the package except for:
//
// - .go files in package documentation
// - files starting with _ or . (likely editor temporary files)
// - files with build constraints not satisfied by the context
//
// If an error occurs, Import returns a non-nil error and a nil
// *PackageData.
func Import(path string, mode build.ImportMode, installSuffix string, buildTags []string) (*PackageData, error) {
buildContext := NewBuildContext(installSuffix, buildTags)
if path == "runtime" || path == "syscall" {
buildContext.GOARCH = build.Default.GOARCH
Expand Down Expand Up @@ -87,7 +101,28 @@ func Import(path string, mode build.ImportMode, installSuffix string, buildTags
}
}

return pkg, nil
jsFiles, err := jsFilesFromDir(pkg.Dir)
if err != nil {
return nil,err
}

return &PackageData{Package: pkg, JSFiles: jsFiles}, nil
}

// ImportDir is like Import but processes the Go package found in the named
// directory.
func ImportDir(dir string, mode build.ImportMode) (*PackageData, error) {
pkg,err := build.ImportDir(dir, mode)
if err != nil {
return nil,err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm on mobile, but isn't there a gofmt issue here? Also lines 115 and 122.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a sharp eye. See PR #338.

Am I imagining things, or didn't this cause CI failures at some point in the past? Regardless, should circle.yml be configured to fail for gofmt issues?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a sharp eye.

Thanks. :)

(Although not sharp enough, I missed line 106.)

Am I imagining things, or didn't this cause CI failures at some point in the past?

I am guessing it might've been the case, but then got lost in the transition from Travis CI to circle CI.

Regardless, should circle.yml be configured to fail for gofmt issues?

Agreed, opened #339.

}

jsFiles, err := jsFilesFromDir(pkg.Dir)
if err != nil {
return nil,err
}

return &PackageData{Package: pkg, JSFiles: jsFiles}, nil
}

// parse parses and returns all .go files of given pkg.
Expand Down Expand Up @@ -248,7 +283,7 @@ func (o *Options) PrintSuccess(format string, a ...interface{}) {

type PackageData struct {
*build.Package
JsFiles []string
JSFiles []string
IsTest bool // IsTest is true if the package is being built for running tests.
SrcModTime time.Time
UpToDate bool
Expand All @@ -275,7 +310,7 @@ func NewSession(options *Options) *Session {
options: options,
Packages: make(map[string]*PackageData),
}
s.ImportContext = compiler.NewImportContext(s.ImportPackage)
s.ImportContext = compiler.NewImportContext(s.BuildImportPath)
if options.Watch {
if out, err := exec.Command("ulimit", "-n").Output(); err == nil {
if n, err := strconv.Atoi(strings.TrimSpace(string(out))); err == nil && n < 1024 {
Expand Down Expand Up @@ -313,7 +348,7 @@ func (s *Session) BuildDir(packagePath string, importPath string, pkgObj string)
if err != nil {
return err
}
pkg.JsFiles = jsFiles
pkg.JSFiles = jsFiles
if err := s.BuildPackage(pkg); err != nil {
return err
}
Expand All @@ -337,7 +372,7 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, packagePath stri

for _, file := range filenames {
if strings.HasSuffix(file, ".inc.js") {
pkg.JsFiles = append(pkg.JsFiles, file)
pkg.JSFiles = append(pkg.JSFiles, file)
continue
}
pkg.GoFiles = append(pkg.GoFiles, file)
Expand All @@ -352,25 +387,18 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, packagePath stri
return s.WriteCommandPackage(pkg, pkgObj)
}

func (s *Session) ImportPackage(path string) (*compiler.Archive, error) {
func (s *Session) BuildImportPath(path string) (*compiler.Archive, error) {
if pkg, found := s.Packages[path]; found {
return pkg.Archive, nil
}

buildPkg, err := Import(path, 0, s.InstallSuffix(), s.options.BuildTags)
if s.Watcher != nil && buildPkg != nil { // add watch even on error
s.Watcher.Add(buildPkg.Dir)
pkg, err := Import(path, 0, s.InstallSuffix(), s.options.BuildTags)
if s.Watcher != nil && pkg != nil { // add watch even on error
s.Watcher.Add(pkg.Dir)
}
if err != nil {
return nil, err
}
pkg := &PackageData{Package: buildPkg}

jsFiles, err := jsFilesFromDir(pkg.Dir)
if err != nil {
return nil, err
}
pkg.JsFiles = jsFiles

if err := s.BuildPackage(pkg); err != nil {
return nil, err
Expand Down Expand Up @@ -415,7 +443,7 @@ func (s *Session) BuildPackage(pkg *PackageData) error {
if importedPkgPath == "unsafe" || ignored {
continue
}
_, err := s.ImportPackage(importedPkgPath)
_, err := s.BuildImportPath(importedPkgPath)
if err != nil {
return err
}
Expand All @@ -425,7 +453,7 @@ func (s *Session) BuildPackage(pkg *PackageData) error {
}
}

for _, name := range append(pkg.GoFiles, pkg.JsFiles...) {
for _, name := range append(pkg.GoFiles, pkg.JSFiles...) {
fileInfo, err := os.Stat(filepath.Join(pkg.Dir, name))
if err != nil {
return err
Expand Down Expand Up @@ -470,7 +498,7 @@ func (s *Session) BuildPackage(pkg *PackageData) error {
}

var jsDecls []*compiler.Decl
for _, jsFile := range pkg.JsFiles {
for _, jsFile := range pkg.JSFiles {
code, err := ioutil.ReadFile(filepath.Join(pkg.Dir, jsFile))
if err != nil {
return err
Expand Down Expand Up @@ -582,7 +610,7 @@ func jsFilesFromDir(dir string) ([]string, error) {
}
var jsFiles []string
for _, file := range files {
if strings.HasSuffix(file.Name(), ".inc.js") && file.Name()[0] != '_' {
if strings.HasSuffix(file.Name(), ".inc.js") && file.Name()[0] != '_' && file.Name()[0] != '.' {
jsFiles = append(jsFiles, file.Name())
}
}
Expand Down
50 changes: 27 additions & 23 deletions tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,10 @@ func main() {
if s.Watcher != nil {
s.Watcher.Add(pkgPath)
}
buildPkg, err := gbuild.Import(pkgPath, 0, s.InstallSuffix(), options.BuildTags)
pkg, err := gbuild.Import(pkgPath, 0, s.InstallSuffix(), options.BuildTags)
if err != nil {
return err
}
pkg := &gbuild.PackageData{Package: buildPkg}
if err := s.BuildPackage(pkg); err != nil {
return err
}
Expand Down Expand Up @@ -176,7 +175,7 @@ func main() {
}
for _, pkgPath := range pkgs {
pkgPath = filepath.ToSlash(pkgPath)
if _, err := s.ImportPackage(pkgPath); err != nil {
if _, err := s.BuildImportPath(pkgPath); err != nil {
return err
}
pkg := s.Packages[pkgPath]
Expand Down Expand Up @@ -254,7 +253,7 @@ func main() {
cmdTest.Flags().AddFlag(flagColor)
cmdTest.Run = func(cmd *cobra.Command, args []string) {
os.Exit(handleError(func() error {
pkgs := make([]*build.Package, len(args))
pkgs := make([]*gbuild.PackageData, len(args))
for i, pkgPath := range args {
pkgPath = filepath.ToSlash(pkgPath)
var err error
Expand All @@ -269,7 +268,7 @@ func main() {
if err != nil {
return err
}
var pkg *build.Package
var pkg *gbuild.PackageData
if strings.HasPrefix(currentDirectory, srcDir) {
pkgPath, err := filepath.Rel(srcDir, currentDirectory)
if err != nil {
Expand All @@ -280,12 +279,12 @@ func main() {
}
}
if pkg == nil {
if pkg, err = build.ImportDir(currentDirectory, 0); err != nil {
if pkg, err = gbuild.ImportDir(currentDirectory, 0); err != nil {
return err
}
pkg.ImportPath = "_" + currentDirectory
}
pkgs = []*build.Package{pkg}
pkgs = []*gbuild.PackageData{pkg}
}

var exitErr error
Expand All @@ -296,9 +295,8 @@ func main() {
}

s := gbuild.NewSession(options)
tests := &testFuncs{Package: pkg}
collectTests := func(buildPkg *build.Package, testPkgName string, needVar *bool) error {
testPkg := &gbuild.PackageData{Package: buildPkg, IsTest: true}
tests := &testFuncs{Package: pkg.Package}
collectTests := func(testPkg *gbuild.PackageData, testPkgName string, needVar *bool) error {
if err := s.BuildPackage(testPkg); err != nil {
return err
}
Expand All @@ -316,20 +314,27 @@ func main() {
return nil
}

if err := collectTests(&build.Package{
ImportPath: pkg.ImportPath,
Dir: pkg.Dir,
GoFiles: append(pkg.GoFiles, pkg.TestGoFiles...),
Imports: append(pkg.Imports, pkg.TestImports...),
if err := collectTests(&gbuild.PackageData{
Package: &build.Package{
ImportPath: pkg.ImportPath,
Dir: pkg.Dir,
GoFiles: append(pkg.GoFiles, pkg.TestGoFiles...),
Imports: append(pkg.Imports, pkg.TestImports...),
},
IsTest: true,
JSFiles: pkg.JSFiles,
}, "_test", &tests.NeedTest); err != nil {
return err
}

if err := collectTests(&build.Package{
ImportPath: pkg.ImportPath + "_test",
Dir: pkg.Dir,
GoFiles: pkg.XTestGoFiles,
Imports: pkg.XTestImports,
if err := collectTests(&gbuild.PackageData{
Package: &build.Package{
ImportPath: pkg.ImportPath + "_test",
Dir: pkg.Dir,
GoFiles: pkg.XTestGoFiles,
Imports: pkg.XTestImports,
},
IsTest: true,
}, "_xtest", &tests.NeedXtest); err != nil {
return err
}
Expand Down Expand Up @@ -496,8 +501,8 @@ func (fs serveCommandFileSystem) Open(name string) (http.File, error) {
if isPkg || isMap || isIndex {
// If we're going to be serving our special files, make sure there's a Go command in this folder.
s := gbuild.NewSession(fs.options)
buildPkg, err := gbuild.Import(path.Dir(name[1:]), 0, s.InstallSuffix(), fs.options.BuildTags)
if err != nil || buildPkg.Name != "main" {
pkg, err := gbuild.Import(path.Dir(name[1:]), 0, s.InstallSuffix(), fs.options.BuildTags)
if err != nil || pkg.Name != "main" {
isPkg = false
isMap = false
isIndex = false
Expand All @@ -508,7 +513,6 @@ func (fs serveCommandFileSystem) Open(name string) (http.File, error) {
buf := bytes.NewBuffer(nil)
browserErrors := bytes.NewBuffer(nil)
exitCode := handleError(func() error {
pkg := &gbuild.PackageData{Package: buildPkg}
if err := s.BuildPackage(pkg); err != nil {
return err
}
Expand Down