forked from majewsky/art
/
source.go
87 lines (77 loc) · 2.42 KB
/
source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*******************************************************************************
*
* Copyright 2017 Stefan Majewsky <majewsky@gmx.net>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/
package main
import (
"os"
"path/filepath"
"sort"
"strings"
)
//Source describes a directory from which packages are read.
type Source struct {
Path string `toml:"path"`
Packages []Package `toml:"-"`
}
func (s *Source) discoverPackages(mcfg MakepkgConfig) error {
dir, err := os.Open(s.Path)
if err != nil {
return err
}
fis, err := dir.Readdir(-1)
if err != nil {
return err
}
sort.Slice(fis, func(i, j int) bool { return fis[i].Name() < fis[j].Name() })
for _, fi := range fis {
pkgPath := filepath.Join(s.Path, fi.Name())
//subdirectory: is a package if there is a PKGBUILD
if fi.IsDir() {
fi2, err := os.Stat(filepath.Join(pkgPath, "PKGBUILD"))
if err != nil {
if os.IsNotExist(err) {
continue
}
return err
}
if isRegularOrSymlink(fi2.Mode()) {
s.Packages = append(s.Packages, &NativePackage{
Path: filepath.Join(pkgPath, "PKGBUILD"),
MakepkgConfig: mcfg,
})
}
}
//regular file or symlink: is a package if suffix ".PKGBUILD"
if isRegularOrSymlink(fi.Mode()) && strings.HasSuffix(pkgPath, ".PKGBUILD") {
s.Packages = append(s.Packages, &NativePackage{
Path: pkgPath,
MakepkgConfig: mcfg,
})
}
//regular file or symlink: is a holo-build package if suffix ".pkg.toml"
if isRegularOrSymlink(fi.Mode()) && strings.HasSuffix(pkgPath, ".pkg.toml") {
s.Packages = append(s.Packages, &HoloBuildPackage{
Path: pkgPath,
MakepkgConfig: mcfg,
})
}
}
return nil
}
func isRegularOrSymlink(mode os.FileMode) bool {
return mode.IsRegular() || (mode&os.ModeType) == os.ModeSymlink
}