Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/maven/org.apache.maven-maven-plug…
Browse files Browse the repository at this point in the history
…in-api-3.9.8
  • Loading branch information
deweyjose authored Jul 9, 2024
2 parents 0bcc217 + b28128d commit 7862e1e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 36 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ Example

- Type: string
- Required: false
- Default: `${project.basedir}/target/generated-sources`
- Default: `${project.build.directory}/generated-sources`

Example:

Expand All @@ -312,12 +312,24 @@ Example:

- Type: string
- Required: false
- Default: `${project.basedir}/target/generated-examples`
- Default: `${project.build.directory}/generated-examples`

Example:

```xml
<outputDir>${project.build.directory}/generated-examples</outputDir>
<exampleOutputDir>${project.build.directory}/generated-examples</exampleOutputDir>
```

## schemaManifestOutputDir

- Type: string
- Required: false
- Default: `${project.build.directory}/graphqlcodegen`

Example:

```xml
<schemaManifestOutputDir>${project.build.directory}/graphqlcodegen</schemaManifestOutputDir>
```

## includeQueries
Expand Down Expand Up @@ -633,7 +645,6 @@ Add the following to your pom files build/plugins section.
<param>src/main/resources/schema/schema.graphqls</param>
</schemaPaths>
<packageName>com.acme.[your_project].generated</packageName>
<addGeneratedAnnotation>true</addGeneratedAnnotation>
</configuration>
</plugin>
```
Expand Down
13 changes: 9 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>io.github.deweyjose</groupId>
<artifactId>graphqlcodegen-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>1.61.0</version>
<version>1.61.1</version>

<name>GraphQL Code Generator</name>
<description>Maven port of the Netflix DGS GraphQL Codegen gradle build plugin</description>
Expand Down Expand Up @@ -40,13 +40,13 @@
<maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version>
<maven-deploy-plugin.version>3.0.0-M1</maven-deploy-plugin.version>
<maven-gpg-plugin.version>3.2.4</maven-gpg-plugin.version>
<maven-plugin-plugin.version>3.13.0</maven-plugin-plugin.version>
<maven-plugin-plugin.version>3.13.1</maven-plugin-plugin.version>
<maven-plugin-api.version>3.9.8</maven-plugin-api.version>
<maven-plugin-annotations.version>3.13.0</maven-plugin-annotations.version>
<maven-plugin-annotations.version>3.13.1</maven-plugin-annotations.version>
<maven-project.version>2.2.1</maven-project.version>
<maven-release-plugin.version>3.0.0-M4</maven-release-plugin.version>
<maven-source-plugin.version>3.3.1</maven-source-plugin.version>
<maven-javadoc-plugin.version>3.6.3</maven-javadoc-plugin.version>
<maven-javadoc-plugin.version>3.7.0</maven-javadoc-plugin.version>
<maven-scm-provider-gitexe.version>1.11.2</maven-scm-provider-gitexe.version>
<nexus-staging-maven-plugin.version>1.6.13</nexus-staging-maven-plugin.version>
<junit.version>5.10.2</junit.version>
Expand Down Expand Up @@ -94,6 +94,11 @@
<artifactId>logback-classic</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>nu.studer</groupId>
<artifactId>java-ordered-properties</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public class Codegen extends AbstractMojo {
@Parameter(property = "exampleOutputDir", defaultValue = "${project.build.directory}/generated-examples")
private File exampleOutputDir;

@Parameter(property = "schemaManifestOutputDir", defaultValue = "${project.build.directory}/graphqlcodegen")
private File schemaManifestOutputDir;

@Parameter(property = "includeQueries")
private String[] includeQueries;

Expand Down Expand Up @@ -209,7 +212,7 @@ public void execute() {

Set<File> schemaPaths = getSchemaPaths();

SchemaFileManifest manifest = new SchemaFileManifest(new File(outputDir, "schema-manifest.props"));
SchemaFileManifest manifest = new SchemaFileManifest(new File(schemaManifestOutputDir, "schema-manifest.props"), project.getBasedir());

if (onlyGenerateChanged) {
manifest.setFiles(new HashSet<>(schemaPaths));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import nu.studer.java.util.OrderedProperties;
import nu.studer.java.util.OrderedProperties.OrderedPropertiesBuilder;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -11,14 +13,13 @@
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

@Slf4j
public class SchemaFileManifest {
private Set<File> files;
private final Properties manifest;
private final File manifestPath;
private final File projectPath;

/**
* Manifest constructor loads the properties file into memory.
Expand All @@ -27,27 +28,17 @@ public class SchemaFileManifest {
*
* @param files
* @param manifestPath
* @param projectPath
*/
public SchemaFileManifest(Set<File> files, File manifestPath) {
public SchemaFileManifest(Set<File> files, File manifestPath, File projectPath) {
this.files = files;
this.manifestPath = manifestPath;
manifest = loadManifest(manifestPath);
this.projectPath = projectPath;
}

public SchemaFileManifest(File manifestPath) {
public SchemaFileManifest(File manifestPath, File projectPath) {
this.manifestPath = manifestPath;
manifest = loadManifest(manifestPath);
}

@SneakyThrows
public static Properties loadManifest(File manifestPath) {
Properties properties = new Properties();
if (manifestPath.exists()) {
try (FileInputStream fis = new FileInputStream(manifestPath)) {
properties.load(fis);
}
}
return properties;
this.projectPath = projectPath;
}

@SneakyThrows
Expand Down Expand Up @@ -105,8 +96,9 @@ public void setFiles(Set<File> files) {
*/
public Set<File> getChangedFiles() {
Set<File> changed = new HashSet<>();
OrderedProperties manifest = loadManifest();
for (File file : files) {
String oldChecksum = manifest.getProperty(file.getPath());
String oldChecksum = manifest.getProperty(relativizeToProject(file));
if (oldChecksum == null) {
log.info("{} is new, will generate code", file.getName());
} else if (!oldChecksum.equals(generateChecksum(file))) {
Expand All @@ -126,14 +118,37 @@ public Set<File> getChangedFiles() {
*/
@SneakyThrows
public void syncManifest() {
manifest.clear();
OrderedProperties manifest = new OrderedPropertiesBuilder()
.withSuppressDateInComment(true)
.build();
for (File file : files) {
manifest.put(file.getPath(), generateChecksum(file));
manifest.setProperty(relativizeToProject(file), generateChecksum(file));
}

if (!manifestPath.exists()) {
manifestPath.getParentFile().mkdirs();
}

try (FileOutputStream fos = new FileOutputStream(manifestPath)) {
manifest.store(fos, "Schema Manifest");
fos.flush();
}
}

@SneakyThrows
private OrderedProperties loadManifest() {
OrderedProperties properties = new OrderedPropertiesBuilder()
.withSuppressDateInComment(true)
.build();
if (manifestPath.exists()) {
try (FileInputStream fis = new FileInputStream(manifestPath)) {
properties.load(fis);
}
}
return properties;
}

private String relativizeToProject(File file) {
return projectPath.toPath().relativize(file.toPath()).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ void testManifestNoChange() {
File foo = getFile("schema/foo.graphqls");

Properties properties = new Properties();
properties.put(bar.getPath(), "7cada13b5b8770e46f7a69e8856abdb9");
properties.put(foo.getPath(), "61bbd2d58c22dfb3c664829ad116f7e9");
properties.put(tempFolder.relativize(bar.toPath()).toString(), "7cada13b5b8770e46f7a69e8856abdb9");
properties.put(tempFolder.relativize(foo.toPath()).toString(), "61bbd2d58c22dfb3c664829ad116f7e9");

File manifest = tempFolder.resolve("manifest.props").toFile();
try (FileOutputStream fis = new FileOutputStream(manifest)) {
properties.store(fis, "Schema Manifest");
}

SchemaFileManifest sfm = new SchemaFileManifest(new HashSet<>(Arrays.asList(foo, bar)), manifest);
SchemaFileManifest sfm = new SchemaFileManifest(new HashSet<>(Arrays.asList(foo, bar)), manifest, tempFolder.toFile());

Assertions.assertTrue(sfm.getChangedFiles().isEmpty());

sfm.syncManifest();

sfm = new SchemaFileManifest(new HashSet<>(Arrays.asList(foo, bar)), manifest);
sfm = new SchemaFileManifest(new HashSet<>(Arrays.asList(foo, bar)), manifest, tempFolder.toFile());

Assertions.assertTrue(sfm.getChangedFiles().isEmpty());
}
Expand All @@ -79,18 +79,18 @@ void testManifestRequiresChange() {
File foo = getFile("schema/foo.graphqls");
File manifest = tempFolder.resolve("manifest.props").toFile();

SchemaFileManifest sfm = new SchemaFileManifest(new HashSet<>(Arrays.asList(foo, bar)), manifest);
SchemaFileManifest sfm = new SchemaFileManifest(new HashSet<>(Arrays.asList(foo, bar)), manifest, tempFolder.toFile());

Assertions.assertTrue(sfm.getChangedFiles().contains(foo));

sfm.syncManifest();
Assertions.assertTrue(sfm.getChangedFiles().isEmpty());

sfm = new SchemaFileManifest(new HashSet<>(Arrays.asList(foo, bar)), manifest);
sfm = new SchemaFileManifest(new HashSet<>(Arrays.asList(foo, bar)), manifest, tempFolder.toFile());
Assertions.assertTrue(sfm.getChangedFiles().isEmpty());
sfm.syncManifest();

sfm = new SchemaFileManifest(new HashSet<>(Arrays.asList(foo, bar)), manifest);
sfm = new SchemaFileManifest(new HashSet<>(Arrays.asList(foo, bar)), manifest, tempFolder.toFile());
Assertions.assertTrue(sfm.getChangedFiles().isEmpty());
}

Expand Down

0 comments on commit 7862e1e

Please sign in to comment.