Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Aug 11, 2022
2 parents a7edc19 + 3e049e7 commit 67ebcd9
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 9 deletions.
16 changes: 15 additions & 1 deletion eo-maven-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This is Maven plugin for EO.
Here is a simple program that gets a year from command line and tells you
whether it's leap or not:

```
```eo
[args...] > main
[y] > leap
or. > @
Expand All @@ -21,6 +21,7 @@ whether it's leap or not:
"%d is %sa leap year!"
(args.get 0).as-int > year!
if. (leap year:y) "" "not "
```

In order to compile this program, put it into `src/main/eo/main.eo` and then
Expand Down Expand Up @@ -108,4 +109,17 @@ for most popular and important objects that any of you will need in order
to write even a simple EO program. There are objects like `string`, `int`, `sprintf`,
`stdout`, and so on. By the way, you may want to contribute there by creating new objects.

## Bypassing object cache

By default, during compilation the plugin will check local cache (`~/.eo`) for required objects
and only download (and cache) them from [Objectionary home](https://github.com/objectionary/home)
in case they are not found locally.

This behaviour can be changed to always download objects from remote by providing
Maven option `-U` (see [Maven CLI docs](https://maven.apache.org/ref/3.1.0/maven-embedder/cli.html)):

```shell
mvn -U clean install
```


96 changes: 96 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/OyFallbackSwap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.maven;

import java.io.IOException;
import org.cactoos.Input;
import org.cactoos.scalar.Sticky;
import org.cactoos.scalar.Unchecked;

/**
* Fallback which can swap primary/secondary repos.
* <p/>
* The key purpose of this class is to allow dynamic determination
* of which Oy (fist or second) to use as primary and which as fallback based on given
* boolean property.
* <p/>
* For {@link PullMojo} this is used to bypass reading from cache by always checking remote
* first and only fallback to local in case of object miss:
* <pre>
* new PullMojo.FallbackSwapOy(
* &lt local &gt,
* &lt remote &gt,
* this.forceUpdate()
* );
* </pre>
* @since 1.0
*/
public final class OyFallbackSwap implements Objectionary {
/**
* Swapped Oy.
*/
private final Unchecked<Objectionary> swapped;

/**
* Ctor.
* @param first Initial primary
* @param second Initial secondary
* @param swap Whether to swap
*/
public OyFallbackSwap(
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();
}
}
18 changes: 11 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 @@ -38,11 +38,6 @@
* 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 +90,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 OyFallbackSwap(
new OyHome(
small,
this.outputPath
Expand All @@ -104,7 +99,8 @@ public void exec() throws IOException {
small,
this.outputPath,
new OyRemote(full)
)
),
this.forceUpdate()
);
}
if (!tojos.isEmpty()) {
Expand All @@ -121,6 +117,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.maven;

import java.io.IOException;
import org.cactoos.io.InputOf;
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

/**
* Tests for {@link OyFallbackSwap}.
* @since 1.0
*/
class OyFallbackSwapTest {
@Test
public void testFallbackNoSwapOy() throws Exception {
MatcherAssert.assertThat(
new TextOf(
new OyFallbackSwap(
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 OyFallbackSwap(
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 OyFallbackSwap(
s -> new InputOf("[] > local\n"),
s -> new InputOf("[] > remote\n"),
true
).get("")
).asString(),
Matchers.containsString("remote")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,4 @@ public void testSimplePull(@TempDir final Path temp) {
Matchers.is(true)
);
}

}

1 comment on commit 67ebcd9

@0pdd
Copy link

@0pdd 0pdd commented on 67ebcd9 Aug 11, 2022

Choose a reason for hiding this comment

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

Puzzle 561-386b5824 disappeared from eo-maven-plugin/src/main/java/org/eolang/maven/PullMojo.java), that's why I closed #624. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.