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