Skip to content

Instantly share code, notes, and snippets.

@maennchen
Last active February 5, 2025 12:24
Show Gist options
  • Save maennchen/356afb57883b374f19a72f8089bda178 to your computer and use it in GitHub Desktop.
Save maennchen/356afb57883b374f19a72f8089bda178 to your computer and use it in GitHub Desktop.
Elixir License Completion Script
#! /workspace/bin/elixir
require Logger
supported_extensions = ~w[.ex .exs .erl .md .cheatmd .yml .yrl .hrl .src .escript .jsonc]
root = Path.dirname(__ENV__.file)
{tracked_files, 0} = System.cmd("git", ["ls-files"], into: [], lines: 1024, cd: root)
files_to_check =
Enum.filter(tracked_files, fn
# Manual
"lib/elixir/src/elixir_json.erl" -> false
# Fixtures
"lib/elixir/test/elixir/fixtures/" <> _ -> false
"lib/ex_unit/test/fixtures/" <> _ -> false
"lib/eex/test/fixtures/" <> _ -> false
"lib/mix/test/fixtures/" <> _ -> false
# Others
file -> Path.extname(file) in supported_extensions
end)
for file <- files_to_check do
contents = File.read!(file)
if contents =~ "SPDX-" do
# Logger.info("#{file} already contains SPDX tags")
else
Logger.info("#{file} does not contain SPDX tags, patching")
{date, 0} = System.cmd("git", ~w[log --diff-filter=A --follow --format=%aI -1] ++ [file])
{:ok, date, _offset} =
date
|> String.trim()
|> DateTime.from_iso8601()
date = DateTime.to_naive(date)
tags = ["SPDX-License-Identifier: Apache-2.0", "SPDX-FileCopyrightText: 2021 The Elixir Team"]
tags =
case NaiveDateTime.compare(date, ~N[2020-01-01 00:00:00]) do
:gt -> tags
:eq -> tags
:lt -> tags ++ ["SPDX-FileCopyrightText: 2012 Plataformatec"]
end
case Path.extname(file) do
".exs" ->
# Contains shebang
if contents =~ "#!/" do
[first_line | rest] = String.split(contents, "\n", parts: 2)
File.write!(file, [
first_line,
?\n,
?\n,
Enum.map_join(tags, "\n", &"# #{&1}") <> "\n\n",
rest
])
else
File.write!(file, [
Enum.map_join(tags, "\n", &"# #{&1}") <> "\n\n",
contents
])
end
".escript" ->
# Contains shebang
if contents =~ "#!/" do
[first_line | rest] = String.split(contents, "\n", parts: 2)
File.write!(file, [
first_line,
?\n,
?\n,
Enum.map_join(tags, "\n", &"%% #{&1}") <> "\n\n",
rest
])
else
File.write!(file, [
Enum.map_join(tags, "\n", &"%% #{&1}") <> "\n\n",
contents
])
end
ext when ext in ~w[.ex .yml] ->
File.write!(file, [
Enum.map_join(tags, "\n", &"# #{&1}") <> "\n\n",
contents
])
ext when ext in ~w[.md .cheatmd] ->
File.write!(file, [
"<!--\n",
Enum.map_join(tags, "\n", &" #{&1}"),
"""
-->
""",
contents
])
ext when ext in ~w[.erl .hrl .src] ->
File.write!(file, [
Enum.map_join(tags, "\n", &"%% #{&1}") <> "\n\n",
contents
])
".yrl" ->
File.write!(file, [
Enum.map_join(tags, "\n", &"%% #{&1}") <> "\n\n",
"%% REUSE-IgnoreStart\n",
"Header ",
Enum.map_join(tags, "\n", &"\"%% #{&1}\""),
"""
.
%% REUSE-IgnoreEnd
""",
contents
])
".jsonc" ->
File.write!(file, [
Enum.map_join(tags, "\n", &"// #{&1}") <> "\n\n",
contents
])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment