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

Remove volatility from schema manifest file #160

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 15 additions & 3 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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
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