Skip to content

Commit

Permalink
Merge pull request #6070 from pkriens/feature/newworkspace
Browse files Browse the repository at this point in the history
Feature/newworkspace
  • Loading branch information
pkriens committed Apr 4, 2024
2 parents b1a3408 + 7157ee6 commit c9cb862
Show file tree
Hide file tree
Showing 30 changed files with 2,633 additions and 99 deletions.
71 changes: 30 additions & 41 deletions aQute.libg/src/aQute/lib/io/IO.java
Expand Up @@ -123,6 +123,16 @@ interface OS {
* @return a safe file name
*/
String toSafeFileName(String name);

/**
* Return a file from a base. The file must use forward slash but may
* start on windows with c:/... or /c:/ to indicate a drive.
*
* @param base the base to resolve the file from
* @param file the path
* @return a file
*/
File getFile(File base, String file);
}

final static OS os = File.separatorChar == '\\' ? new Windows() : new Other();
Expand Down Expand Up @@ -840,47 +850,7 @@ public static File getFile(String file) {
}

public static File getFile(File base, String file) {
StringRover rover = new StringRover(file);
if (rover.startsWith("~/")) {
rover.increment(2);
if (!rover.startsWith("~/")) {
return getFile(home, rover.substring(0));
}
}
if (rover.startsWith("~")) {
return getFile(home.getParentFile(), rover.substring(1));
}

File f = new File(rover.substring(0));
if (f.isAbsolute()) {
return f;
}

if (base == null) {
base = work;
}

for (f = base.getAbsoluteFile(); !rover.isEmpty();) {
int n = rover.indexOf('/');
if (n < 0) {
n = rover.length();
}
if ((n == 0) || ((n == 1) && (rover.charAt(0) == '.'))) {
// case "" or "."
} else if ((n == 2) && (rover.charAt(0) == '.') && (rover.charAt(1) == '.')) {
// case ".."
File parent = f.getParentFile();
if (parent != null) {
f = parent;
}
} else {
String segment = rover.substring(0, n);
f = new File(f, segment);
}
rover.increment(n + 1);
}

return f.getAbsoluteFile();
return os.getFile(base, file);
}

/**
Expand Down Expand Up @@ -1794,4 +1764,23 @@ public static String getJavaExecutablePath(String name) {
return name;
}

/**
* Create a new unique file name in the given folder
*
* @param folder the folder to create a File in
* @param stem the name stem, "untitled-" if null
* @return a file in the folder that does not exist
*/
public static File unique(File folder, String stem) {
if (stem == null)
stem = "untitled-";
int n = 0;
while (true) {
File f = new File(folder, stem + n);
if (!f.exists())
return f;
n++;
}
}

}
18 changes: 18 additions & 0 deletions aQute.libg/src/aQute/lib/io/NullAppendable.java
@@ -0,0 +1,18 @@
package aQute.lib.io;
public class NullAppendable implements Appendable {

@Override
public Appendable append(CharSequence csq) {
return this;
}

@Override
public Appendable append(CharSequence csq, int start, int end) {
return this;
}

@Override
public Appendable append(char c) {
return this;
}
}
51 changes: 51 additions & 0 deletions aQute.libg/src/aQute/lib/io/Other.java
Expand Up @@ -5,6 +5,7 @@
import java.nio.file.Path;

import aQute.lib.io.IO.OS;
import aQute.lib.stringrover.StringRover;

class Other implements OS {

Expand Down Expand Up @@ -46,4 +47,54 @@ public String toSafeFileName(String string) {
}
return sb.toString();
}

@Override
public File getFile(File base, String file) {
return getFile0(base, file);
}

