sylixos-uploader/parser/filepath_paser.go

121 lines
2.9 KiB
Go

package parser
import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
const (
levelDebug = "debug"
levelRelease = "release"
configMK = "config.mk"
Output = "$(Output)"
PrjPath = "$(ProjectPath)"
WorkspacePrefix = "$(WORKSPACE_"
)
var outputMap = map[string]string{
levelDebug: "Debug",
levelRelease: "Release",
}
var (
ErrInvalidXMLLocalPathFormat = errors.New("invalid local path format for this file")
)
func getProjectName(bspPrjPath string) string {
return filepath.Base(bspPrjPath)
}
func convertFilepath(xmlLocalPath string) (yamlLocalPath string, err error) {
if !strings.HasPrefix(xmlLocalPath, WorkspacePrefix) {
// fixedPath := fixOthersInPath(xmlLocalPath, "OVERRIDE-Needed")
return xmlLocalPath, ErrInvalidXMLLocalPathFormat
}
tmpPath := fixWorkspacePrefix(xmlLocalPath)
// tmpPath = fixOutputInPath(tmpPath)
// tmpPath = fixOthersInPath(tmpPath, "OVERRIDE-by-Others")
// make it unix like
yamlLocalPath = strings.ReplaceAll(tmpPath, "\\", "/")
if yamlLocalPath[:1] == "/" {
return yamlLocalPath[1:], nil
}
return yamlLocalPath, nil
}
// getDebugLevel 读取 config.mk 文件中的 DEBUG_LEVEL
func getDebugLevel(bspPrjPath string) (string, error) {
file, err := os.Open(filepath.Join(bspPrjPath, configMK))
if err != nil {
return "", err
}
defer file.Close()
scanner := bufio.NewScanner(file)
re, err := regexp.Compile(`^DEBUG_LEVEL\s*:=\s*(\w+)$`)
if err != nil {
return "", err
}
for scanner.Scan() {
line := scanner.Text()
if matches := re.FindStringSubmatch(line); matches != nil {
if matches[1] != levelDebug && matches[1] != levelRelease {
return "", fmt.Errorf("invalid DEBUG_LEVEL: %s", matches[1])
}
return matches[1], nil // 返回 DEBUG_LEVEL 的值
}
}
if err := scanner.Err(); err != nil {
return "", err
}
return "", fmt.Errorf("DEBUG_LEVEL not found in %s", bspPrjPath)
}
func hasOutputField(LocalPath string) bool {
return strings.Contains(LocalPath, Output)
}
func updateOutputInPath(LocalPath string, outPutPath string) string {
re, err := regexp.Compile(`\$\(Output\)`)
if err != nil {
return LocalPath
}
return re.ReplaceAllString(LocalPath, outPutPath)
}
func updateProjectPathInPath(LocalPath string, projectPath string) string {
re, err := regexp.Compile(`\$\(ProjectPath\)`)
if err != nil {
return LocalPath
}
return re.ReplaceAllString(LocalPath, projectPath)
}
func updateOthersInPath(LocalPath string, overrideString string) string {
re, err := regexp.Compile(`\$\(([^)]+)\)`)
if err != nil {
return LocalPath
}
return re.ReplaceAllString(LocalPath, overrideString)
}
// fixWorkspacePrefix 过滤掉路径中的 $(WORKSPACE_xxx)\ 或 $(WORKSPACE_xxx)/ 部分
func fixWorkspacePrefix(xmlLocalPath string) string {
re, err := regexp.Compile(`\$\(WORKSPACE_[^)]+\)`)
if err != nil {
return xmlLocalPath
}
return re.ReplaceAllString(xmlLocalPath, PrjPath)
}