Skip to content

Commit

Permalink
#77 - Moving cacheWheels config implementation to INSTALL phase (#79)
Browse files Browse the repository at this point in the history
* HAB-77 - Moving cacheWheels config implementation from PACKAGE phase to INSTALL phase

* Adding details to lifecycle phase documentation

---------

Co-authored-by: Jeff Ross <[email protected]>
  • Loading branch information
JeffreyRoss and Jeff Ross authored Jan 5, 2024
1 parent d4bf767 commit 9f899f6
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ Uses [behave](https://github.com/behave/behave) to execute BDD scenarios that ar
Builds the `sdist` and `wheel` archives of this project using `poetry build`. It also generates a `requirements.txt` file which is useful when installing the package in a Docker container where you may want to install the dependencies in a specific Docker layer to optimize caching.

##### install #####
Publishes the `pom.xml` for the module into your local Maven Repository (`~/.m2/repository`).
Publishes the `pom.xml` for the module into your local Maven Repository (`~/.m2/repository`). If the **cacheWheels** configuration is set to True, the `wheel` archive will be copied to the poetry cache directory (`~/{poetry-cache-dir}/cache/repositories/wheels/{artifact-id}/`). The **cacheWheels** configuration default behavior is not to cache the `wheel` archive.

##### deploy #####

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,6 @@ public void doExecute() throws MojoExecutionException, MojoFailureException {

setUpPlaceholderFileAsMavenArtifact();
}

if(cacheWheels){
cacheWheelFile();
}
}

private void logLocalMonorepoCaveats() {
Expand Down Expand Up @@ -166,27 +162,5 @@ protected void setUpPlaceholderFileAsMavenArtifact() {
}

project.getArtifact().setFile(mavenArtifactFile);
}

private void cacheWheelFile() {
PoetryCommandHelper poetryHelper = createPoetryCommandHelper();
try{
File wheelSourceDirectory = new File(project.getBuild().getDirectory());
String poetryCacheDirectoryPath = poetryHelper.getPoetryCacheDirectoryPath();
File poetryWheelCacheDirectory = new File(String.format("%s/cache/repositories/wheels/%s", poetryCacheDirectoryPath, project.getArtifactId()));
//conditional will throw an error if cache directory isn't created
if(poetryWheelCacheDirectory.exists() || poetryWheelCacheDirectory.mkdirs()){
List<File> wheelFiles = Stream.of(wheelSourceDirectory.listFiles())
.filter(file -> file.getAbsolutePath().endsWith(".whl"))
.map(File::getAbsoluteFile)
.collect(Collectors.toList());
for(File file : wheelFiles){
HabushuUtil.copyFile(file.getPath(), String.format("%s/%s", poetryWheelCacheDirectory.toPath().toString(), file.getName()));
getLog().info(String.format("Cached the %s file", file.getName()));
}
}
} catch (Exception e){
throw new HabushuException("Could not cache the " + project.getArtifactId() + " wheel file(s)!", e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.technologybrewery.habushu;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.technologybrewery.habushu.exec.PoetryCommandHelper;
import org.technologybrewery.habushu.util.HabushuUtil;

/**
* Caches the generated poetry Wheel file when the
* {@link cacheWheels} flag configuration is set to
* true during the {@link LifecyclePhase#INSTALL}
* build phase.
* By default, the {@link cacheWheels} flag is set
* to false and will not cache the wheel files.
*/
@Mojo(name = "cache-wheels", defaultPhase = LifecyclePhase.INSTALL)
public class CacheWheelsMojo extends AbstractHabushuMojo {

@Parameter(property = "habushu.cacheWheels", required = false, defaultValue = "false")
protected boolean cacheWheels;

@Override
public void doExecute() throws MojoExecutionException, MojoFailureException {
if(cacheWheels){
PoetryCommandHelper poetryHelper = createPoetryCommandHelper();
try{
File wheelSourceDirectory = new File(project.getBuild().getDirectory());
String poetryCacheDirectoryPath = poetryHelper.getPoetryCacheDirectoryPath();
File poetryWheelCacheDirectory = new File(String.format("%s/cache/repositories/wheels/%s", poetryCacheDirectoryPath, project.getArtifactId()));
//conditional will throw an error if cache directory isn't created
if(poetryWheelCacheDirectory.exists() || poetryWheelCacheDirectory.mkdirs()){
List<File> wheelFiles = Stream.of(wheelSourceDirectory.listFiles())
.filter(file -> file.getAbsolutePath().endsWith(".whl"))
.map(File::getAbsoluteFile)
.collect(Collectors.toList());
for(File file : wheelFiles){
HabushuUtil.copyFile(file.getPath(), String.format("%s/%s", poetryWheelCacheDirectory.toPath().toString(), file.getName()));
getLog().info(String.format("Cached the %s file", file.getName()));
}
}
} catch (Exception e){
throw new HabushuException("Could not cache the " + project.getArtifactId() + " wheel file(s)!", e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
<process-classes>org.technologybrewery.habushu:habushu-maven-plugin:${project.version}:format-python</process-classes>
<test>org.technologybrewery.habushu:habushu-maven-plugin:${project.version}:behave-bdd-test</test>
<package>org.technologybrewery.habushu:habushu-maven-plugin:${project.version}:build-deployment-artifacts</package>
<install>org.apache.maven.plugins:maven-install-plugin:install</install>
<install>
org.technologybrewery.habushu:habushu-maven-plugin:${project.version}:cache-wheels,
org.apache.maven.plugins:maven-install-plugin:install
</install>
<deploy>
org.technologybrewery.habushu:habushu-maven-plugin:${project.version}:publish-to-pypi-repo,
org.apache.maven.plugins:maven-deploy-plugin:deploy
Expand Down
3 changes: 0 additions & 3 deletions habushu-mixology/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@
<plugin>
<groupId>org.technologybrewery.habushu</groupId>
<artifactId>habushu-maven-plugin</artifactId>
<configuration>
<cacheWheels>true</cacheWheels>
</configuration>
</plugin>
</plugins>
</build>
Expand Down

0 comments on commit 9f899f6

Please sign in to comment.