001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package org.wpilib.system;
006
007import java.io.File;
008import org.wpilib.framework.RobotBase;
009
010/**
011 * Class for interacting with the Filesystem, particularly, interacting with FRC-related paths on
012 * the system, such as the launch and deploy directories.
013 *
014 * <p>This class is primarily used for obtaining resources in src/main/deploy, and the systemcore
015 * path /home/systemcore in a simulation-compatible way.
016 */
017public final class Filesystem {
018  private Filesystem() {}
019
020  /**
021   * Obtains the current working path that the program was launched with. This is analogous to the
022   * `pwd` command on unix.
023   *
024   * @return The current working directory (launch directory)
025   */
026  public static File getLaunchDirectory() {
027    // workaround for
028    // https://www.chiefdelphi.com/t/filesystem-getdeploydirectory-returning-wrong-location-how-to-fix/427292
029    String path =
030        System.getProperty("user.dir")
031            .replace(
032                File.separator + "build" + File.separator + "jni" + File.separator + "release", "");
033    return new File(path).getAbsoluteFile();
034  }
035
036  /**
037   * Obtains the operating directory of the program. On the systemcore, this is /home/systemcore. In
038   * simulation, it is where the simulation was launched from (`pwd`).
039   *
040   * @return The operating directory
041   */
042  public static File getOperatingDirectory() {
043    if (!RobotBase.isSimulation()) {
044      return new File("/home/systemcore");
045    } else {
046      return getLaunchDirectory();
047    }
048  }
049
050  /**
051   * Obtains the 'deploy' directory of the program, located at src/main/deploy, which is deployed by
052   * default. On the systemcore, this is /home/systemcore/deploy. In simulation, it is where the
053   * simulation was launched from, in the subdirectory "src/main/deploy" (`pwd`/src/main/deploy).
054   *
055   * @return The 'deploy' directory
056   */
057  public static File getDeployDirectory() {
058    if (!RobotBase.isSimulation()) {
059      return new File(getOperatingDirectory(), "deploy");
060    } else {
061      return new File(
062          getOperatingDirectory(), "src" + File.separator + "main" + File.separator + "deploy");
063    }
064  }
065}