Skip to content

Commit

Permalink
Show only latest VS, VC prompts by default (#11326)
Browse files Browse the repository at this point in the history
## Summary of the Pull Request

Similar to `vswhere -latest`, show only the latest Visual Studio command prompts / developer PowerShell. This was tested by deleting the local package state and testing against fresh state with both VS2019 and VS2022 Preview installed, and indeed VS2022 Preview (both cmd and powershell) show. The other profiles were generated but hidden by default.

## References

Modification of PR #7774

## PR Checklist
* [x] Closes #11307
* [x] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA
* [x] Tests added/passed
* [ ] Documentation updated. If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/terminal) and link it here: #xxx
* [ ] Schema updated.
* [x] I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan. Issue number where discussion took place: #xxx

## Detailed Description of the Pull Request / Additional comments

The sort algorithm is the same basic algorithm I used in https://github.com/microsoft/vswhere. It sorts first by installation version with a secondary sort based on the install date in case the installation versions are the same.

## Validation Steps Performed

With both VS2019 and VS2022 Preview installed, I made sure the initial state was expected, and tried different combinations of hiding and unhiding generated entries, and restarted Terminal to make sure my settings "stuck".
  • Loading branch information
heaths authored Sep 29, 2021
1 parent d869913 commit 37e8769
Show file tree
Hide file tree
Showing 17 changed files with 372 additions and 123 deletions.
1 change: 1 addition & 0 deletions .github/actions/spelling/allow/microsoft.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ DTDs
DWINRT
enablewttlogging
Intelli
IVisual
LKG
LOCKFILE
Lxss
Expand Down
1 change: 1 addition & 0 deletions .github/actions/spelling/patterns/patterns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ VERIFY_ARE_EQUAL\(L"[^"]+"
std::memory_order_[\w]+
D2DERR_SHADER_COMPILE_FAILED
TIL_FEATURE_[0-9A-Z_]+
vcvars\w*
36 changes: 0 additions & 36 deletions src/cascadia/TerminalSettingsModel/BaseVisualStudioGenerator.cpp

This file was deleted.

35 changes: 0 additions & 35 deletions src/cascadia/TerminalSettingsModel/BaseVisualStudioGenerator.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

#include "AzureCloudShellGenerator.h"
#include "PowershellCoreProfileGenerator.h"
#include "VsDevCmdGenerator.h"
#include "VsDevShellGenerator.h"
#include "VisualStudioGenerator.h"
#include "WslDistroGenerator.h"

// The following files are generated at build time into the "Generated Files" directory.
Expand Down Expand Up @@ -131,8 +130,7 @@ void SettingsLoader::GenerateProfiles()
_executeGenerator(PowershellCoreProfileGenerator{});
_executeGenerator(WslDistroGenerator{});
_executeGenerator(AzureCloudShellGenerator{});
_executeGenerator(VsDevCmdGenerator{});
_executeGenerator(VsDevShellGenerator{});
_executeGenerator(VisualStudioGenerator{});
}

// A new settings.json gets a special treatment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
<Import Project="$(OpenConsoleDir)src\cppwinrt.build.pre.props" />
<!-- ========================= Headers ======================== -->
<ItemGroup>
<ClInclude Include="BaseVisualStudioGenerator.h" />
<ClInclude Include="VcDevCmdGenerator.h" />
<ClInclude Include="VisualStudioGenerator.h" />
<ClInclude Include="DefaultTerminal.h">
<DependentUpon>DefaultTerminal.idl</DependentUpon>
</ClInclude>
Expand Down Expand Up @@ -84,7 +85,8 @@
</ItemGroup>
<!-- ========================= Cpp Files ======================== -->
<ItemGroup>
<ClCompile Include="BaseVisualStudioGenerator.cpp" />
<ClCompile Include="VcDevCmdGenerator.cpp" />
<ClCompile Include="VisualStudioGenerator.cpp" />
<ClCompile Include="DefaultTerminal.cpp">
<DependentUpon>DefaultTerminal.idl</DependentUpon>
</ClCompile>
Expand Down Expand Up @@ -241,7 +243,6 @@
</ItemDefinitionGroup>
<!-- ========================= Globals ======================== -->
<Import Project="$(OpenConsoleDir)src\cppwinrt.build.post.props" />

