This repository was archived by the owner on Aug 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
add links for depedencies, extra licenses, and output as file #20
Open
mayel
wants to merge
4
commits into
unnawut:master
Choose a base branch
from
bonfire-networks:pr
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,52 +1,73 @@ | ||
| defmodule Licensir.FileAnalyzer do | ||
| # The file names to check for licenses | ||
| @license_files ["LICENSE", "LICENSE.md", "LICENSE.txt"] | ||
| @license_files ["LICENSE", "LICENSE.md", "LICENSE.txt", "LICENCE"] | ||
|
|
||
| # The files that contain the actual text for each license | ||
| @files [ | ||
| apache2: ["Apache2_text.txt", "Apache2_text.variant-2.txt", "Apache2_url.txt"], | ||
| agpl_v3: ["AGPLv3.txt"], | ||
| apache2: [ | ||
| "Apache2_text.txt", | ||
| "Apache2_text.variant-2.txt", | ||
| "Apache2_text.variant-3.txt", | ||
| "Apache2_url.txt" | ||
| ], | ||
| bsd: ["BSD-3.txt", "BSD-3.variant-2.txt"], | ||
| cc0: ["CC0-1.0.txt"], | ||
| gpl_v2: ["GPLv2.txt"], | ||
| gpl_v3: ["GPLv3.txt"], | ||
| isc: ["ISC.txt", "ISC.variant-2.txt"], | ||
| lgpl: ["LGPL.txt"], | ||
| mit: ["MIT.txt", "MIT.variant-2.txt", "MIT.variant-3.txt"], | ||
| mpl2: ["MPL2.txt"], | ||
| mpl2: ["MPL2.txt", "MPL2b.txt"], | ||
| licensir_mock_license: ["LicensirMockLicense.txt"] | ||
| ] | ||
|
|
||
| def analyze(dir_path) do | ||
|
|
||
| Enum.find_value(@license_files, fn file_name -> | ||
| dir_path | ||
| |> Path.join(file_name) | ||
| |> File.read() | ||
| |> case do | ||
| {:ok, content} -> analyze_content(content) | ||
| {:ok, content} -> | ||
| analyze_content(content) | ||
| {:error, _} -> nil | ||
| end | ||
| end) | ||
| end | ||
|
|
||
| # Returns the first license that matches | ||
| defp analyze_content(content) do | ||
| content = clean(content) | ||
|
|
||
| Enum.find_value(@files, fn {license, license_files} -> | ||
| found = | ||
| Enum.find(license_files, fn license_file -> | ||
| license = | ||
| license_text = | ||
| :licensir | ||
| |> :code.priv_dir() | ||
| |> Path.join("licenses") | ||
| |> Path.join(license_file) | ||
| |> File.read!() | ||
| |> clean() | ||
|
|
||
| # Returns true only if the content is a superset of the license text | ||
| clean(content) =~ clean(license) | ||
| content =~ license_text | ||
| end) | ||
|
|
||
| if found, do: license, else: nil | ||
| end) || :unrecognized_license_file | ||
| end) || unrecognised(content) | ||
| end | ||
|
|
||
| defp unrecognised(_content) do | ||
| :unrecognized_license_file | ||
| end | ||
|
|
||
| defp clean(content), do: String.replace(content, ~r/\v/, "") | ||
| def clean(content), | ||
| do: | ||
| content | ||
| |> String.replace("\n", " ") | ||
| |> String.replace(~r/\s\s+/, " ") | ||
| |> String.trim() | ||
| |> String.downcase() | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,45 +9,93 @@ defmodule Mix.Tasks.Licenses do | |
| """ | ||
| use Mix.Task | ||
|
|
||
| @output_file "DEPENDENCIES.md" | ||
| @shortdoc "Lists each dependency's licenses" | ||
| @recursive true | ||
| @switches [ | ||
| top_level_only: :boolean, | ||
| csv: :boolean | ||
| csv: :boolean, | ||
| only_license: :boolean | ||
| ] | ||
|
|
||
| def run(argv) do | ||
| {opts, _argv} = OptionParser.parse!(argv, switches: @switches) | ||
|
|
||
| Licensir.Scanner.scan(opts) | ||
| |> Enum.sort_by(fn license -> license.name end) | ||
| |> Enum.map(&to_row/1) | ||
| |> Enum.sort_by(fn lib -> lib.name end) | ||
| |> Enum.map(&to_row(&1, opts)) | ||
| |> render(opts) | ||
| end | ||
|
|
||
| defp to_row(map) do | ||
| [map.name, map.version, map.license] | ||
| defp to_row(map, opts) do | ||
| if Keyword.get(opts, :only_license), | ||
| do: [map.name, map.license], | ||
| else: [map.name, map.license, map.version, map.link] | ||
| end | ||
|
|
||
| defp render(rows, opts) do | ||
| if Keyword.get(opts, :only_license), | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the short hand
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those are single lines though |
||
| do: render(rows, opts, ["Package", "License"]), | ||
| else: render(rows, opts, ["Package", "License", "Version", "Link"]) | ||
| end | ||
|
|
||
| defp render(rows, opts, headers) do | ||
| cond do | ||
| Keyword.get(opts, :csv) -> render_csv(rows) | ||
| true -> render_ascii_table(rows) | ||
| Keyword.get(opts, :csv) -> | ||
| render_csv(rows, headers) | ||
|
|
||
| true -> | ||
| render_ascii_table(rows, headers) | ||
| end | ||
| end | ||
|
|
||
| defp render_ascii_table(rows) do | ||
| _ = Mix.Shell.IO.info([:yellow, "Notice: This is not a legal advice. Use the information below at your own risk."]) | ||
| defp render_ascii_table(rows, headers) do | ||
| _ = | ||
| Mix.Shell.IO.info([ | ||
| :yellow, | ||
| "Notice: This is not a legal advice. Use the generated licensing information at your own risk." | ||
| ]) | ||
|
|
||
| rows | ||
| |> TableRex.quick_render!(["Package", "Version", "License"]) | ||
| |> IO.puts() | ||
| |> TableRex.quick_render!(headers) | ||
| |> file_touch() | ||
| |> output() | ||
| end | ||
|
|
||
| defp render_csv(rows) do | ||
| defp render_csv(rows, headers) do | ||
| rows | ||
| |> List.insert_at(0, ["Package", "Version", "License"]) | ||
| |> List.insert_at(0, headers) | ||
| |> Licensir.CSV.encode() | ||
| |> Enum.each(&IO.write/1) | ||
| |> file_touch() | ||
| |> Enum.each(&output/1) | ||
| end | ||
|
|
||
| defp file_touch(text) do | ||
| if @output_file do | ||
| with {:ok, file} <- File.open(@output_file, [:write]) do | ||
| # IO.puts("\n\nSaving the output to " <> @output_file) | ||
| IO.binwrite(file, "\n") | ||
| text | ||
| else | ||
| _e -> | ||
| _ = | ||
| Mix.Shell.IO.info([ | ||
| :yellow, | ||
| "WARNING: Could not write to " <> @output_file | ||
| ]) | ||
|
|
||
| nil | ||
| end | ||
| end | ||
| end | ||
|
|
||
| defp output(text) do | ||
| filewriter = fn filename, data -> | ||
| File.open(filename, [:append]) | ||
| |> elem(1) | ||
| |> IO.binwrite(data) | ||
| end | ||
|
|
||
| if @output_file, do: filewriter.(@output_file, text) | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I remember correctly, there shouldn't be any case where the 2nd argument is an empty string. So I think if it falls into this case we should fix the root cause than handling it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a case of a dependency with no license which results in an empty license column in the final rather than "Undefined" if this line is present.