Commit e7d64223e1902914c4535701ec803f890d0d4834

Thomas de Grivel 2022-11-03T14:18:49

header

diff --git a/lib/header/c.ex b/lib/header/c.ex
new file mode 100644
index 0000000..a9160c6
--- /dev/null
+++ b/lib/header/c.ex
@@ -0,0 +1,57 @@
+## kmxgit
+## 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.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\n" <> rest, acc) do
+    split("*/\n" <> rest, acc)
+  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/cli.ex b/lib/header/cli.ex
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/header/cli.ex
diff --git a/lib/header/make.ex b/lib/header/make.ex
new file mode 100644
index 0000000..673c058
--- /dev/null
+++ b/lib/header/make.ex
@@ -0,0 +1,44 @@
+## kmxgit
+## 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([line | rest], acc) do
+    case Regex.run(~r/^\s*$/, line) do
+      [_] -> split(rest, acc)
+      _ ->
+        acc = Enum.reverse(acc) |> Enum.join("\n")
+        rest = [line | rest] |> Enum.join("\n")
+        {acc, rest}
+    end
+  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\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..8bbf0af
--- /dev/null
+++ b/lib/mix/tasks/licence.ex
@@ -0,0 +1,13 @@
+## kmxgit
+## Copyright 2022 kmx.io <contact@kmx.io>
+
+defmodule Mix.Tasks.Licence do
+  use Mix.Task
+
+  def run(_) do
+    {ex_files, 0} = System.cmd("find", [".", "-name", "[a-z]*.ex",
+                                        "-or", "-name", "[a-z]*.exs"])
+    ex_files = ex_files |> String.split("\n") |> Enum.filter(& &1 != "")
+    UpdateHeader.update("mix.exs", ex_files)
+  end
+end
diff --git a/lib/update_header.ex b/lib/update_header.ex
index 806c818..8d2bc16 100644
--- a/lib/update_header.ex
+++ b/lib/update_header.ex
@@ -1,18 +1,28 @@
 defmodule UpdateHeader do
-  @moduledoc """
-  Documentation for `UpdateHeader`.
-  """
 
-  @doc """
-  Hello world.
-
-  ## Examples
+  def main(_args) do
+    IO.puts("update_header cli")
+  end
 
-      iex> UpdateHeader.hello()
-      :world
+  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([src_path | dest])
+  end
 
-  """
-  def hello do
-    :world
+  def run(argv) do
+    update(argv)
   end
 end
diff --git a/mix.exs b/mix.exs
index b51e2f3..6b881b2 100644
--- a/mix.exs
+++ b/mix.exs
@@ -1,3 +1,6 @@
+## update_header
+## Copyright 2022 kmx.io <contact@kmx.io>
+
 defmodule UpdateHeader.MixProject do
   use Mix.Project
 
@@ -8,6 +11,7 @@ defmodule UpdateHeader.MixProject do
       elixir: "~> 1.14",
       start_permanent: Mix.env() == :prod,
       deps: deps()
+      escript: [main_module: UpdateHeader]
     ]
   end