Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize Terminal Preview settings from Terminal settings #12907

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/CascadiaSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

private:
static const std::filesystem::path& _settingsPath();
static const std::filesystem::path& _releaseSettingsPath();

winrt::com_ptr<implementation::Profile> _createNewProfile(const std::wstring_view& name) const;
Model::Profile _getProfileForCommandLine(const winrt::hstring& commandLine) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,28 @@ void SettingsLoader::_executeGenerator(const IDynamicProfileGenerator& generator
Model::CascadiaSettings CascadiaSettings::LoadAll()
try
{
const auto settingsString = ReadUTF8FileIfExists(_settingsPath()).value_or(std::string{});
const auto firstTimeSetup = settingsString.empty();
auto settingsString = ReadUTF8FileIfExists(_settingsPath()).value_or(std::string{});
auto firstTimeSetup = settingsString.empty();

// If it's the firstTimeSetup and a preview build, then try to
// read settings.json from the Release stable file path if it exists.
// Otherwise use default settings file provided from original settings file
bool releaseSettingExists = false;
if (firstTimeSetup)
{
#if defined(WT_BRANDING_PREVIEW)
Comment on lines +775 to +778
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever, simple, easy to parse what's happening. 👍

{
try
{
settingsString = ReadUTF8FileIfExists(_releaseSettingsPath()).value_or(std::string{});
releaseSettingExists = settingsString.empty() ? false : true;
}
catch (...)
{
}
}
#endif
}

// GH#11119: If we find that the settings file doesn't exist, or is empty,
// then let's quick delete the state file as well. If the user does have a
Expand All @@ -780,7 +800,9 @@ try
ApplicationState::SharedInstance().Reset();
}

const auto settingsStringView = firstTimeSetup ? UserSettingsJson : settingsString;
// Only uses default settings when firstTimeSetup is true and releaseSettingExists is false
// Otherwise use existing settingsString
const auto settingsStringView = (firstTimeSetup && !releaseSettingExists) ? UserSettingsJson : settingsString;
auto mustWriteToDisk = firstTimeSetup;

SettingsLoader loader{ settingsStringView, DefaultJson };
Expand All @@ -791,7 +813,8 @@ try

// ApplyRuntimeInitialSettings depends on generated profiles.
// --> ApplyRuntimeInitialSettings must be called after GenerateProfiles.
if (firstTimeSetup)
// Doesn't run when there is a Release settings.json that exists
if (firstTimeSetup && !releaseSettingExists)
{
loader.ApplyRuntimeInitialSettings();
}
Expand Down Expand Up @@ -953,6 +976,18 @@ const std::filesystem::path& CascadiaSettings::_settingsPath()
return path;
}

// Method Description:
// - Returns the path of the settings.json file from stable file path
// Arguments:
// - <none>
// Return Value:
// - Path to stable settings
const std::filesystem::path& CascadiaSettings::_releaseSettingsPath()
{
static const auto path = GetReleaseSettingsPath() / SettingsFilename;
return path;
}

// function Description:
// - Returns the full path to the settings file, either within the application
// package, or in its unpackaged location. This path is under the "Local
Expand Down
27 changes: 27 additions & 0 deletions src/cascadia/TerminalSettingsModel/FileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

static constexpr std::string_view Utf8Bom{ "\xEF\xBB\xBF", 3 };
static constexpr std::wstring_view UnpackagedSettingsFolderName{ L"Microsoft\\Windows Terminal\\" };
static constexpr std::wstring_view ReleaseSettingsFolder{ L"Packages\\Microsoft.WindowsTerminal_8wekyb3d8bbwe\\LocalState\\" };

namespace winrt::Microsoft::Terminal::Settings::Model
{
Expand Down Expand Up @@ -43,6 +44,32 @@ namespace winrt::Microsoft::Terminal::Settings::Model
return baseSettingsPath;
}

// Returns a path like C:\Users\<username>\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState
// to the path of the stable release settings
std::filesystem::path GetReleaseSettingsPath()
{
static std::filesystem::path baseSettingsPath = []() {
wil::unique_cotaskmem_string localAppDataFolder;
// We're using KF_FLAG_NO_PACKAGE_REDIRECTION to ensure that we always get the
// user's actual local AppData directory.
THROW_IF_FAILED(SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_NO_PACKAGE_REDIRECTION, nullptr, &localAppDataFolder));

// Returns a path like C:\Users\<username>\AppData\Local
std::filesystem::path parentDirectoryForSettingsFile{ localAppDataFolder.get() };

//Appending \Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState to the settings path
parentDirectoryForSettingsFile /= ReleaseSettingsFolder;

if (!IsPackaged())
{
parentDirectoryForSettingsFile /= UnpackagedSettingsFolderName;
}
Comment on lines +61 to +66
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is right for unpackaged versions of the Terminal. I might want to tap in @DHowett for this bit, hes a bit more familiar with running the Terminal unpackaged.

Just looking at my own files, I've only got a %localAppData%\Microsoft\Windows Terminal.

Heck, I'm not sure this is ever relevant for Preview builds running unpackaged - I think they always just use %localAppData%\Microsoft\Windows Terminal anyways. I might be wrong here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah -- preview builds just use the same storage as stable when they run unpackaged, so there's no worry here :)


return parentDirectoryForSettingsFile;
}();
return baseSettingsPath;
}

// Function Description:
// - Checks the permissions on this file, to make sure it can only be opened
// for writing by admins. We will be checking to see if the file is owned
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/FileUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace winrt::Microsoft::Terminal::Settings::Model
{
std::filesystem::path GetBaseSettingsPath();
std::filesystem::path GetReleaseSettingsPath();
std::string ReadUTF8File(const std::filesystem::path& path, const bool elevatedOnly = false);
std::optional<std::string> ReadUTF8FileIfExists(const std::filesystem::path& path, const bool elevatedOnly = false);
void WriteUTF8File(const std::filesystem::path& path, const std::string_view& content, const bool elevatedOnly = false);
Expand Down