]> Cypherpunks.ru repositories - goredo.git/blob - do.go
ab200c611d82a27612388c7bc874c9ad0aba0378
[goredo.git] / do.go
1 /*
2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2024 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         "io"
24         "path"
25         "strings"
26 )
27
28 const (
29         EnvTopDir = "REDO_TOP_DIR"
30         TopFile   = "top"
31 )
32
33 var TopDir string
34
35 func existsDo(w io.Writer, fdDepName, cwd, pth string) (bool, error) {
36         if FileExists(path.Join(cwd, pth)) {
37                 return true, nil
38         }
39         return false, ifcreate(w, fdDepName, pth)
40 }
41
42 func findDo(w io.Writer, fdDepName, cwd, tgt string) (string, int, error) {
43         doFile := tgt + ".do"
44         exists, err := existsDo(w, fdDepName, cwd, doFile)
45         if err != nil {
46                 return "", 0, err
47         }
48         if exists {
49                 return doFile, 0, nil
50         }
51         doFileOrig := doFile
52         levels := []string{}
53         extsOrig := strings.Split(tgt, ".")[1:]
54         dirAbsPrev := ""
55         for {
56                 exts := extsOrig
57                 updir := path.Join(levels...)
58                 for len(exts) > 0 {
59                         doFile = strings.Join(append(
60                                 []string{"default"}, append(exts, "do")...,
61                         ), ".")
62                         if len(levels) > 0 || (doFile != doFileOrig && doFile != tgt) {
63                                 exists, err = existsDo(w, fdDepName, cwd, path.Join(updir, doFile))
64                                 if err != nil {
65                                         return "", 0, err
66                                 }
67                                 if exists {
68                                         return doFile, len(levels), nil
69                                 }
70                         }
71                         exts = exts[1:]
72                 }
73                 doFile = "default.do"
74                 if len(levels) > 0 || (doFile != doFileOrig && doFile != tgt) {
75                         exists, err = existsDo(w, fdDepName, cwd, path.Join(updir, doFile))
76                         if err != nil {
77                                 return "", 0, err
78                         }
79                         if exists {
80                                 return doFile, len(levels), nil
81                         }
82                 }
83                 levels = append(levels, "..")
84                 dirAbs := mustAbs(path.Join(cwd, updir))
85                 if err != nil {
86                         panic(err)
87                 }
88                 if dirAbs == TopDir {
89                         break
90                 }
91                 if FileExists(path.Join(dirAbs, RedoDir, TopFile)) {
92                         break
93                 }
94                 if dirAbs == dirAbsPrev {
95                         break
96                 }
97                 dirAbsPrev = dirAbs
98         }
99         return "", 0, nil
100 }