all repos — onyx @ main

minimal map annotation and location data sharing tool

buildtools/sourcemapper.go (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main

import (
  "os"
  "fmt"
  "strings"
  "strconv"
)

type LineRange struct {
  Start int
  End int
}

func srcMapDeserialize(s string) map[string]LineRange {
  self := map[string]LineRange{}
  totalLines := 0
  lines := strings.Split(s, "\n");
  for _, l := range lines {
    file := ""
    nLines := 0
    
    fmt.Sscanf(l, "%s\t%d", &file, &nLines)
    rg := LineRange{
      Start: totalLines,
      End: totalLines + nLines,
    }
    totalLines += nLines
    self[file] = rg;
  }
  return self
}

func buildOutputTransform(buildOutput string, srcMap map[string]LineRange) string {
  self := ""
  lines := strings.Split(buildOutput, "\n")
  for _, l := range lines {
    parts := strings.Split(l, ":")
    if (len(parts) > 1) {
      lineNumber, err := strconv.ParseInt(parts[1], 10, 32)
      if err == nil {
        for f, rg := range srcMap {
          if lineNumber <= int64(rg.End) && lineNumber > int64(rg.Start) {
            self += strings.Replace(
              strings.Replace(l, parts[1], strconv.FormatInt(lineNumber - int64(rg.Start), 10), 1),
              parts[0], f, 1) + "\n";
          }
        }      
      } else {
        panic(err.Error())
      }
    } else {
      self += l
    }
  }
  return self
}

func main() {
  if len(os.Args) == 3 {
    errFilename := os.Args[1];
    srcMapFilename := os.Args[2];
    errFile, err1 := os.ReadFile(errFilename);
    srcMapFile, err2 := os.ReadFile(srcMapFilename);
    if err1 != nil || err2 != nil {
      panic("Couldn't open either the error temp file or the source map");
    }
    srcMap := srcMapDeserialize(string(srcMapFile[:]))
    fmt.Print(buildOutputTransform(string(errFile[:]), srcMap))
  } else {
    fmt.Println("usage: sourcemapper ERRFILE SRCMAP");
  }
}