-
Notifications
You must be signed in to change notification settings - Fork 0
/
CmdletEnvPath.cs
144 lines (126 loc) · 3.65 KB
/
CmdletEnvPath.cs
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Management.Automation;
using EnvPath;
namespace CmdletEnvPath
{
/// <summary>
/// Add path to Environment Path Variable
/// </summary>
[Cmdlet(VerbsCommon.Add, "EnvPath",HelpUri ="https://www.acfun.cn")]
[OutputType(typeof(List<string>))]
public class AddEnvPath : Cmdlet
{
[Parameter(
Mandatory = true,
Position = 0,
ValueFromPipeline = true)]
public string Path { get; set; }
[Parameter]
[Alias("Target")]
public EnvironmentVariableTarget EnvVarTarget { get; set; }
/// <summary>
/// if switch,no output.
/// </summary>
[Parameter]
public SwitchParameter Quiet { get; set; }
public AddEnvPath()
: base()
{
}
protected override void BeginProcessing()
{
base.BeginProcessing();
}
protected override void ProcessRecord()
{
base.ProcessRecord();
var envPath = EnvPaths.GetEnvPaths(EnvVarTarget);
if(EnvPaths.ExistsInUserAndMachine(Path))
{
if(!Quiet)
WriteObject($"{"Warning : The path is existed in machine/user path,so it will not be written."}{Environment.NewLine}{envPath.GetPaths()}");
}
else
{
if (envPath.AddPath(Path) != string.Empty)
{
if (Quiet)
envPath.SavePaths();
else
WriteObject($"{envPath.SavePaths()}");
}
}
}
protected override void EndProcessing()
{
base.EndProcessing();
}
}
/// <summary>
/// Get All Path of specified target.
/// </summary>
[Cmdlet(VerbsCommon.Get, "EnvPaths")]
[OutputType(typeof(List<string>))]
public class GetEnvPaths : Cmdlet
{
[Parameter]
[Alias("Target")]
public EnvironmentVariableTarget EnvVarTarget { get; set; }
public GetEnvPaths()
: base()
{
}
protected override void ProcessRecord()
{
base.ProcessRecord();
WriteObject(EnvPaths.GetEnvPaths(EnvVarTarget));
}
}
/// <summary>
/// Remove path
/// </summary>
[Cmdlet(VerbsCommon.Remove, "EnvPath")]
[OutputType(typeof(List<string>))]
public class RemoveEnvPath : Cmdlet
{
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
public string Path { get; set; }
[Parameter]
[Alias("Target")]
public EnvironmentVariableTarget EnvVarTarget { get; set; }
[Parameter]
public SwitchParameter Quiet { get; set; }
public RemoveEnvPath()
: base()
{
}
protected override void BeginProcessing()
{
base.BeginProcessing();
}
protected override void ProcessRecord()
{
base.ProcessRecord();
var envPath = EnvPaths.GetEnvPaths(EnvVarTarget);
if (EnvPaths.ExistsInUserAndMachine(Path))
{
envPath.RemovePath(Path);
envPath.SavePaths();
if(!Quiet)
WriteObject(envPath.GetPaths());
}
else
{
if(!Quiet)
{
WriteObject(envPath.GetPaths());
}
}
}
protected override void EndProcessing()
{
base.EndProcessing();
}
}
}