1414
1515namespace Microsoft . DotNet . Tools . Install . Tool
1616{
17- public class InstallToolCommand : CommandBase
17+ internal class InstallToolCommand : CommandBase
1818 {
19- private static string _packageId ;
20- private static string _packageVersion ;
21- private static string _configFilePath ;
22- private static string _framework ;
23- private static string _source ;
24- private static bool _global ;
19+ private readonly IToolPackageObtainer _toolPackageObtainer ;
20+ private readonly IEnvironmentPathInstruction _environmentPathInstruction ;
21+ private readonly IShellShimMaker _shellShimMaker ;
22+ private readonly IReporter _reporter ;
23+
24+ private readonly string _packageId ;
25+ private readonly string _packageVersion ;
26+ private readonly string _configFilePath ;
27+ private readonly string _framework ;
28+ private readonly string _source ;
29+ private readonly bool _global ;
2530
2631 public InstallToolCommand (
2732 AppliedOption appliedCommand ,
28- ParseResult parseResult )
33+ ParseResult parseResult ,
34+ IToolPackageObtainer toolPackageObtainer = null ,
35+ IShellShimMaker shellShimMaker = null ,
36+ IEnvironmentPathInstruction environmentPathInstruction = null ,
37+ IReporter reporter = null )
2938 : base ( parseResult )
3039 {
3140 if ( appliedCommand == null )
@@ -39,6 +48,27 @@ public InstallToolCommand(
3948 _framework = appliedCommand . ValueOrDefault < string > ( "framework" ) ;
4049 _source = appliedCommand . ValueOrDefault < string > ( "source" ) ;
4150 _global = appliedCommand . ValueOrDefault < bool > ( "global" ) ;
51+
52+ var cliFolderPathCalculator = new CliFolderPathCalculator ( ) ;
53+ var executablePackagePath = new DirectoryPath ( cliFolderPathCalculator . ExecutablePackagesPath ) ;
54+ var offlineFeedPath = new DirectoryPath ( cliFolderPathCalculator . CliFallbackFolderPath ) ;
55+ _toolPackageObtainer = toolPackageObtainer ?? new ToolPackageObtainer (
56+ executablePackagePath ,
57+ offlineFeedPath ,
58+ ( ) => new DirectoryPath ( Path . GetTempPath ( ) )
59+ . WithSubDirectories ( Path . GetRandomFileName ( ) )
60+ . WithFile ( Path . GetRandomFileName ( ) + ".csproj" ) ,
61+ new Lazy < string > ( BundledTargetFramework . GetTargetFrameworkMoniker ) ,
62+ new PackageToProjectFileAdder ( ) ,
63+ new ProjectRestorer ( ) ) ;
64+
65+ _environmentPathInstruction = environmentPathInstruction
66+ ?? EnvironmentPathFactory
67+ . CreateEnvironmentPathInstruction ( ) ;
68+
69+ _shellShimMaker = shellShimMaker ?? new ShellShimMaker ( executablePackagePath . Value ) ;
70+
71+ _reporter = reporter ?? Reporter . Output ;
4272 }
4373
4474 public override int Execute ( )
@@ -48,33 +78,25 @@ public override int Execute()
4878 throw new GracefulException ( LocalizableStrings . InstallToolCommandOnlySupportGlobal ) ;
4979 }
5080
51- var cliFolderPathCalculator = new CliFolderPathCalculator ( ) ;
52- var executablePackagePath = new DirectoryPath ( cliFolderPathCalculator . ExecutablePackagesPath ) ;
53- var offlineFeedPath = new DirectoryPath ( cliFolderPathCalculator . CliFallbackFolderPath ) ;
54-
55- var toolConfigurationAndExecutablePath = ObtainPackage ( executablePackagePath , offlineFeedPath ) ;
81+ var toolConfigurationAndExecutablePath = ObtainPackage ( ) ;
5682
57- var shellShimMaker = new ShellShimMaker ( executablePackagePath . Value ) ;
5883 var commandName = toolConfigurationAndExecutablePath . Configuration . CommandName ;
59- shellShimMaker . EnsureCommandNameUniqueness ( commandName ) ;
84+ _shellShimMaker . EnsureCommandNameUniqueness ( commandName ) ;
6085
61- shellShimMaker . CreateShim (
86+ _shellShimMaker . CreateShim (
6287 toolConfigurationAndExecutablePath . Executable . Value ,
6388 commandName ) ;
6489
65- EnvironmentPathFactory
66- . CreateEnvironmentPathInstruction ( )
90+ _environmentPathInstruction
6791 . PrintAddPathInstructionIfPathDoesNotExist ( ) ;
6892
69- Reporter . Output . WriteLine (
93+ _reporter . WriteLine (
7094 string . Format ( LocalizableStrings . InstallationSucceeded , commandName ) ) ;
7195
7296 return 0 ;
7397 }
7498
75- private static ToolConfigurationAndExecutablePath ObtainPackage (
76- DirectoryPath executablePackagePath ,
77- DirectoryPath offlineFeedPath )
99+ private ToolConfigurationAndExecutablePath ObtainPackage ( )
78100 {
79101 try
80102 {
@@ -84,18 +106,7 @@ private static ToolConfigurationAndExecutablePath ObtainPackage(
84106 configFile = new FilePath ( _configFilePath ) ;
85107 }
86108
87- var toolPackageObtainer =
88- new ToolPackageObtainer (
89- executablePackagePath ,
90- offlineFeedPath ,
91- ( ) => new DirectoryPath ( Path . GetTempPath ( ) )
92- . WithSubDirectories ( Path . GetRandomFileName ( ) )
93- . WithFile ( Path . GetRandomFileName ( ) + ".csproj" ) ,
94- new Lazy < string > ( BundledTargetFramework . GetTargetFrameworkMoniker ) ,
95- new PackageToProjectFileAdder ( ) ,
96- new ProjectRestorer ( ) ) ;
97-
98- return toolPackageObtainer . ObtainAndReturnExecutablePath (
109+ return _toolPackageObtainer . ObtainAndReturnExecutablePath (
99110 packageId : _packageId ,
100111 packageVersion : _packageVersion ,
101112 nugetconfig : configFile ,
0 commit comments