private void ValidatePscxPath(string parameterName, PscxPathAttribute pathAttrib, PscxPathInfo pscxPath) { WriteDebug(String.Format("ValidatePscxPath: parameter {0} ; pscxPath {1}", parameterName, pscxPath)); if (pscxPath.IsValid == false) { // todo: localize string description = String.Format( "The path '{0}' supplied for {1} is invalid.", pscxPath, parameterName); // path syntax error OnPscxPathError(parameterName, description, PscxPathState.Invalid, pscxPath); } PscxPathType pathType = PscxPathType.Unknown; // explicit true or unset if (pathAttrib.ShouldExist || (pathAttrib._shouldExist == null)) { if (pathAttrib.ShouldExist) { // explicit true, so check existance ValidateExists(parameterName, pscxPath, ref pathType); } else { // shouldexist not specified, so grab path type via invokeprovider pathType = this.InvokeProvider.Item.IsContainer(pscxPath.ToString()) ? PscxPathType.Container : PscxPathType.Leaf; } PscxPathType expectedPathType = pathAttrib.PathType; // do we have a path type constraint? if (expectedPathType != PscxPathType.None) { ValidatePathType(parameterName, pscxPath, pathType, expectedPathType); } } else // shouldexist explicit false { // NOTE: for Pscx developers Trace.Assert(pathAttrib.PathType == PscxPathType.None, String.Format( "Pscx Developer Error: {0}\n\nIf a PathType constraint is placed on a parameter, " + "ShouldExist cannot be explicitly set to false. Remove " + "the ShouldExist condition from the attribute, or set it " + "explicitly to true.\n\nIf you are seeing this message " + "as a Pscx end-user, please log an issue on " + "http://www.codeplex.com/powershellcx", CmdletName)); } // any provider constraints defined on the PscxPath attribute, // or on a Type-level ProviderConstraint attribute? if ((pathAttrib.ProviderTypes != null) || (_defaultProviderConstraint != null)) { CheckProviderConstraints(pscxPath, pathAttrib, parameterName); } }
private void ValidatePathType(string parameterName, PscxPathInfo pscxPath, PscxPathType pathType, PscxPathType expectedPathType) { WriteDebug("ValidatePathType: expecting " + pathType); if ((pathType & expectedPathType) != pathType) { // TODO: localize string description = String.Format("Path type is {0}; expecting {1}", pathType, expectedPathType); // path type violation (terminating) OnPscxPathError(parameterName, description, PscxPathState.InvalidPathType, pscxPath); } }
/// <summary> /// /// </summary> /// <param name="pscxPath"></param> /// <param name="pathType"></param> /// <returns></returns> public static bool Exists(PscxPathInfo pscxPath, ref PscxPathType pathType) { string qualifiedPath = pscxPath.ToString(); pathType = PscxPathType.Unknown; string pathArg = (pscxPath.IsUnresolved) ? "LiteralPath" : "Path"; if (Exists(pscxPath)) { bool isLeaf = PipelineHelper.ExecuteScalar <bool>( new Command("Test-Path"), new CommandArgument() { Name = pathArg, Value = qualifiedPath }, new CommandArgument() { Name = "PathType", Value = "Leaf" }); if (isLeaf) { pathType = PscxPathType.Leaf; } else { bool isContainer = PipelineHelper.ExecuteScalar <bool>( new Command("Test-Path"), new CommandArgument() { Name = pathArg, Value = qualifiedPath }, new CommandArgument() { Name = "PathType", Value = "Container" }); if (isContainer) { pathType = PscxPathType.Container; } } return(true); } return(false); }
private void ValidateExists(string parameterName, PscxPathInfo pscxPath, ref PscxPathType pathType) { WriteDebug("ValidateExists"); bool exists = PscxPathInfo.Exists(pscxPath, ref pathType); if (!exists) { // TODO: localize string description = String.Format( "The path '{0}' supplied for {1} must exist.", pscxPath, parameterName); // terminates by default (unless overridden) OnPscxPathError(parameterName, description, PscxPathState.NotExist, pscxPath); } }
/// <summary> /// /// </summary> /// <param name="pscxPath"></param> /// <param name="pathType"></param> /// <returns></returns> public static bool Exists(PscxPathInfo pscxPath, ref PscxPathType pathType) { string qualifiedPath = pscxPath.ToString(); pathType = PscxPathType.Unknown; string pathArg = (pscxPath.IsUnresolved) ? "LiteralPath" : "Path"; if (Exists(pscxPath)) { bool isLeaf = PipelineHelper.ExecuteScalar<bool>( new Command("Test-Path"), new CommandArgument() {Name = pathArg, Value = qualifiedPath}, new CommandArgument() {Name = "PathType", Value = "Leaf"}); if (isLeaf) { pathType = PscxPathType.Leaf; } else { bool isContainer = PipelineHelper.ExecuteScalar<bool>( new Command("Test-Path"), new CommandArgument() {Name = pathArg, Value = qualifiedPath}, new CommandArgument() {Name = "PathType", Value = "Container"}); if (isContainer) { pathType = PscxPathType.Container; } } return true; } return false; }