Exemplo n.º 1
0
        internal static DirectoryInfo CreateModuleDirectory(PSCmdlet cmdlet, string moduleNameOrPath, bool force)
        {
            DirectoryInfo targetObject = null;

            try
            {
                string str = ModuleCmdletBase.ResolveRootedFilePath(moduleNameOrPath, cmdlet.Context);
                if (string.IsNullOrEmpty(str) && moduleNameOrPath.StartsWith(".", StringComparison.OrdinalIgnoreCase))
                {
                    str = Path.Combine(cmdlet.CurrentProviderLocation(cmdlet.Context.ProviderNames.FileSystem).ProviderPath, moduleNameOrPath);
                }
                if (string.IsNullOrEmpty(str))
                {
                    str = Path.Combine(ModuleIntrinsics.GetPersonalModulePath(), moduleNameOrPath);
                }
                targetObject = new DirectoryInfo(str);
                if (targetObject.Exists)
                {
                    if (!force)
                    {
                        ErrorDetails details     = new ErrorDetails(string.Format(CultureInfo.InvariantCulture, PathUtilsStrings.ExportPSSession_ErrorDirectoryExists, new object[] { targetObject.FullName }));
                        ErrorRecord  errorRecord = new ErrorRecord(new ArgumentException(details.Message), "ExportProxyCommand_OutputDirectoryExists", ErrorCategory.ResourceExists, targetObject);
                        cmdlet.ThrowTerminatingError(errorRecord);
                    }
                    return(targetObject);
                }
                targetObject.Create();
            }
            catch (Exception exception)
            {
                CommandProcessorBase.CheckForSevereException(exception);
                ErrorDetails details2 = new ErrorDetails(string.Format(CultureInfo.InvariantCulture, PathUtilsStrings.ExportPSSession_CannotCreateOutputDirectory, new object[] { moduleNameOrPath, exception.Message }));
                ErrorRecord  record2  = new ErrorRecord(new ArgumentException(details2.Message, exception), "ExportProxyCommand_CannotCreateOutputDirectory", ErrorCategory.ResourceExists, moduleNameOrPath);
                cmdlet.ThrowTerminatingError(record2);
            }
            return(targetObject);
        }
Exemplo n.º 2
0
        internal static DirectoryInfo CreateModuleDirectory(PSCmdlet cmdlet, string moduleNameOrPath, bool force)
        {
            Dbg.Assert(cmdlet != null, "Caller should verify cmdlet != null");
            Dbg.Assert(!string.IsNullOrEmpty(moduleNameOrPath), "Caller should verify !string.IsNullOrEmpty(moduleNameOrPath)");

            DirectoryInfo directoryInfo = null;

            try
            {
                // Even if 'moduleNameOrPath' is a rooted path, 'ResolveRootedFilePath' may return null when the path doesn't exist yet,
                // or when it contains wildcards but cannot be resolved to a single path.
                string rootedPath = ModuleCmdletBase.ResolveRootedFilePath(moduleNameOrPath, cmdlet.Context);
                if (string.IsNullOrEmpty(rootedPath) && moduleNameOrPath.StartsWith('.'))
                {
                    PathInfo currentPath = cmdlet.CurrentProviderLocation(cmdlet.Context.ProviderNames.FileSystem);
                    rootedPath = Path.Combine(currentPath.ProviderPath, moduleNameOrPath);
                }

                if (string.IsNullOrEmpty(rootedPath))
                {
                    if (Path.IsPathRooted(moduleNameOrPath))
                    {
                        rootedPath = moduleNameOrPath;
                    }
                    else
                    {
                        string personalModuleRoot = ModuleIntrinsics.GetPersonalModulePath();
                        if (string.IsNullOrEmpty(personalModuleRoot))
                        {
                            cmdlet.ThrowTerminatingError(
                                new ErrorRecord(
                                    new ArgumentException(StringUtil.Format(PathUtilsStrings.ExportPSSession_ErrorModuleNameOrPath, moduleNameOrPath)),
                                    "ExportPSSession_ErrorModuleNameOrPath",
                                    ErrorCategory.InvalidArgument,
                                    cmdlet));
                        }

                        rootedPath = Path.Combine(personalModuleRoot, moduleNameOrPath);
                    }
                }

                directoryInfo = new DirectoryInfo(rootedPath);
                if (directoryInfo.Exists)
                {
                    if (!force)
                    {
                        string errorMessage = string.Format(
                            CultureInfo.InvariantCulture, // directory name should be treated as culture-invariant
                            PathUtilsStrings.ExportPSSession_ErrorDirectoryExists,
                            directoryInfo.FullName);
                        ErrorDetails details     = new ErrorDetails(errorMessage);
                        ErrorRecord  errorRecord = new ErrorRecord(
                            new ArgumentException(details.Message),
                            "ExportProxyCommand_OutputDirectoryExists",
                            ErrorCategory.ResourceExists,
                            directoryInfo);
                        cmdlet.ThrowTerminatingError(errorRecord);
                    }
                }
                else
                {
                    directoryInfo.Create();
                }
            }
            catch (Exception e)
            {
                string errorMessage = string.Format(
                    CultureInfo.InvariantCulture, // directory name should be treated as culture-invariant
                    PathUtilsStrings.ExportPSSession_CannotCreateOutputDirectory,
                    moduleNameOrPath,
                    e.Message);
                ErrorDetails details     = new ErrorDetails(errorMessage);
                ErrorRecord  errorRecord = new ErrorRecord(
                    new ArgumentException(details.Message, e),
                    "ExportProxyCommand_CannotCreateOutputDirectory",
                    ErrorCategory.ResourceExists,
                    moduleNameOrPath);
                cmdlet.ThrowTerminatingError(errorRecord);
            }

            return(directoryInfo);
        }
