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

Feature/newworkspace #6070

Merged
merged 5 commits into from
Apr 4, 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
71 changes: 30 additions & 41 deletions aQute.libg/src/aQute/lib/io/IO.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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 @@
}
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));
Dismissed Show dismissed Hide dismissed
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);
Dismissed Show dismissed Hide dismissed
}
rover.increment(n + 1);
}

return f.getAbsoluteFile();
}

}
10 changes: 10 additions & 0 deletions aQute.libg/src/aQute/lib/io/Windows.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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 @@
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"));
Dismissed Show dismissed Hide dismissed
}
return Other.getFile0(base, file);
}
}
8 changes: 8 additions & 0 deletions aQute.libg/src/aQute/libg/re/Catalog.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading
Loading