From 7448d91817c4eca88b2d5ab82eae519407ee6613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B2=D0=B3=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=A5=D1=80?= =?UTF-8?q?=D0=B0=D0=BC=D0=BE=D0=B2?= Date: Tue, 9 Dec 2025 11:33:14 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=B2=D0=BB=D0=B5=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B8=D0=BC=D0=B5=D0=BD=D0=B8=20=D1=80?= =?UTF-8?q?=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8=D1=82=D0=BE=D1=80=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=B8=D0=B7=20=D0=BF=D1=83=D1=82=D0=B8=20=D0=BA=20=D1=81=D0=BA?= =?UTF-8?q?=D1=80=D0=B8=D0=BF=D1=82=D1=83=20=D0=BF=D1=80=D0=B8=20=D1=81?= =?UTF-8?q?=D0=B1=D0=BE=D1=80=D0=BA=D0=B5=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7?= =?UTF-8?q?=20-s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/build/build.go | 2 +- internal/build/utils.go | 26 ++++++++++++++++++ internal/build/utils_test.go | 53 ++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/internal/build/build.go b/internal/build/build.go index f9ee734..a7f220d 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -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, diff --git a/internal/build/utils.go b/internal/build/utils.go index 914a80f..b30b1a9 100644 --- a/internal/build/utils.go +++ b/internal/build/utils.go @@ -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 +} diff --git a/internal/build/utils_test.go b/internal/build/utils_test.go index 62da90c..cb0900e 100644 --- a/internal/build/utils_test.go +++ b/internal/build/utils_test.go @@ -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) + } + }) + } } \ No newline at end of file