Извлечение имени репозитория из пути к скрипту при сборке через -s
All checks were successful
Pre-commit / pre-commit (pull_request) Successful in 3m47s
All checks were successful
Pre-commit / pre-commit (pull_request) Successful in 3m47s
This commit is contained in:
@@ -293,7 +293,7 @@ func (b *Builder) BuildPackageFromScript(
|
||||
) ([]*BuiltDep, error) {
|
||||
return b.BuildPackage(ctx, &BuildInput{
|
||||
script: args.Script,
|
||||
repository: "default",
|
||||
repository: ExtractRepoNameFromPath(args.Script),
|
||||
packages: args.Packages,
|
||||
pkgFormat: args.PkgFormat(),
|
||||
opts: args.Opts,
|
||||
|
||||
@@ -331,3 +331,29 @@ func removeDuplicatesSources(sources, checksums []string) ([]string, []string) {
|
||||
}
|
||||
return newSources, newChecksums
|
||||
}
|
||||
|
||||
// ExtractRepoNameFromPath извлекает имя репозитория из пути к скрипту.
|
||||
// Ожидаемый формат: repo-name/package-name/alr.sh или /abs/path/repo-name/package-name/alr.sh
|
||||
// Возвращает "default", если не удалось извлечь имя.
|
||||
func ExtractRepoNameFromPath(scriptPath string) string {
|
||||
// Нормализуем путь
|
||||
cleanPath := filepath.Clean(scriptPath)
|
||||
|
||||
// Разбиваем путь на компоненты
|
||||
dir := filepath.Dir(cleanPath) // package-name
|
||||
if dir == "." || dir == "/" {
|
||||
return "default"
|
||||
}
|
||||
|
||||
repoDir := filepath.Dir(dir) // repo-name
|
||||
if repoDir == "." || repoDir == "/" {
|
||||
return "default"
|
||||
}
|
||||
|
||||
repoName := filepath.Base(repoDir)
|
||||
if repoName == "." || repoName == "/" || repoName == "" {
|
||||
return "default"
|
||||
}
|
||||
|
||||
return repoName
|
||||
}
|
||||
|
||||
@@ -155,4 +155,57 @@ func TestRegexpALRPackageName(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractRepoNameFromPath(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
scriptPath string
|
||||
expectedRepo string
|
||||
}{
|
||||
{
|
||||
name: "относительный путь - стандартная структура",
|
||||
scriptPath: "alr-default/alr-bin/alr.sh",
|
||||
expectedRepo: "alr-default",
|
||||
},
|
||||
{
|
||||
name: "абсолютный путь",
|
||||
scriptPath: "/home/user/repos/alr-default/alr-bin/alr.sh",
|
||||
expectedRepo: "alr-default",
|
||||
},
|
||||
{
|
||||
name: "репозиторий без префикса alr-",
|
||||
scriptPath: "my-repo/my-package/alr.sh",
|
||||
expectedRepo: "my-repo",
|
||||
},
|
||||
{
|
||||
name: "только имя файла",
|
||||
scriptPath: "alr.sh",
|
||||
expectedRepo: "default",
|
||||
},
|
||||
{
|
||||
name: "один уровень директории",
|
||||
scriptPath: "package/alr.sh",
|
||||
expectedRepo: "default",
|
||||
},
|
||||
{
|
||||
name: "путь с точками",
|
||||
scriptPath: "./alr-default/alr-bin/alr.sh",
|
||||
expectedRepo: "alr-default",
|
||||
},
|
||||
{
|
||||
name: "путь с двойными точками",
|
||||
scriptPath: "../alr-default/alr-bin/alr.sh",
|
||||
expectedRepo: "alr-default",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := ExtractRepoNameFromPath(tt.scriptPath)
|
||||
if result != tt.expectedRepo {
|
||||
t.Errorf("ExtractRepoNameFromPath(%q) = %q, ожидается %q", tt.scriptPath, result, tt.expectedRepo)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user