Skip to content

Conversation

eksperimental
Copy link
Contributor

  • Store in mix.exs the defaults for various options
  • Add function to convert to nil when an empty string is passes in the command line arguments
  • Add function to set default arguments in case they are not set

@eksperimental
Copy link
Contributor Author

I am wondering if we should have functions ExDoc.default_config/1-2 that delegate to ExDoc.Mixfile

@josevalim
Copy link
Member

@eksperimental this is backwards. :) Your code should not depend on the Mixfile. The Mixfile may not even be loaded when the code is running as a dependency.

@eksperimental
Copy link
Contributor Author

@josevalim I sort of remember that we talked about it in the past, and it was when I implemented ExDoc.version/0. So I took the same approach.
Let me know how it looks now

lib/ex_doc.ex Outdated
extras: [],
filter_prefix: nil,
formatter: "html",
formatter: Mix.Project.config[:default][:formatter],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same issue. It is bad practice to invoke anything from Mix in your lib code because Mix is a build tool and it may not be available after the software is built. Even though you are using it only at compile time, doing this here means it is very easy for someone to accidentally depend on this at runtime.

What is this PR trying to achieve? Are the values being duplicated somewhere?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is duplication, we should handle it on this file exclusively.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I was too tired yesterday and overlooked this.
Please have a look again at the rebased commit

lib/ex_doc.ex Outdated
@@ -1,3 +1,20 @@
defmodule ExDoc.Default do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modules should have a @moduledoc tag.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need a new module, it can just be in ExDoc. Don't complicate the feature. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the reason is that ExDoc module depends on ExDoc.Config, and ExDoc.Config depends on the default config function. I tried creating ExDoc.Config.deafult/0-1 functions an undefined function error. that was why,
If you can find a way that this will work without the extra module, please let me know.. cuz I couldn't

"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []},
"ssl_verify_hostname": {:hex, :ssl_verify_hostname, "1.0.5", "2e73e068cd6393526f9fa6d399353d7c9477d6886ba005f323b592d389fb47be", [:make], []}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't commit mix.lock unless you are changing deps.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ericmj may I ask why the lock file change if no dependencies have been updated.
and what's the best way to deal with it, use git stash?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eksperimental Sometimes I do the following:

git update-index --assume-unchanged mix.lock

HTH

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eksperimental It's because a newer version of Hex was used to build the mix.lock.

@eksperimental
Copy link
Contributor Author

ping

@josevalim
Copy link
Member

I don't believe those changes belong here. If a source can give invalid input, such as empty strings, then those need to be removed and validated at the point of entry and not internally in the code.

end

defp normalize_options(options) do
pattern = options[:source_url_pattern] || guess_url(options[:source_url], options[:source_ref] || "master")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is doing exactly what the old code does, except the new code is much more verbose. Why change it?

lib/ex_doc.ex Outdated
end
end

defp normalize_option(options, field, default) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function can be written as: options[field] || default. Which is how it was being used before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reason was that I was dealing with empty strings as well

@eksperimental
Copy link
Contributor Author

@josevalim Please have a look at the new approach.

lib/ex_doc.ex Outdated

@spec config :: map
def config,
do: @config
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we tend to avoid the , do: style when there's only one function clause and go with do ... end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about that.

lib/ex_doc.ex Outdated
options =
options
|> normalize_directory([:assets, :output, :source_root])
|> normalize_source_url_pattern
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parens here?

lib/ex_doc.ex Outdated
pattern = options[:source_url_pattern] || guess_url(options[:source_url], options[:source_ref] || "master")
options = Keyword.put(options, :source_url_pattern, pattern)
def normalize_directory(options, field) when is_atom(field) do
if is_bitstring(options[field]) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this meant to be is_binary? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah, that's right

lib/ex_doc.ex Outdated
end

@doc """
Updates `field` in `options`, if its value is `nil`.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to myself: remove the comma

Store defaults for various options in ExDoc.Default
…elpers.

Replace ExDoc.normalize_options/1 with a more general function named ExDoc.normalize_options/3

Introduce:
- ExDoc.nilify_options/1
- ExDoc.normalize_source_url_pattern/2
- ExDoc.normalize_directory/2
@eksperimental
Copy link
Contributor Author

Suggestions addressed

@josevalim
Copy link
Member

Sorry @eksperimental but I am still having a hard time to understand why this pull request is addressing. Can we break it apart into smaller pull requests? I don't understand why we have to nilify options. If someone passes --foo "" in the command line, why are we nillifying it? Why do we need a whole separate module for keeping defaults? Can't we use module attributes? Maybe we need to break this PR into smaller ones as I can't see the benefits in here after multiple reviews.

@eksperimental
Copy link
Contributor Author

@josevalim I'm addressing your suggestions and submitting two new PRs

@josevalim
Copy link
Member

Thank you! ❤️

@josevalim josevalim closed this May 18, 2017
@eksperimental eksperimental deleted the default_config branch August 19, 2020 07:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants