forked from misaka00251/go2spec
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9585a324b0 |
@@ -173,7 +173,7 @@ func pkgVersionFromGit(gitdir string, u *upstream, preferredRev string, forcePre
|
||||
u.version = fmt.Sprintf("%s.%s+git%s", u.version, dateStr, lastCommitHash)
|
||||
} else {
|
||||
// without tag: 0.git20250101.96ee002 96ee0021ea0fb9174681b8004d8deba3c499d7f5
|
||||
u.version = fmt.Sprintf("0+git%s.%s\n%%define commit_id %s", dateStr, lastCommitHash, fullCommitHash)
|
||||
u.version = fmt.Sprintf("0.git%s.%s\n%%define commit_id %s", dateStr, lastCommitHash, fullCommitHash)
|
||||
}
|
||||
return u.version, nil
|
||||
}
|
||||
|
||||
@@ -63,6 +63,14 @@ func markdownToLongDescription(markdown string) (string, error) {
|
||||
return reformatForControl(out), nil
|
||||
}
|
||||
|
||||
func limitLines(s string, maxLines int) string {
|
||||
lines := strings.Split(s, "\n")
|
||||
if len(lines) <= maxLines {
|
||||
return s
|
||||
}
|
||||
return strings.Join(lines[:maxLines], "\n")
|
||||
}
|
||||
|
||||
// getDescriptionForGopkg reads from README.md (or equivalent) from GitHub,
|
||||
// intended for extended description in debian/control.
|
||||
func getLongDescriptionForGopkg(gopkg string) (string, error) {
|
||||
@@ -95,5 +103,9 @@ func getLongDescriptionForGopkg(gopkg string) (string, error) {
|
||||
return reformatForControl(content), nil
|
||||
}
|
||||
|
||||
return markdownToLongDescription(content)
|
||||
out, err := markdownToLongDescription(content)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return limitLines(out, 100), nil
|
||||
}
|
||||
|
||||
39
main.go
39
main.go
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/google/go-github/v60/github"
|
||||
@@ -12,17 +11,6 @@ var (
|
||||
gitHub *github.Client
|
||||
)
|
||||
|
||||
// TokenTransport implements http.RoundTripper for Bearer token authentication
|
||||
type TokenTransport struct {
|
||||
Token string
|
||||
Transport http.RoundTripper
|
||||
}
|
||||
|
||||
func (t *TokenTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
req.Header.Add("Authorization", "Bearer "+t.Token)
|
||||
return t.Transport.RoundTrip(req)
|
||||
}
|
||||
|
||||
func printHelp() {
|
||||
helpText := `go2spec - A tool to package Go modules into RPM spec files.
|
||||
|
||||
@@ -41,28 +29,13 @@ If there are no commands provided, the tool will default to executing the 'pack'
|
||||
}
|
||||
|
||||
func main() {
|
||||
token := os.Getenv("GITHUB_TOKEN")
|
||||
|
||||
var client *http.Client
|
||||
if token != "" {
|
||||
// Use token authentication for better rate limits
|
||||
client = &http.Client{
|
||||
Transport: &TokenTransport{
|
||||
Token: token,
|
||||
Transport: httpcache.NewMemoryCacheTransport(),
|
||||
},
|
||||
}
|
||||
} else {
|
||||
// Fallback to basic auth if token is not provided
|
||||
transport := github.BasicAuthTransport{
|
||||
Username: os.Getenv("GITHUB_USERNAME"),
|
||||
Password: os.Getenv("GITHUB_PASSWORD"),
|
||||
OTP: os.Getenv("GITHUB_OTP"),
|
||||
Transport: httpcache.NewMemoryCacheTransport(),
|
||||
}
|
||||
client = transport.Client()
|
||||
transport := github.BasicAuthTransport{
|
||||
Username: os.Getenv("GITHUB_USERNAME"),
|
||||
Password: os.Getenv("GITHUB_PASSWORD"),
|
||||
OTP: os.Getenv("GITHUB_OTP"),
|
||||
Transport: httpcache.NewMemoryCacheTransport(),
|
||||
}
|
||||
gitHub = github.NewClient(client)
|
||||
gitHub = github.NewClient(transport.Client())
|
||||
|
||||
args := os.Args[1:]
|
||||
|
||||
|
||||
48
pack.go
48
pack.go
@@ -417,14 +417,8 @@ func createDirectory(openRuyiSrc string) (string, error) {
|
||||
return "", fmt.Errorf("get cwd: %w", err)
|
||||
}
|
||||
dir := filepath.Join(wd, openRuyiSrc)
|
||||
|
||||
// Try to create the directory
|
||||
err = os.Mkdir(dir, 0755)
|
||||
if err != nil {
|
||||
// If directory already exists, that's ok (it was verified to be empty)
|
||||
if !os.IsExist(err) {
|
||||
return "", fmt.Errorf("mkdir: %w", err)
|
||||
}
|
||||
if err := os.Mkdir(dir, 0755); err != nil {
|
||||
return "", fmt.Errorf("mkdir: %w", err)
|
||||
}
|
||||
return dir, nil
|
||||
}
|
||||
@@ -669,6 +663,9 @@ func mainPack(args []string, usage func()) {
|
||||
|
||||
if pkgType != typeGuess {
|
||||
openRuyiSrc = nameFromGopkg(gopkg, pkgType, customProgPkgName, allowUnknownHoster)
|
||||
if _, err := os.Stat(openRuyiSrc); err == nil {
|
||||
log.Fatalf("Output directory %q already exists, aborting\n", openRuyiSrc)
|
||||
}
|
||||
}
|
||||
|
||||
if strings.ToLower(gopkg) != gopkg {
|
||||
@@ -682,7 +679,23 @@ func mainPack(args []string, usage func()) {
|
||||
gopkg, strings.ToLower(gopkg))
|
||||
}
|
||||
|
||||
// NOTE: directory existence is checked after determining final openRuyiSrc
|
||||
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
|
||||
u, err := makeUpstreamSourceTarball(gopkg, gitRevision, forcePrerelease)
|
||||
@@ -700,23 +713,6 @@ func mainPack(args []string, usage func()) {
|
||||
}
|
||||
}
|
||||
|
||||
// Now that we know the final package name, check output directory
|
||||
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)
|
||||
}
|
||||
|
||||
orig := fmt.Sprintf("%s_%s.orig.tar.%s", openRuyiSrc, u.version, u.compression)
|
||||
log.Printf("Moving tempfile to %q\n", orig)
|
||||
// We need to copy the file, merely renaming is not enough since the file
|
||||
|
||||
37
spec.go
37
spec.go
@@ -50,7 +50,7 @@ func writeSpec(dir, gopkg, openRuyiSrc, openRuyiLib, openRuyiProgram, version st
|
||||
// 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, "# SPDX-FileContributor: Julian Zhu <julian.oerv@isrc.iscas.ac.cn>\n")
|
||||
fmt.Fprintf(f, "#\n")
|
||||
fmt.Fprintf(f, "# SPDX-License-Identifier: MulanPSL-2.0\n")
|
||||
fmt.Fprintf(f, "\n")
|
||||
@@ -58,40 +58,17 @@ func writeSpec(dir, gopkg, openRuyiSrc, openRuyiLib, openRuyiProgram, version st
|
||||
// Macros
|
||||
fmt.Fprintf(f, "%%define _name %s\n", upstreamName)
|
||||
fmt.Fprintf(f, "%%define go_import_path %s\n", gopkg)
|
||||
|
||||
// If pkgVersionFromGit embedded a commit_id define in u.version, extract and write it here
|
||||
commitRe := regexp.MustCompile(`%define\s+commit_id\s+([0-9a-fA-F]+)`)
|
||||
if m := commitRe.FindStringSubmatch(u.version); m != nil {
|
||||
fmt.Fprintf(f, "%%define commit_id %s\n", m[1])
|
||||
// Remove the embedded %define line from version so it doesn't get written to Version: field
|
||||
version = commitRe.ReplaceAllString(version, "")
|
||||
version = strings.TrimSpace(version)
|
||||
}
|
||||
fmt.Fprintf(f, "\n")
|
||||
|
||||
// Header
|
||||
fmt.Fprintf(f, "Name: %s\n", openRuyiSrc)
|
||||
|
||||
// Some times typeLibrary is treat as typeProgram,
|
||||
// So we add an additional Name line, and keep one of those mannually
|
||||
switch pkgType {
|
||||
case typeProgram:
|
||||
fmt.Fprintf(f, "Name: %s\n", openRuyiLib)
|
||||
}
|
||||
|
||||
fmt.Fprintf(f, "Version: %s\n", version)
|
||||
fmt.Fprintf(f, "Release: %%autorelease\n")
|
||||
fmt.Fprintf(f, "Summary: %s\n", description)
|
||||
fmt.Fprintf(f, "License: %s\n", license)
|
||||
fmt.Fprintf(f, "URL: https://github.com/%s/%s\n", owner, repo)
|
||||
fmt.Fprintf(f, "#!RemoteAsset\n")
|
||||
// If the computed version text contains a commit_id definition (see pkgVersionFromGit),
|
||||
// use the commit_id tarball instead of v%{version}.tar.gz
|
||||
if strings.Contains(u.version, "commit_id") {
|
||||
fmt.Fprintf(f, "Source0: https://github.com/%s/%s/archive/%%{commit_id}.tar.gz#/%%{_name}-%%{version}.tar.gz\n", owner, repo)
|
||||
} else {
|
||||
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 {
|
||||
case typeLibrary:
|
||||
@@ -148,7 +125,6 @@ func writeSpec(dir, gopkg, openRuyiSrc, openRuyiLib, openRuyiProgram, version st
|
||||
func writeRPMLibraryPackage(f *os.File, gopkg, openRuyiLib, longdesc string, deps []string) {
|
||||
fmt.Fprintf(f, "\n")
|
||||
fmt.Fprintf(f, "Provides: go(%s) = %%{version}\n", gopkg)
|
||||
fmt.Fprintf(f, "\n")
|
||||
// 库包的运行时依赖
|
||||
if len(deps) > 0 {
|
||||
for _, dep := range deps {
|
||||
@@ -165,13 +141,10 @@ func writeRPMLibrarySubpackage(f *os.File, gopkg, openRuyiLib, openRuyiSrc, long
|
||||
fmt.Fprintf(f, "Summary: Development files of %s\n", filepath.Base(gopkg))
|
||||
fmt.Fprintf(f, "Provides: go(%s) = %%{version}\n", gopkg)
|
||||
fmt.Fprintf(f, "BuildArch: noarch\n")
|
||||
if len(deps) > 0 {
|
||||
for _, dep := range deps {
|
||||
fmt.Fprintf(f, "Requires: %s\n", dep)
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintf(f, "\n")
|
||||
for _, dep := range deps {
|
||||
fmt.Fprintf(f, "Requires: %s\n", dep)
|
||||
}
|
||||
fmt.Fprintf(f, "\n")
|
||||
fmt.Fprintf(f, "%%description -n %s\n", openRuyiLib)
|
||||
fmt.Fprintf(f, "%s\n", longdesc)
|
||||
fmt.Fprintf(f, "\n")
|
||||
|
||||
Reference in New Issue
Block a user