Skip to content
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

#1386: Get latest eo-runtime dependency version from Maven #1407

Merged
merged 7 commits into from
Nov 8, 2022

Conversation

volodya-lombrozo
Copy link
Member

Get latest eo-runtime dependency version from Maven.

Closes: #1386

…n from Maven Cenral

feat(objectionary#1386): get runtime depenedncy from maven

feat(objectionary#1386): cache runtime maven dependency

feat(objectionary#1386): fix all linter mistakes
@volodya-lombrozo
Copy link
Member Author

@mximp Can you take a look, please?

/**
* Hardcoded dependency.
*/
static final Unchecked<Dependency> HARDCODED = DcsWithRuntime.dependency("0.28.10");
Copy link
Contributor

Choose a reason for hiding this comment

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

@volodya-lombrozo why do we hardcode the version? Can't we pass it via constructor?

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually it's not used anywhere within the class - must be moved out.

Copy link
Member Author

Choose a reason for hiding this comment

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

@mximp done. I've remove the static field at all. I've used it in tests previously.

* The dependencies source.
* Dependency downloaded by HTTP from Maven Central.
*/
static final Unchecked<Dependency> FROM_MAVEN = fromMaven();
Copy link
Contributor

Choose a reason for hiding this comment

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

@volodya-lombrozo Can we make it private?

Copy link
Member Author

Choose a reason for hiding this comment

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

@mximp done.

Copy link
Contributor

@mximp mximp left a comment

Choose a reason for hiding this comment

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

@volodya-lombrozo please check comments above

* @param delegate Dependencies delegate.
* @param source Runtime dependency source.
*/
DcsWithRuntime(final Dependencies delegate, final Unchecked<Dependency> source) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@volodya-lombrozo what is the point of supplying runtime dependency to add? I believe the whole idea of this class is to download it from Maven Central in case it's missing in delegate, am I right?
Maybe we can rename the class to DcsRuntimeCentral to make it clear and get rid of two-args constructor?

Copy link
Member Author

Choose a reason for hiding this comment

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

@mximp You miss one point here. The idea of the class is not to download dependency from Maven Central. The idea the class is to add eo-runtime dependency to the collection of other dependencies. Moreover, we don't have to add eo-runtime dependency if it is already present in the collection. By that reason, we can have different sources where we can get eo-runtime dependency:

  1. hardcoded - we used hardcoded version previously 0.28.10.
  2. maven central - we are using it now
    Of course, we can remove the constructor and hardcode only maven central, but it could be useful for the future testing to have additional constructor in order to "mock" source, because getting of "eo-runtime" dependency by http is a quite expensive .

Copy link
Contributor

Choose a reason for hiding this comment

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

@volodya-lombrozo got it. I believe better name for hardcoded would be fixed or supplied. Please check renaming suggestions for better readability.

* @param delegate Dependencies delegate.
* @param source Runtime dependency source.
*/
DcsWithRuntime(final Dependencies delegate, final Unchecked<Dependency> source) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@volodya-lombrozo got it. I believe better name for hardcoded would be fixed or supplied. Please check renaming suggestions for better readability.

* The main constructor.
* Runtime dependency source.
*/
private final Unchecked<Dependency> source;
Copy link
Contributor

Choose a reason for hiding this comment

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

@volodya-lombrozo let's rename to fixed

Copy link
Member Author

Choose a reason for hiding this comment

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

@mximp renamed to supplied.

*
* @return Runtime dependency from Maven Central.
*/
private static Unchecked<Dependency> fromMaven() {
Copy link
Contributor

Choose a reason for hiding this comment

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

@volodya-lombrozo let's rename to download

Copy link
Member Author

Choose a reason for hiding this comment

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

@mximp I don't think it's a good name for that static method according with that post.

Copy link
Member Author

Choose a reason for hiding this comment

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

@mximp renamed to mavenDependency

* The dependencies source.
* Dependency downloaded by HTTP from Maven Central.
*/
private static final Unchecked<Dependency> FROM_MAVEN = fromMaven();
Copy link
Contributor

Choose a reason for hiding this comment

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

@volodya-lombrozo let's rename to dynamic. Also static methods should be called via class name.
So this would look like this:
private static final Unchecked<Dependency> DYNAMIC = DcsWithRuntime.download()

Copy link
Contributor

Choose a reason for hiding this comment

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

@volodya-lombrozo Actually you can use Sticky here from Cactoos lib and get rid of static method.

Copy link
Member Author

Choose a reason for hiding this comment

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

@mximp I'm already using Sticky class:

 private static Unchecked<Dependency> dependency(final String version) {
        return new Unchecked<>(
            new Sticky<>(
                () -> {
                    final Dependency dependency = new Dependency();
                    dependency.setGroupId("org.eolang");
                    dependency.setArtifactId("eo-runtime");
                    dependency.setVersion(version);
                    dependency.setClassifier("");
                    return dependency;
                }
            )
        );
    }

I can't declare field as Sticky, because in that case I have to deal with Exception handling.

Copy link
Member Author

Choose a reason for hiding this comment

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

@mximp also I can't get rid of the static method, because I wanna leave private static final Unchecked<Dependency> FROM_MAVEN as a static field in order to increase speed of the tests. (when we have a static field, the method will be called only ~2 times, if you make it as a simple object field - the download will be happened each time when you create a new DcsWithRuntime object > 10 times)

Copy link
Contributor

Choose a reason for hiding this comment

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

@volodya-lombrozo I just mean why do we need the method dependency. It just creates an object and used only in one place - we can inline it:

private static final Unchecked<Dependency> MAVEN_DEPENDENCY = new Unchecked<>(
    new Sticky<>(() -> {
        final Dependency dependency = new Dependency();
        dependency.setGroupId("org.eolang");
        dependency.setArtifactId("eo-runtime");
        dependency.setVersion(version);
        dependency.setClassifier("");
        return dependency;
    })
);

Copy link
Contributor

Choose a reason for hiding this comment

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

@volodya-lombrozo corrected inlining:

private static final Unchecked<Dependency> MAVEN_DEPENDENCY =
    new Unchecked<>(
        new Sticky<>(() -> {
            final Dependency dependency = new Dependency();
            dependency.setGroupId("org.eolang");
            dependency.setArtifactId("eo-runtime");
            dependency.setVersion(
                new XMLDocument(
                    new URL(
                        String.format(
                            "https://repo.maven.apache.org/maven2/%s/maven-metadata.xml",
                            "org/eolang/eo-runtime"
                        )
                    )
                ).xpath("//latest/text()").get(0)
            );
            dependency.setClassifier("");
            return dependency;
        })
    );

Copy link
Member Author

@volodya-lombrozo volodya-lombrozo Nov 7, 2022

Choose a reason for hiding this comment

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

@mximp I got your idea. It has several advantages:

  • we don't create extra methods
  • we reduce total number of lines

On the other hand, that approach:

  • creates lots of code that is placed above fields declarations and constructors (it's confusing to see a lot of code even before declaring variables).
  • looks like a solid piece of code that do several things at once: (1) Creates dependency (2) Downloads latest version of library from maven. (3) Creates Sticky object. (some sort of violation of the principle of single responsibility)

I prefer using small methods and classes for achieving granularity and better code readability. But I can be mistaken, of course. Especially in "better code readability".

Copy link
Contributor

Choose a reason for hiding this comment

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

@volodya-lombrozo I believe the biggest advantage of the inlined version is that I can see the whole logic of defining MAVEN_DEPENDENCY in front of me: that it's sticky (i.e. cached), which URL used and that latest version is extracted. It's only several lines of code and I don't need to jump around the methods.

Regarding SRP, in this case I think we need to look at it a bit broader: we just extract version from Maven Central. I would probably agree that dealing with URL and parsing XML for a version might be a distinguishable bit of functionality but in this case making it to be a separate method doesn't provide any benefits: we just spread logic across multiple places.

Copy link
Member Author

Choose a reason for hiding this comment

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

@yegor256 what is the better way?

@volodya-lombrozo
Copy link
Member Author

@yegor256 take a look, please.

@yegor256
Copy link
Member

yegor256 commented Nov 8, 2022

@rultor merge

@rultor
Copy link
Contributor

rultor commented Nov 8, 2022

@rultor merge

@yegor256 OK, I'll try to merge now. You can check the progress of the merge here

@rultor rultor merged commit 2dd0579 into objectionary:master Nov 8, 2022
@rultor
Copy link
Contributor

rultor commented Nov 8, 2022

@rultor merge

@yegor256 Done! FYI, the full log is here (took me 7min)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DcsWithRuntime.java:34-38: Hardcoded version of...
4 participants