func (s *session) writeCommandPackage(pkg *packageData, pkgObj string) error { if !pkg.IsCommand() || pkg.UpToDate { return nil } if err := os.MkdirAll(filepath.Dir(pkgObj), 0777); err != nil { return err } codeFile, err := os.Create(pkgObj) if err != nil { return err } defer codeFile.Close() mapFile, err := os.Create(pkgObj + ".map") if err != nil { return err } defer mapFile.Close() var allPkgs []*translator.Archive for _, depPath := range pkg.Archive.Dependencies { dep, err := s.importPackage(depPath) if err != nil { return err } allPkgs = append(allPkgs, dep) } m := sourcemap.Map{File: filepath.Base(pkgObj)} s.t.WriteProgramCode(allPkgs, pkg.ImportPath, &translator.SourceMapFilter{Writer: codeFile, MappingCallback: func(generatedLine, generatedColumn int, fileSet *token.FileSet, originalPos token.Pos) { pos := fileSet.Position(originalPos) file := pos.Filename if strings.HasPrefix(file, build.Default.GOPATH) { file = filepath.ToSlash(filepath.Join("gopath", file[len(build.Default.GOPATH):])) } if strings.HasPrefix(file, build.Default.GOROOT) { file = filepath.ToSlash(filepath.Join("goroot", file[len(build.Default.GOROOT):])) } m.AddMapping(&sourcemap.Mapping{GeneratedLine: generatedLine, GeneratedColumn: generatedColumn, OriginalFile: file, OriginalLine: pos.Line, OriginalColumn: pos.Column}) }}) fmt.Fprintf(codeFile, "//# sourceMappingURL=%s.map\n", filepath.Base(pkgObj)) m.WriteTo(mapFile) return nil }
func NewMappingCallback(m *sourcemap.Map, goroot, gopath string) func(generatedLine, generatedColumn int, originalPos token.Position) { return func(generatedLine, generatedColumn int, originalPos token.Position) { if !originalPos.IsValid() { m.AddMapping(&sourcemap.Mapping{GeneratedLine: generatedLine, GeneratedColumn: generatedColumn}) return } file := originalPos.Filename switch hasGopathPrefix, prefixLen := hasGopathPrefix(file, gopath); { case hasGopathPrefix: file = filepath.ToSlash(file[prefixLen+4:]) case strings.HasPrefix(file, goroot): file = filepath.ToSlash(file[len(goroot)+4:]) default: file = filepath.Base(file) } m.AddMapping(&sourcemap.Mapping{GeneratedLine: generatedLine, GeneratedColumn: generatedColumn, OriginalFile: file, OriginalLine: originalPos.Line, OriginalColumn: originalPos.Column}) } }
func (s *Session) WriteCommandPackage(pkg *PackageData, pkgObj string) error { if !pkg.IsCommand() || pkg.UpToDate { return nil } if err := os.MkdirAll(filepath.Dir(pkgObj), 0777); err != nil { return err } codeFile, err := os.Create(pkgObj) if err != nil { return err } defer codeFile.Close() sourceMapFilter := &compiler.SourceMapFilter{Writer: codeFile} if s.options.CreateMapFile { m := sourcemap.Map{File: filepath.Base(pkgObj)} mapFile, err := os.Create(pkgObj + ".map") if err != nil { return err } defer func() { m.WriteTo(mapFile) mapFile.Close() fmt.Fprintf(codeFile, "//# sourceMappingURL=%s.map\n", filepath.Base(pkgObj)) }() sourceMapFilter.MappingCallback = func(generatedLine, generatedColumn int, fileSet *token.FileSet, originalPos token.Pos) { if !originalPos.IsValid() { m.AddMapping(&sourcemap.Mapping{GeneratedLine: generatedLine, GeneratedColumn: generatedColumn}) return } pos := fileSet.Position(originalPos) file := pos.Filename switch { case strings.HasPrefix(file, s.options.GOPATH): file = filepath.ToSlash(filepath.Join("/gopath", file[len(s.options.GOPATH):])) case strings.HasPrefix(file, s.options.GOROOT): file = filepath.ToSlash(filepath.Join("/goroot", file[len(s.options.GOROOT):])) default: file = filepath.Base(file) } m.AddMapping(&sourcemap.Mapping{GeneratedLine: generatedLine, GeneratedColumn: generatedColumn, OriginalFile: file, OriginalLine: pos.Line, OriginalColumn: pos.Column}) } } deps, err := s.ImportDependencies(pkg.Archive) if err != nil { return err } compiler.WriteProgramCode(deps, s.ImportContext, sourceMapFilter) return nil }