<Import Project="..\..\..\packages\Microsoft.UI.Xaml.2.7.0-prerelease.210913003\build\native\Microsoft.UI.Xaml.targets" Condition="Exists('..\..\..\packages\Microsoft.UI.Xaml.2.7.0-prerelease.210913003\build\native\Microsoft.UI.Xaml.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
Expand All @@ -266,4 +267,4 @@
</Target>
<Import Project="$(SolutionDir)build\rules\CollectWildcardResources.targets" />
<Import Project="..\..\..\packages\Microsoft.VisualStudio.Setup.Configuration.Native.2.3.2262\build\native\Microsoft.VisualStudio.Setup.Configuration.Native.targets" Condition="Exists('..\..\..\packages\Microsoft.VisualStudio.Setup.Configuration.Native.2.3.2262\build\native\Microsoft.VisualStudio.Setup.Configuration.Native.targets')" />
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<ClCompile Include="IconPathConverter.cpp" />
<ClCompile Include="DefaultTerminal.cpp" />
<ClCompile Include="FileUtils.cpp" />
<ClCompile Include="BaseVisualStudioGenerator.cpp">
<ClCompile Include="VisualStudioGenerator.cpp">
<Filter>profileGeneration</Filter>
</ClCompile>
<ClCompile Include="VsDevCmdGenerator.cpp">
Expand All @@ -46,6 +46,9 @@
<ClCompile Include="VsSetupConfiguration.cpp">
<Filter>profileGeneration</Filter>
</ClCompile>
<ClCompile Include="VcDevCmdGenerator.cpp">
<Filter>profileGeneration</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
Expand Down Expand Up @@ -80,7 +83,7 @@
<ClInclude Include="DefaultTerminal.h" />
<ClInclude Include="FileUtils.h" />
<ClInclude Include="HashUtils.h" />
<ClInclude Include="BaseVisualStudioGenerator.h">
<ClInclude Include="VisualStudioGenerator.h">
<Filter>profileGeneration</Filter>
</ClInclude>
<ClInclude Include="VsDevCmdGenerator.h">
Expand All @@ -92,6 +95,9 @@
<ClInclude Include="VsSetupConfiguration.h">
<Filter>profileGeneration</Filter>
</ClInclude>
<ClInclude Include="VcDevCmdGenerator.h">
<Filter>profileGeneration</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Midl Include="ActionArgs.idl" />
Expand Down Expand Up @@ -123,4 +129,4 @@
<UniqueIdentifier>{81a6314f-aa5b-4533-a499-13bc3a5c4af0}</UniqueIdentifier>
</Filter>
</ItemGroup>
</Project>
</Project>
94 changes: 94 additions & 0 deletions src/cascadia/TerminalSettingsModel/VcDevCmdGenerator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#include "pch.h"
#include "DynamicProfileUtils.h"
#include "VcDevCmdGenerator.h"

using namespace winrt::Microsoft::Terminal::Settings::Model;

void VcDevCmdGenerator::GenerateProfiles(const VsSetupConfiguration::VsSetupInstance& instance, bool hidden, std::vector<winrt::com_ptr<implementation::Profile>>& profiles) const
{
try
{
const std::filesystem::path root{ GetVcCmdScriptDirectory(instance) };
if (!std::filesystem::exists(root))
{
return;
}

// x64 environments only installed on 64-bit machines.
#ifdef _WIN64
const auto vcvars64 = root / L"vcvars64.bat";
if (std::filesystem::exists(vcvars64))
{
auto profile = CreateProfile(instance, L"x64", vcvars64, hidden);
profiles.emplace_back(std::move(profile));

// Only the VC environment for the matching architecture should be shown by default.
hidden = true;
}

const auto vcvarsamd64_x86 = root / L"vcvarsamd64_x86.bat";
if (std::filesystem::exists(vcvarsamd64_x86))
{
auto profile = CreateProfile(instance, L"x64_x86", vcvarsamd64_x86, true);
profiles.emplace_back(std::move(profile));
}

const auto vcvarsx86_amd64 = root / L"vcvarsx86_amd64.bat";
if (std::filesystem::exists(vcvarsx86_amd64))
{
auto profile = CreateProfile(instance, L"x86_x64", vcvarsx86_amd64, true);
profiles.emplace_back(std::move(profile));
}
#endif // _WIN64

const auto vcvars32 = root / L"vcvars32.bat";
if (std::filesystem::exists(vcvars32))
{
auto profile = CreateProfile(instance, L"x86", vcvars32, hidden);
profiles.emplace_back(std::move(profile));
}
}
CATCH_LOG();
}

winrt::com_ptr<implementation::Profile> VcDevCmdGenerator::CreateProfile(const VsSetupConfiguration::VsSetupInstance& instance, const std::wstring_view& prefix, const std::filesystem::path& path, bool hidden) const
{
const auto seed = GetProfileGuidSeed(instance, path);
const winrt::guid profileGuid{ ::Microsoft::Console::Utils::CreateV5Uuid(TERMINAL_PROFILE_NAMESPACE_GUID, gsl::as_bytes(gsl::make_span(seed))) };

auto profile = winrt::make_self<implementation::Profile>(profileGuid);
profile->Name(winrt::hstring{ GetProfileName(instance, prefix) });
profile->Commandline(winrt::hstring{ GetProfileCommandLine(path) });
profile->StartingDirectory(winrt::hstring{ instance.GetInstallationPath() });
profile->Icon(winrt::hstring{ GetProfileIconPath() });
profile->Hidden(hidden);

return profile;
}

std::wstring VcDevCmdGenerator::GetProfileGuidSeed(const VsSetupConfiguration::VsSetupInstance& instance, const std::filesystem::path& path) const
{
return L"VsDevCmd" + instance.GetInstanceId() + path.native();
}

