-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added first very basic widget downloader using gist github api #38
Added first very basic widget downloader using gist github api #38
Conversation
531dad1
to
85a72d6
Compare
One of the things we talked about in #18 is to not have a different command for widgets and jobs, instead, if the file is an Elixir file, then treat it as a job (we can update it for hooks later). If it's an HTML or JS file, then make it a widget. This would shorten the command to just |
Right I can easily add it |
I think that a command like At the moment i feel fetching from gist should be explicit, like |
@JanStevens also make sure to run credo ( |
I've update the original MR to reflect recent changes:
|
9abbb11
to
ee0ce5f
Compare
] | ||
@shortdoc "Install a custom Widget from a Github gist" | ||
|
||
@moduledoc """ |
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.
Can you place the moduledoc at the top inside the module?
|
||
# Process the install incase we have a gist and widget name | ||
# It will download the gist files and place them in the right location | ||
# |
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.
✂️
For additional information, refer to the documentation for | ||
`Mix.Tasks.Run`. | ||
""" | ||
def run(args) do |
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.
Add a separate @doc
for this function.
Recursively extract the last part of the gist and use it for the building the url
…e gist does not exist anymore
…ut we didn't specify the widget directory
|
||
# Other files all go into the widget dir | ||
defp determine_file_location(file, widget_name) do | ||
Map.put(file, :path, "./widgets/#{widget_name}/") |
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.
Please use Path.join to create the path.
|
||
defp download_gist(url), do: url |> HTTPoison.get! |> process_response | ||
|
||
defp build_gist_url(gist_url) when length(gist_url) == 1, do: '#{@github_url}#{hd(gist_url)}' |
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.
Why don't you use double a quoted string?
|
||
## Options | ||
|
||
* `--widget` - specifies the widget name that will be used as folder name |
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.
change folder
to directory
def atomify_map(map) do | ||
for {key, value} <- map, into: %{}, do: {String.to_atom(key), value} | ||
end | ||
|
||
def assert_file(file) do |
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.
All those file assertions and logic deserve their own helper module.
end | ||
|
||
test "places all the files in the correct locations" do | ||
in_tmp "installes widgets and jobs", fn -> |
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.
Fix typo s/installes/installs
|
||
test "fails when `--gist` is not provided" do | ||
Mix.Tasks.Kitto.Install.run(["--widget", "numbers"]) | ||
assert_received {:mix_shell, :error, ["Unsupported arguments"]} |
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.
Please leave a newline above assertions.
test "places all the files in the correct locations" do | ||
in_tmp "installes widgets and jobs", fn -> | ||
with_mock HTTPoison, [get!: mock_gist_with(200, @gist_response)] do | ||
Mix.Tasks.Kitto.Install.run(["--gist", "aaaa", "--widget", "number"]) |
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.
Substitute "aaaaa" with something that could be an actual argument.
end | ||
end | ||
|
||
test "fails when the gist has widget but no `--widget` provided" do |
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.
Most gists will contains widgets, why should the user specify --widget
for all those cases?
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.
Widget is used for the subdirectory under widgets
. I added it so people could pull different number
widgets by specifying a different directory
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.
It's ok to have that flag, but when i try to fetch a gist containing a JS file with gitlab.js
i think that the intuitive thing to do is to create a widgets/gitlab/
dir unless specified otherwise using the flag.
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.
That's indeed obvious, can we agree on the following order?
- Use
--widget
if provided - Use
filename
from the JS file, note we will have to sanitize it, since it can contain capitalized letters spaces and so on
Will have to think about it a bit since this will require some changes
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.
Agreed. Let's also not sanitize the filename, it's up to the gist maintainer to have a "package" which works.
assert File.regular?(file), "Expected #{file} to exist, but does not" | ||
end | ||
|
||
def assert_file(file, match) do |
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.
Should we mention that this code segment is taken from: https://github.com/phoenixframework/phoenix/blob/master/installer/test/mix_helper.exs
?
What do you think?
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.
Yes I'll add reference
in_tmp "installes widgets and jobs", fn -> | ||
with_mock HTTPoison, [get!: mock_gist_with(200, @gist_response)] do | ||
Mix.Tasks.Kitto.Install.run(["--gist", "aaaa", "--widget", "number"]) | ||
assert_file "widgets/number/number_job.js", fn file -> |
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.
Change file
argument to contents
makes it explicit that we're performing the assertion on the file contents.
Installs community Widget/Job from a Github Gist | ||
|
||
mix kitto.install --widget test_widget --gist JanStevens/0209a4a80cee782e5cdbe11a1e9bc393 | ||
mix kitto.install --gist 0209a4a80cee782e5cdbe11a1e9bc393 |
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.
Why is the second evocation missing the owner of the gist. I expected it to also have JanStevens/0209a4a80cee782e5cdbe11a1e9bc393
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.
Because we only need the last part of the gist url, you could even copy past the full url and it would still work
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 didn't know that. I tried it and it works! 🌈
@@ -0,0 +1,100 @@ | |||
defmodule Mix.Tasks.Kitto.Install do | |||
use Mix.Task | |||
@shortdoc "Install community Widget/Job from a Github Gist" |
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.
Please add newline above.
end | ||
|
||
# Elixir files we place in the jobs dir | ||
defp determine_file_location(%{language: "Elixir"} = file, _) do |
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 think that we can make the assumption that .exs files are for jobs and .ex belong to lib
.
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.
Oke I'll look into it, do we also want to namespace the ex
files in lib
?
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.
No need to namespace them.
@JanStevens I rebased and tidied up the branch a bit before merging it. Also changed it to place .exs files under |
Allright, this is a first very basic implementation for downloading a gist and installing it as a widget. Current feature list:
Mix.Generator.create_file
)Example usage
Concerns / Ideas