Skip to content

Commit 0b731d4

Browse files
committed
fix: fixed sync issue and path command
1 parent ee3877e commit 0b731d4

File tree

4 files changed

+46
-24
lines changed

4 files changed

+46
-24
lines changed

src/main/java/org/codejive/jpm/Jpm.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ public Jpm build() {
8686
*
8787
* @param artifactNames The artifacts to copy.
8888
* @param sync Whether to sync the target directory or not.
89-
* @return An instance of {@link SyncStats} containing the statistics of the copy operation.
89+
* @return An instance of {@link SyncResult} containing the statistics of the copy operation.
9090
* @throws IOException If an error occurred during the copy operation.
9191
* @throws DependencyResolutionException If an error occurred during the dependency resolution.
9292
*/
93-
public SyncStats copy(String[] artifactNames, boolean sync)
93+
public SyncResult copy(String[] artifactNames, boolean sync)
9494
throws IOException, DependencyResolutionException {
9595
return copy(artifactNames, Collections.emptyMap(), sync);
9696
}
@@ -101,11 +101,11 @@ public SyncStats copy(String[] artifactNames, boolean sync)
101101
* @param artifactNames The artifacts to copy.
102102
* @param repos A map of additional repository names to URLs where artifacts can be found.
103103
* @param sync Whether to sync the target directory or not.
104-
* @return An instance of {@link SyncStats} containing the statistics of the copy operation.
104+
* @return An instance of {@link SyncResult} containing the statistics of the copy operation.
105105
* @throws IOException If an error occurred during the copy operation.
106106
* @throws DependencyResolutionException If an error occurred during the dependency resolution.
107107
*/
108-
public SyncStats copy(String[] artifactNames, Map<String, String> repos, boolean sync)
108+
public SyncResult copy(String[] artifactNames, Map<String, String> repos, boolean sync)
109109
throws IOException, DependencyResolutionException {
110110
List<Path> files = Resolver.create(artifactNames, repos).resolvePaths();
111111
return FileUtils.syncArtifacts(files, directory, noLinks, !sync);
@@ -141,11 +141,11 @@ private static String artifactGav(Artifact artifact) {
141141
* basically means sync-copying the artifacts to the target directory.
142142
*
143143
* @param artifactNames The artifacts to install.
144-
* @return An instance of {@link SyncStats} containing the statistics of the install operation.
144+
* @return An instance of {@link SyncResult} containing the statistics of the install operation.
145145
* @throws IOException If an error occurred during the install operation.
146146
* @throws DependencyResolutionException If an error occurred during the dependency resolution.
147147
*/
148-
public SyncStats install(String[] artifactNames)
148+
public SyncResult install(String[] artifactNames)
149149
throws IOException, DependencyResolutionException {
150150
return install(artifactNames, Collections.emptyMap());
151151
}
@@ -158,18 +158,18 @@ public SyncStats install(String[] artifactNames)
158158
*
159159
* @param artifactNames The artifacts to install.
160160
* @param extraRepos A map of additional repository names to URLs where artifacts can be found.
161-
* @return An instance of {@link SyncStats} containing the statistics of the install operation.
161+
* @return An instance of {@link SyncResult} containing the statistics of the install operation.
162162
* @throws IOException If an error occurred during the install operation.
163163
* @throws DependencyResolutionException If an error occurred during the dependency resolution.
164164
*/
165-
public SyncStats install(String[] artifactNames, Map<String, String> extraRepos)
165+
public SyncResult install(String[] artifactNames, Map<String, String> extraRepos)
166166
throws IOException, DependencyResolutionException {
167167
AppInfo appInfo = AppInfo.read();
168168
String[] artifacts = getArtifacts(artifactNames, appInfo);
169169
Map<String, String> repos = getRepositories(extraRepos, appInfo);
170170
if (artifacts.length > 0) {
171171
List<Path> files = Resolver.create(artifacts, repos).resolvePaths();
172-
SyncStats stats = FileUtils.syncArtifacts(files, directory, noLinks, true);
172+
SyncResult stats = FileUtils.syncArtifacts(files, directory, noLinks, true);
173173
if (artifactNames.length > 0) {
174174
for (String dep : artifactNames) {
175175
int p = dep.lastIndexOf(':');
@@ -182,7 +182,7 @@ public SyncStats install(String[] artifactNames, Map<String, String> extraRepos)
182182
}
183183
return stats;
184184
} else {
185-
return new SyncStats();
185+
return new SyncResult();
186186
}
187187
}
188188

@@ -216,7 +216,13 @@ public List<Path> path(String[] artifactNames, Map<String, String> extraRepos)
216216
String[] deps = getArtifacts(artifactNames, appInfo);
217217
Map<String, String> repos = getRepositories(extraRepos, appInfo);
218218
if (deps.length > 0) {
219-
return Resolver.create(deps, repos).resolvePaths();
219+
List<Path> files = Resolver.create(deps, repos).resolvePaths();
220+
if (artifactNames.length > 0) {
221+
return files;
222+
} else {
223+
SyncResult result = FileUtils.syncArtifacts(files, directory, noLinks, true);
224+
return result.files;
225+
}
220226
} else {
221227
return Collections.emptyList();
222228
}

src/main/java/org/codejive/jpm/Main.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//DEPS org.jline:jline-console-ui:3.30.5 org.jline:jline-terminal-jni:3.30.5
66
//DEPS org.slf4j:slf4j-api:2.0.17 org.slf4j:slf4j-simple:2.0.17
77
//SOURCES Jpm.java config/AppInfo.java util/CommandsParser.java util/FileUtils.java util/Resolver.java
8-
//SOURCES util/ScriptUtils.java util/SearchResult.java util/SearchUtils.java util/SyncStats.java util/Version.java
8+
//SOURCES util/ScriptUtils.java util/SearchResult.java util/SearchUtils.java util/SyncResult.java util/Version.java
99
// spotless:on
1010

1111
package org.codejive.jpm;
@@ -17,7 +17,7 @@
1717
import java.util.*;
1818
import java.util.concurrent.Callable;
1919
import java.util.stream.Collectors;
20-
import org.codejive.jpm.util.SyncStats;
20+
import org.codejive.jpm.util.SyncResult;
2121
import org.codejive.jpm.util.Version;
2222
import org.jline.consoleui.elements.InputValue;
2323
import org.jline.consoleui.elements.ListChoice;
@@ -78,7 +78,7 @@ static class Copy implements Callable<Integer> {
7878

7979
@Override
8080
public Integer call() throws Exception {
81-
SyncStats stats =
81+
SyncResult stats =
8282
Jpm.builder()
8383
.directory(artifactsMixin.depsMixin.directory)
8484
.noLinks(artifactsMixin.depsMixin.noLinks)
@@ -142,7 +142,7 @@ public Integer call() throws Exception {
142142
String selectedArtifact = getSelectedId(result, "item");
143143
String artifactAction = getSelectedId(result, "action");
144144
if ("install".equals(artifactAction)) {
145-
SyncStats stats =
145+
SyncResult stats =
146146
Jpm.builder()
147147
.directory(depsMixin.directory)
148148
.noLinks(depsMixin.noLinks)
@@ -152,7 +152,7 @@ public Integer call() throws Exception {
152152
printStats(stats);
153153
}
154154
} else if ("copy".equals(artifactAction)) {
155-
SyncStats stats =
155+
SyncResult stats =
156156
Jpm.builder()
157157
.directory(depsMixin.directory)
158158
.noLinks(depsMixin.noLinks)
@@ -286,7 +286,7 @@ static class Install implements Callable<Integer> {
286286

287287
@Override
288288
public Integer call() throws Exception {
289-
SyncStats stats =
289+
SyncResult stats =
290290
Jpm.builder()
291291
.directory(optionalArtifactsMixin.depsMixin.directory)
292292
.noLinks(optionalArtifactsMixin.depsMixin.noLinks)
@@ -627,7 +627,7 @@ static class QuietMixin {
627627
private boolean quiet;
628628
}
629629

630-
private static void printStats(SyncStats stats) {
630+
private static void printStats(SyncResult stats) {
631631
System.err.printf(
632632
"Artifacts new: %d, updated: %d, deleted: %d%n",
633633
(Integer) stats.copied, (Integer) stats.updated, (Integer) stats.deleted);

src/main/java/org/codejive/jpm/util/FileUtils.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ public class FileUtils {
2121
* @param directory target directory
2222
* @param noLinks if true, copy artifacts instead of creating symbolic links
2323
* @param noDelete if true, do not delete artifacts that are no longer needed
24-
* @return An instance of {@link SyncStats} with statistics about the synchronization
24+
* @return An instance of {@link SyncResult} with statistics about the synchronization
2525
* @throws IOException if an error occurred during the synchronization
2626
*/
27-
public static SyncStats syncArtifacts(
27+
public static SyncResult syncArtifacts(
2828
List<Path> artifacts, Path directory, boolean noLinks, boolean noDelete)
2929
throws IOException {
30-
SyncStats stats = new SyncStats();
30+
SyncResult stats = new SyncResult();
3131

3232
// Make sure the target directory exists
3333
Files.createDirectories(directory);
@@ -46,14 +46,20 @@ public static SyncStats syncArtifacts(
4646
// Copy artifacts
4747
for (Path artifact : artifacts) {
4848
String artifactName = artifact.getFileName().toString();
49+
artifactsToDelete.remove(artifactName);
4950
Path target = directory.resolve(artifactName);
51+
stats.files.add(target);
5052
if (!Files.exists(target)) {
5153
copyDependency(artifact, directory, noLinks);
52-
artifactsToDelete.remove(artifactName);
5354
stats.copied++;
5455
} else if (Files.isSymbolicLink(target) == noLinks) {
5556
copyDependency(artifact, directory, noLinks);
5657
stats.updated++;
58+
} else if (Files.size(target) != Files.size(artifact)
59+
|| !Files.getLastModifiedTime(target)
60+
.equals(Files.getLastModifiedTime(artifact))) {
61+
copyDependency(artifact, directory, noLinks);
62+
stats.updated++;
5763
}
5864
}
5965

@@ -82,7 +88,11 @@ private static void copyDependency(Path artifact, Path directory, boolean noLink
8288
// fall through and try again by simply copying the file
8389
}
8490
}
85-
Files.copy(artifact, target, StandardCopyOption.REPLACE_EXISTING);
91+
Files.copy(
92+
artifact,
93+
target,
94+
StandardCopyOption.REPLACE_EXISTING,
95+
StandardCopyOption.COPY_ATTRIBUTES);
8696
}
8797

8898
public static Path safePath(String w) {

src/main/java/org/codejive/jpm/util/SyncStats.java renamed to src/main/java/org/codejive/jpm/util/SyncResult.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package org.codejive.jpm.util;
22

3+
import java.nio.file.Path;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
37
/** Utility class for keeping track of synchronization statistics. */
4-
public class SyncStats {
8+
public class SyncResult {
9+
public List<Path> files = new ArrayList<>();
10+
511
/** The number of new artifacts that were copied. */
612
public int copied;
713

0 commit comments

Comments
 (0)