Skip to content

Commit 4915112

Browse files
committed
Add flag for specifying NSIS template
This allows specifying a external file instead of only using the built-in file generic.nsi.j2 Usage: `--template=builtin:generic.nsi.j2` (default) or `--template=any/readable/file.nsi.j2`
1 parent 9da325a commit 4915112

2 files changed

Lines changed: 45 additions & 6 deletions

File tree

syrup/__main__.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import click
55

66
from .functions import (
7+
DEFAULT_NSIS_TEMPLATE,
78
cleanArtifacts,
89
cleanBuild,
910
copySrc,
@@ -37,8 +38,19 @@ def clean(build_dir, artifact_dir, clean_artifacts, **kwargs):
3738
def validate_version(ctx, param, value):
3839
try:
3940
return Version(*[int(x) if x else 0 for x in value.split(".")])
40-
except:
41-
raise click.BadParameter("version must be in major.minor.build format. (1.0.0)")
41+
except Exception as ex:
42+
raise click.BadParameter(
43+
"version must be in major.minor.build format. (1.0.0)"
44+
) from ex
45+
46+
47+
def validate_template(ctx, param, value):
48+
if value.startswith("builtin:"):
49+
return value
50+
51+
return click.Path(
52+
exists=True, resolve_path=True, readable=True, file_okay=True, dir_okay=False
53+
)(value=value, param=param, ctx=ctx)
4254

4355

4456
@cli.command()
@@ -118,6 +130,12 @@ def validate_version(ctx, param, value):
118130
"--website-url",
119131
help="Website(about) URL to display in 'Add/Remove Programs'. mailto: is allowed.",
120132
)
133+
@click.option(
134+
"--template",
135+
help="Jinja2 NSIS Template",
136+
default=DEFAULT_NSIS_TEMPLATE,
137+
callback=validate_template,
138+
)
121139
@click.pass_context
122140
def build(
123141
ctx,
@@ -135,6 +153,7 @@ def build(
135153
help_url,
136154
update_url,
137155
website_url,
156+
template,
138157
executable,
139158
postinstall,
140159
):
@@ -153,6 +172,7 @@ def build(
153172
build_dir=build_dir,
154173
artifact_dir=artifact_dir,
155174
executables=executable,
175+
template_name=template,
156176
version=version,
157177
icon=icon,
158178
license=license,

syrup/functions.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,34 @@ def makeIco(icon, name, build_dir):
271271
return fn
272272

273273

274-
def compileNSISTemplate(build_dir, artifact_dir, executables, **kwargs):
274+
DEFAULT_NSIS_TEMPLATE = "builtin:generic.nsi.j2"
275+
276+
277+
def compileNSISTemplate(
278+
build_dir,
279+
artifact_dir,
280+
executables,
281+
template_name=DEFAULT_NSIS_TEMPLATE,
282+
**kwargs,
283+
):
275284
"Generates NSIS script from jinja2 template"
276285
print("Generating NSIS script...")
277-
loader = jinja2.PackageLoader(__package__)
286+
loader = jinja2.ChoiceLoader(
287+
[
288+
jinja2.PrefixLoader(
289+
{
290+
"builtin": jinja2.PackageLoader(__package__),
291+
},
292+
delimiter=":",
293+
),
294+
jinja2.FileSystemLoader("/", followlinks=True),
295+
]
296+
)
278297
env = jinja2.Environment(
279298
loader=loader, autoescape=False, undefined=jinja2.StrictUndefined
280299
)
281300

282-
template = env.get_template("generic.nsi.j2")
301+
template = env.get_template(template_name)
283302

284303
install_files = []
285304
install_dirs = []
@@ -320,7 +339,7 @@ def compileNSISTemplate(build_dir, artifact_dir, executables, **kwargs):
320339
}
321340
template_variables.update(kwargs)
322341

323-
nsis_script = os.path.join(build_dir, "generic.nsi")
342+
nsis_script = os.path.join(build_dir, "syrup.nsi")
324343
with open(nsis_script, "w", encoding="UTF-8") as fh: # TODO: Temp file name.
325344
template.stream(**template_variables).dump(fh)
326345
return nsis_script

0 commit comments

Comments
 (0)