]> Cypherpunks.ru repositories - goredo.git/blob - tgt.go
Binary format and many optimisations
[goredo.git] / tgt.go
1 package main
2
3 import (
4         "path"
5         "path/filepath"
6 )
7
8 var TgtCache = make(map[string]*Tgt)
9
10 func mustAbs(pth string) string {
11         pth, err := filepath.Abs(pth)
12         if err != nil {
13                 panic(err)
14         }
15         return pth
16 }
17
18 func mustRel(basepath, targpath string) string {
19         pth, err := filepath.Rel(basepath, targpath)
20         if err != nil {
21                 panic(err)
22         }
23         return pth
24 }
25
26 func cwdMustRel(paths ...string) string {
27         return mustRel(Cwd, path.Join(paths...))
28 }
29
30 func pathSplit(a string) (h, t string) {
31         h, t = path.Split(a)
32         if len(h) > 1 {
33                 h = h[:len(h)-1]
34         }
35         return
36 }
37
38 type Tgt struct {
39         a   string // absolute path
40         rel string // relative to Cwd
41         dep string // path to dependency file
42 }
43
44 func NewTgt(tgt string) *Tgt {
45         a := mustAbs(tgt)
46         if TgtCache != nil {
47                 if t := TgtCache[a]; t != nil {
48                         return t
49                 }
50         }
51         h, t := pathSplit(a)
52         res := Tgt{
53                 a:   a,
54                 rel: mustRel(Cwd, a),
55                 dep: path.Join(h, RedoDir, t+DepSuffix),
56         }
57         if TgtCache != nil {
58                 TgtCache[a] = &res
59         }
60         return &res
61 }
62
63 func (tgt *Tgt) String() string {
64         return tgt.rel
65 }
66
67 func (tgt *Tgt) RelTo(cwd string) string {
68         return mustRel(cwd, tgt.a)
69 }