feat(project): support PowerShell modules

resolves #3636
This commit is contained in:
Jan De Dobbeleer 2023-03-29 21:15:08 +02:00 committed by Jan De Dobbeleer
parent 17bd13b0fc
commit d8f1c4c942
3 changed files with 173 additions and 0 deletions

View file

@ -118,6 +118,11 @@ func (n *Project) Init(props properties.Properties, env platform.Environment) {
Files: []string{"JuliaProject.toml", "Project.toml"},
Fetcher: n.getProjectData,
},
{
Name: "powershell",
Files: []string{"*.psd1"},
Fetcher: n.getPowerShellModuleData,
},
}
}
@ -228,6 +233,40 @@ func (n *Project) getDotnetProject(_ ProjectItem) *ProjectData {
}
}
func (n *Project) getPowerShellModuleData(_ ProjectItem) *ProjectData {
files := n.env.LsDir(n.env.Pwd())
var content string
// get the first match only
for _, file := range files {
if filepath.Ext(file.Name()) == ".psd1" {
content = n.env.FileContent(file.Name())
break
}
}
data := &ProjectData{}
lines := strings.Split(content, "\n")
for _, line := range lines {
splitted := strings.SplitN(line, "=", 2)
if len(splitted) < 2 {
continue
}
key := strings.TrimSpace(splitted[0])
value := strings.TrimSpace(splitted[1])
value = strings.Trim(value, "'\"")
switch key {
case "ModuleVersion":
data.Version = value
case "RootModule":
data.Name = strings.TrimRight(value, ".psm1")
}
}
return data
}
func (n *Project) getProjectData(item ProjectItem) *ProjectData {
content := n.env.FileContent(item.Files[0])

View file

@ -378,3 +378,48 @@ func TestDotnetProject(t *testing.T) {
}
}
}
func TestPowerShellModuleProject(t *testing.T) {
cases := []struct {
Case string
HasFiles bool
ExpectedString string
ExpectedEnabled bool
}{
{
Case: "valid PowerShell module file",
HasFiles: true,
ExpectedEnabled: true,
ExpectedString: "\uf487 1.0.0.0 oh-my-posh",
},
}
for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On(hasFiles, testify_mock.Anything).Run(func(args testify_mock.Arguments) {
for _, c := range env.ExpectedCalls {
if c.Method == hasFiles {
c.ReturnArguments = testify_mock.Arguments{args.Get(0).(string) == "*.psd1"}
}
}
})
env.On("Pwd").Return("posh")
env.On("LsDir", "posh").Return([]fs.DirEntry{
&MockDirEntry{
name: "oh-my-posh.psd1",
},
})
var moduleContent string
if tc.HasFiles {
content, _ := os.ReadFile("../test/oh-my-posh.psd1")
moduleContent = string(content)
}
env.On("FileContent", "oh-my-posh.psd1").Return(moduleContent)
pkg := &Project{}
pkg.Init(properties.Map{}, env)
assert.Equal(t, tc.ExpectedEnabled, pkg.Enabled(), tc.Case)
if tc.ExpectedEnabled {
assert.Equal(t, tc.ExpectedString, renderTemplate(env, pkg.Template(), pkg), tc.Case)
}
}
}

89
src/test/oh-my-posh.psd1 Normal file
View file

@ -0,0 +1,89 @@
#
# Module manifest for module 'oh-my-posh'
#
# Generated by: Jan
#
# Generated on: 28-Jun-16
#
@{
# Version number of this module.
ModuleVersion = '1.0.0.0'
# Script module or binary module file associated with this manifest.
RootModule = 'oh-my-posh.psm1'
# ID used to uniquely identify this module
GUID = '7d7c4a78-e2fe-4e5f-9510-34ac893e4562'
# Company or vendor of this module
CompanyName = 'Unknown'
# Author of this module
Author = 'Jan De Dobbeleer'
# Copyright statement for this module
Copyright = '(c) 2018 Jan De Dobbeleer. All rights reserved.'
# Description of the functionality provided by this module
Description = 'Theming capabilities for the PowerShell prompt in ConEmu'
# Minimum version of the Windows PowerShell engine required by this module
PowerShellVersion = '5.0'
# List of all files packaged with this module
FileList = @('defaults.ps1', 'Themes/Agnoster.psm1', 'Themes/Avit.psm1', 'Themes/Darkblood.psm1', 'Themes/Fish.psm1', 'Themes/Honukai.psm1', 'Themes/Paradox.psm1', 'Themes/Sorin.psm1', 'Helpers/PoshGit.ps1', 'Helpers/Prompt.ps1')
# Cmdlets to export from this module
CmdletsToExport = @()
# Variables to export from this module
VariablesToExport = @()
# Aliases to export from this module
AliasesToExport = '*'
# Functions to export from this module
FunctionsToExport = @('Write-ColorPreview',
'Show-Colors',
'Show-ThemeColors',
'Show-ThemeSymbols',
'Get-Theme',
'Set-Theme',
'Get-VCSStatus',
'Get-VcsInfo',
'Get-FormattedRootLocation',
'Get-ShortPath',
'Get-FullPath',
'Set-CursorForRightBlockWrite',
'Set-CursorUp',
'Test-VirtualEnv',
'Get-VirtualEnvName',
'Test-NotDefaultUser',
'Test-Administrator',
'Get-ComputerName',
'Set-Newline',
'Get-BatteryInfo',
'Get-ThemesLocation'
'Set-Prompt')
# Private data to pass to the module specified in RootModule. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{
PSData = @{
# Tags applied to this module. These help with module discovery in online galleries.
Tags = @('git', 'agnoster', 'theme', 'zsh', 'posh-git', 'prompt', 'paradox')
# A URL to the license for this module.
LicenseUri = 'https://github.com/JanDeDobbeleer/oh-my-posh2/blob/master/LICENSE'
# A URL to the main website for this project.
ProjectUri = 'https://github.com/JanDeDobbeleer/oh-my-posh2'
} # End of PSData hashtable
} # End of PrivateData hashtable
}