diff --git a/gen.go b/gen.go new file mode 100644 index 0000000..5941ae0 --- /dev/null +++ b/gen.go @@ -0,0 +1,46 @@ +package main + +import ( + "os" + + "github.com/urfave/cli/v2" + "lure.sh/lure/pkg/gen" +) + +var genCmd = &cli.Command{ + Name: "generate", + Usage: "Generate a LURE script from a template", + Aliases: []string{"gen"}, + Subcommands: []*cli.Command{ + genPipCmd, + }, +} + +var genPipCmd = &cli.Command{ + Name: "pip", + Usage: "Generate a LURE script for a pip module", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "name", + Aliases: []string{"n"}, + Required: true, + }, + &cli.StringFlag{ + Name: "version", + Aliases: []string{"v"}, + Required: true, + }, + &cli.StringFlag{ + Name: "description", + Aliases: []string{"d"}, + Value: "A Python Pip module", + }, + }, + Action: func(c *cli.Context) error { + return gen.Pip(os.Stdout, gen.PipOptions{ + Name: c.String("name"), + Version: c.String("version"), + Description: c.String("description"), + }) + }, +} diff --git a/main.go b/main.go index e63c61f..41811ff 100644 --- a/main.go +++ b/main.go @@ -62,8 +62,9 @@ var app = &cli.App{ removerepoCmd, refreshCmd, fixCmd, - versionCmd, + genCmd, helperCmd, + versionCmd, }, Before: func(c *cli.Context) error { ctx := c.Context diff --git a/pkg/gen/funcs.go b/pkg/gen/funcs.go new file mode 100644 index 0000000..6906a94 --- /dev/null +++ b/pkg/gen/funcs.go @@ -0,0 +1,9 @@ +package gen + +import "text/template" + +var funcs = template.FuncMap{ + "firstchar": func(s string) string { + return s[:1] + }, +} diff --git a/pkg/gen/pip.go b/pkg/gen/pip.go new file mode 100644 index 0000000..a76b123 --- /dev/null +++ b/pkg/gen/pip.go @@ -0,0 +1,57 @@ +package gen + +import ( + _ "embed" + "fmt" + "io" + "net/http" + "path" + "text/template" +) + +//go:embed tmpls/pip.tmpl.sh +var pipTmpl string + +type PipOptions struct { + Name string + Version string + Description string +} + +func Pip(w io.Writer, opts PipOptions) error { + tmpl, err := template.New("pip"). + Funcs(funcs). + Parse(pipTmpl) + if err != nil { + return err + } + + params := map[string]any{ + "name": opts.Name, + "version": opts.Version, + "description": opts.Description, + } + + url := fmt.Sprintf( + "https://files.pythonhosted.org/packages/source/%s/%s/%s-%s.tar.gz", + opts.Name[:1], + opts.Name, + opts.Name, + opts.Version, + ) + + res, err := http.Head(url) + if err != nil { + return err + } + + dir := path.Dir(res.Request.URL.Path) + checksum := path.Base(dir) + dir = path.Dir(dir) + checksum = path.Base(dir) + checksum + dir = path.Dir(dir) + checksum = path.Base(dir) + checksum + params["checksum"] = "blake2b-256:" + checksum + + return tmpl.Execute(w, params) +} diff --git a/pkg/gen/tmpls/pip.tmpl.sh b/pkg/gen/tmpls/pip.tmpl.sh new file mode 100644 index 0000000..f5167b9 --- /dev/null +++ b/pkg/gen/tmpls/pip.tmpl.sh @@ -0,0 +1,33 @@ +name='{{.name}}' +version='{{.version}}' +release='1' +desc='{{.description}}' +homepage='https://example.com' +maintainer='Example ' +architectures=('all') +license=('custom:Unknown') +provides=('{{.name}}') +conflicts=('{{.name}}') + +deps=("python3") +deps_arch=("python") +deps_alpine=("python3") + +build_deps=("python3" "python3-setuptools") +build_deps_arch=("python" "python-setuptools") +build_deps_alpine=("python3" "py3-setuptools") + +sources=("https://files.pythonhosted.org/packages/source/{{.name | firstchar}}/{{.name}}/{{.name}}-${version}.tar.gz") +checksums=('{{.checksum}}') + +build() { + cd "$srcdir/{{.name}}-${version}" + python3 setup.py build +} + +package() { + cd "$srcdir/{{.name}}-${version}" + python3 setup.py install --root="${pkgdir}/" --optimize=1 || return 1 +} + +