From a1bb9f784ed28ea2747c637f144ebbd7eb810598 Mon Sep 17 00:00:00 2001 From: Nathan Gill Date: Mon, 9 Feb 2026 10:40:23 +0000 Subject: [PATCH] just write this thing in Perl instead --- .gitignore | 37 ------------ .ocamlformat | 2 - templates/bsd2.txt => LICENSE | 2 +- README.md | 4 ++ bin/dune | 4 -- bin/main.ml | 9 --- dune-project | 17 ------ flake.nix | 14 +---- lib/dune | 7 --- lib/generate.ml | 6 -- lib/generate.mli | 2 - lib/template.ml | 1 - lib/template.mli | 2 - lic.pl | 104 ++++++++++++++++++++++++++++++++++ opam/lic.opam | 24 -------- templates/bsd2 | 22 +++++++ test/dune | 2 - test/test_lic.ml | 1 - 18 files changed, 134 insertions(+), 126 deletions(-) delete mode 100644 .ocamlformat rename templates/bsd2.txt => LICENSE (97%) create mode 100644 README.md delete mode 100644 bin/dune delete mode 100644 bin/main.ml delete mode 100644 dune-project delete mode 100644 lib/dune delete mode 100644 lib/generate.ml delete mode 100644 lib/generate.mli delete mode 100644 lib/template.ml delete mode 100644 lib/template.mli create mode 100755 lic.pl delete mode 100644 opam/lic.opam create mode 100644 templates/bsd2 delete mode 100644 test/dune delete mode 100644 test/test_lic.ml diff --git a/.gitignore b/.gitignore index c14c5ad..fa5ba6b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,39 +1,2 @@ -*.annot -*.cmo -*.cma -*.cmi -*.a -*.o -*.cmx -*.cmxs -*.cmxa - -# Files containing detailed information about the compilation (generated -# by `ocamlc`/`ocamlopt` when invoked using the option `-bin-annot`). -# These files are typically useful for code inspection tools -# (e.g. Merlin). -*.cmt -*.cmti - -# ocamlbuild and Dune default working directory -_build/ - -# ocamlbuild targets -*.byte -*.native - -# oasis generated files -setup.data -setup.log - -# Merlin configuring file for Vim and Emacs -.merlin - -# Dune generated files -*.install - -# Local OPAM switch -_opam/ - # direnv .direnv/ diff --git a/.ocamlformat b/.ocamlformat deleted file mode 100644 index 81bbcc8..0000000 --- a/.ocamlformat +++ /dev/null @@ -1,2 +0,0 @@ -profile = default -version = 0.28.1 diff --git a/templates/bsd2.txt b/LICENSE similarity index 97% rename from templates/bsd2.txt rename to LICENSE index bdcd1fd..afd53b6 100644 --- a/templates/bsd2.txt +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) {{ year }}, {{ owner }} +Copyright (c) 2026, Nathan Gill Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/README.md b/README.md new file mode 100644 index 0000000..d9a8319 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# lic + +A template-based license generator script in Perl + diff --git a/bin/dune b/bin/dune deleted file mode 100644 index dcf11ce..0000000 --- a/bin/dune +++ /dev/null @@ -1,4 +0,0 @@ -(executable - (public_name lic) - (name main) - (libraries lic str)) diff --git a/bin/main.ml b/bin/main.ml deleted file mode 100644 index fa3a1b8..0000000 --- a/bin/main.ml +++ /dev/null @@ -1,9 +0,0 @@ -open Lic - -let () = - let output = - Generate.render_template - ~vars:[ ("year", "2026"); ("owner", "Test") ] - Template.bsd2 - in - print_string output diff --git a/dune-project b/dune-project deleted file mode 100644 index 5f14063..0000000 --- a/dune-project +++ /dev/null @@ -1,17 +0,0 @@ -(lang dune 3.20) -(name lic) - -(license LICENSE) - -(generate_opam_files true) -(opam_file_location inside_opam_directory) - -(formatting - (enabled_for dune)) - -(package - (name lic) - (depends - (ocaml (>= 4.08)) - (ppx_blob (>= 0.9.0)) - )) diff --git a/flake.nix b/flake.nix index 09f9c5b..fca50a2 100644 --- a/flake.nix +++ b/flake.nix @@ -19,17 +19,9 @@ in { devShells.default = pkgs.mkShell { - buildInputs = - (with pkgs; [ - ocaml - dune - ]) - ++ (with pkgs.ocamlPackages; [ - ocaml-lsp - utop - ocamlformat - ppx_blob - ]); + buildInputs = with pkgs; [ + perl + ]; }; } ); diff --git a/lib/dune b/lib/dune deleted file mode 100644 index daf9626..0000000 --- a/lib/dune +++ /dev/null @@ -1,7 +0,0 @@ -(library - (name lic) - (libraries str) - (preprocess - (pps ppx_blob)) - (preprocessor_deps - (file ../templates/bsd2.txt))) diff --git a/lib/generate.ml b/lib/generate.ml deleted file mode 100644 index 5602d02..0000000 --- a/lib/generate.ml +++ /dev/null @@ -1,6 +0,0 @@ -let render_template ~vars tmpl = - let re key = Str.regexp ("{{ *" ^ key ^ " *}}") in - List.fold_left - (fun acc (key, value) -> Str.global_replace (re key) value acc) - tmpl vars - diff --git a/lib/generate.mli b/lib/generate.mli deleted file mode 100644 index de82a30..0000000 --- a/lib/generate.mli +++ /dev/null @@ -1,2 +0,0 @@ -val render_template: vars:(string * string) list -> string -> string -(** Render a license template with given values *) diff --git a/lib/template.ml b/lib/template.ml deleted file mode 100644 index 45debe1..0000000 --- a/lib/template.ml +++ /dev/null @@ -1 +0,0 @@ -let bsd2 : string = [%blob "../templates/bsd2.txt"] diff --git a/lib/template.mli b/lib/template.mli deleted file mode 100644 index b44eff8..0000000 --- a/lib/template.mli +++ /dev/null @@ -1,2 +0,0 @@ -val bsd2 : string -(** BSD 2-Clause license text *) diff --git a/lic.pl b/lic.pl new file mode 100755 index 0000000..b9adb92 --- /dev/null +++ b/lic.pl @@ -0,0 +1,104 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use File::Spec; +use File::Basename; + +my $VERSION = "0.1"; + +sub usage { + print "Usage: lic [KEY=VALUE] ...\n"; + print "See 'lic --help' for more information.\n"; +} + +sub help { + print "Usage: lic [KEY=VALUE] ...\n"; + print "\nGenerate license text from a template, optionally substituting variables.\n"; + print "\n may be either:\n"; + print " a path to a license template file, or\n"; + print " the name of a template in a standard location\n"; + print "\nStandard locations include:\n"; + print " directories listed in \$LICPATH (colon-separated)\n"; + print " ~/.local/share/lic\n"; + print " /usr/share/lic\n"; + print "\nEach KEY=VALUE replaces all occurrences of {{KEY}} with VALUE in the template.\n"; + print "Whitespace inside {{ }} is ignored.\n"; + print "\nOuput is written to standard output.\n"; +} + +sub version { + print "lic version $VERSION\n"; + print "License: BSD 2-Clause\n"; +} + +my $license; +my @substs = (); +my $file; + +for my $arg (@ARGV) { + if ($arg eq "-h" || $arg eq "--help") { + help; + exit; + } + elsif ($arg eq "-V" || $arg eq "--version") { + version; + exit; + } + elsif (!defined $license) { + $license = $arg; + } + elsif ($arg =~ /([^=]+)=([^=]+)/) { + push @substs, [$1, $2]; + } +} + +unless (defined $license) { + usage; + exit; +} + +if (-f $license) { + $file = $license; +} +else { + my @search_dirs; + + if (defined $ENV{LICPATH}) { + push @search_dirs, split /:/, $ENV{LICPATH}; + } + + push @search_dirs, + File::Spec->catdir($ENV{HOME}, ".local", "share", "lic") + if defined $ENV{HOME}; + + push @search_dirs, "/usr/share/lic"; + + for my $dir (@search_dirs) { + my $candidate = File::Spec->catfile($dir, $license); + if (-f $candidate) { + $file = $candidate; + last; + } + } + + die "license template $license not found in:\n " + . join("\n ", @search_dirs) . "\n" + unless defined $file; +} + +open my $input, '<', $file or die "can't open file $file: $!\n"; + +while (<$input>) { + chomp; + my $line = $_; + + foreach my $subst (@substs) { + my ($key, $val) = @$subst; + $line =~ s/\{\{\s*\Q$key\E\s*\}\}/$val/g; + } + + print $line, "\n"; +} + +close $input or die "can't close file $file: $!\n"; + diff --git a/opam/lic.opam b/opam/lic.opam deleted file mode 100644 index bd12975..0000000 --- a/opam/lic.opam +++ /dev/null @@ -1,24 +0,0 @@ -# This file is generated by dune, edit dune-project instead -opam-version: "2.0" -license: "LICENSE" -depends: [ - "dune" {>= "3.20"} - "ocaml" {>= "4.08"} - "ppx_blob" {>= "0.9.0"} - "odoc" {with-doc} -] -build: [ - ["dune" "subst"] {dev} - [ - "dune" - "build" - "-p" - name - "-j" - jobs - "@install" - "@runtest" {with-test} - "@doc" {with-doc} - ] -] -x-maintenance-intent: ["(latest)"] diff --git a/templates/bsd2 b/templates/bsd2 new file mode 100644 index 0000000..3c172cf --- /dev/null +++ b/templates/bsd2 @@ -0,0 +1,22 @@ +Copyright (c) {{year}}, {{owner}} + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER +OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/test/dune b/test/dune deleted file mode 100644 index 69757cf..0000000 --- a/test/dune +++ /dev/null @@ -1,2 +0,0 @@ -(test - (name test_lic)) diff --git a/test/test_lic.ml b/test/test_lic.ml deleted file mode 100644 index 8b13789..0000000 --- a/test/test_lic.ml +++ /dev/null @@ -1 +0,0 @@ - -- 2.51.2