Class: Mnenv::Shells::PowerShellShell

Inherits:
BaseShell
  • Object
show all
Defined in:
lib/mnenv/shells/power_shell.rb

Overview

PowerShell shell implementation (Windows)

Instance Method Summary collapse

Instance Method Details

#config_fileObject



101
102
103
# File 'lib/mnenv/shells/power_shell.rb', line 101

def config_file
  File.expand_path('~\Documents\PowerShell\Microsoft.PowerShell_profile.ps1')
end

#nameObject



9
10
11
# File 'lib/mnenv/shells/power_shell.rb', line 9

def name
  'powershell'
end

#shim_content(executable_name) ⇒ Object



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
# File 'lib/mnenv/shells/power_shell.rb', line 17

def shim_content(executable_name)
  <<~SHIM
    # mnenv shim for PowerShell
    $ErrorActionPreference = "Stop"

    $MNENV_ROOT = if ($env:MNENV_ROOT) { $env:MNENV_ROOT } else { "$env:USERPROFILE\\.mnenv" }

    # Resolve version and source using mnenv resolver
    $versionFile = "$MNENV_ROOT\\version"
    $sourceFile = "$MNENV_ROOT\\source"

    # Check environment variables first
    $VERSION = $env:METANORMA_VERSION
    $SOURCE = $env:METANORMA_SOURCE

    # Check local .metanorma-version file
    if (-not $VERSION) {
      $dir = (Get-Location).Path
      $root = [System.IO.Path]::GetPathRoot($dir)
      while ($dir -and $dir -ne $root) {
        $localVersionFile = Join-Path $dir ".metanorma-version"
        if (Test-Path $localVersionFile) {
          $VERSION = Get-Content $localVersionFile -Raw
          $VERSION = $VERSION.Trim()
          break
        }
        $localSourceFile = Join-Path $dir ".metanorma-source"
        if (-not $SOURCE -and (Test-Path $localSourceFile)) {
          $SOURCE = Get-Content $localSourceFile -Raw
          $SOURCE = $SOURCE.Trim()
        }
        $dir = Split-Path $dir -Parent
      }
    }

    # Check global version file
    if (-not $VERSION -and (Test-Path $versionFile)) {
      $VERSION = Get-Content $versionFile -Raw
      $VERSION = $VERSION.Trim()
    }

    if (-not $SOURCE -and (Test-Path $sourceFile)) {
      $SOURCE = Get-Content $sourceFile -Raw
      $SOURCE = $SOURCE.Trim()
    }

    # Default source
    if (-not $SOURCE) {
      $SOURCE = "gemfile"
    }

    if (-not $VERSION) {
      Write-Error "mnenv: version not set or invalid"
      Write-Error "Set a version with: mnenv global <version> or mnenv local <version>"
      exit 1
    }

    # Determine executable path based on source
    # Installed versions are in $MNENV_ROOT\\installed\\<version>-<source>\\
    if ($SOURCE -eq "binary") {
      $EXECUTABLE = "$MNENV_ROOT\\installed\\$VERSION-$SOURCE\\metanorma.exe"
    } else {
      # Bundler creates .cmd files on Windows, not .bat
      $EXECUTABLE = "$MNENV_ROOT\\installed\\$VERSION-$SOURCE\\bin\\#{executable_name}.cmd"
    }

    if (-not (Test-Path $EXECUTABLE)) {
      Write-Error "mnenv: #{executable_name} not installed for version $VERSION (source: $SOURCE)"
      Write-Error "Install it with: mnenv install $VERSION --source $SOURCE"
      exit 1
    }

    & $EXECUTABLE $args
  SHIM
end

#shim_extensionObject



13
14
15
# File 'lib/mnenv/shells/power_shell.rb', line 13

def shim_extension
  '.ps1'
end

#use_output(version, source) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/mnenv/shells/power_shell.rb', line 93

def use_output(version, source)
  lines = []
  lines << "$env:METANORMA_VERSION = '#{version}'"
  lines << "$env:METANORMA_SOURCE = '#{source}'" if source
  lines << '# Run this in your PowerShell session'
  lines.join("\n")
end

#windows?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/mnenv/shells/power_shell.rb', line 105

def windows?
  true
end