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