Commit 31476f0f8a1f46dc2d51ee8c7f89dd9a6ad1ec08

Thomas de Grivel 2022-11-02T17:39:10

licence

diff --git a/lib/header/c.ex b/lib/header/c.ex
new file mode 100644
index 0000000..13a9909
--- /dev/null
+++ b/lib/header/c.ex
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2022 Thomas de Grivel <thoxdg@gmail.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+defmodule Header.C do
+  def split(src) do
+    split(src, "")
+  end
+
+  def split("/*" <> rest, "") do
+    split(rest, "/*")
+  end
+  def split("/*" <> rest, acc) do
+    {"", acc <> "/*" <> rest}
+  end
+  def split("*/\n" <> rest, acc) do
+    header = acc <> "*/"
+    {header, rest}
+  end
+  def split(<<c, rest::binary>>, acc) when is_binary(acc) do
+    split(rest, acc <> <<c>>)
+  end
+  def split("", acc) do
+    {"", acc}
+  end
+
+  def main([src_path | dest_paths]) do
+    case File.read(src_path) do
+      {:ok, src} ->
+        {header, _} = split(src)
+        Enum.each dest_paths, fn dest_path ->
+          case File.read(dest_path) do
+            {:ok, dest} ->
+              {_, rest} = split(dest)
+              result = header <> "\n" <> rest
+              File.write(dest_path, result)
+            {:error, e} ->
+              IO.inspect("Error: #{dest_path}: #{e}")
+          end
+        end
+      {:error, e} ->
+        IO.inspect "Error: #{src_path}: #{e}"
+    end
+  end
+end
diff --git a/lib/header/make.ex b/lib/header/make.ex
new file mode 100644
index 0000000..0b0a9be
--- /dev/null
+++ b/lib/header/make.ex
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2022 Thomas de Grivel <thoxdg@gmail.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+## cl89
+## Copyright 2022 kmx.io <contact@kmx.io>
+##
+## Permission is hereby granted to use this software granted
+## the above copyright notice and this permission paragraph
+## are included in all copies and substantial portions of this
+## software.
+##
+## THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
+## PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
+## AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
+## THIS SOFTWARE.
+
+defmodule Header.Make do
+  def split(src) do
+    split(src, [])
+  end
+
+  def split([line = ("#" <> _) | rest], acc) do
+    split(rest, [line | acc])
+  end
+  def split(rest, acc) do
+    {Enum.reverse(acc) |> Enum.join("\n"), rest |> Enum.join("\n")}
+  end
+
+  def main([src_path | dest_paths]) do
+    {:ok, src} = File.read(src_path)
+    src_lines = String.split(src, "\n")
+    {header, _} = split(src_lines)
+    Enum.each dest_paths, fn dest_path ->
+      {:ok, dest} = File.read(dest_path)
+      dest_lines = String.split(dest, "\n")
+      {_, rest} = split(dest_lines)
+      result = header <> "\n" <> rest
+      File.write(dest_path, result)
+    end
+  end
+end
diff --git a/lib/mix/tasks/licence.ex b/lib/mix/tasks/licence.ex
new file mode 100644
index 0000000..741b765
--- /dev/null
+++ b/lib/mix/tasks/licence.ex
@@ -0,0 +1,43 @@
+## c3
+## Copyright 2022 kmx.io <contact@kmx.io>
+##
+## Permission is hereby granted to use this software granted
+## the above copyright notice and this permission paragraph
+## are included in all copies and substantial portions of this
+## software.
+##
+## THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
+## PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
+## AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
+## THIS SOFTWARE.
+
+defmodule Mix.Tasks.Licence do
+  use Mix.Task
+
+  def update(src_path, dest) do
+    module = case src_path do
+               "Makefile" -> Header.Make
+               "configure" -> Header.Make
+               "config.subr" -> Header.Make
+               _ ->
+                 case Regex.run(~r/[.][ch]$/, src_path) do
+                   [_] -> Header.C
+                   _ ->
+                     case Regex.run(~r/[.]exs?$/, src_path) do
+                       [_] -> Header.Make
+                       _ -> raise "error"
+                     end
+                 end
+             end
+    module.main(dest)
+  end
+
+  def run(_) do
+    {c_files, 0} = System.cmd("find", ["c_src", "-name", "[a-z]*.c",
+                                       "-or", "-name", "[a-z]*.h"])
+    {ex_files, 0} = System.cmd("find", ["lib", "-name", "[a-z]*.ex",
+                                        "-or", "-name", "[a-z]*.exs"])
+    update("c_src/git_nif.c", c_files)
+    update("lib/kmxgit.ex", ex_files)
+  end
+end