1ba991379Sopenharmony_ci/*
2ba991379Sopenharmony_ci * Copyright (c) 2022-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 dayu200
17ba991379Sopenharmony_ci
18ba991379Sopenharmony_ciimport (
19ba991379Sopenharmony_ci	"context"
20ba991379Sopenharmony_ci	"fotff/pkg"
21ba991379Sopenharmony_ci	"fotff/pkg/gitee_common"
22ba991379Sopenharmony_ci	"fotff/res"
23ba991379Sopenharmony_ci	"fotff/utils"
24ba991379Sopenharmony_ci	"github.com/sirupsen/logrus"
25ba991379Sopenharmony_ci	"strconv"
26ba991379Sopenharmony_ci	"strings"
27ba991379Sopenharmony_ci)
28ba991379Sopenharmony_ci
29ba991379Sopenharmony_citype Manager struct {
30ba991379Sopenharmony_ci	ArchiveDir     string `key:"archive_dir" default:"archive"`
31ba991379Sopenharmony_ci	WatchCI        string `key:"watch_ci" default:"false"`
32ba991379Sopenharmony_ci	Workspace      string `key:"workspace" default:"workspace"`
33ba991379Sopenharmony_ci	Branch         string `key:"branch" default:"master"`
34ba991379Sopenharmony_ci	ManifestBranch string `key:"manifest_branch" default:"master"`
35ba991379Sopenharmony_ci	FlashTool      string `key:"flash_tool" default:"python"`
36ba991379Sopenharmony_ci	LocationIDList string `key:"location_id_list"`
37ba991379Sopenharmony_ci
38ba991379Sopenharmony_ci	*gitee_common.Manager
39ba991379Sopenharmony_ci	locations map[string]string
40ba991379Sopenharmony_ci}
41ba991379Sopenharmony_ci
42ba991379Sopenharmony_ci// These commands are copied from ci project.
43ba991379Sopenharmony_ciconst (
44ba991379Sopenharmony_ci	preCompileCMD = `rm -rf prebuilts/clang/ohos/darwin-x86_64/clang-480513;rm -rf prebuilts/clang/ohos/windows-x86_64/clang-480513;rm -rf prebuilts/clang/ohos/linux-x86_64/clang-480513;bash build/prebuilts_download.sh`
45ba991379Sopenharmony_ci	// compileCMD is copied from ci project and trim useless build-target 'make_test' to enhance build efficiency.
46ba991379Sopenharmony_ci	compileCMD = `echo 'start' && export NO_DEVTOOL=1 && export CCACHE_LOG_SUFFIX="dayu200-arm32" && export CCACHE_NOHASHDIR="true" && export CCACHE_SLOPPINESS="include_file_ctime" && ./build.sh --product-name rk3568 --ccache --build-target make_all --gn-args enable_notice_collection=false`
47ba991379Sopenharmony_ci)
48ba991379Sopenharmony_ci
49ba991379Sopenharmony_ci// This list is copied from ci project. Some of them are not available, has been annotated.
50ba991379Sopenharmony_civar imgList = []string{
51ba991379Sopenharmony_ci	"out/rk3568/packages/phone/images/MiniLoaderAll.bin",
52ba991379Sopenharmony_ci	"out/rk3568/packages/phone/images/boot_linux.img",
53ba991379Sopenharmony_ci	"out/rk3568/packages/phone/images/parameter.txt",
54ba991379Sopenharmony_ci	"out/rk3568/packages/phone/images/system.img",
55ba991379Sopenharmony_ci	"out/rk3568/packages/phone/images/uboot.img",
56ba991379Sopenharmony_ci	"out/rk3568/packages/phone/images/userdata.img",
57ba991379Sopenharmony_ci	"out/rk3568/packages/phone/images/vendor.img",
58ba991379Sopenharmony_ci	"out/rk3568/packages/phone/images/resource.img",
59ba991379Sopenharmony_ci	"out/rk3568/packages/phone/images/config.cfg",
60ba991379Sopenharmony_ci	"out/rk3568/packages/phone/images/ramdisk.img",
61ba991379Sopenharmony_ci	// "out/rk3568/packages/phone/images/chipset.img",
62ba991379Sopenharmony_ci	"out/rk3568/packages/phone/images/sys_prod.img",
63ba991379Sopenharmony_ci	"out/rk3568/packages/phone/images/chip_prod.img",
64ba991379Sopenharmony_ci	"out/rk3568/packages/phone/images/updater.img",
65ba991379Sopenharmony_ci	// "out/rk3568/packages/phone/updater/bin/updater_binary",
66ba991379Sopenharmony_ci}
67ba991379Sopenharmony_ci
68ba991379Sopenharmony_cifunc NewManager() pkg.Manager {
69ba991379Sopenharmony_ci	var ret Manager
70ba991379Sopenharmony_ci	utils.ParseFromConfigFile("dayu200", &ret)
71ba991379Sopenharmony_ci	watchCI, err := strconv.ParseBool(ret.WatchCI)
72ba991379Sopenharmony_ci	if err != nil {
73ba991379Sopenharmony_ci		logrus.Panicf("can not parse 'watch_ci', please check")
74ba991379Sopenharmony_ci	}
75ba991379Sopenharmony_ci	ret.Manager = gitee_common.NewManager("dayu200", ret.Branch, ret.ManifestBranch, ret.ArchiveDir, ret.Workspace, watchCI)
76ba991379Sopenharmony_ci	devs := res.DeviceList()
77ba991379Sopenharmony_ci	locs := strings.Split(ret.LocationIDList, ",")
78ba991379Sopenharmony_ci	if len(devs) != len(locs) {
79ba991379Sopenharmony_ci		logrus.Panicf("location_id_list and devices mismatch")
80ba991379Sopenharmony_ci	}
81ba991379Sopenharmony_ci	ret.locations = map[string]string{}
82ba991379Sopenharmony_ci	for i, loc := range locs {
83ba991379Sopenharmony_ci		ret.locations[devs[i]] = loc
84ba991379Sopenharmony_ci	}
85ba991379Sopenharmony_ci	return &ret
86ba991379Sopenharmony_ci}
87ba991379Sopenharmony_ci
88ba991379Sopenharmony_ci// Flash function implements pkg.Manager. Flash images in the 'pkg' directory to the given device.
89ba991379Sopenharmony_ci// If not all necessary images are available in the 'pkg' directory, will build them.
90ba991379Sopenharmony_cifunc (m *Manager) Flash(device string, pkg string, ctx context.Context) error {
91ba991379Sopenharmony_ci	logrus.Infof("now flash %s", pkg)
92ba991379Sopenharmony_ci	buildConfig := gitee_common.BuildConfig{
93ba991379Sopenharmony_ci		Pkg:           pkg,
94ba991379Sopenharmony_ci		PreCompileCMD: preCompileCMD,
95ba991379Sopenharmony_ci		CompileCMD:    compileCMD,
96ba991379Sopenharmony_ci		ImgList:       imgList,
97ba991379Sopenharmony_ci	}
98ba991379Sopenharmony_ci	if err := m.Build(buildConfig, ctx); err != nil {
99ba991379Sopenharmony_ci		logrus.Errorf("build %s fail, err: %v", pkg, err)
100ba991379Sopenharmony_ci		return err
101ba991379Sopenharmony_ci	}
102ba991379Sopenharmony_ci	logrus.Infof("%s is available now, start to flash it", pkg)
103ba991379Sopenharmony_ci	return m.flashDevice(device, pkg, ctx)
104ba991379Sopenharmony_ci}
105