File size: 2,693 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.IO;
using System.Management.Automation;
using System.Management.Automation.Help;

using Microsoft.PowerShell.Commands;

namespace System.Management.Automation
{
    internal static class HelpUtils
    {
        private static string userHomeHelpPath = null;

        /// <summary>
        /// Get the path to $HOME.
        /// </summary>
        internal static string GetUserHomeHelpSearchPath()
        {
            if (userHomeHelpPath == null)
            {
#if UNIX
                var userModuleFolder = Platform.SelectProductNameForDirectory(Platform.XDG_Type.USER_MODULES);
                string userScopeRootPath = System.IO.Path.GetDirectoryName(userModuleFolder);
#else
                string userScopeRootPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PowerShell");
#endif
                userHomeHelpPath = Path.Combine(userScopeRootPath, "Help");
            }

            return userHomeHelpPath;
        }

        internal static string GetModuleBaseForUserHelp(string moduleBase, string moduleName)
        {
            string newModuleBase = moduleBase;

            // In case of inbox modules, the help is put under $PSHOME/<current_culture>,
            // since the dlls are not published under individual module folders, but under $PSHome.
            // In case of other modules, the help is under moduleBase/<current_culture> or
            // under moduleBase/<Version>/<current_culture>.
            // The code below creates a similar layout for CurrentUser scope.
            // If the scope is AllUsers, then the help goes under moduleBase.

            var userHelpPath = GetUserHomeHelpSearchPath();
            string moduleBaseParent = Directory.GetParent(moduleBase).Name;

            if (moduleBase.EndsWith(moduleName, StringComparison.OrdinalIgnoreCase))
            {
                // This module is not an inbox module, so help goes under <userHelpPath>/<moduleName>
                newModuleBase = Path.Combine(userHelpPath, moduleName);
            }
            else if (string.Equals(moduleBaseParent, moduleName, StringComparison.OrdinalIgnoreCase))
            {
                // This module has version folder.
                var moduleVersion = Path.GetFileName(moduleBase);
                newModuleBase = Path.Combine(userHelpPath, moduleName, moduleVersion);
            }
            else
            {
                // This module is inbox module, help should be under <userHelpPath>
                newModuleBase = userHelpPath;
            }

            return newModuleBase;
        }
    }
}