You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2018/04/19 14:32:28 UTC

[sling-ide-tooling] 01/04: SLING-7587 - Create a CLI-only tool to sync content

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-ide-tooling.git

commit 72978e5d2240fec3a9797b22c1e388a2fa892cbf
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Tue Apr 17 15:09:03 2018 +0300

    SLING-7587 - Create a CLI-only tool to sync content
    
    Improve handling of non-existing resources.
---
 ...rkspacePaths.java => NonExistingResources.java} | 20 +++---
 .../sling/ide/sync/content/WorkspacePaths.java     |  5 ++
 .../sync/content/impl/NonExistingDirectory.java}   | 56 ++++++++-------
 .../ide/sync/content/impl/NonExistingFile.java     | 84 ++++++++++++++++++++++
 .../content/sync/fs/impl/FSWorkspaceDirectory.java | 13 +++-
 .../content/sync/fs/impl/FSWorkspaceProject.java   |  7 +-
 .../sync/fs/impl/FSWorkspaceDirectoryTest.java     | 14 ++++
 .../sync/fs/impl/FSWorkspaceProjectTest.java       | 16 +++++
 8 files changed, 176 insertions(+), 39 deletions(-)

diff --git a/shared/modules/api/src/main/java/org/apache/sling/ide/sync/content/WorkspacePaths.java b/shared/modules/api/src/main/java/org/apache/sling/ide/sync/content/NonExistingResources.java
similarity index 61%
copy from shared/modules/api/src/main/java/org/apache/sling/ide/sync/content/WorkspacePaths.java
copy to shared/modules/api/src/main/java/org/apache/sling/ide/sync/content/NonExistingResources.java
index 1f745f4..0162433 100644
--- a/shared/modules/api/src/main/java/org/apache/sling/ide/sync/content/WorkspacePaths.java
+++ b/shared/modules/api/src/main/java/org/apache/sling/ide/sync/content/NonExistingResources.java
@@ -16,22 +16,20 @@
  */
 package org.apache.sling.ide.sync.content;
 
-import java.io.File;
-import java.nio.file.Path;
+import org.apache.sling.ide.sync.content.impl.NonExistingDirectory;
+import org.apache.sling.ide.sync.content.impl.NonExistingFile;
 
 /**
- * 
- * Holds utility methods related to path instances
+ * Factory class to aid in the construction of non-existing resources
  *
  */
-public abstract class WorkspacePaths {
+public abstract class NonExistingResources {
 
-    public static WorkspacePath fromOsPath(Path path) {
-        
-        return new WorkspacePath(path.toString().replace(File.separatorChar, '/'));
+    public static WorkspaceDirectory newDirectory(WorkspacePath path, WorkspaceProject project) {
+        return new NonExistingDirectory(path, project);
     }
-    
-    private WorkspacePaths() {
-        
+
+    public static WorkspaceFile newFile(WorkspacePath path, WorkspaceDirectory parent) {
+        return new NonExistingFile(path, parent);
     }
 }
diff --git a/shared/modules/api/src/main/java/org/apache/sling/ide/sync/content/WorkspacePaths.java b/shared/modules/api/src/main/java/org/apache/sling/ide/sync/content/WorkspacePaths.java
index 1f745f4..62263f4 100644
--- a/shared/modules/api/src/main/java/org/apache/sling/ide/sync/content/WorkspacePaths.java
+++ b/shared/modules/api/src/main/java/org/apache/sling/ide/sync/content/WorkspacePaths.java
@@ -18,6 +18,7 @@ package org.apache.sling.ide.sync.content;
 
 import java.io.File;
 import java.nio.file.Path;
+import java.nio.file.Paths;
 
 /**
  * 
@@ -31,6 +32,10 @@ public abstract class WorkspacePaths {
         return new WorkspacePath(path.toString().replace(File.separatorChar, '/'));
     }
     
+    public static Path toOsPath(WorkspacePath path) {
+        return Paths.get(path.asPortableString().replace('/', File.separatorChar));
+    }
+    
     private WorkspacePaths() {
         
     }
diff --git a/shared/modules/sync-fs/src/main/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceDirectory.java b/shared/modules/api/src/main/java/org/apache/sling/ide/sync/content/impl/NonExistingDirectory.java
similarity index 58%
copy from shared/modules/sync-fs/src/main/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceDirectory.java
copy to shared/modules/api/src/main/java/org/apache/sling/ide/sync/content/impl/NonExistingDirectory.java
index 91af787..70412cf 100644
--- a/shared/modules/sync-fs/src/main/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceDirectory.java
+++ b/shared/modules/api/src/main/java/org/apache/sling/ide/sync/content/impl/NonExistingDirectory.java
@@ -14,28 +14,36 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.sling.ide.content.sync.fs.impl;
+package org.apache.sling.ide.sync.content.impl;
 
-import java.io.File;
-import java.util.Arrays;
+import java.nio.file.Path;
 import java.util.List;
-import java.util.stream.Collectors;
 
 import org.apache.sling.ide.sync.content.WorkspaceDirectory;
 import org.apache.sling.ide.sync.content.WorkspaceFile;
 import org.apache.sling.ide.sync.content.WorkspacePath;
+import org.apache.sling.ide.sync.content.WorkspacePaths;
 import org.apache.sling.ide.sync.content.WorkspaceProject;
 import org.apache.sling.ide.sync.content.WorkspaceResource;
 
-public class FSWorkspaceDirectory extends FSWorkspaceResource implements WorkspaceDirectory {
+public class NonExistingDirectory implements WorkspaceDirectory {
 
-    private final FSWorkspaceProject project;
     private final WorkspacePath path;
-
-    public FSWorkspaceDirectory(File dir, FSWorkspaceProject project) {
-        super(dir, true);
+    private final WorkspaceProject project;
+    
+    public NonExistingDirectory(WorkspacePath path, WorkspaceProject project) {
+        this.path = path;
         this.project = project;
-        this.path = getPath(project, dir);
+    }
+
+    @Override
+    public boolean exists() {
+        return false;
+    }
+
+    @Override
+    public boolean isIgnored() {
+        return false;
     }
 
     @Override
@@ -44,38 +52,38 @@ public class FSWorkspaceDirectory extends FSWorkspaceResource implements Workspa
     }
 
     @Override
+    public Path getOSPath() {
+        return WorkspacePaths.toOsPath(path);
+    }
+
+    @Override
     public WorkspaceProject getProject() {
         return project;
     }
 
     @Override
+    public long getLastModified() {
+        throw new IllegalArgumentException("Directory at " + path + " does not exist");
+    }
+
+    @Override
     public Object getTransientProperty(String propertyName) {
-        // TODO Auto-generated method stub
-        return null;
+        throw new IllegalArgumentException("Directory at " + path + " does not exist");
     }
 
     @Override
     public WorkspaceFile getFile(WorkspacePath relativePath) {
-        return new FSWorkspaceFile(new File(backingFile(), relativePath.asPortableString().replace('/', File.separatorChar)), project);
+        throw new IllegalArgumentException("Directory at " + path + " does not exist");
     }
 
     @Override
     public WorkspaceDirectory getDirectory(WorkspacePath relativePath) {
-        return new FSWorkspaceDirectory(new File(backingFile(), relativePath.asPortableString().replace('/', File.separatorChar)), project);
+        throw new IllegalArgumentException("Directory at " + path + " does not exist");
     }
 
     @Override
     public List<WorkspaceResource> getChildren() {
-        return Arrays.stream(backingFile().listFiles())
-                .map( f -> {
-                    if ( f.isFile() )
-                        return new FSWorkspaceFile(f, project);
-                    else if ( f.isDirectory() )
-                        return new FSWorkspaceDirectory(f, project);
-                    else return null;
-                })
-                .filter( r -> r != null)
-                .collect(Collectors.toList());
+        throw new IllegalArgumentException("Directory at " + path + " does not exist");
     }
 
 }
diff --git a/shared/modules/api/src/main/java/org/apache/sling/ide/sync/content/impl/NonExistingFile.java b/shared/modules/api/src/main/java/org/apache/sling/ide/sync/content/impl/NonExistingFile.java
new file mode 100644
index 0000000..8bbf2c1
--- /dev/null
+++ b/shared/modules/api/src/main/java/org/apache/sling/ide/sync/content/impl/NonExistingFile.java
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.ide.sync.content.impl;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Path;
+
+import org.apache.sling.ide.sync.content.WorkspaceDirectory;
+import org.apache.sling.ide.sync.content.WorkspaceFile;
+import org.apache.sling.ide.sync.content.WorkspacePath;
+import org.apache.sling.ide.sync.content.WorkspacePaths;
+import org.apache.sling.ide.sync.content.WorkspaceProject;
+
+public class NonExistingFile implements WorkspaceFile {
+    
+    private final WorkspacePath path;
+    private final WorkspaceDirectory parent;
+
+    public NonExistingFile(WorkspacePath path, WorkspaceDirectory parent) {
+        this.path = path;
+        this.parent = parent;
+    }
+
+    @Override
+    public boolean exists() {
+        return false;
+    }
+
+    @Override
+    public boolean isIgnored() {
+        return false;
+    }
+
+    @Override
+    public WorkspacePath getLocalPath() {
+        return path;
+    }
+
+    @Override
+    public Path getOSPath() {
+        return WorkspacePaths.toOsPath(path);
+    }
+
+    @Override
+    public WorkspaceProject getProject() {
+        return parent.getProject();
+    }
+
+    @Override
+    public long getLastModified() {
+        throw new IllegalArgumentException("File at " + path + " does not exist");
+    }
+
+    @Override
+    public Object getTransientProperty(String propertyName) {
+        throw new IllegalArgumentException("File at " + path + " does not exist");
+    }
+
+    @Override
+    public InputStream getContents() throws IOException {
+        throw new IllegalArgumentException("File at " + path + " does not exist");
+    }
+
+    @Override
+    public WorkspaceDirectory getParent() {
+        return parent;
+    }
+
+}
diff --git a/shared/modules/sync-fs/src/main/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceDirectory.java b/shared/modules/sync-fs/src/main/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceDirectory.java
index 91af787..a80d7bc 100644
--- a/shared/modules/sync-fs/src/main/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceDirectory.java
+++ b/shared/modules/sync-fs/src/main/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceDirectory.java
@@ -21,9 +21,11 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.stream.Collectors;
 
+import org.apache.sling.ide.sync.content.NonExistingResources;
 import org.apache.sling.ide.sync.content.WorkspaceDirectory;
 import org.apache.sling.ide.sync.content.WorkspaceFile;
 import org.apache.sling.ide.sync.content.WorkspacePath;
+import org.apache.sling.ide.sync.content.WorkspacePaths;
 import org.apache.sling.ide.sync.content.WorkspaceProject;
 import org.apache.sling.ide.sync.content.WorkspaceResource;
 
@@ -50,18 +52,23 @@ public class FSWorkspaceDirectory extends FSWorkspaceResource implements Workspa
 
     @Override
     public Object getTransientProperty(String propertyName) {
-        // TODO Auto-generated method stub
         return null;
     }
 
     @Override
     public WorkspaceFile getFile(WorkspacePath relativePath) {
-        return new FSWorkspaceFile(new File(backingFile(), relativePath.asPortableString().replace('/', File.separatorChar)), project);
+        final File osFile = new File(backingFile(), WorkspacePaths.toOsPath(relativePath).toString());
+        if ( !osFile.isFile() )
+            return NonExistingResources.newFile(getLocalPath().append(relativePath), this);
+        return new FSWorkspaceFile(osFile, project);
     }
 
     @Override
     public WorkspaceDirectory getDirectory(WorkspacePath relativePath) {
-        return new FSWorkspaceDirectory(new File(backingFile(), relativePath.asPortableString().replace('/', File.separatorChar)), project);
+        final File osFile = new File(backingFile(), WorkspacePaths.toOsPath(relativePath).toString());
+        if ( !osFile.isDirectory() )
+            return NonExistingResources.newDirectory(getLocalPath().append(relativePath), project);
+        return new FSWorkspaceDirectory(osFile, project);
     }
 
     @Override
diff --git a/shared/modules/sync-fs/src/main/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceProject.java b/shared/modules/sync-fs/src/main/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceProject.java
index a15385d..43a4503 100644
--- a/shared/modules/sync-fs/src/main/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceProject.java
+++ b/shared/modules/sync-fs/src/main/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceProject.java
@@ -23,8 +23,10 @@ import java.io.InputStream;
 
 import org.apache.sling.ide.filter.Filter;
 import org.apache.sling.ide.filter.FilterLocator;
+import org.apache.sling.ide.sync.content.NonExistingResources;
 import org.apache.sling.ide.sync.content.WorkspaceDirectory;
 import org.apache.sling.ide.sync.content.WorkspacePath;
+import org.apache.sling.ide.sync.content.WorkspacePaths;
 import org.apache.sling.ide.sync.content.WorkspaceProject;
 
 public class FSWorkspaceProject extends FSWorkspaceResource implements WorkspaceProject {
@@ -80,7 +82,10 @@ public class FSWorkspaceProject extends FSWorkspaceResource implements Workspace
 
     @Override
     public WorkspaceDirectory getDirectory(WorkspacePath path) {
-        return new FSWorkspaceDirectory(new File(backingFile(), path.asPortableString().replace('/', File.separatorChar)), this);
+        final File osFile = new File(backingFile(), WorkspacePaths.toOsPath(path).toString());
+        if ( !osFile.isDirectory() )
+            return NonExistingResources.newDirectory(getLocalPath().append(path), this);
+        return new FSWorkspaceDirectory(osFile, this);
     }
 
 }
diff --git a/shared/modules/sync-fs/src/test/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceDirectoryTest.java b/shared/modules/sync-fs/src/test/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceDirectoryTest.java
index f577400..0c9f242 100644
--- a/shared/modules/sync-fs/src/test/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceDirectoryTest.java
+++ b/shared/modules/sync-fs/src/test/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceDirectoryTest.java
@@ -117,6 +117,13 @@ public class FSWorkspaceDirectoryTest {
     }
     
     @Test
+    public void getFile_isDirectory() {
+        WorkspaceFile file = dir.getFile(new WorkspacePath("dir"));
+        assertThat("file.exists", file.exists(), equalTo(false));
+        assertThat("file.localPath", file.getLocalPath(), equalTo(new WorkspacePath("/test-project/jcr_root/dir")));
+    }
+    
+    @Test
     public void getDir_exists() {
         WorkspaceDirectory directory = dir.getDirectory(new WorkspacePath("dir"));
         assertThat("directory.exists", directory.exists(), equalTo(true));
@@ -129,4 +136,11 @@ public class FSWorkspaceDirectoryTest {
         assertThat("directory.exists", directory.exists(), equalTo(false));
         assertThat("file.localPath", directory.getLocalPath(), equalTo(new WorkspacePath("/test-project/jcr_root/does-not-exist")));
     }
+    
+    @Test
+    public void getDir_isFile() {
+        WorkspaceDirectory directory = dir.getDirectory(new WorkspacePath("file.txt"));
+        assertThat("directory.exists", directory.exists(), equalTo(false));
+        assertThat("file.localPath", directory.getLocalPath(), equalTo(new WorkspacePath("/test-project/jcr_root/file.txt")));
+    }
 }
diff --git a/shared/modules/sync-fs/src/test/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceProjectTest.java b/shared/modules/sync-fs/src/test/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceProjectTest.java
index 127a351..5e90eb7 100644
--- a/shared/modules/sync-fs/src/test/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceProjectTest.java
+++ b/shared/modules/sync-fs/src/test/java/org/apache/sling/ide/content/sync/fs/impl/FSWorkspaceProjectTest.java
@@ -106,4 +106,20 @@ public class FSWorkspaceProjectTest {
 
         assertThat("filter", project.getFilter(), equalTo(MockFilterLocator.MOCK_FILTER));
     }
+    
+    @Test
+    public void getDirectory_missing() {
+        
+        WorkspaceDirectory directory = project.getDirectory(new WorkspacePath("missing"));
+        assertThat("directory.exists", directory.exists(), equalTo(false));
+        assertThat("directory.localPath", directory.getLocalPath(), equalTo(new WorkspacePath("/test-project/missing")));
+    }
+    
+    @Test
+    public void getDirectory_file() {
+        
+        WorkspaceDirectory directory = project.getDirectory(new WorkspacePath("content/META-INF/vault/filter.xml"));
+        assertThat("directory.exists", directory.exists(), equalTo(false));
+        assertThat("directory.localPath", directory.getLocalPath(), equalTo(new WorkspacePath("/test-project/content/META-INF/vault/filter.xml")));
+    }   
 }

-- 
To stop receiving notification emails like this one, please contact
rombert@apache.org.