@@ -43,7 +43,21 @@ func NewBuildContext(installSuffix string, buildTags []string) *build.Context {
43
43
}
44
44
}
45
45
46
- func Import (path string , mode build.ImportMode , installSuffix string , buildTags []string ) (* build.Package , error ) {
46
+ // Import returns details about the Go package named by the import path. If the
47
+ // path is a local import path naming a package that can be imported using
48
+ // a standard import path, the returned package will set p.ImportPath to
49
+ // that path.
50
+ //
51
+ // In the directory containing the package, .go and .inc.js files are
52
+ // considered part of the package except for:
53
+ //
54
+ // - .go files in package documentation
55
+ // - files starting with _ or . (likely editor temporary files)
56
+ // - files with build constraints not satisfied by the context
57
+ //
58
+ // If an error occurs, Import returns a non-nil error and a nil
59
+ // *PackageData.
60
+ func Import (path string , mode build.ImportMode , installSuffix string , buildTags []string ) (* PackageData , error ) {
47
61
buildContext := NewBuildContext (installSuffix , buildTags )
48
62
if path == "runtime" || path == "syscall" {
49
63
buildContext .GOARCH = build .Default .GOARCH
@@ -87,7 +101,28 @@ func Import(path string, mode build.ImportMode, installSuffix string, buildTags
87
101
}
88
102
}
89
103
90
- return pkg , nil
104
+ jsFiles , err := jsFilesFromDir (pkg .Dir )
105
+ if err != nil {
106
+ return nil ,err
107
+ }
108
+
109
+ return & PackageData {Package : pkg , JSFiles : jsFiles }, nil
110
+ }
111
+
112
+ // ImportDir is like Import but processes the Go package found in the named
113
+ // directory.
114
+ func ImportDir (dir string , mode build.ImportMode ) (* PackageData , error ) {
115
+ pkg ,err := build .ImportDir (dir , mode )
116
+ if err != nil {
117
+ return nil ,err
118
+ }
119
+
120
+ jsFiles , err := jsFilesFromDir (pkg .Dir )
121
+ if err != nil {
122
+ return nil ,err
123
+ }
124
+
125
+ return & PackageData {Package : pkg , JSFiles : jsFiles }, nil
91
126
}
92
127
93
128
// parse parses and returns all .go files of given pkg.
@@ -248,7 +283,7 @@ func (o *Options) PrintSuccess(format string, a ...interface{}) {
248
283
249
284
type PackageData struct {
250
285
* build.Package
251
- JsFiles []string
286
+ JSFiles []string
252
287
IsTest bool // IsTest is true if the package is being built for running tests.
253
288
SrcModTime time.Time
254
289
UpToDate bool
@@ -275,7 +310,7 @@ func NewSession(options *Options) *Session {
275
310
options : options ,
276
311
Packages : make (map [string ]* PackageData ),
277
312
}
278
- s .ImportContext = compiler .NewImportContext (s .ImportPackage )
313
+ s .ImportContext = compiler .NewImportContext (s .BuildImportPath )
279
314
if options .Watch {
280
315
if out , err := exec .Command ("ulimit" , "-n" ).Output (); err == nil {
281
316
if n , err := strconv .Atoi (strings .TrimSpace (string (out ))); err == nil && n < 1024 {
@@ -313,7 +348,7 @@ func (s *Session) BuildDir(packagePath string, importPath string, pkgObj string)
313
348
if err != nil {
314
349
return err
315
350
}
316
- pkg .JsFiles = jsFiles
351
+ pkg .JSFiles = jsFiles
317
352
if err := s .BuildPackage (pkg ); err != nil {
318
353
return err
319
354
}
@@ -337,7 +372,7 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, packagePath stri
337
372
338
373
for _ , file := range filenames {
339
374
if strings .HasSuffix (file , ".inc.js" ) {
340
- pkg .JsFiles = append (pkg .JsFiles , file )
375
+ pkg .JSFiles = append (pkg .JSFiles , file )
341
376
continue
342
377
}
343
378
pkg .GoFiles = append (pkg .GoFiles , file )
@@ -352,25 +387,18 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, packagePath stri
352
387
return s .WriteCommandPackage (pkg , pkgObj )
353
388
}
354
389
355
- func (s * Session ) ImportPackage (path string ) (* compiler.Archive , error ) {
390
+ func (s * Session ) BuildImportPath (path string ) (* compiler.Archive , error ) {
356
391
if pkg , found := s .Packages [path ]; found {
357
392
return pkg .Archive , nil
358
393
}
359
394
360
- buildPkg , err := Import (path , 0 , s .InstallSuffix (), s .options .BuildTags )
361
- if s .Watcher != nil && buildPkg != nil { // add watch even on error
362
- s .Watcher .Add (buildPkg .Dir )
395
+ pkg , err := Import (path , 0 , s .InstallSuffix (), s .options .BuildTags )
396
+ if s .Watcher != nil && pkg != nil { // add watch even on error
397
+ s .Watcher .Add (pkg .Dir )
363
398
}
364
399
if err != nil {
365
400
return nil , err
366
401
}
367
- pkg := & PackageData {Package : buildPkg }
368
-
369
- jsFiles , err := jsFilesFromDir (pkg .Dir )
370
- if err != nil {
371
- return nil , err
372
- }
373
- pkg .JsFiles = jsFiles
374
402
375
403
if err := s .BuildPackage (pkg ); err != nil {
376
404
return nil , err
@@ -415,7 +443,7 @@ func (s *Session) BuildPackage(pkg *PackageData) error {
415
443
if importedPkgPath == "unsafe" || ignored {
416
444
continue
417
445
}
418
- _ , err := s .ImportPackage (importedPkgPath )
446
+ _ , err := s .BuildImportPath (importedPkgPath )
419
447
if err != nil {
420
448
return err
421
449
}
@@ -425,7 +453,7 @@ func (s *Session) BuildPackage(pkg *PackageData) error {
425
453
}
426
454
}
427
455
428
- for _ , name := range append (pkg .GoFiles , pkg .JsFiles ... ) {
456
+ for _ , name := range append (pkg .GoFiles , pkg .JSFiles ... ) {
429
457
fileInfo , err := os .Stat (filepath .Join (pkg .Dir , name ))
430
458
if err != nil {
431
459
return err
@@ -470,7 +498,7 @@ func (s *Session) BuildPackage(pkg *PackageData) error {
470
498
}
471
499
472
500
var jsDecls []* compiler.Decl
473
- for _ , jsFile := range pkg .JsFiles {
501
+ for _ , jsFile := range pkg .JSFiles {
474
502
code , err := ioutil .ReadFile (filepath .Join (pkg .Dir , jsFile ))
475
503
if err != nil {
476
504
return err
@@ -582,7 +610,7 @@ func jsFilesFromDir(dir string) ([]string, error) {
582
610
}
583
611
var jsFiles []string
584
612
for _ , file := range files {
585
- if strings .HasSuffix (file .Name (), ".inc.js" ) && file .Name ()[0 ] != '_' {
613
+ if strings .HasSuffix (file .Name (), ".inc.js" ) && file .Name ()[0 ] != '_' && file . Name ()[ 0 ] != '.' {
586
614
jsFiles = append (jsFiles , file .Name ())
587
615
}
588
616
}
0 commit comments