1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16package gitee_build
17
18import (
19	"context"
20	"fotff/pkg"
21	"fotff/pkg/gitee_common"
22	"fotff/utils"
23	"github.com/sirupsen/logrus"
24	"strings"
25)
26
27type Manager struct {
28	ArchiveDir     string   `key:"archive_dir" default:"archive"`
29	Workspace      string   `key:"workspace" default:"workspace"`
30	Branch         string   `key:"branch" default:"master"`
31	ManifestBranch string   `key:"manifest_branch" default:"master"`
32	Component      string   `key:"component"`
33	PreCompileCMD  string   `key:"pre_compile_cmd"`
34	CompileCMD     string   `key:"compile_cmd"`
35	ImageList      []string `key:"image_list"`
36
37	*gitee_common.Manager
38}
39
40func NewManager() pkg.Manager {
41	var ret Manager
42	utils.ParseFromConfigFile("gitee_build", &ret)
43	ret.Manager = gitee_common.NewManager(ret.Component, ret.Branch, ret.ManifestBranch, ret.ArchiveDir, ret.Workspace, true)
44	return &ret
45}
46
47func (m *Manager) GetNewer(cur string) (string, error) {
48	return m.GetNewerOrFail(cur)
49}
50
51// Flash function implements pkg.Manager. Since this gitee_build just tests package buildings,
52// there is no need to flash images actually, just build it and return nil unconditionally.
53func (m *Manager) Flash(device string, pkg string, ctx context.Context) error {
54	logrus.Infof("now flash %s", pkg)
55	buildConfig := gitee_common.BuildConfig{
56		Pkg:           pkg,
57		PreCompileCMD: m.PreCompileCMD,
58		CompileCMD:    m.CompileCMD,
59		ImgList:       m.ImageList,
60	}
61	if m.PkgAvailable(buildConfig) {
62		return nil
63	}
64	if strings.Contains(buildConfig.Pkg, "build_fail") {
65		logrus.Warnf("here is a known build_fail token package")
66	} else {
67		if err := m.BuildNoRetry(buildConfig, true, ctx); err != nil {
68			logrus.Warnf("build %s fail, err: %v", pkg, err)
69		} else {
70			logrus.Infof("%s is available now", pkg)
71		}
72	}
73	logrus.Infof("since fotff just tests package buildings, there is no need to flash images actually, mark flash as a success unconditionally")
74	return nil
75}
76