Skip to content

Commit

Permalink
add: new requires, build-requires parameters #14
Browse files Browse the repository at this point in the history
  • Loading branch information
jiro4989 committed Dec 31, 2023
1 parent 6a966c1 commit af91dde
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 5 deletions.
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ 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.'
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

0 comments on commit af91dde

Please sign in to comment.