@Override
 public Source mapDartUri(String dartUri) {
   String libraryName;
   String relativePath;
   int index = dartUri.indexOf('/');
   if (index >= 0) {
     libraryName = dartUri.substring(0, index);
     relativePath = dartUri.substring(index + 1);
   } else {
     libraryName = dartUri;
     relativePath = "";
   }
   SdkLibrary library = getSdkLibrary(libraryName);
   if (library == null) {
     return null;
   }
   try {
     File file = new File(getLibraryDirectory(), library.getPath());
     if (!relativePath.isEmpty()) {
       file = file.getParentFile();
       file = new File(file, relativePath);
     }
     return new FileBasedSource(new URI(dartUri), file);
   } catch (URISyntaxException exception) {
     return null;
   }
 }
  public static JpsRemoteProto.Message.Request createSetupRequest(
      final Map<String, String> pathVars,
      List<GlobalLibrary> sdkAndLibs,
      @Nullable String globalEncoding,
      String ignoredFilesPatterns) {
    final JpsRemoteProto.Message.Request.SetupCommand.Builder cmdBuilder =
        JpsRemoteProto.Message.Request.SetupCommand.newBuilder();

    if (!pathVars.isEmpty()) {
      for (Map.Entry<String, String> entry : pathVars.entrySet()) {
        final String var = entry.getKey();
        final String value = entry.getValue();
        if (var != null && value != null) {
          cmdBuilder.addPathVariable(createPair(var, value));
        }
      }
    }

    if (!sdkAndLibs.isEmpty()) {
      for (GlobalLibrary lib : sdkAndLibs) {
        final JpsRemoteProto.Message.Request.SetupCommand.GlobalLibrary.Builder libBuilder =
            JpsRemoteProto.Message.Request.SetupCommand.GlobalLibrary.newBuilder();
        libBuilder.setName(lib.getName()).addAllPath(lib.getPaths());
        if (lib instanceof SdkLibrary) {
          final SdkLibrary sdk = (SdkLibrary) lib;
          libBuilder.setHomePath(sdk.getHomePath());
          libBuilder.setTypeName(sdk.getTypeName());
          final String additional = sdk.getAdditionalDataXml();
          if (additional != null) {
            libBuilder.setAdditionalDataXml(additional);
          }
        }
        cmdBuilder.addGlobalLibrary(libBuilder.build());
      }
    }

    cmdBuilder.setGlobalEncoding(globalEncoding);
    cmdBuilder.setIgnoredFilesPatterns(ignoredFilesPatterns);

    return JpsRemoteProto.Message.Request.newBuilder()
        .setRequestType(JpsRemoteProto.Message.Request.Type.SETUP_COMMAND)
        .setSetupCommand(cmdBuilder.build())
        .build();
  }
 @Override
 public Source fromFileUri(URI uri) {
   File file = new File(uri);
   String filePath = file.getAbsolutePath();
   String libPath = getLibraryDirectory().getAbsolutePath();
   if (!filePath.startsWith(libPath + File.separator)) {
     return null;
   }
   filePath = filePath.substring(libPath.length() + 1);
   for (SdkLibrary library : libraryMap.getSdkLibraries()) {
     String libraryPath = library.getPath();
     if (filePath.replace('\\', '/').equals(libraryPath)) {
       String path = library.getShortName();
       try {
         return new FileBasedSource(new URI(path), file);
       } catch (URISyntaxException exception) {
         AnalysisEngine.getInstance()
             .getLogger()
             .logInformation("Failed to create URI: " + path, exception);
         return null;
       }
     }
     libraryPath = new File(libraryPath).getParent();
     if (filePath.startsWith(libraryPath + File.separator)) {
       String path = library.getShortName() + "/" + filePath.substring(libraryPath.length() + 1);
       try {
         return new FileBasedSource(new URI(path), file);
       } catch (URISyntaxException exception) {
         AnalysisEngine.getInstance()
             .getLogger()
             .logInformation("Failed to create URI: " + path, exception);
         return null;
       }
     }
   }
   return null;
 }
  private static CmdlineRemoteProto.Message.ControllerMessage.GlobalSettings buildGlobalSettings() {
    final Map<String, String> data = new HashMap<String, String>();

    for (Map.Entry<String, String> entry : PathMacrosImpl.getGlobalSystemMacros().entrySet()) {
      data.put(entry.getKey(), FileUtil.toSystemIndependentName(entry.getValue()));
    }

    final PathMacros pathVars = PathMacros.getInstance();
    for (String name : pathVars.getAllMacroNames()) {
      final String path = pathVars.getValue(name);
      if (path != null) {
        data.put(name, FileUtil.toSystemIndependentName(path));
      }
    }

    final List<GlobalLibrary> globals = new ArrayList<GlobalLibrary>();

    fillSdks(globals);
    fillGlobalLibraries(globals);

    final CmdlineRemoteProto.Message.ControllerMessage.GlobalSettings.Builder cmdBuilder =
        CmdlineRemoteProto.Message.ControllerMessage.GlobalSettings.newBuilder();

    if (!data.isEmpty()) {
      for (Map.Entry<String, String> entry : data.entrySet()) {
        final String var = entry.getKey();
        final String value = entry.getValue();
        if (var != null && value != null) {
          cmdBuilder.addPathVariable(CmdlineProtoUtil.createPair(var, value));
        }
      }
    }

    if (!globals.isEmpty()) {
      for (GlobalLibrary lib : globals) {
        final CmdlineRemoteProto.Message.ControllerMessage.GlobalSettings.GlobalLibrary.Builder
            libBuilder =
                CmdlineRemoteProto.Message.ControllerMessage.GlobalSettings.GlobalLibrary
                    .newBuilder();
        libBuilder.setName(lib.getName()).addAllPath(lib.getPaths());
        if (lib instanceof SdkLibrary) {
          final SdkLibrary sdk = (SdkLibrary) lib;
          libBuilder.setHomePath(sdk.getHomePath());
          libBuilder.setTypeName(sdk.getTypeName());
          final String additional = sdk.getAdditionalDataXml();
          if (additional != null) {
            libBuilder.setAdditionalDataXml(additional);
          }
          final String version = sdk.getVersion();
          if (version != null) {
            libBuilder.setVersion(version);
          }
        }
        cmdBuilder.addGlobalLibrary(libBuilder.build());
      }
    }

    final String defaultCharset = EncodingManager.getInstance().getDefaultCharsetName();
    if (defaultCharset != null) {
      cmdBuilder.setGlobalEncoding(defaultCharset);
    }

    final String ignoredFilesList = FileTypeManager.getInstance().getIgnoredFilesList();
    cmdBuilder.setIgnoredFilesPatterns(ignoredFilesList);
    return cmdBuilder.build();
  }