private static Path getOsSpecificDirectory(
      Properties properties,
      Map<String, String> environment,
      String xdgEnvVariable,
      String linuxFolder,
      String windowsFolder,
      String macFolder) {

    Path windowsSubDirectory = JIB_SUBDIRECTORY_OTHER.resolve(windowsFolder);
    String rawOsName = properties.getProperty("os.name");
    String osName = rawOsName.toLowerCase(Locale.ENGLISH);
    String xdgHome = environment.get(xdgEnvVariable);
    String userHome = properties.getProperty("user.home");
    Path xdgPath = Paths.get(userHome, linuxFolder);

    if (osName.contains("linux")) {
      // Use XDG environment variable if set and not empty.
      if (xdgHome != null && !xdgHome.trim().isEmpty()) {
        return Paths.get(xdgHome).resolve(JIB_SUBDIRECTORY_LINUX);
      }
      return xdgPath.resolve(JIB_SUBDIRECTORY_LINUX);

    } else if (osName.contains("windows")) {
      // Use XDG environment variable if set and not empty.
      if (xdgHome != null && !xdgHome.trim().isEmpty()) {
        return Paths.get(xdgHome).resolve(windowsSubDirectory);
      }

      // Use %LOCALAPPDATA% for Windows.
      String localAppDataEnv = environment.get("LOCALAPPDATA");
      if (localAppDataEnv == null || localAppDataEnv.trim().isEmpty()) {
        LOGGER.warning("LOCALAPPDATA environment is invalid or missing");
        return xdgPath.resolve(windowsSubDirectory);
      }
      Path localAppData = Paths.get(localAppDataEnv);
      if (!Files.exists(localAppData)) {
        LOGGER.warning(() -> localAppData + " does not exist");
        return xdgPath.resolve(windowsSubDirectory);
      }
      return localAppData.resolve(windowsSubDirectory);

    } else if (osName.contains("mac") || osName.contains("darwin")) {
      // Use XDG environment variable if set and not empty.
      if (xdgHome != null && !xdgHome.trim().isEmpty()) {
        return Paths.get(xdgHome).resolve(JIB_SUBDIRECTORY_OTHER);
      }

      // Use '~/Library/...' for macOS.
      Path macDirectory = Paths.get(userHome, "Library", macFolder);
      if (!Files.exists(macDirectory)) {
        LOGGER.warning(() -> macDirectory + " does not exist");
        return xdgPath.resolve(JIB_SUBDIRECTORY_OTHER);
      }
      return macDirectory.resolve(JIB_SUBDIRECTORY_OTHER);
    }

    throw new IllegalStateException("Unknown OS: " + rawOsName);
  }
Helper method for resolving directories on different operating systems.

@param xdgEnvVariable the name of the environment variable used to resolve the XDG base
    directory
@param linuxFolder ".config" or ".cache"
@param windowsFolder "Config" or "Cache"
@param macFolder "Preferences" or "Caches"
@return the full path constructed from the given parameters
jib-core/src/main/java/com/google/cloud/tools/jib/filesystem/XdgDirectories.java