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