/
detect.go
111 lines (94 loc) · 2.82 KB
/
detect.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) The Amphitheatre Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aptos
import (
"fmt"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/buildpacks/libcnb"
)
const (
PlanEntryAptos = "aptos"
)
type Detect struct {
}
type Package struct {
Dependencies map[string]*Dependency `toml:"dependencies"`
}
type Dependency struct {
Git string `toml:"git"`
Rev string `toml:"rev"`
Subdir string `toml:"subdir"`
}
func (d Detect) Detect(context libcnb.DetectContext) (libcnb.DetectResult, error) {
found, err := d.aptosProject(context.Application.Path)
if err != nil {
return libcnb.DetectResult{Pass: false}, fmt.Errorf("unable to detect Aptos requirements\n%w", err)
}
if !found {
return libcnb.DetectResult{Pass: false}, nil
}
return libcnb.DetectResult{
Pass: true,
Plans: []libcnb.BuildPlan{
{
Provides: []libcnb.BuildPlanProvide{
{Name: PlanEntryAptos},
},
Requires: []libcnb.BuildPlanRequire{
{Name: PlanEntryAptos},
},
},
},
}, nil
}
func (d Detect) aptosProject(appDir string) (bool, error) {
projectName := filepath.Join(appDir, "Move.toml")
_, err := os.Stat(projectName)
if os.IsNotExist(err) || err != nil {
return false, fmt.Errorf("unable to determine if %s exists\n%w", projectName, err)
}
var packageConfig Package
if _, err := toml.DecodeFile(projectName, &packageConfig); err != nil {
return false, fmt.Errorf("unable to parsing %s\n%w", projectName, err)
}
if _, ok := packageConfig.Dependencies["AptosFramework"]; !ok {
return false, fmt.Errorf("unable to determine if Aptos project")
}
buildDirectory := filepath.Join(appDir, ".")
extension := ".move"
if err := existsFilesWithExtension(buildDirectory, extension); err != nil {
return false, fmt.Errorf("unable to determine if '%s' exists\n%w", extension, err)
}
return true, nil
}
func existsFilesWithExtension(directory, extension string) error {
var found bool
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Check if the file has the specified extension.
if !info.IsDir() && filepath.Ext(path) == extension {
found = true
return nil
}
return nil
})
if !found {
return fmt.Errorf("no files with extension '%s' found", extension)
}
return err
}