Skip to content

Commit

Permalink
fix: Watch uses same logic as build to monitor changed assembly files
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa committed Jun 29, 2020
1 parent bad7145 commit 019d1c1
Show file tree
Hide file tree
Showing 21 changed files with 420 additions and 354 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.eclipse.jkube.kit.common.AssemblyFileSet;
import org.eclipse.jkube.kit.common.Assembly;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand All @@ -35,7 +37,8 @@ class AssemblyConfigurationUtils {

private AssemblyConfigurationUtils() {}

static AssemblyConfiguration getAssemblyConfigurationOrCreateDefault(BuildConfiguration buildConfiguration) {
@Nonnull
static AssemblyConfiguration getAssemblyConfigurationOrCreateDefault(@Nullable BuildConfiguration buildConfiguration) {
final AssemblyConfiguration ac = Optional.ofNullable(buildConfiguration)
.map(BuildConfiguration::getAssembly)
.orElse(AssemblyConfiguration.builder().user(DEFAULT_USER).build());
Expand All @@ -53,14 +56,16 @@ static AssemblyConfiguration getAssemblyConfigurationOrCreateDefault(BuildConfig
return builder.build();
}

static List<AssemblyFileSet> getJKubeAssemblyFileSets(AssemblyConfiguration configuration) {
@Nonnull
static List<AssemblyFileSet> getJKubeAssemblyFileSets(@Nullable AssemblyConfiguration configuration) {
return Optional.ofNullable(configuration)
.map(AssemblyConfiguration::getInline)
.map(Assembly::getFileSets)
.orElse(Collections.emptyList());
}

static List<String> getJKubeAssemblyFileSetsExcludes(AssemblyConfiguration assemblyConfiguration) {
@Nonnull
static List<String> getJKubeAssemblyFileSetsExcludes(@Nullable AssemblyConfiguration assemblyConfiguration) {
return getJKubeAssemblyFileSets(assemblyConfiguration).stream()
.filter(Objects::nonNull)
.map(AssemblyFileSet::getExcludes)
Expand All @@ -70,6 +75,7 @@ static List<String> getJKubeAssemblyFileSetsExcludes(AssemblyConfiguration assem
.collect(Collectors.toList());
}

@Nonnull
static List<AssemblyFile> getJKubeAssemblyFiles(AssemblyConfiguration configuration) {
return Optional.ofNullable(configuration)
.map(AssemblyConfiguration::getInline)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
*/
package org.eclipse.jkube.kit.build.core.assembly;

import org.eclipse.jkube.kit.common.AssemblyFileEntry;

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

/**
* Collection of assembly files which need to be monitored for checking when
Expand All @@ -26,7 +29,7 @@
public class AssemblyFiles {

private final File assemblyDirectory;
private List<Entry> entries = new ArrayList<>();
private List<AssemblyFileEntry> entries = new ArrayList<>();

/**
* Create a collection of assembly files
Expand All @@ -40,28 +43,21 @@ public AssemblyFiles(File assemblyDirectory) {
/**
* Add a entry to the list of assembly files which possible should be monitored
*
* @param srcFile source file to monitor. The source file must exist.
* @param destFile the destination to which it is eventually copied. The destination file must be relative.
* @param assemblyFileEntry to monitor.
*/
public void addEntry(File srcFile, File destFile) {
entries.add(new Entry(srcFile, destFile));
public void addEntry(AssemblyFileEntry assemblyFileEntry) {
entries.add(assemblyFileEntry);
}

/**
* Get the list of all updated entries i.e. all entries which have modification date
* which is newer than the last time check. ATTENTION: As a side effect this method also
* updates the timestamp of entries.
* updates the timestamp of updated entries.
*
* @return list of all entries which has been updated since the last call to this method or an empty list
*/
public List<Entry> getUpdatedEntriesAndRefresh() {
List<Entry> ret = new ArrayList<>();
for (Entry entry : entries) {
if (entry.isUpdated()) {
ret.add(entry);
}
}
return ret;
public List<AssemblyFileEntry> getUpdatedEntriesAndRefresh() {
return entries.stream().filter(AssemblyFileEntry::isUpdated).collect(Collectors.toList());
}

/**
Expand All @@ -82,49 +78,4 @@ public File getAssemblyDirectory() {
return assemblyDirectory;
}

// ===============================================================================
// Inner class remembering the modification date of a source file and its destination

public static class Entry {

private long lastModified;
private File srcFile;
private File destFile;

private Entry(File srcFile, File destFile) {
this.srcFile = srcFile;
this.destFile = destFile;
if (!srcFile.exists()) {
throw new IllegalArgumentException("Source " + srcFile + " does not exist");
}
if (!destFile.exists()) {
throw new IllegalArgumentException("Destination " + destFile + " does not exist");
}
if (srcFile.isDirectory()) {
throw new IllegalArgumentException("Can only watch files, not directories: " + srcFile);
}
this.lastModified = this.srcFile.lastModified();
}

public File getSrcFile() {
return srcFile;
}

/**
* @return destination file which is absolute (and withing AssemblyFiles.assemblyDirectory)
*/
public File getDestFile() {
return destFile;
}

boolean isUpdated() {
if (srcFile.lastModified() > lastModified) {
// Update last modified as a side effect
lastModified = srcFile.lastModified();
return true;
} else {
return false;
}
}
}
}
Loading

0 comments on commit 019d1c1

Please sign in to comment.