]> Cypherpunks.ru repositories - goredo.git/blob - do.go
default.do-s must not depend on themselves
[goredo.git] / do.go
1 /*
2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2021 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // .do finder
19
20 package main
21
22 import (
23         "os"
24         "path"
25         "path/filepath"
26         "strings"
27 )
28
29 const (
30         EnvTopDir = "REDO_TOP_DIR"
31         TopFile   = "top"
32 )
33
34 var TopDir string
35
36 func existsDo(fdDep *os.File, cwd, pth string) (bool, error) {
37         if _, err := os.Stat(path.Join(cwd, pth)); err == nil {
38                 return true, nil
39         }
40         return false, ifcreate(fdDep, pth)
41 }
42
43 func findDo(fdDep *os.File, cwd, tgt string) (string, int, error) {
44         doFile := tgt + ".do"
45         exists, err := existsDo(fdDep, cwd, doFile)
46         if err != nil {
47                 return "", 0, err
48         }
49         if exists {
50                 return doFile, 0, nil
51         }
52         doFileOrig := doFile
53         levels := []string{}
54         extsOrig := strings.Split(tgt, ".")[1:]
55         dirAbsPrev := ""
56         for {
57                 exts := extsOrig
58                 updir := path.Join(levels...)
59                 for len(exts) > 0 {
60                         doFile = strings.Join(append(
61                                 []string{"default"}, append(exts, "do")...,
62                         ), ".")
63                         if len(levels) > 0 || (doFile != doFileOrig && doFile != tgt) {
64                                 exists, err = existsDo(fdDep, cwd, path.Join(updir, doFile))
65                                 if err != nil {
66                                         return "", 0, err
67                                 }
68                                 if exists {
69                                         return doFile, len(levels), nil
70                                 }
71                         }
72                         exts = exts[1:]
73                 }
74                 doFile = "default.do"
75                 if len(levels) > 0 || (doFile != doFileOrig && doFile != tgt) {
76                         exists, err = existsDo(fdDep, cwd, path.Join(updir, doFile))
77                         if err != nil {
78                                 return "", 0, err
79                         }
80                         if exists {
81                                 return doFile, len(levels), nil
82                         }
83                 }
84                 levels = append(levels, "..")
85                 dirAbs, err := filepath.Abs(path.Join(cwd, updir))
86                 if err != nil {
87                         panic(err)
88                 }
89                 if dirAbs == TopDir {
90                         break
91                 }
92                 if _, err = os.Stat(path.Join(dirAbs, RedoDir, TopFile)); err == nil {
93                         break
94                 }
95                 if dirAbs == dirAbsPrev {
96                         break
97                 }
98                 dirAbsPrev = dirAbs
99         }
100         return "", 0, nil
101 }