src/install_utility/main.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include <iostream> | ||
| 7 | #include <string> | ||
| 8 | #include <cstdlib> | ||
| 9 | #include <regex> | ||
| 10 | #include <sstream> | ||
| 11 | #if defined __APPLE__ | ||
| 12 | # include <mach-o/dyld.h> /* _NSGetExecutablePath */ | ||
| 13 | # include <limits.h> /* PATH_MAX */ | ||
| 14 | #elif defined _WIN32 | ||
| 15 | # include <windows.h> | ||
| 16 | #endif | ||
| 17 | #include "../utilities/core/Filesystem.hpp" | ||
| 18 | |||
| 19 | #ifdef __APPLE__ | ||
| 20 | # include <boost/filesystem.hpp> | ||
| 21 | namespace fs = boost::filesystem; | ||
| 22 | #endif | ||
| 23 | |||
| 24 | inline std::string applicationFilePath() { | ||
| 25 | #ifdef __APPLE__ | ||
| 26 | // This is for create_symlink which is not included in utilities core filesystem | ||
| 27 | char path[PATH_MAX + 1]; | ||
| 28 | uint32_t size = sizeof(path); | ||
| 29 | if (_NSGetExecutablePath(path, &size) == 0) { | ||
| 30 | return {path}; | ||
| 31 | } | ||
| 32 | #elif defined _WIN32 | ||
| 33 | TCHAR szPath[MAX_PATH]; | ||
| 34 | if (GetModuleFileName(nullptr, szPath, MAX_PATH)) { | ||
| 35 | return std::string(szPath); | ||
| 36 | } | ||
| 37 | #endif | ||
| 38 | return {}; | ||
| 39 | } | ||
| 40 | |||
| 41 | ✗ | int main(int argc, const char* argv[]) { | |
| 42 | |||
| 43 | #ifdef __APPLE__ | ||
| 44 | if (argc > 1u) { | ||
| 45 | openstudio::filesystem::path appDir = openstudio::filesystem::path(applicationFilePath()).parent_path(); | ||
| 46 | openstudio::filesystem::path cliPath = appDir / "openstudio"; | ||
| 47 | |||
| 48 | if (std::string(argv[1]) == "Install") { | ||
| 49 | try { | ||
| 50 | if (openstudio::filesystem::exists(cliPath)) { | ||
| 51 | openstudio::filesystem::path dir("/usr/local/bin"); | ||
| 52 | if (!openstudio::filesystem::exists(dir)) { | ||
| 53 | openstudio::filesystem::create_directories(dir); | ||
| 54 | } | ||
| 55 | try { | ||
| 56 | openstudio::filesystem::remove("/usr/local/bin/openstudio"); | ||
| 57 | } catch (...) { | ||
| 58 | } | ||
| 59 | fs::create_symlink(cliPath.string(), "/usr/local/bin/openstudio"); | ||
| 60 | } | ||
| 61 | } catch (...) { | ||
| 62 | } | ||
| 63 | |||
| 64 | } else if (std::string(argv[1]) == "Remove") { | ||
| 65 | try { | ||
| 66 | openstudio::filesystem::remove("/usr/local/bin/openstudio"); | ||
| 67 | } catch (...) { | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | #endif | ||
| 72 | |||
| 73 | #if defined _WIN32 | ||
| 74 | if (argc > 1u) { | ||
| 75 | if (std::string(argv[1]) == "Install") { | ||
| 76 | openstudio::filesystem::path appDir = openstudio::filesystem::path(applicationFilePath()).parent_path(); | ||
| 77 | openstudio::filesystem::path cliPath = appDir / "openstudio.exe"; | ||
| 78 | openstudio::filesystem::path mongoPath = appDir.parent_path() / "pat\\mongo\\bin\\mongod.exe"; | ||
| 79 | |||
| 80 | std::ostringstream oss; | ||
| 81 | oss << "netsh advfirewall firewall add rule name=\"Allow OpenStudio CLI\" dir=in program=" << "\"" << cliPath.string() << "\" " | ||
| 82 | << "action=allow"; | ||
| 83 | std::system(oss.str().c_str()); | ||
| 84 | |||
| 85 | std::ostringstream oss2; | ||
| 86 | oss2 << "netsh advfirewall firewall add rule name=\"Allow OpenStudio Mongo\" dir=in program=" << "\"" << mongoPath.string() << "\" " | ||
| 87 | << "action=allow"; | ||
| 88 | std::system(oss2.str().c_str()); | ||
| 89 | |||
| 90 | } else if (std::string(argv[1]) == "Remove") { | ||
| 91 | std::ostringstream oss; | ||
| 92 | oss << "netsh advfirewall firewall delete rule name=\"Allow OpenStudio CLI\""; | ||
| 93 | std::system(oss.str().c_str()); | ||
| 94 | |||
| 95 | std::ostringstream oss2; | ||
| 96 | oss2 << "netsh advfirewall firewall delete rule name=\"Allow OpenStudio Mongo\""; | ||
| 97 | std::system(oss2.str().c_str()); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | #endif | ||
| 101 | |||
| 102 | ✗ | return 0; | |
| 103 | } | ||
| 104 |