File size: 14,403 Bytes
8c763fb | 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Management.Automation.Language;
using System.Text;
using Dbg = System.Management.Automation.Diagnostics;
//
// Now define the set of commands for manipulating modules.
//
namespace Microsoft.PowerShell.Commands
{
#region Module Specification class
/// <summary>
/// Represents module specification written in a module manifest (i.e. in RequiredModules member/field).
///
/// Module manifest allows 2 forms of module specification:
/// 1. string - module name
/// 2. hashtable - [string]ModuleName (required) + [Version]ModuleVersion/RequiredVersion (required) + [Guid]GUID (optional)
///
/// so we have a constructor that takes a string and a constructor that takes a hashtable
/// (so that LanguagePrimitives.ConvertTo can cast a string or a hashtable to this type)
/// </summary>
public class ModuleSpecification
{
/// <summary>
/// Default constructor.
/// </summary>
public ModuleSpecification()
{
}
/// <summary>
/// Construct a module specification from the module name.
/// </summary>
/// <param name="moduleName">The module name.</param>
public ModuleSpecification(string moduleName)
{
ArgumentException.ThrowIfNullOrEmpty(moduleName);
this.Name = moduleName;
// Alias name of miniumVersion
this.Version = null;
this.RequiredVersion = null;
this.MaximumVersion = null;
this.Guid = null;
}
/// <summary>
/// Construct a module specification from a hashtable.
/// Keys can be ModuleName, ModuleVersion, and Guid.
/// ModuleName must be convertible to <see cref="string"/>.
/// ModuleVersion must be convertible to <see cref="Version"/>.
/// Guid must be convertible to <see cref="Guid"/>.
/// </summary>
/// <param name="moduleSpecification">The module specification as a hashtable.</param>
public ModuleSpecification(Hashtable moduleSpecification)
{
ArgumentNullException.ThrowIfNull(moduleSpecification);
var exception = ModuleSpecificationInitHelper(this, moduleSpecification);
if (exception != null)
{
throw exception;
}
}
/// <summary>
/// Initialize moduleSpecification from hashtable. Return exception object, if hashtable cannot be converted.
/// Return null, in the success case.
/// </summary>
/// <param name="moduleSpecification">Object to initialize.</param>
/// <param name="hashtable">Contains info about object to initialize.</param>
/// <returns></returns>
internal static Exception ModuleSpecificationInitHelper(ModuleSpecification moduleSpecification, Hashtable hashtable)
{
StringBuilder badKeys = new StringBuilder();
try
{
foreach (DictionaryEntry entry in hashtable)
{
string field = entry.Key.ToString();
if (field.Equals("ModuleName", StringComparison.OrdinalIgnoreCase))
{
moduleSpecification.Name = LanguagePrimitives.ConvertTo<string>(entry.Value);
}
else if (field.Equals("ModuleVersion", StringComparison.OrdinalIgnoreCase))
{
moduleSpecification.Version = LanguagePrimitives.ConvertTo<Version>(entry.Value);
}
else if (field.Equals("RequiredVersion", StringComparison.OrdinalIgnoreCase))
{
moduleSpecification.RequiredVersion = LanguagePrimitives.ConvertTo<Version>(entry.Value);
}
else if (field.Equals("MaximumVersion", StringComparison.OrdinalIgnoreCase))
{
moduleSpecification.MaximumVersion = LanguagePrimitives.ConvertTo<string>(entry.Value);
ModuleCmdletBase.GetMaximumVersion(moduleSpecification.MaximumVersion);
}
else if (field.Equals("GUID", StringComparison.OrdinalIgnoreCase))
{
moduleSpecification.Guid = LanguagePrimitives.ConvertTo<Guid?>(entry.Value);
}
else
{
if (badKeys.Length > 0)
{
badKeys.Append(", ");
}
badKeys.Append('\'');
badKeys.Append(entry.Key.ToString());
badKeys.Append('\'');
}
}
}
// catch all exceptions here, we are going to report them via return value.
// Example of caught exception: one of conversions to Version failed.
catch (Exception e)
{
return e;
}
string message;
if (badKeys.Length != 0)
{
message = StringUtil.Format(Modules.InvalidModuleSpecificationMember, "ModuleName, ModuleVersion, RequiredVersion, GUID", badKeys);
return new ArgumentException(message);
}
if (string.IsNullOrEmpty(moduleSpecification.Name))
{
message = StringUtil.Format(Modules.RequiredModuleMissingModuleName);
return new MissingMemberException(message);
}
if (moduleSpecification.RequiredVersion == null && moduleSpecification.Version == null && moduleSpecification.MaximumVersion == null)
{
message = StringUtil.Format(Modules.RequiredModuleMissingModuleVersion);
return new MissingMemberException(message);
}
if (moduleSpecification.RequiredVersion != null && moduleSpecification.Version != null)
{
message = StringUtil.Format(SessionStateStrings.GetContent_TailAndHeadCannotCoexist, "ModuleVersion", "RequiredVersion");
return new ArgumentException(message);
}
if (moduleSpecification.RequiredVersion != null && moduleSpecification.MaximumVersion != null)
{
message = StringUtil.Format(SessionStateStrings.GetContent_TailAndHeadCannotCoexist, "MaximumVersion", "RequiredVersion");
return new ArgumentException(message);
}
return null;
}
internal string GetRequiredModuleNotFoundVersionMessage()
{
if (RequiredVersion is not null)
{
return StringUtil.Format(
Modules.RequiredModuleNotFoundRequiredVersion,
Name,
RequiredVersion);
}
bool hasVersion = Version is not null;
bool hasMaximumVersion = MaximumVersion is not null;
if (hasVersion && hasMaximumVersion)
{
return StringUtil.Format(
Modules.RequiredModuleNotFoundModuleAndMaximumVersion,
Name,
Version,
MaximumVersion);
}
if (hasVersion)
{
return StringUtil.Format(
Modules.RequiredModuleNotFoundModuleVersion,
Name,
Version);
}
if (hasMaximumVersion)
{
return StringUtil.Format(
Modules.RequiredModuleNotFoundMaximumVersion,
Name,
MaximumVersion);
}
return StringUtil.Format(
Modules.RequiredModuleNotFoundWithoutVersion,
Name);
}
internal ModuleSpecification(PSModuleInfo moduleInfo)
{
ArgumentNullException.ThrowIfNull(moduleInfo);
this.Name = moduleInfo.Name;
this.Version = moduleInfo.Version;
this.Guid = moduleInfo.Guid;
}
/// <summary>
/// Implements ToString() for a module specification. If the specification
/// just contains a Name, then that is returned as is. Otherwise, the object is
/// formatted as a PowerSHell hashtable.
/// </summary>
/// <returns></returns>
public override string ToString()
{
if (Guid == null && Version == null && RequiredVersion == null && MaximumVersion == null)
{
return Name;
}
var moduleSpecBuilder = new StringBuilder();
moduleSpecBuilder.Append("@{ ModuleName = '").Append(Name).Append('\'');
if (Guid != null)
{
moduleSpecBuilder.Append("; Guid = '{").Append(Guid).Append("}' ");
}
if (RequiredVersion != null)
{
moduleSpecBuilder.Append("; RequiredVersion = '").Append(RequiredVersion).Append('\'');
}
else
{
if (Version != null)
{
moduleSpecBuilder.Append("; ModuleVersion = '").Append(Version).Append('\'');
}
if (MaximumVersion != null)
{
moduleSpecBuilder.Append("; MaximumVersion = '").Append(MaximumVersion).Append('\'');
}
}
moduleSpecBuilder.Append(" }");
return moduleSpecBuilder.ToString();
}
/// <summary>
/// Parse the specified string into a ModuleSpecification object.
/// </summary>
/// <param name="input">The module specification string.</param>
/// <param name="result">The ModuleSpecification object.</param>
/// <returns></returns>
public static bool TryParse(string input, out ModuleSpecification result)
{
result = null;
try
{
Hashtable hashtable;
if (Parser.TryParseAsConstantHashtable(input, out hashtable))
{
result = new ModuleSpecification(hashtable);
return true;
}
}
catch
{
// Ignoring the exceptions to return false
}
return false;
}
/// <summary>
/// Copy the module specification while normalizing the name
/// so that paths become absolute and use the right directory separators.
/// </summary>
/// <param name="context">The current execution context. Used for path normalization.</param>
/// <param name="basePath">The base path where a relative path should be interpreted with respect to.</param>
/// <returns>A fresh module specification object with the name normalized for use internally.</returns>
internal ModuleSpecification WithNormalizedName(ExecutionContext context, string basePath)
{
// Save allocating a new module spec if we don't need to change anything
if (!ModuleIntrinsics.IsModuleNamePath(Name))
{
return this;
}
return new ModuleSpecification()
{
Guid = Guid,
MaximumVersion = MaximumVersion,
Version = Version,
RequiredVersion = RequiredVersion,
Name = ModuleIntrinsics.NormalizeModuleName(Name, basePath, context)
};
}
/// <summary>
/// The module name.
/// </summary>
public string Name { get; internal set; }
/// <summary>
/// The module GUID, if specified.
/// </summary>
public Guid? Guid { get; internal set; }
/// <summary>
/// The module version number if specified, otherwise null.
/// </summary>
public Version Version { get; internal set; }
/// <summary>
/// The module maxVersion number if specified, otherwise null.
/// </summary>
public string MaximumVersion { get; internal set; }
/// <summary>
/// The exact version of the module if specified, otherwise null.
/// </summary>
public Version RequiredVersion { get; internal set; }
}
/// <summary>
/// Compares two ModuleSpecification objects for equality.
/// </summary>
internal class ModuleSpecificationComparer : IEqualityComparer<ModuleSpecification>
{
/// <summary>
/// Check if two module specifications are property-wise equal.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns>True if the specifications are equal, false otherwise.</returns>
public bool Equals(ModuleSpecification x, ModuleSpecification y)
{
if (x == y)
{
return true;
}
return x != null && y != null
&& string.Equals(x.Name, y.Name, StringComparison.OrdinalIgnoreCase)
&& Guid.Equals(x.Guid, y.Guid)
&& Version.Equals(x.RequiredVersion, y.RequiredVersion)
&& Version.Equals(x.Version, y.Version)
&& string.Equals(x.MaximumVersion, y.MaximumVersion);
}
/// <summary>
/// Get a property-based hashcode for a ModuleSpecification object.
/// </summary>
/// <param name="obj">The module specification for the object.</param>
/// <returns>A hashcode that is always the same for any module specification with the same properties.</returns>
public int GetHashCode(ModuleSpecification obj)
{
if (obj == null)
{
return 0;
}
return HashCode.Combine(obj.Name, obj.Guid, obj.RequiredVersion, obj.Version, obj.MaximumVersion);
}
}
#endregion
}
|