Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/hex/package.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ defmodule Hex.Package do
can include wildcards. Defaults to `#{inspect(default_files())}`.
* `:exclude_patterns` - List of patterns matching files and directories to
exclude from the package.
* `:licenses` - List of licenses used by the package.
* `:licenses` - List of licenses used by the package. Use SPDX license
identifiers or `LicenseRef-<idstring>` for custom licenses included in
the package.
* `:links` - Map of links relevant to the package.
* `:build_tools` - List of build tools that can build the package. Hex will
try to automatically detect the build tools based on the files in the
Expand Down
28 changes: 27 additions & 1 deletion src/mix_hex_licenses.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Vendored from hex_core v0.15.0 (90f9f59), do not edit manually
%% Vendored from hex_core v0.15.0 (4ff8378), do not edit manually

%% @doc
%% Hex Licenses.
Expand Down Expand Up @@ -667,4 +667,30 @@ valid(<<"xlock">>) -> true;
valid(<<"xpp">>) -> true;
valid(<<"xzoom">>) -> true;
valid(<<"zlib-acknowledgement">>) -> true;
valid(<<"LicenseRef-", IdString/binary>>) -> valid_license_ref_idstring(IdString);
valid(_) -> false.

valid_license_ref_idstring(<<>>) ->
false;
valid_license_ref_idstring(IdString) ->
valid_license_ref_idstring(IdString, true).

valid_license_ref_idstring(<<>>, Valid) ->
Valid;
valid_license_ref_idstring(_, false) ->
false;
valid_license_ref_idstring(<<Char, Rest/binary>>, true) ->
valid_license_ref_idstring(Rest, valid_license_ref_char(Char)).

valid_license_ref_char(Char) when Char >= $A, Char =< $Z ->
true;
valid_license_ref_char(Char) when Char >= $a, Char =< $z ->
true;
valid_license_ref_char(Char) when Char >= $0, Char =< $9 ->
true;
valid_license_ref_char($-) ->
true;
valid_license_ref_char($.) ->
true;
valid_license_ref_char(_) ->
false.
24 changes: 24 additions & 0 deletions test/mix/tasks/hex.build_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ defmodule Mix.Tasks.Hex.BuildTest do
:mix_hex_erl_tar.extract({:binary, files[~c"contents.tar.gz"]}, [:compressed, cwd: path])
end

test "vendored licenses accept custom LicenseRef identifiers" do
assert :mix_hex_licenses.valid("LicenseRef-Journey")
assert :mix_hex_licenses.valid("LicenseRef-acme.1-2")
refute :mix_hex_licenses.valid("LicenseRef-")
refute :mix_hex_licenses.valid("LicenseRef-Journey License")
refute :mix_hex_licenses.valid("LicenseRef-Journey_License")
end

test "create" do
Process.put(:hex_test_app_name, :build_app_name)
Mix.Project.push(ReleaseSimple.MixProject)
Expand Down Expand Up @@ -66,6 +74,22 @@ defmodule Mix.Tasks.Hex.BuildTest do
purge([ReleaseInvalidLicenses.MixProject])
end

test "create with custom LicenseRef license" do
Process.put(:hex_test_app_name, :release_license_ref)
Mix.Project.push(ReleaseLicenseRef.MixProject)

in_tmp(fn ->
Hex.State.put(:cache_home, tmp_path())
File.write!("myfile.txt", "hello")
File.write!("LICENSE", "Journey License")
Mix.Tasks.Hex.Build.run([])

assert package_created?("release_license_ref-0.0.1")
end)
after
purge([ReleaseLicenseRef.MixProject])
end

test "create private package with invalid licenses" do
Process.put(:hex_test_app_name, :release_repo_invalid_licenses)
Mix.Project.push(ReleaseRepoInvalidLicenses.MixProject)
Expand Down
15 changes: 15 additions & 0 deletions test/support/release_samples.ex
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,21 @@ defmodule ReleaseInvalidLicenses.MixProject do
end
end

defmodule ReleaseLicenseRef.MixProject do
def project do
[
app: :release_license_ref,
description: "Package with a custom license reference",
version: "0.0.1",
package: [
licenses: ["LicenseRef-Journey"],
files: ["myfile.txt", "LICENSE"],
links: %{"a" => "http://a"}
]
]
end
end

defmodule ReleaseAppFalseDep.MixProject do
def project do
[
Expand Down
Loading