Exemplo n.º 3
0
        private void ProcessTypeFiles()
        {
            Collection <string> collection  = UpdateData.Glob(base.PrependPath, "TypesPrependPathException", this);
            Collection <string> collection2 = UpdateData.Glob(base.AppendPath, "TypesAppendPathException", this);

            if (((base.PrependPath.Length <= 0) && (base.AppendPath.Length <= 0)) || ((collection.Count != 0) || (collection2.Count != 0)))
            {
                string updateTypeDataAction = UpdateDataStrings.UpdateTypeDataAction;
                string updateTarget         = UpdateDataStrings.UpdateTarget;
                if (base.Context.RunspaceConfiguration != null)
                {
                    for (int i = collection.Count - 1; i >= 0; i--)
                    {
                        string target = string.Format(CultureInfo.InvariantCulture, updateTarget, new object[] { collection[i] });
                        if (base.ShouldProcess(target, updateTypeDataAction))
                        {
                            base.Context.RunspaceConfiguration.Types.Prepend(new TypeConfigurationEntry(collection[i]));
                        }
                    }
                    foreach (string str4 in collection2)
                    {
                        string str5 = string.Format(CultureInfo.InvariantCulture, updateTarget, new object[] { str4 });
                        if (base.ShouldProcess(str5, updateTypeDataAction))
                        {
                            base.Context.RunspaceConfiguration.Types.Append(new TypeConfigurationEntry(str4));
                        }
                    }
                    try
                    {
                        base.Context.CurrentRunspace.RunspaceConfiguration.Types.Update(true);
                        return;
                    }
                    catch (RuntimeException exception)
                    {
                        base.WriteError(new ErrorRecord(exception, "TypesXmlUpdateException", ErrorCategory.InvalidOperation, null));
                        return;
                    }
                }
                if (base.Context.InitialSessionState != null)
                {
                    HashSet <string> set = new HashSet <string>(StringComparer.CurrentCultureIgnoreCase);
                    Collection <SessionStateTypeEntry> collection3 = new Collection <SessionStateTypeEntry>();
                    for (int j = collection.Count - 1; j >= 0; j--)
                    {
                        string str6 = string.Format(CultureInfo.InvariantCulture, updateTarget, new object[] { collection[j] });
                        string item = ModuleCmdletBase.ResolveRootedFilePath(collection[j], base.Context) ?? collection[j];
                        if (base.ShouldProcess(str6, updateTypeDataAction) && !set.Contains(item))
                        {
                            set.Add(item);
                            collection3.Add(new SessionStateTypeEntry(collection[j]));
                        }
                    }
                    foreach (SessionStateTypeEntry entry in base.Context.InitialSessionState.Types)
                    {
                        if (entry.FileName != null)
                        {
                            string str8 = ModuleCmdletBase.ResolveRootedFilePath(entry.FileName, base.Context) ?? entry.FileName;
                            if (!set.Contains(str8))
                            {
                                set.Add(str8);
                                collection3.Add(entry);
                            }
                        }
                        else
                        {
                            collection3.Add(entry);
                        }
                    }
                    foreach (string str9 in collection2)
                    {
                        string str10 = string.Format(CultureInfo.InvariantCulture, updateTarget, new object[] { str9 });
                        string str11 = ModuleCmdletBase.ResolveRootedFilePath(str9, base.Context) ?? str9;
                        if (base.ShouldProcess(str10, updateTypeDataAction) && !set.Contains(str11))
                        {
                            set.Add(str11);
                            collection3.Add(new SessionStateTypeEntry(str9));
                        }
                    }
                    base.Context.InitialSessionState.Types.Clear();
                    Collection <string> errors = new Collection <string>();
                    bool clearTable            = true;
                    foreach (SessionStateTypeEntry entry2 in collection3)
                    {
                        try
                        {
                            if (entry2.TypeTable != null)
                            {
                                PSInvalidOperationException exception2 = new PSInvalidOperationException(UpdateDataStrings.CannotUpdateTypeWithTypeTable);
                                base.WriteError(new ErrorRecord(exception2, "CannotUpdateTypeWithTypeTable", ErrorCategory.InvalidOperation, null));
                                continue;
                            }
                            if (entry2.FileName != null)
                            {
                                bool flag2;
                                base.Context.TypeTable.Update(entry2.FileName, errors, clearTable, base.Context.AuthorizationManager, base.Context.InitialSessionState.Host, out flag2);
                            }
                            else
                            {
                                base.Context.TypeTable.Update(entry2.TypeData, errors, entry2.IsRemove, clearTable);
                            }
                        }
                        catch (RuntimeException exception3)
                        {
                            base.WriteError(new ErrorRecord(exception3, "TypesXmlUpdateException", ErrorCategory.InvalidOperation, null));
                        }
                        base.Context.InitialSessionState.Types.Add(entry2);
                        if (errors.Count > 0)
                        {
                            foreach (string str12 in errors)
                            {
                                RuntimeException exception4 = new RuntimeException(str12);
                                base.WriteError(new ErrorRecord(exception4, "TypesXmlUpdateException", ErrorCategory.InvalidOperation, null));
                            }
                            errors.Clear();
                        }
                        clearTable = false;
                    }
                }
            }
        }
