-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #224 from TechnologyBrewery/217-uv-poetry-initiali…
…zehabushumojo [217] Support UV /Poetry switch for maven plugin - InitializeHabushuMojo
- Loading branch information
Showing
7 changed files
with
325 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,3 +62,4 @@ Thumbs.db | |
# https://python-poetry.org/docs/configuration/#virtualenvsin-project | ||
.venv | ||
poetry.lock | ||
uv.lock |
53 changes: 53 additions & 0 deletions
53
...u-maven-plugin/src/main/java/org/technologybrewery/habushu/AbstractInitializeHabushu.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.technologybrewery.habushu; | ||
|
||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugin.MojoFailureException; | ||
import org.apache.maven.plugin.logging.Log; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* Abstract class that ensures pre-requisite tools that Habushu leverages are installed and available on the | ||
* developer's machine to support the same functionality across multiple Mojo implementations. | ||
*/ | ||
public abstract class AbstractInitializeHabushu { | ||
/** | ||
* Base directory from which to write Python package and dependency management files. | ||
*/ | ||
protected File baseDir; | ||
|
||
/** | ||
* Logger from calling class to leverage. | ||
*/ | ||
protected Log log; | ||
|
||
/** | ||
* Boolean to check whether to override package version | ||
*/ | ||
protected boolean overridePackageVersion; | ||
|
||
/** | ||
* Expected PackageVersion from pom file. | ||
*/ | ||
protected String expectedPythonPackageVersion; | ||
|
||
|
||
/** | ||
* New instance - these values are typically passed in from Maven-enabled parameters in the calling Mojo. | ||
* | ||
* @param baseDir base directory from which to operate for this module | ||
* @param log the logger to use for output | ||
* @param overridePackageVersion whether we override package version | ||
* @param expectedPythonPackageVersion expected PackageVersion from pom file | ||
*/ | ||
public AbstractInitializeHabushu(File baseDir, Log log, boolean overridePackageVersion, String expectedPythonPackageVersion) { | ||
this.baseDir = baseDir; | ||
this.log = log; | ||
this.overridePackageVersion = overridePackageVersion; | ||
this.expectedPythonPackageVersion = expectedPythonPackageVersion; | ||
} | ||
|
||
public abstract void doExecute() throws MojoExecutionException, MojoFailureException; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...shu-maven-plugin/src/main/java/org/technologybrewery/habushu/InitializeHabushuPoetry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.technologybrewery.habushu; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugin.MojoFailureException; | ||
import org.apache.maven.plugin.logging.Log; | ||
import org.technologybrewery.habushu.exec.PoetryCommandHelper; | ||
|
||
import java.io.File; | ||
import java.util.Arrays; | ||
|
||
public class InitializeHabushuPoetry extends AbstractInitializeHabushu{ | ||
|
||
public InitializeHabushuPoetry( File baseDir, Log log, boolean overridePackageVersion, String expectedPythonPackageVersion) { | ||
super(baseDir, log, overridePackageVersion, expectedPythonPackageVersion); | ||
} | ||
|
||
|
||
@Override | ||
public void doExecute() throws MojoExecutionException, MojoFailureException { | ||
log.info("Validating Poetry-based project structure..."); | ||
PoetryCommandHelper poetryHelper = new PoetryCommandHelper(baseDir); | ||
try { | ||
poetryHelper.execute(Arrays.asList("check")); | ||
} catch (HabushuException e) { | ||
log.debug("Failure encountered while running 'poetry check'!", e); | ||
log.warn("poetry check failed (debug contains more details) - this is likely due to a " | ||
+ "mismatch between your pyproject.toml and poetry.lock file - attempting to correct..."); | ||
poetryHelper.execute(poetryHelper.createLockCommand()); | ||
log.warn("Corrected - pyproject.toml and poetry.lock now synced"); | ||
} | ||
|
||
String currentPythonPackageVersion = poetryHelper.execute(Arrays.asList("version", "-s")); | ||
|
||
if (!StringUtils.equals(currentPythonPackageVersion, expectedPythonPackageVersion)) { | ||
if (overridePackageVersion) { | ||
log.info(String.format("Setting Poetry package version to %s", expectedPythonPackageVersion)); | ||
log.info( | ||
"If you do *not* want the Poetry package version to be automatically synced with the POM version, set <overridePackageVersion>false</overridePackageVersion> in the plugin's <configuration>"); | ||
poetryHelper.executeAndLogOutput(Arrays.asList("version", expectedPythonPackageVersion)); | ||
} else { | ||
log.debug(String.format( | ||
"Poetry package version set to %s in pyproject.toml does not align with expected POM-derived version of %s", | ||
currentPythonPackageVersion, expectedPythonPackageVersion)); | ||
} | ||
|
||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
habushu-maven-plugin/src/main/java/org/technologybrewery/habushu/InitializeHabushuUV.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.technologybrewery.habushu; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugin.logging.Log; | ||
import org.technologybrewery.habushu.exec.UVCommandHelper; | ||
|
||
import java.io.File; | ||
import java.util.Arrays; | ||
|
||
public class InitializeHabushuUV extends AbstractInitializeHabushu{ | ||
|
||
public InitializeHabushuUV( File baseDir, Log log, boolean overridePackageVersion, String expectedPythonPackageVersion) { | ||
super(baseDir, log, overridePackageVersion, expectedPythonPackageVersion); | ||
} | ||
|
||
@Override | ||
public void doExecute() throws MojoExecutionException { | ||
log.info("Validating UV-based project structure..."); | ||
UVCommandHelper uvHelper = new UVCommandHelper(baseDir); | ||
try { | ||
uvHelper.execute(Arrays.asList("lock", "--check")); | ||
} catch (HabushuException e) { | ||
log.debug("Failure encountered while running 'uv lock --check'!", e); | ||
log.warn("uv lock --check failed (debug contains more details) - this is likely due to a " | ||
+ "mismatch between your pyproject.toml and uv.lock file or missing uv.lock file. - attempting to correct..."); | ||
uvHelper.execute(uvHelper.createLockCommand()); | ||
log.warn("Corrected - pyproject.toml and uv.lock now synced"); | ||
} | ||
|
||
String currentPythonPackageVersion = uvHelper.executeUVTool(Arrays.asList("--from=toml-cli", "toml", "get", "--toml-path=pyproject.toml", "project.version")); | ||
if (!StringUtils.equals(currentPythonPackageVersion, expectedPythonPackageVersion)) { | ||
if (overridePackageVersion) { | ||
log.info(String.format("Setting UV package version to %s", expectedPythonPackageVersion)); | ||
log.info( | ||
"If you do *not* want the UV package version to be automatically synced with the POM version, set <overridePackageVersion>false</overridePackageVersion> in the plugin's <configuration>"); | ||
uvHelper.executeUVToolAndLogOutput(Arrays.asList("--from=toml-cli", "toml", "get", "--toml-path=pyproject.toml", "project.version", expectedPythonPackageVersion)); | ||
} else { | ||
log.debug(String.format( | ||
"UV package version set to %s in pyproject.toml does not align with expected POM-derived version of %s", | ||
currentPythonPackageVersion, expectedPythonPackageVersion)); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.