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

624 bypass caching #1023

Merged
merged 7 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
74 changes: 67 additions & 7 deletions eo-maven-plugin/src/main/java/org/eolang/maven/PullMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,14 @@
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.cactoos.Input;
import org.cactoos.scalar.Sticky;
import org.cactoos.scalar.Unchecked;

/**
* Pull EO XML files from Objectionary and parse them into XML.
*
* @since 0.1
* @todo #561:30min Add a parameter to bypass/overwrite cache
* for combination of Local and Caching and Remote.
* It was suggested by @yegor256 to rely on -U parameter of Maven
* (https://github.com/objectionary/eo/issues/561#issuecomment-1007128430).
* If it is possible to access it from the plugin.
*/
@Mojo(
name = "pull",
Expand Down Expand Up @@ -95,7 +93,7 @@ public void exec() throws IOException {
if (this.objectionary == null) {
final String full = new HashOfTag(this.hash).hash();
final String small = full.substring(0, 7);
this.objectionary = new OyFallback(
this.objectionary = new PullMojo.FallbackSwapOy(
new OyHome(
small,
this.outputPath
Expand All @@ -104,7 +102,8 @@ public void exec() throws IOException {
small,
this.outputPath,
new OyRemote(full)
)
),
this.forceUpdate()
);
}
if (!tojos.isEmpty()) {
Expand All @@ -121,6 +120,14 @@ public void exec() throws IOException {
}
}

/**
* Is force update option enabled.
* @return True if option enabled and false otherwise
*/
private boolean forceUpdate() {
return this.session.getRequest().isUpdateSnapshots();
}

/**
* Pull one object.
*
Expand Down Expand Up @@ -150,4 +157,57 @@ private Path pull(final String name) throws IOException {
return src;
}

/**
* Fallback which can swap primary/secondary repos.
* @since 1.0
*/
public static final class FallbackSwapOy implements Objectionary {
Copy link
Member

Choose a reason for hiding this comment

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

@mximp I think it would be better to make it an outside class, like all others that start with Oy

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yegor256 The reason I made it nested is that it's quite specific implementation of Oy tightly bound to PullMojo. For now there is only one use case for this - Oy build strategy for PullMojo.
Wrapping this logic into class simplifies testing of it and usage, but I don't see a reason to make it standalone.

Copy link
Member

Choose a reason for hiding this comment

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

@mximp all other Oy classes are only used by PullMojo. This one is no different.

/**
* Swapped Oy.
*/
private final Unchecked<Objectionary> swapped;

/**
* Ctor.
*
* @param first Initial primary
* @param second Initial secondary
* @param swap Whether to swap
*/
public FallbackSwapOy(
final Objectionary first,
final Objectionary second,
final boolean swap
) {
this.swapped = new Unchecked<>(
new Sticky<>(
() -> {
final Objectionary result;
if (swap) {
result = new OyFallback(
second,
first
);
} else {
result = new OyFallback(
first,
second
);
}
return result;
}
)
);
}

@Override
public Input get(final String name) throws IOException {
return this.swapped.value().get(name);
}

@Override
public String toString() {
return this.swapped.value().toString();
}
}
}
45 changes: 45 additions & 0 deletions eo-maven-plugin/src/test/java/org/eolang/maven/PullMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@

import com.yegor256.tojos.Json;
import com.yegor256.tojos.MonoTojos;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.cactoos.io.InputOf;
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -70,4 +72,47 @@ public void testSimplePull(@TempDir final Path temp) {
);
}

@Test
public void testFallbackNoSwapOy() throws Exception {
MatcherAssert.assertThat(
new TextOf(
new PullMojo.FallbackSwapOy(
s -> new InputOf("[] > local\n"),
s -> new InputOf("[] > remote\n"),
false
).get("")
).asString(),
Matchers.containsString("local")
);
}

@Test
public void testFallbackSwapOyFail() throws Exception {
MatcherAssert.assertThat(
new TextOf(
new PullMojo.FallbackSwapOy(
s -> new InputOf("[] > local\n"),
s -> {
throw new IOException("Can't get object");
},
false
).get("")
).asString(),
Matchers.containsString("local")
);
}

@Test
public void testFallbackSwapOy() throws Exception {
MatcherAssert.assertThat(
new TextOf(
new PullMojo.FallbackSwapOy(
s -> new InputOf("[] > local\n"),
s -> new InputOf("[] > remote\n"),
true
).get("")
).asString(),
Matchers.containsString("remote")
);
}
}