Skip to content

Commit cf46071

Browse files
committed
Boot - further unification of home path resolution
Both the API and the Ruby server do home directory detection and relative path resolution. Ideally this should be in one place, but in lieu of that, it is essential they both behave similarly. Resolution now matches QDir::homePath(): The path specified by the SONIC_PI_HOME environment variable. The path specified by the USERPROFILE environment variable. (windows only) The path formed by concatenating the HOMEDRIVE and HOMEPATH environment variables. (windows only) The path specified by the HOME environment variable.
1 parent 63f0b7a commit cf46071

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

app/api/src/sonicpi_api.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,31 @@ fs::path SonicPiAPI::FindHomePath() const
8383
auto pszHome = std::getenv("SONIC_PI_HOME");
8484
if (pszHome != nullptr)
8585
{
86-
homePath = fs::path(pszHome);
86+
return pszHome;
8787
}
8888

89-
// Check for home path existence and if not, use user documents path
90-
if (!fs::exists(homePath))
89+
#if defined(WIN32)
90+
auto usrprofHome = std::getenv("USERPROFILE");
91+
if (usrprofHome != nullptr)
9192
{
92-
homePath = fs::path(sago::getDocumentsFolder()).parent_path();
93+
return fs::path(usrprofHome);
9394
}
9495

95-
// Final attempt at getting the folder; try to create it if possible
96-
if (!fs::exists(homePath))
96+
auto homeDrive = std::getenv("HOMEDRIVE");
97+
auto homePath = std::getenv("HOMEPATH");
98+
if ((homeDrive != nullptr) && (homePath != nullptr))
9799
{
98-
std::error_code err;
99-
if (!fs::create_directories(homePath, err))
100-
{
101-
// Didn't exist, and failed to create it
102-
return fs::path();
103-
}
100+
return fs::path(homeDrive) / homePath;
104101
}
105-
return homePath;
102+
#endif
103+
104+
auto home = std::getenv("HOME");
105+
if (home != nullptr)
106+
{
107+
return fs::path(home);
108+
}
109+
110+
return fs::path(sago::getDocumentsFolder()).parent_path();
106111
}
107112

108113
std::error_code SonicPiAPI::RunProcess(const std::vector<std::string>& args, std::string* pOutput)

app/server/ruby/paths.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,30 @@
1616
module SonicPi
1717
module Paths
1818
def self.user_dir
19+
return File.expand_path(ENV["SONIC_PI_HOME"]) if ENV["SONIC_PI_HOME"]
1920

2021
# Figure out the user's home directory
2122
case os
2223
when :windows
24+
return File.expand_path(ENV["USERPROFILE"]) if ENV["USERPROFILE"]
2325
# On Windows, Ruby lets HOME take precedence if it exists, which
2426
# is not what Sonic Pi should do to behave like a native Windows
2527
# app. To get the same path as QDir::homePath() used by the
2628
# GUI, we must use HOMEDRIVE and HOMEPATH instead, if they are
2729
# set.
2830
home_drive = ENV["HOMEDRIVE"]
2931
home_path = ENV["HOMEPATH"]
30-
if home_drive and home_path
31-
File.expand_path(home_drive + home_path)
32-
else
33-
File.expand_path(Dir.home)
34-
end
32+
return File.absolute_path("#{home_drive}/#{home_path}") if home_drive and home_path
33+
return File.expand_path(ENV["HOME"]) if ENV["HOME"]
34+
return File.expand_path(Dir.home)
3535
else
36-
File.expand_path(Dir.home)
36+
return File.expand_path(ENV["HOME"]) if ENV["HOME"]
37+
return File.expand_path(Dir.home)
3738
end
3839
end
3940

4041
def self.home_dir_path
41-
File.expand_path((ENV['SONIC_PI_HOME'] || user_dir) + '/.sonic-pi/')
42+
File.absolute_path("#{user_dir}/.sonic-pi/")
4243
end
4344

4445
def self.project_path

0 commit comments

Comments
 (0)