Compare commits

...

4 Commits

3 changed files with 46 additions and 69 deletions

View File

@@ -153,6 +153,16 @@ func pkgVersionFromGit(gitdir string, u *upstream, preferredRev string, forcePre
lastCommitHash = string(submatches[1]) lastCommitHash = string(submatches[1])
u.commitIsh = strings.TrimSpace(string(describeBytes)) u.commitIsh = strings.TrimSpace(string(describeBytes))
} }
// Fetch full commit hash
cmd = exec.Command("git", "rev-parse", "HEAD")
cmd.Dir = gitdir
fullCommitHashBytes, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("git rev-parse HEAD: %w", err)
}
fullCommitHash := strings.TrimSpace(string(fullCommitHashBytes))
u.version = fmt.Sprintf("%sgit%s.%s", u.version = fmt.Sprintf("%sgit%s.%s",
mainVer, mainVer,
time.Unix(lastCommitUnix, 0).UTC().Format("20060102"), time.Unix(lastCommitUnix, 0).UTC().Format("20060102"),
@@ -162,8 +172,8 @@ func pkgVersionFromGit(gitdir string, u *upstream, preferredRev string, forcePre
// have tag: 1.2.3.20250101+git9f107c8 // have tag: 1.2.3.20250101+git9f107c8
u.version = fmt.Sprintf("%s.%s+git%s", u.version, dateStr, lastCommitHash) u.version = fmt.Sprintf("%s.%s+git%s", u.version, dateStr, lastCommitHash)
} else { } else {
// without tag: 20250101+git9f107c8 // without tag: 0.git20250101.96ee002 96ee0021ea0fb9174681b8004d8deba3c499d7f5
u.version = fmt.Sprintf("%s+git%s", dateStr, lastCommitHash) u.version = fmt.Sprintf("0.git%s.%s\n%%define commit_id %s", dateStr, lastCommitHash, fullCommitHash)
} }
return u.version, nil return u.version, nil
} }

22
pack.go
View File

@@ -676,6 +676,24 @@ func mainPack(args []string, usage func()) {
gopkg, strings.ToLower(gopkg)) gopkg, strings.ToLower(gopkg))
} }
info, err := os.Stat(openRuyiSrc)
if err == nil {
if !info.IsDir() {
log.Fatalf("%q exists but is not a directory\n", openRuyiSrc)
}
entries, err := os.ReadDir(openRuyiSrc)
if err != nil {
log.Fatalf("Failed to read directory %q: %v\n", openRuyiSrc, err)
}
if len(entries) != 0 {
log.Fatalf("Output directory %q exists and is non-empty, aborting\n", openRuyiSrc)
}
} else if !os.IsNotExist(err) {
log.Fatalf("Failed to stat %q: %v\n", openRuyiSrc, err)
}
// Create a tarball of the upstream source // Create a tarball of the upstream source
u, err := makeUpstreamSourceTarball(gopkg, gitRevision, forcePrerelease) u, err := makeUpstreamSourceTarball(gopkg, gitRevision, forcePrerelease)
if err != nil { if err != nil {
@@ -692,10 +710,6 @@ func mainPack(args []string, usage func()) {
} }
} }
if _, err := os.Stat(openRuyiSrc); err == nil {
log.Fatalf("Output directory %q already exists, aborting\n", openRuyiSrc)
}
orig := fmt.Sprintf("%s_%s.orig.tar.%s", openRuyiSrc, u.version, u.compression) orig := fmt.Sprintf("%s_%s.orig.tar.%s", openRuyiSrc, u.version, u.compression)
log.Printf("Moving tempfile to %q\n", orig) log.Printf("Moving tempfile to %q\n", orig)
// We need to copy the file, merely renaming is not enough since the file // We need to copy the file, merely renaming is not enough since the file

79
spec.go
View File