Exemplo n.º 4
0
 protected override void ProcessRecord()
 {
     if (base.ParameterSetName == "RemoveFileSet")
     {
         string removeTypeFileAction    = UpdateDataStrings.RemoveTypeFileAction;
         string updateTarget            = UpdateDataStrings.UpdateTarget;
         Collection <string> collection = UpdateData.Glob(this.typeFiles, "TypePathException", this);
         if (collection.Count != 0)
         {
             Dictionary <string, List <int> > fileNameToIndexMap = new Dictionary <string, List <int> >(StringComparer.OrdinalIgnoreCase);
             List <int> list = new List <int>();
             if (base.Context.RunspaceConfiguration != null)
             {
                 for (int i = 0; i < base.Context.RunspaceConfiguration.Types.Count; i++)
                 {
                     string fileName = base.Context.RunspaceConfiguration.Types[i].FileName;
                     if (fileName != null)
                     {
                         ConstructFileToIndexMap(fileName, i, fileNameToIndexMap);
                     }
                 }
             }
             else if (base.Context.InitialSessionState != null)
             {
                 for (int j = 0; j < base.Context.InitialSessionState.Types.Count; j++)
                 {
                     string filePath = base.Context.InitialSessionState.Types[j].FileName;
                     if (filePath != null)
                     {
                         filePath = ModuleCmdletBase.ResolveRootedFilePath(filePath, base.Context) ?? filePath;
                         ConstructFileToIndexMap(filePath, j, fileNameToIndexMap);
                     }
                 }
             }
             foreach (string str5 in collection)
             {
                 string target = string.Format(CultureInfo.InvariantCulture, updateTarget, new object[] { str5 });
                 if (base.ShouldProcess(target, removeTypeFileAction))
                 {
                     List <int> list2;
                     if (fileNameToIndexMap.TryGetValue(str5, out list2))
                     {
                         list.AddRange(list2);
                     }
                     else
                     {
                         base.WriteError(this.NewError("TypeFileNotExistsInCurrentSession", "TypeFileNotExistsInCurrentSession", null, new object[] { str5 }));
                     }
                 }
             }
             if (list.Count > 0)
             {
                 list.Sort();
                 for (int k = list.Count - 1; k >= 0; k--)
                 {
                     if (base.Context.RunspaceConfiguration != null)
                     {
                         base.Context.RunspaceConfiguration.Types.RemoveItem(list[k]);
                     }
                     else if (base.Context.InitialSessionState != null)
                     {
                         base.Context.InitialSessionState.Types.RemoveItem(list[k]);
                     }
                 }
                 try
                 {
                     if (base.Context.RunspaceConfiguration != null)
                     {
                         base.Context.RunspaceConfiguration.Types.Update();
                     }
                     else if (base.Context.InitialSessionState != null)
                     {
                         bool refreshTypeAndFormatSetting = base.Context.InitialSessionState.RefreshTypeAndFormatSetting;
                         try
                         {
                             base.Context.InitialSessionState.RefreshTypeAndFormatSetting = true;
                             base.Context.InitialSessionState.UpdateTypes(base.Context, false, false);
                         }
                         finally
                         {
                             base.Context.InitialSessionState.RefreshTypeAndFormatSetting = refreshTypeAndFormatSetting;
                         }
                     }
                 }
                 catch (RuntimeException exception)
                 {
                     base.WriteError(new ErrorRecord(exception, "TypesFileRemoveException", ErrorCategory.InvalidOperation, null));
                 }
             }
         }
     }
     else
     {
         string removeTypeDataAction = UpdateDataStrings.RemoveTypeDataAction;
         string removeTypeDataTarget = UpdateDataStrings.RemoveTypeDataTarget;
         string typeName             = null;
         if (base.ParameterSetName == "RemoveTypeDataSet")
         {
             typeName = this.typeData.TypeName;
         }
         else
         {
             if (string.IsNullOrWhiteSpace(this.typeName))
             {
                 base.ThrowTerminatingError(this.NewError("TargetTypeNameEmpty", "TargetTypeNameEmpty", this.typeName, new object[0]));
             }
             typeName = this.typeName;
         }
         System.Management.Automation.Runspaces.TypeData type = new System.Management.Automation.Runspaces.TypeData(typeName);
         string str10 = string.Format(CultureInfo.InvariantCulture, removeTypeDataTarget, new object[] { typeName });
         if (base.ShouldProcess(str10, removeTypeDataAction))
         {
             try
             {
                 Collection <string> errors = new Collection <string>();
                 base.Context.TypeTable.Update(type, errors, true, false);
                 if (errors.Count > 0)
                 {
                     foreach (string str11 in errors)
                     {
                         RuntimeException exception2 = new RuntimeException(str11);
                         base.WriteError(new ErrorRecord(exception2, "TypesDynamicRemoveException", ErrorCategory.InvalidOperation, null));
                     }
                 }
                 else if (base.Context.RunspaceConfiguration != null)
                 {
                     base.Context.RunspaceConfiguration.Types.Append(new TypeConfigurationEntry(type, true));
                 }
                 else if (base.Context.InitialSessionState != null)
                 {
                     base.Context.InitialSessionState.Types.Add(new SessionStateTypeEntry(type, true));
                 }
             }
             catch (RuntimeException exception3)
             {
                 base.WriteError(new ErrorRecord(exception3, "TypesDynamicRemoveException", ErrorCategory.InvalidOperation, null));
             }
         }
     }
 }