setEnvironment static method

Future<bool> setEnvironment(
  1. String key,
  2. String value
)

Implementation

static Future<bool> setEnvironment(String key, String value) async {
  if (Dart.isWeb) {
    return false;
  }
  if (Dart.isLinux || Dart.isMacOS) {
    List<String> file_rcs = {
      ".bashrc",
      ".zshrc",
    }.toList();
    bool is_set_pocces = false;
    for (var index = 0; index < file_rcs.length; index++) {
      File file = File(path.join(home_directory.path, file_rcs[index]));
      if (!file.existsSync()) {
        continue;
      }
      String readFile = await file.readAsString();
      List<String> bashrcs = readFile.split("\n");

      RegExp regExp = RegExp(
          "export([ ]+)${key}=${RegExp.escape(json.encode(value))}",
          caseSensitive: false);
      bool is_not_found_path = true;
      for (var i = 0; i < bashrcs.length; i++) {
        String bashrc = bashrcs[i];
        if (regExp.hasMatch(bashrc)) {
          is_not_found_path = false;
          break;
        }
      }
      if (is_not_found_path) {
        bashrcs.add("export ${key}=${json.encode(value)}");
        await file.writeAsString(bashrcs.join("\n"));
      }

      if (Dart.isLinux) {
        try {
          // await Process.start(
          //   "gnome-terminal",
          //   [
          //     "-q",
          //     "--",
          //     "source",
          //     (file.path),
          //   ],
          //   workingDirectory: home_directory.path,
          //   // runInShell: true,
          // );
        } catch (e) {
          print(e);
        }
      }
      is_set_pocces = true;
    }
    return is_set_pocces;
  }
  if (Dart.isWindows) {
    await Process.run(
      "setx",
      [
        key,
        value,
      ],
      runInShell: true,
    );
    return true;
  }

  return false;
}