Skip to content

Commit

Permalink
Allow using create-plugin with more complicated example names
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsie committed May 14, 2024
1 parent be1e917 commit a86eb35
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 153 deletions.
59 changes: 34 additions & 25 deletions contrib/create-plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,9 @@
subst = {}


def _fix_case(value: str) -> str:
return value[0].upper() + value[1:].lower()


def _subst_add_string(key: str, value: str) -> None:
# sanity check
if not value.isascii():
raise NotImplementedError(f"{key} can only be ASCII, got {value}")
if len(value) < 2:
raise NotImplementedError(f"{key} has to be at least length 3, got {value}")

subst[key] = value
subst[key.lower()] = value.lower()
subst[key.upper()] = value.upper()
# convert a snake-case name into CamelCase
def _to_camelcase(value: str) -> str:
return "".join([tmp[0].upper() + tmp[1:].lower() for tmp in value.split("_")])


def _subst_replace(data: str) -> str:
Expand Down Expand Up @@ -61,25 +50,45 @@ def _subst_replace(data: str) -> str:
args = parser.parse_args()

try:
_subst_add_string("Vendor", _fix_case(args.vendor))
_subst_add_string("Example", _fix_case(args.example))
_subst_add_string("Parent", args.parent)
_subst_add_string("Year", str(args.year))
_subst_add_string("Author", args.author)
_subst_add_string("Email", args.email)
vendor: str = args.vendor.replace("-", "_")
example: str = args.example.replace("-", "_")

# first in list
subst["VendorExample"] = _to_camelcase(vendor) + _to_camelcase(example)
subst["vendor_example"] = vendor.lower() + "_" + example.lower()
subst["vendor_dash_example"] = vendor.lower() + "-" + example.lower()
subst["VENDOR_EXAMPLE"] = vendor.upper() + "_" + example.upper()

# second
for key, value in {
"Vendor": vendor,
"Example": example,
"Parent": args.parent,
"Year": str(args.year),
"Author": args.author,
"Email": args.email,
}.items():
subst[key] = _to_camelcase(value)
subst[key.lower()] = value.lower()
subst[key.upper()] = value.upper()

except NotImplementedError as e:
print(e)
sys.exit(1)

template_src = "vendor-example"
template_src: str = "vendor-example"
os.makedirs(os.path.join("plugins", _subst_replace(template_src)), exist_ok=True)

srcdir: str = sys.argv[0].rsplit("/", maxsplit=2)[0]
env = Environment(
loader=FileSystemLoader("."),
loader=FileSystemLoader(srcdir),
autoescape=select_autoescape(),
keep_trailing_newline=True,
)
for fn in glob.iglob(f"./plugins/{template_src}/*"):
template = env.get_template(fn)
with open(_subst_replace(fn.replace(".in", "")), "wb") as f_dst:
for fn in glob.iglob(f"{srcdir}/plugins/{template_src}/*"):
fn_rel: str = os.path.relpath(fn, srcdir)
template = env.get_template(fn_rel)
filename: str = _subst_replace(fn_rel.replace(".in", ""))
with open(filename, "wb") as f_dst:
f_dst.write(template.render(subst).encode())
print(f"wrote {filename}")
2 changes: 1 addition & 1 deletion plugins/vendor-example/README.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ The vendor ID is set from the TODO vendor, in this instance set to `TODO:0x273F`

This plugin uses the following plugin-specific quirks:

### {{Vendor}}{{Example}}StartAddr
### {{VendorExample}}StartAddr

The bla bla bla.

Expand Down
4 changes: 2 additions & 2 deletions plugins/vendor-example/fu-vendor-example-common.c.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

#include "config.h"

#include "fu-{{vendor}}-{{example}}-common.h"
#include "fu-{{vendor_dash_example}}-common.h"

const gchar *
fu_{{vendor}}_{{example}}_strerror(guint8 code)
fu_{{vendor_example}}_strerror(guint8 code)
{
if (code == 0)
return "success";
Expand Down
2 changes: 1 addition & 1 deletion plugins/vendor-example/fu-vendor-example-common.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
#include <glib-object.h>

const gchar *
fu_{{vendor}}_{{example}}_strerror(guint8 code);
fu_{{vendor_example}}_strerror(guint8 code);

0 comments on commit a86eb35

Please sign in to comment.