2017-11-27 10:45:43 -08:00
|
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
2018-01-13 09:40:48 -08:00
|
|
|
|
using Microsoft.DotNet.Tools;
|
2017-11-27 10:45:43 -08:00
|
|
|
|
using Microsoft.Extensions.EnvironmentAbstractions;
|
|
|
|
|
|
2017-12-04 14:13:24 -08:00
|
|
|
|
namespace Microsoft.DotNet.ShellShim
|
2017-11-27 10:45:43 -08:00
|
|
|
|
{
|
|
|
|
|
public class ShellShimMaker
|
|
|
|
|
{
|
|
|
|
|
private readonly string _pathToPlaceShim;
|
|
|
|
|
|
|
|
|
|
public ShellShimMaker(string pathToPlaceShim)
|
|
|
|
|
{
|
|
|
|
|
_pathToPlaceShim =
|
|
|
|
|
pathToPlaceShim ?? throw new ArgumentNullException(nameof(pathToPlaceShim));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CreateShim(string packageExecutablePath, string shellCommandName)
|
|
|
|
|
{
|
|
|
|
|
var packageExecutable = new FilePath(packageExecutablePath);
|
|
|
|
|
|
|
|
|
|
var script = new StringBuilder();
|
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
|
{
|
|
|
|
|
script.AppendLine("@echo off");
|
|
|
|
|
script.AppendLine($"dotnet {packageExecutable.ToQuotedString()} %*");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
script.AppendLine("#!/bin/sh");
|
|
|
|
|
script.AppendLine($"dotnet {packageExecutable.ToQuotedString()} \"$@\"");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FilePath scriptPath = GetScriptPath(shellCommandName);
|
|
|
|
|
File.WriteAllText(scriptPath.Value, script.ToString());
|
|
|
|
|
|
|
|
|
|
SetUserExecutionPermissionToShimFile(scriptPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void EnsureCommandNameUniqueness(string shellCommandName)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(Path.Combine(_pathToPlaceShim, shellCommandName)))
|
|
|
|
|
{
|
|
|
|
|
throw new GracefulException(
|
2018-01-13 09:40:48 -08:00
|
|
|
|
string.Format(CommonLocalizableStrings.FailInstallToolSameName,
|
|
|
|
|
shellCommandName));
|
2017-11-27 10:45:43 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Remove(string shellCommandName)
|
|
|
|
|
{
|
|
|
|
|
File.Delete(GetScriptPath(shellCommandName).Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private FilePath GetScriptPath(string shellCommandName)
|
|
|
|
|
{
|
|
|
|
|
var scriptPath = Path.Combine(_pathToPlaceShim, shellCommandName);
|
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
|
{
|
|
|
|
|
scriptPath += ".cmd";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new FilePath(scriptPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void SetUserExecutionPermissionToShimFile(FilePath scriptPath)
|
|
|
|
|
{
|
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return;
|
|
|
|
|
|
|
|
|
|
CommandResult result = new CommandFactory()
|
|
|
|
|
.Create("chmod", new[] {"u+x", scriptPath.Value})
|
|
|
|
|
.CaptureStdOut()
|
|
|
|
|
.CaptureStdErr()
|
|
|
|
|
.Execute();
|
|
|
|
|
|
|
|
|
|
if (result.ExitCode != 0)
|
|
|
|
|
{
|
|
|
|
|
throw new GracefulException(
|
2018-01-13 09:40:48 -08:00
|
|
|
|
string.Format(CommonLocalizableStrings.FailInstallToolPermission, result.StdErr,
|
|
|
|
|
result.StdOut));
|
2017-11-27 10:45:43 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|