std::wstring VcDevCmdGenerator::GetProfileName(const VsSetupConfiguration::VsSetupInstance& instance, const std::wstring_view& prefix) const
{
std::wstring name{ prefix + L" Native Tools Command Prompt for VS " };
name.append(instance.GetProfileNameSuffix());
return name;
}

std::wstring VcDevCmdGenerator::GetProfileCommandLine(const std::filesystem::path& path) const
{
std::wstring commandLine{ L"cmd.exe /k \"" + path.native() + L"\"" };

return commandLine;
}

std::wstring VcDevCmdGenerator::GetVcCmdScriptDirectory(const VsSetupConfiguration::VsSetupInstance& instance) const
{
return instance.ResolvePath(L"VC\\Auxiliary\\Build\\");
}
40 changes: 40 additions & 0 deletions src/cascadia/TerminalSettingsModel/VcDevCmdGenerator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*++
Copyright (c) Microsoft Corporation
Licensed under the MIT license.
Module Name:
- VcDevCmdGenerator
Abstract:
- Dynamic profile generator for Visual C++ development environments.
Author(s):
- Heath Stewart - September 2021
--*/

#pragma once
#include "VisualStudioGenerator.h"
#include "VsSetupConfiguration.h"

namespace winrt::Microsoft::Terminal::Settings::Model
{
class VcDevCmdGenerator final : public VisualStudioGenerator::IVisualStudioProfileGenerator
{
public:
void GenerateProfiles(const VsSetupConfiguration::VsSetupInstance& instance, bool hidden, std::vector<winrt::com_ptr<implementation::Profile>>& profiles) const override;

private:
winrt::com_ptr<implementation::Profile> CreateProfile(const VsSetupConfiguration::VsSetupInstance& instance, const std::wstring_view& prefix, const std::filesystem::path& path, bool hidden) const;

std::wstring GetProfileIconPath() const
{
return L"ms-appx:///ProfileIcons/{0caa0dad-35be-5f56-a8ff-afceeeaa6101}.png";
}

std::wstring GetProfileGuidSeed(const VsSetupConfiguration::VsSetupInstance& instance, const std::filesystem::path& path) const;
std::wstring GetProfileName(const VsSetupConfiguration::VsSetupInstance& instance, const std::wstring_view& prefix) const;
std::wstring GetProfileCommandLine(const std::filesystem::path& path) const;
std::wstring GetVcCmdScriptDirectory(const VsSetupConfiguration::VsSetupInstance& instance) const;
};
};
36 changes: 36 additions & 0 deletions src/cascadia/TerminalSettingsModel/VisualStudioGenerator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#include "pch.h"
#include "DynamicProfileUtils.h"
#include "VisualStudioGenerator.h"
#include "VcDevCmdGenerator.h"
#include "VsDevCmdGenerator.h"
#include "VsDevShellGenerator.h"

using namespace winrt::Microsoft::Terminal::Settings::Model;

std::wstring_view VisualStudioGenerator::GetNamespace() const noexcept
{
return std::wstring_view{ L"Windows.Terminal.VisualStudio" };
}

void VisualStudioGenerator::GenerateProfiles(std::vector<winrt::com_ptr<implementation::Profile>>& profiles) const
{
const auto instances = VsSetupConfiguration::QueryInstances();

VsDevCmdGenerator devCmdGenerator;
VcDevCmdGenerator vcCmdGenerator;
VsDevShellGenerator devShellGenerator;

// Instances are ordered from latest to oldest. Hide all but the profiles for the latest instance.
bool hidden = false;
for (auto const& instance : instances)
{
devCmdGenerator.GenerateProfiles(instance, hidden, profiles);
vcCmdGenerator.GenerateProfiles(instance, hidden, profiles);
devShellGenerator.GenerateProfiles(instance, hidden, profiles);

hidden = true;
}
}
38 changes: 38 additions & 0 deletions src/cascadia/TerminalSettingsModel/VisualStudioGenerator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*++
Copyright (c) Microsoft Corporation
Licensed under the MIT license.
Module Name:
- VisualStudioGenerator
Abstract:
- Generator for Visual Studio shell profiles. Actual profile generation is delegated
to separate classes that encapsulate different logic for cmd- and powershell-based shells,
as well as VC startup scripts specific to the current processor architecture.
Author(s):
- Charles Willis - October 2020
- Heath Stewart - September 2021
--*/

#pragma once

#include "IDynamicProfileGenerator.h"
#include "VsSetupConfiguration.h"

namespace winrt::Microsoft::Terminal::Settings::Model
{
class VisualStudioGenerator : public IDynamicProfileGenerator
{
public:
std::wstring_view GetNamespace() const noexcept override;
void GenerateProfiles(std::vector<winrt::com_ptr<implementation::Profile>>& profiles) const override;

class IVisualStudioProfileGenerator
{
public:
virtual void GenerateProfiles(const VsSetupConfiguration::VsSetupInstance& instance, bool hidden, std::vector<winrt::com_ptr<implementation::Profile>>& profiles) const = 0;
};
};
};
Loading

0 comments on commit 37e8769

Please sign in to comment.