| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Text; |
| | 4 | | using Mklinker.Abstractions; |
| | 5 | | using System.IO.Abstractions; |
| | 6 | | using System.Diagnostics; |
| | 7 | | using static Mklinker.ConfigLink; |
| | 8 | |
|
| | 9 | | namespace Mklinker { |
| | 10 | |
|
| | 11 | | class UnixLinker : ILinker { |
| | 12 | |
|
| | 13 | | readonly IConsole console; |
| | 14 | | readonly IFileSystem fileSystem; |
| | 15 | | readonly IProcess process; |
| | 16 | |
|
| 0 | 17 | | public bool verbose { get; set; } |
| | 18 | |
|
| 10 | 19 | | public UnixLinker(IConsole console, IFileSystem fileSystem, IProcess process) { |
| 5 | 20 | | this.console = console; |
| 5 | 21 | | this.fileSystem = fileSystem; |
| 5 | 22 | | this.process = process; |
| 5 | 23 | | } |
| | 24 | |
|
| 0 | 25 | | internal ProcessStartInfo GetProcessInfo(IFileSystem fileSystem, string resolvedSourcePath, string resolvedTargetPat |
| 0 | 26 | | return new ProcessStartInfo { |
| 0 | 27 | | FileName = "ln", |
| 0 | 28 | | Arguments = $"\"{ resolvedSourcePath }\"" + |
| 0 | 29 | | $" \"{ resolvedTargetPath }\"" + |
| 0 | 30 | | $" { GetLinkTypeArgument(fileSystem, linkType, resolvedSourcePath) }", |
| 0 | 31 | | RedirectStandardOutput = true, |
| 0 | 32 | | RedirectStandardError = true, |
| 0 | 33 | | UseShellExecute = false |
| 0 | 34 | | }; |
| 0 | 35 | | } |
| | 36 | |
|
| 5 | 37 | | internal string GetLinkTypeArgument(IFileSystem fileSystem, LinkType linkType, string resolvedSourcePath) { |
| 7 | 38 | | if (fileSystem.File.Exists(resolvedSourcePath)) { |
| 2 | 39 | | return linkType == LinkType.Hard ? "" : "-s"; |
| 5 | 40 | | } else if (fileSystem.Directory.Exists(resolvedSourcePath)) { |
| 2 | 41 | | return "-s"; |
| | 42 | | } |
| | 43 | |
|
| 1 | 44 | | return ""; |
| 5 | 45 | | } |
| | 46 | |
|
| 0 | 47 | | public bool CreateLink(string resolvedSourcePath, string resolvedTargetPath, ConfigLink.LinkType linkType) { |
| 0 | 48 | | if (!fileSystem.File.Exists(resolvedSourcePath) && !fileSystem.Directory.Exists(resolvedSourcePath)) { |
| 0 | 49 | | console.WriteLine("Path '{0}' does not exist!", resolvedSourcePath); |
| | 50 | |
|
| 0 | 51 | | return false; |
| | 52 | | } |
| | 53 | |
|
| 0 | 54 | | ProcessStartInfo processStartInfo = GetProcessInfo(fileSystem, resolvedSourcePath, resolvedTargetPath, linkType); |
| | 55 | |
|
| 0 | 56 | | if (verbose) { |
| 0 | 57 | | console.WriteLine ($"$ {processStartInfo.FileName} {processStartInfo.Arguments}"); |
| 0 | 58 | | } |
| | 59 | |
|
| 0 | 60 | | IProcess mklinkProcess = process.Start(processStartInfo); |
| 0 | 61 | | bool success = true; |
| | 62 | |
|
| 0 | 63 | | while (!mklinkProcess.StandardOutput.EndOfStream) { |
| 0 | 64 | | success = false; |
| 0 | 65 | | console.WriteLine(mklinkProcess.StandardOutput.ReadLine()); |
| 0 | 66 | | } |
| | 67 | |
|
| 0 | 68 | | while (!mklinkProcess.StandardError.EndOfStream) { |
| 0 | 69 | | success = false; |
| 0 | 70 | | console.WriteLine(mklinkProcess.StandardError.ReadLine()); |
| 0 | 71 | | } |
| | 72 | |
|
| 0 | 73 | | return success; |
| 0 | 74 | | } |
| | 75 | | } |
| | 76 | |
|
| | 77 | | } |