]> Cypherpunks.ru repositories - goredo.git/blob - ifchange.go
Various refactoring
[goredo.git] / ifchange.go
1 /*
2 goredo -- redo implementation on pure Go
3 Copyright (C) 2020 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 package main
19
20 import "sync"
21
22 var (
23         Force bool = false
24         Jobs  sync.WaitGroup
25 )
26
27 func isOkRun(err error) bool {
28         if err == nil {
29                 return true
30         }
31         if err, ok := err.(RunErr); ok && err.Err == nil {
32                 trace(CRedo, "%s", err.Name())
33                 return true
34         }
35         trace(CErr, "%s", err)
36         return false
37 }
38
39 func ifchange(tgts []string) (bool, error) {
40         jsInit()
41         defer jsAcquire("ifchange exiting")
42         defer Jobs.Wait()
43         errs := make(chan error, len(tgts))
44         jobs := 0
45         ok := true
46         var err error
47         for _, tgt := range tgts {
48                 var ood bool
49                 if Force {
50                         ood = true
51                 } else {
52                         ood, err = isOOD(Cwd, tgt, 0)
53                         if err != nil {
54                                 return false, err
55                         }
56                 }
57                 if !ood {
58                         continue
59                 }
60                 if isSrc(Cwd, tgt) {
61                         trace(CDebug, "%s is source, not redoing", tgt)
62                         continue
63                 }
64                 if err = runScript(tgt, errs); err != nil {
65                         return false, err
66                 }
67                 if Force {
68                         // Sequentially run jobs
69                         err = <-errs
70                         Jobs.Wait()
71                         if isOkRun(err) {
72                                 continue
73                         }
74                         return false, nil
75                 }
76                 jobs++
77         }
78         for i := 0; i < jobs; i++ {
79                 err = <-errs
80                 ok = ok && isOkRun(err)
81         }
82         return ok, nil
83 }