Skip to content

Commit

Permalink
Serializing Archieves
Browse files Browse the repository at this point in the history
  • Loading branch information
grantnelson-wf committed Dec 5, 2024
1 parent c2773f3 commit 0718ede
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"sort"
"strconv"
"strings"
"sync"
"time"

"github.com/fsnotify/fsnotify"
Expand Down Expand Up @@ -938,23 +939,40 @@ func (s *Session) buildImportPathWithSrcDir(path string, srcDir string) (*Packag
return pkg, archive, nil
}

// getExeModTime will determine the mod time of the GopherJS binary
// the first time this is called and cache the result for subsequent calls.
var getExeModTime = func() func() time.Time {
var (
once sync.Once
result time.Time
)
getTime := func() {
var fileInfo os.FileInfo
gopherjsBinary, err := os.Executable()
if err == nil {
fileInfo, err = os.Stat(gopherjsBinary)
if err == nil {
result = fileInfo.ModTime()
return
}
}
os.Stderr.WriteString("Could not get GopherJS binary's modification timestamp. Please report issue.\n")
result = time.Now()
}
return func() time.Time {
once.Do(getTime)
return result
}
}()

// BuildPackage compiles an already loaded package.
func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
if archive, ok := s.UpToDateArchives[pkg.ImportPath]; ok {
return archive, nil
}

var fileInfo os.FileInfo
gopherjsBinary, err := os.Executable()
if err == nil {
fileInfo, err = os.Stat(gopherjsBinary)
if err == nil && fileInfo.ModTime().After(pkg.SrcModTime) {
pkg.SrcModTime = fileInfo.ModTime()
}
}
if err != nil {
os.Stderr.WriteString("Could not get GopherJS binary's modification timestamp. Please report issue.\n")
pkg.SrcModTime = time.Now()
if exeModTime := getExeModTime(); exeModTime.After(pkg.SrcModTime) {
pkg.SrcModTime = exeModTime
}

for _, importedPkgPath := range pkg.Imports {
Expand All @@ -966,14 +984,13 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
return nil, err
}

impModTime := importedPkg.SrcModTime
if impModTime.After(pkg.SrcModTime) {
if impModTime := importedPkg.SrcModTime; impModTime.After(pkg.SrcModTime) {
pkg.SrcModTime = impModTime
}
}

if pkg.FileModTime().After(pkg.SrcModTime) {
pkg.SrcModTime = pkg.FileModTime()
if fileModTime := pkg.FileModTime(); fileModTime.After(pkg.SrcModTime) {
pkg.SrcModTime = fileModTime
}

if !s.options.NoCache {
Expand Down Expand Up @@ -1018,7 +1035,8 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
fmt.Println(pkg.ImportPath)
}

s.buildCache.StoreArchive(archive, time.Now())
archiveBuiltTime := time.Now()
s.buildCache.StoreArchive(archive, archiveBuiltTime)
s.UpToDateArchives[pkg.ImportPath] = archive

return archive, nil
Expand Down

0 comments on commit 0718ede

Please sign in to comment.