| 1 |
#include "PlatformCommon.hpp" |
| 2 |
|
| 3 |
#include "file_logger.h" |
| 4 |
#include "procutils.h" |
| 5 |
|
| 6 |
#include <algorithm> |
| 7 |
#include <wx/arrstr.h> |
| 8 |
|
| 9 |
bool PlatformCommon::WhichWithVersion(const wxString& command, const std::vector<int>& versions, |
| 10 |
wxString* command_fullpath) |
| 11 |
{ |
| 12 |
std::vector<int> sorted_versions = versions; |
| 13 |
|
| 14 |
// sort the numbers in descending order |
| 15 |
std::sort(sorted_versions.begin(), sorted_versions.end(), [](int a, int b) { return b < a; }); |
| 16 |
|
| 17 |
wxArrayString names; |
| 18 |
names.reserve(sorted_versions.size() + 1); |
| 19 |
|
| 20 |
// executable without number should come first |
| 21 |
names.Add(command); |
| 22 |
for(auto ver : sorted_versions) { |
| 23 |
names.Add(wxString() << command << "-" << ver); |
| 24 |
} |
| 25 |
for(const wxString& name : names) { |
| 26 |
if(Which(name, command_fullpath)) { |
| 27 |
return true; |
| 28 |
} |
| 29 |
} |
| 30 |
return false; |
| 31 |
} |
| 32 |
|
| 33 |
/// Locate rustup bin folder |
| 34 |
/// the path is set to: |
| 35 |
/// $HOME/.rustup/TOOLCHAIN-NAME/bin |
| 36 |
bool PlatformCommon::FindRustupToolchainBinDir(wxString* rustup_bin_dir) |
| 37 |
{ |
| 38 |
#ifdef __WXMSW__ |
| 39 |
return false; |
| 40 |
#else |
| 41 |
wxString homedir; |
| 42 |
FindHomeDir(&homedir); |
| 43 |
|
| 44 |
wxString rustup_exe; |
| 45 |
rustup_exe << homedir << "/.cargo/bin/rustup"; |
| 46 |
if(!wxFileName::FileExists(rustup_exe)) { |
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
// locate the default toolchain |
| 51 |
wxString toolchain_name = ProcUtils::GrepCommandOutput({ rustup_exe, "toolchain", "list" }, "(default)"); |
| 52 |
toolchain_name = toolchain_name.BeforeLast('('); |
| 53 |
toolchain_name.Trim().Trim(false); |
| 54 |
if(toolchain_name.empty()) { |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
// build the path |
| 59 |
*rustup_bin_dir << homedir << "/.rustup/toolchains/" << toolchain_name << "/bin"; |
| 60 |
clDEBUG() << "Rust toolchain path:" << *rustup_bin_dir << endl; |
| 61 |
return true; |
| 62 |
#endif |
| 63 |
} |