Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: new requires, build-requires parameters #14 #15

Merged
merged 7 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,7 @@ jobs:
arch: 'x86_64'
desc: 'test package'
post: 'echo post1'
# build_requires: 'perl-Git = 2.43.0'
# requires: 'git-core = 2.43.0'
- run: |
docker run --rm -v $PWD:/work -t centos:7 bash -c 'rpm -Uvh /work/*.rpm && testbin'
10 changes: 9 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,22 @@ inputs:
post:
description: 'Package post.'
default: ''
build_requires:
description: 'Package build requires.'
# NOTE: for nim parseops package bugs
default: '-'
requires:
description: 'Package requires.'
# NOTE: for nim parseops package bugs
default: '-'
outputs:
file_name:
description: 'File name of resulting .rpm file. This does not contain a debuginfo file.'
debuginfo_file_name:
description: 'File name of resulting .rpm debuginfo file.'
runs:
using: 'docker'
image: 'docker://jiro4989/build-rpm-action:2.4.0'
image: 'docker://jiro4989/build-rpm-action:2.5.0'

# Ref: https://haya14busa.github.io/github-action-brandings/
# TODO: update branding if you want.
Expand Down
2 changes: 2 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ fi

/replacetool \
--specfile:/template.spec \
--build-requires:"$INPUT_BUILD_REQUIRES" \
--requires:"$INPUT_REQUIRES" \
--summary:"$INPUT_SUMMARY" \
--package-root:"$INPUT_PACKAGE_ROOT" \
--package:"$INPUT_PACKAGE" \
Expand Down
2 changes: 2 additions & 0 deletions template.spec
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Vendor: {{VENDOR}}
Source: tmp.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-buildroot
BuildArch: {{ARCH}}
{{BUILD_REQUIRES}}
{{REQUIRES}}

%description
{{DESC}}
Expand Down
25 changes: 21 additions & 4 deletions tools/src/replacetool.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import os, strutils, parseopt
type
Options = object
specFile, summary, package, packageRoot, maintainer, version, arch, desc,
license, vendor, post: string
license, vendor, post, buildRequires, requires: string

proc getCmdOpts(params: seq[string]): Options =
var optParser = initOptParser(params)
Expand Down Expand Up @@ -35,6 +35,10 @@ proc getCmdOpts(params: seq[string]): Options =
result.license = val
of "post":
result.post = val
of "build-requires":
result.buildRequires = val
of "requires":
result.requires = val
of cmdEnd:
assert false # cannot happen
else:
Expand All @@ -46,7 +50,7 @@ proc generateInstallScript(path: string): seq[string] =
result.add("cp -p " & path[1..^1] & " %{buildroot}" & head & "/")

proc replaceTemplate(body, summary, package, maintainer, version, arch, desc,
install, files, license, vendor, post: string): string =
install, files, license, vendor, post, buildRequires, requires: string): string =
result =
body
.replace("{{SUMMARY}}", summary)
Expand All @@ -60,19 +64,22 @@ proc replaceTemplate(body, summary, package, maintainer, version, arch, desc,
.replace("{{FILES}}", files)
.replace("{{LICENSE}}", license)
.replace("{{POST}}", post)
.replace("{{BUILD_REQUIRES}}", buildRequires)
.replace("{{REQUIRES}}", requires)

proc formatDescription(desc: string): string =
"Description: " & desc

proc fixFile(file, summary, package, maintainer, version, arch, desc, install,
files, license, vendor, post: string) =
files, license, vendor, post, buildRequires, requires: string) =
let
body = readFile(file)
fixedBody = replaceTemplate(body, summary = summary, package = package, maintainer = maintainer,
version = version, arch = arch, desc = desc,
install = install, files = files,
license = license,
vendor = vendor, post = post)
vendor = vendor, post = post,
buildRequires = buildRequires, requires = requires)
writeFile(file, fixedBody)

proc getInstallFiles(packageRoot: string): (seq[string], seq[string]) =
Expand Down Expand Up @@ -103,6 +110,14 @@ when isMainModule and not defined modeTest:
license = params.license
post = params.post

# NOTE: for bugs std/parseopt
buildRequires =
if params.buildRequires == "-": ""
else: "BuildRequires: " & params.buildRequires
requires =
if params.requires == "-": ""
else: "Requires: " & params.requires

let (installScript, files) = getInstallFiles(packageRoot)
fixFile(params.specfile,
summary = summary,
Expand All @@ -116,4 +131,6 @@ when isMainModule and not defined modeTest:
license = license,
vendor = vendor,
post = post,
buildRequires = buildRequires,
requires = requires,
)
74 changes: 73 additions & 1 deletion tools/tests/test1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Vendor: {{VENDOR}}
Source: tmp.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-buildroot
BuildArch: {{ARCH}}
{{BUILD_REQUIRES}}
{{REQUIRES}}

%description
{{DESC}}
Expand Down Expand Up @@ -73,7 +75,9 @@ suite "replaceTemplate":
files = "/usr/bin/test1\n/usr/bin/test2",
license = "MIT",
vendor = "author.org",
post = "echo 1;\necho 2"
post = "echo 1;\necho 2",
buildRequires = "BuildRequires: perl-Git = 2.43.0",
requires = "Requires: git-core = 2.43.0",
)
const want = """Summary: package summary
Name: test
Expand All @@ -87,6 +91,74 @@ Vendor: author.org
Source: tmp.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-buildroot
BuildArch: noarch
BuildRequires: perl-Git = 2.43.0
Requires: git-core = 2.43.0

%description
description

%prep
rm -rf $RPM_BUILD_ROOT

%setup -n %{name}

%build

%install
mkdir -p /usr/bin
cp -p test1 /usr/bin/
mkdir -p /usr/bin
cp -p test2 /usr/bin/

%clean
rm -rf $RPM_BUILD_ROOT

%post
echo 1;
echo 2

%postun

%files
%defattr(-, root, root)
/usr/bin/test1
/usr/bin/test2

%changelog
"""
check want == got

test "empty requires":
let got = replaceTemplate(
templateSpec,
summary = "package summary",
package = "test",
maintainer = "author",
version = "1.0.0",
arch = "noarch",
desc = "description",
install = "mkdir -p /usr/bin\ncp -p test1 /usr/bin/\nmkdir -p /usr/bin\ncp -p test2 /usr/bin/",
files = "/usr/bin/test1\n/usr/bin/test2",
license = "MIT",
vendor = "author.org",
post = "echo 1;\necho 2",
buildRequires = "",
requires = "",
)
const want = """Summary: package summary
Name: test
Version: 1.0.0
Release: 1%{?dist}
Group: Applications
License: MIT
Packager: author
Vendor: author.org

Source: tmp.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-buildroot
BuildArch: noarch



%description
description
Expand Down