summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/remote/RemoteFile.java
blob: 4a2e11379364430ec2fa6a623015ed5351de714b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.intellij.remote;

import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author traff
 */
public class RemoteFile {

  private final boolean myWin;
  private final String myPath;

  public RemoteFile(@NotNull String path, boolean isWin) {
    myPath = toSystemDependent(path, isWin);
    myWin = isWin;
  }

  public RemoteFile(@NotNull String parent, String child) {
    this(resolveChild(parent, child, isWindowsPath(parent)), isWindowsPath(parent));
  }

  public RemoteFile(@NotNull String parent, String child, boolean isWin) {
    this(resolveChild(parent, child, isWin), isWin);
  }

  @Nullable
  public String getName() {
    int ind = myPath.lastIndexOf(getSeparator(myWin));
    if (ind != -1 && ind < myPath.length() - 1) { //not last char
      return myPath.substring(ind + 1);
    }
    else {
      return null;
    }
  }

  private static String resolveChild(@NotNull String parent, @NotNull String child, boolean win) {
    String separator = getSeparator(win);

    String path;
    if (parent.endsWith(separator)) {
      path = parent + child;
    }
    else {
      path = parent + separator + child;
    }
    return path;
  }

  private static String getSeparator(boolean win) {
    String separator;
    if (win) {
      separator = "\\";
    }
    else {
      separator = "/";
    }
    return separator;
  }


  public String getPath() {
    return myPath;
  }

  public boolean isWin() {
    return isWindowsPath(myPath);
  }

  public static boolean isWindowsPath(@NotNull String path) {
    path = RemoteSdkCredentialsHolder.getInterpreterPathFromFullPath(path);

    return (path.length() > 1 && path.charAt(1) == ':');
  }

  private static String toSystemDependent(@NotNull String path, boolean isWin) {
    char separator = isWin ? '\\' : '/';
    return FileUtil.toSystemIndependentName(path).replace('/', separator);
  }

  public static RemoteFileBuilder detectSystemByPath(@NotNull String path) {
    return new RemoteFileBuilder(isWindowsPath(path));
  }

  public static RemoteFile createRemoteFile(String path, String script) {
    return detectSystemByPath(path).createRemoteFile(path, script);
  }

  public static RemoteFile createRemoteFile(final String path, final String script, final boolean isWindows) {
    return new RemoteFileBuilder(isWindows).createRemoteFile(path, script);
  }

  public static class RemoteFileBuilder {
    private final boolean isWin;

    private RemoteFileBuilder(boolean win) {
      isWin = win;
    }

    public RemoteFile createRemoteFile(String path) {
      return new RemoteFile(path, isWin);
    }

    public RemoteFile createRemoteFile(String path, String child) {
      return new RemoteFile(path, child, isWin);
    }
  }
}