This repository was archived by the owner on Mar 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathCompareTextCommand.cs
More file actions
100 lines (85 loc) · 2.81 KB
/
Copy pathCompareTextCommand.cs
File metadata and controls
100 lines (85 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.IO;
using System.Management.Automation;
using DiffMatchPatch;
namespace Microsoft.PowerShell.TextUtility
{
public struct CompareTextDiff
{
public List<Diff> Diff;
}
public enum CompareTextView
{
Inline,
SideBySide,
}
[Cmdlet(VerbsData.Compare, "Text")]
[OutputType(typeof(CompareTextDiff))]
public sealed class CompareTextCommand : PSCmdlet
{
/// <summary>
/// Gets or sets the left hand side text to compare.
/// </summary>
[Parameter(Position=0, Mandatory=true)]
public string LeftText { get; set; }
/// <summary>
/// Gets or sets the right hand side text to compare.
/// </summary>
[Parameter(Position=1, Mandatory=true)]
public string RightText { get; set; }
/// <summary>
/// Gets or sets the view type.
/// </summary>
[Parameter()]
public CompareTextView View { get; set; }
private string _leftFile = null;
private string _rightFile = null;
protected override void BeginProcessing()
{
try
{
string leftFile = SessionState.Path.GetUnresolvedProviderPathFromPSPath(LeftText);
string rightFile = SessionState.Path.GetUnresolvedProviderPathFromPSPath(RightText);
if (File.Exists(leftFile))
{
_leftFile = leftFile;
LeftText = File.ReadAllText(_leftFile);
}
if (File.Exists(rightFile))
{
_rightFile = rightFile;
RightText = File.ReadAllText(_rightFile);
}
}
catch
{
// do nothing and treat as text
}
}
protected override void ProcessRecord()
{
diff_match_patch dmp = new diff_match_patch();
List<Diff> diff = dmp.diff_main(LeftText, RightText);
dmp.diff_cleanupSemantic(diff);
var output = new CompareTextDiff();
output.Diff = diff;
var psObj = new PSObject(output);
if (_leftFile != null)
{
psObj.Properties.Add(new PSNoteProperty("LeftFile", _leftFile));
}
if (_rightFile != null)
{
psObj.Properties.Add(new PSNoteProperty("RightFile", _rightFile));
}
if (View == CompareTextView.SideBySide)
{
psObj.TypeNames.Insert(0, "Microsoft.PowerShell.TextUtility.CompareTextDiff#SideBySide");
}
WriteObject(psObj);
}
}
}