static File getFile0(File base, String path) {
StringRover rover = new StringRover(path);
if (rover.startsWith("~/")) {
rover.increment(2);
if (!rover.startsWith("~/")) {
return getFile0(IO.home, rover.substring(0));
}
}
if (rover.startsWith("~")) {
return getFile0(IO.home.getParentFile(), rover.substring(1));
}

File f = new File(rover.substring(0));
if (f.isAbsolute()) {
return f;
}

if (base == null) {
base = IO.work;
}

for (f = base.getAbsoluteFile(); !rover.isEmpty();) {
int n = rover.indexOf('/');
if (n < 0) {
n = rover.length();
}
if ((n == 0) || ((n == 1) && (rover.charAt(0) == '.'))) {
// case "" or "."
} else if ((n == 2) && (rover.charAt(0) == '.') && (rover.charAt(1) == '.')) {
// case ".."
File parent = f.getParentFile();
if (parent != null) {
f = parent;
}
} else {
String segment = rover.substring(0, n);
f = new File(f, segment);
}
rover.increment(n + 1);
}

return f.getAbsoluteFile();
}

}
10 changes: 10 additions & 0 deletions aQute.libg/src/aQute/lib/io/Windows.java
Expand Up @@ -12,6 +12,7 @@ class Windows implements OS {
final static Pattern WINDOWS_BAD_FILE_NAME_P = Pattern.compile(
"(?:(:?.*[\u0000-\u001F<>:\"|/\\\\?*].*)|\\.\\.|CON|PRN|AUX|NUL|COM\\d|COM¹|COM²|COM³|LPT\\d|LPT¹|LPT²|LPT³)(?:\\.\\w+)?",
Pattern.CASE_INSENSITIVE);
final static Pattern DRIVE_P = Pattern.compile("/?(?<drive>[a-z]:)", Pattern.CASE_INSENSITIVE);

@Override
public File getBasedFile(File base, String subPath) throws IOException {
Expand Down Expand Up @@ -72,4 +73,13 @@ public String toSafeFileName(String string) {
return sb.toString();
}

@Override
public File getFile(File base, String file) {
file = file.replace('\\', '/');
Matcher m = DRIVE_P.matcher(file);
if (m.lookingAt()) {
base = new File(m.group("drive"));
}
return Other.getFile0(base, file);
}
}
8 changes: 8 additions & 0 deletions aQute.libg/src/aQute/libg/re/Catalog.java
Expand Up @@ -1189,6 +1189,14 @@ public Optional<MatchGroup> group(int group) {
return Optional.of(new MatchGroupImplIndex(group, value));
}

@Override
public String presentGroup(String groupName) {
String group = matcher.group(groupName);
if (group == null)
throw new IllegalArgumentException("no such group " + groupName);
return group;
}

}
return Optional.of(new MatchImpl());
} else
Expand Down
8 changes: 8 additions & 0 deletions aQute.libg/src/aQute/libg/re/RE.java
Expand Up @@ -323,6 +323,14 @@ default boolean check(RE expected) {
String tryMatch(RE match);

Optional<MatchGroup> group(int group);

/**
* This gets the value of a group but throws an exception of the group
* is not there.
*
* @param groupName the name of the group
*/
String presentGroup(String groupName);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions aQute.libg/test/aQute/lib/io/IOTest.java
Expand Up @@ -89,6 +89,17 @@ public void testSafeFileNameWindows() {
assertEquals("COM2_", IO.toSafeFileName("COM2"));
}

@Test
@EnabledOnOs(WINDOWS)
public void testGetFileOnWindows() {
assertThat(IO.getFile("f:/abc")).isEqualTo(new File("f:\\abc"));
assertThat(IO.getFile("/f:/abc")).isEqualTo(new File("f:\\abc"));

File f = new File("f:");
assertThat(IO.getFile(f, "abc")).isEqualTo(new File("f:\\abc"));
}


@Test
public void testFilesetCopy(@InjectTemporaryDirectory
File destDir) throws Exception {
Expand Down
1 change: 1 addition & 0 deletions biz.aQute.bndlib/bnd.bnd
Expand Up @@ -36,6 +36,7 @@ Export-Package: \
aQute.bnd.util.home;-noimport:=true,\
aQute.bnd.util.repository;-noimport:=true,\
aQute.bnd.version;-noimport:=true,\
aQute.bnd.wstemplates;-noimport:=true,\
aQute.lib.deployer;-noimport:=true,\
aQute.service.reporter;-noimport:=true

Expand Down

0 comments on commit c9cb862

Please sign in to comment.