@@ -47,6 +47,14 @@ func writeSpec(dir, gopkg, openRuyiSrc, openRuyiLib, openRuyiProgram, version st
// Write the spec file content // Write the spec file content
// SPDX header
fmt.Fprintf(f, "# SPDX-FileCopyrightText: (C) 2026 Institute of Software, Chinese Academy of Sciences (ISCAS)\n")
fmt.Fprintf(f, "# SPDX-FileCopyrightText: (C) 2026 openRuyi Project Contributors\n")
fmt.Fprintf(f, "# SPDX-FileContributor: \n")
fmt.Fprintf(f, "#\n")
fmt.Fprintf(f, "# SPDX-License-Identifier: MulanPSL-2.0\n")
fmt.Fprintf(f, "\n")
// Macros // Macros
fmt.Fprintf(f, "%%define _name %s\n", upstreamName) fmt.Fprintf(f, "%%define _name %s\n", upstreamName)
fmt.Fprintf(f, "%%define go_import_path %s\n", gopkg) fmt.Fprintf(f, "%%define go_import_path %s\n", gopkg)
@@ -59,17 +67,20 @@ func writeSpec(dir, gopkg, openRuyiSrc, openRuyiLib, openRuyiProgram, version st
fmt.Fprintf(f, "Summary: %s\n", description) fmt.Fprintf(f, "Summary: %s\n", description)
fmt.Fprintf(f, "License: %s\n", license) fmt.Fprintf(f, "License: %s\n", license)
fmt.Fprintf(f, "URL: https://github.com/%s/%s\n", owner, repo) fmt.Fprintf(f, "URL: https://github.com/%s/%s\n", owner, repo)
fmt.Fprintf(f, "#!RemoteAsset\n")
fmt.Fprintf(f, "Source0: https://github.com/%s/%s/archive/v%%{version}.tar.gz#/%%{_name}-%%{version}.tar.gz\n", owner, repo) fmt.Fprintf(f, "Source0: https://github.com/%s/%s/archive/v%%{version}.tar.gz#/%%{_name}-%%{version}.tar.gz\n", owner, repo)
switch pkgType { switch pkgType {
case typeLibrary: case typeLibrary:
fmt.Fprintf(f, "BuildArch: noarch\n") fmt.Fprintf(f, "BuildArch: noarch\n")
fmt.Fprintf(f, "BuildSystem: golangmodules\n")
fmt.Fprintf(f, "\n") fmt.Fprintf(f, "\n")
case typeProgram, typeLibraryProgram, typeProgramLibrary: case typeProgram, typeLibraryProgram, typeProgramLibrary:
fmt.Fprintf(f, "\n") fmt.Fprintf(f, "\n")
} }
fmt.Fprintf(f, "BuildRequires: go\n") fmt.Fprintf(f, "BuildRequires: go\n")
fmt.Fprintf(f, "BuildRequires: go-rpm-macros\n")
// And other BuildRequires from dependencies // And other BuildRequires from dependencies
rpmDeps := convertDependenciesToRPM(dependencies) rpmDeps := convertDependenciesToRPM(dependencies)
sort.Strings(rpmDeps) sort.Strings(rpmDeps)
@@ -100,64 +111,6 @@ func writeSpec(dir, gopkg, openRuyiSrc, openRuyiLib, openRuyiProgram, version st
fmt.Fprintf(f, "%s\n", longdescription) fmt.Fprintf(f, "%s\n", longdescription)
fmt.Fprintf(f, "\n") fmt.Fprintf(f, "\n")
// %prep
fmt.Fprintf(f, "%%prep\n")
fmt.Fprintf(f, "%%autosetup -n %%{_name}-%%{version}\n")
switch pkgType {
case typeProgram, typeLibraryProgram, typeProgramLibrary:
fmt.Fprintf(f, "mkdir -p %%{_builddir}/go/src/%%{go_import_path}\n")
fmt.Fprintf(f, "cp -a . %%{_builddir}/go/src/%%{go_import_path}\n")
}
if len(u.vendorDirs) > 0 {
fmt.Fprintf(f, "# Remove bundled dependencies\n")
for _, vdir := range u.vendorDirs {
fmt.Fprintf(f, "rm -rf %s\n", vdir)
}
}
if u.hasGodeps {
fmt.Fprintf(f, "rm -rf Godeps/_workspace\n")
}
fmt.Fprintf(f, "\n")
// %build
fmt.Fprintf(f, "%%build\n")
switch pkgType {
case typeLibrary:
fmt.Fprintf(f, "# Library package - no build needed\n")
case typeProgram, typeLibraryProgram, typeProgramLibrary:
fmt.Fprintf(f, "export GO111MODULE=off\n")
fmt.Fprintf(f, "export GOPATH=%%{_builddir}/go:%%{_datadir}/gocode\n")
fmt.Fprintf(f, "export GOFLAGS=\"-buildmode=pie -trimpath -mod=readonly -modcacherw\"\n")
fmt.Fprintf(f, "go build -v -o %%{_name} .\n")
}
fmt.Fprintf(f, "\n")
// %install
fmt.Fprintf(f, "%%install\n")
switch pkgType {
case typeLibrary:
fmt.Fprintf(f, "# Install source code for library package\n")
fmt.Fprintf(f, "install -d %%{buildroot}%%{_datadir}/gocode/src/%%{go_import_path}\n")
fmt.Fprintf(f, "cp -a . %%{buildroot}%%{_datadir}/gocode/src/%%{go_import_path}\n")
case typeProgram:
fmt.Fprintf(f, "install -D -m 0755 %%{_name} %%{buildroot}%%{_bindir}/%%{_name}\n")
case typeLibraryProgram, typeProgramLibrary:
fmt.Fprintf(f, "# Install binary\n")
fmt.Fprintf(f, "install -D -m 0755 %%{_name} %%{buildroot}%%{_bindir}/%%{_name}\n")
fmt.Fprintf(f, "\n")
fmt.Fprintf(f, "# Install source code for library package\n")
fmt.Fprintf(f, "install -d %%{buildroot}%%{_datadir}/gocode/src/%%{go_import_path}\n")
fmt.Fprintf(f, "cp -a . %%{buildroot}%%{_datadir}/gocode/src/%%{go_import_path}\n")
}
fmt.Fprintf(f, "\n")
// %check
fmt.Fprintf(f, "%%check\n")
fmt.Fprintf(f, "export GO111MODULE=off\n")
fmt.Fprintf(f, "export GOPATH=%%{_builddir}/go:%%{_datadir}/gocode\n")
fmt.Fprintf(f, "go test -v ./...\n")
fmt.Fprintf(f, "\n")
// %files // %files
writeRPMFilesSection(f, openRuyiSrc, openRuyiLib, openRuyiProgram, pkgType) writeRPMFilesSection(f, openRuyiSrc, openRuyiLib, openRuyiProgram, pkgType)
@@ -171,7 +124,7 @@ func writeSpec(dir, gopkg, openRuyiSrc, openRuyiLib, openRuyiProgram, version st
// For library package // For library package
func writeRPMLibraryPackage(f *os.File, gopkg, openRuyiLib, longdesc string, deps []string) { func writeRPMLibraryPackage(f *os.File, gopkg, openRuyiLib, longdesc string, deps []string) {
fmt.Fprintf(f, "\n") fmt.Fprintf(f, "\n")
fmt.Fprintf(f, "Provides: go(%s)\n", gopkg) fmt.Fprintf(f, "Provides: go(%s) = %%{version}\n", gopkg)
// 库包的运行时依赖 // 库包的运行时依赖
if len(deps) > 0 { if len(deps) > 0 {
for _, dep := range deps { for _, dep := range deps {
@@ -186,7 +139,7 @@ func writeRPMLibrarySubpackage(f *os.File, gopkg, openRuyiLib, openRuyiSrc, long
fmt.Fprintf(f, "\n") fmt.Fprintf(f, "\n")
fmt.Fprintf(f, "%%package -n %s\n", openRuyiLib) fmt.Fprintf(f, "%%package -n %s\n", openRuyiLib)
fmt.Fprintf(f, "Summary: Development files of %s\n", filepath.Base(gopkg)) fmt.Fprintf(f, "Summary: Development files of %s\n", filepath.Base(gopkg))
fmt.Fprintf(f, "Provides: go(%s)\n", gopkg) fmt.Fprintf(f, "Provides: go(%s) = %%{version}\n", gopkg)
fmt.Fprintf(f, "BuildArch: noarch\n") fmt.Fprintf(f, "BuildArch: noarch\n")
for _, dep := range deps { for _, dep := range deps {
fmt.Fprintf(f, "Requires: %s\n", dep) fmt.Fprintf(f, "Requires: %s\n", dep)
@@ -216,7 +169,7 @@ func writeRPMFilesSection(f *os.File, openRuyiSrc, openRuyiLib, openRuyiProgram
fmt.Fprintf(f, "%%files\n") fmt.Fprintf(f, "%%files\n")
fmt.Fprintf(f, "%%license LICENSE*\n") fmt.Fprintf(f, "%%license LICENSE*\n")
fmt.Fprintf(f, "%%doc README*\n") fmt.Fprintf(f, "%%doc README*\n")
fmt.Fprintf(f, "%%{_datadir}/gocode/src/%%{go_import_path}\n") fmt.Fprintf(f, "%%{go_sys_gopath}/%%{go_import_path}\n")
fmt.Fprintf(f, "\n") fmt.Fprintf(f, "\n")
case typeProgram: case typeProgram:
@@ -231,7 +184,7 @@ func writeRPMFilesSection(f *os.File, openRuyiSrc, openRuyiLib, openRuyiProgram
fmt.Fprintf(f, "%%files\n") fmt.Fprintf(f, "%%files\n")
fmt.Fprintf(f, "%%license LICENSE*\n") fmt.Fprintf(f, "%%license LICENSE*\n")
fmt.Fprintf(f, "%%doc README*\n") fmt.Fprintf(f, "%%doc README*\n")
fmt.Fprintf(f, "%%{_datadir}/gocode/src/%%{go_import_path}\n") fmt.Fprintf(f, "%%{go_sys_gopath}/%%{go_import_path}\n")
fmt.Fprintf(f, "\n") fmt.Fprintf(f, "\n")
// 程序子包文件 // 程序子包文件
fmt.Fprintf(f, "%%files -n %s\n", openRuyiProgram) fmt.Fprintf(f, "%%files -n %s\n", openRuyiProgram)
@@ -249,7 +202,7 @@ func writeRPMFilesSection(f *os.File, openRuyiSrc, openRuyiLib, openRuyiProgram
// 库子包文件 // 库子包文件
fmt.Fprintf(f, "%%files -n %s\n", openRuyiLib) fmt.Fprintf(f, "%%files -n %s\n", openRuyiLib)
fmt.Fprintf(f, "%%license LICENSE*\n") fmt.Fprintf(f, "%%license LICENSE*\n")
fmt.Fprintf(f, "%%{_datadir}/gocode/src/%%{go_import_path}\n") fmt.Fprintf(f, "%%{go_sys_gopath}/%%{go_import_path}\n")
fmt.Fprintf(f, "\n") fmt.Fprintf(f, "\n")
} }
} }