Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@ jobs:
ChocoApiKey: ${{ secrets.ChocoApiKey }}

steps:
- uses: actions/checkout@v2

- uses: actions/checkout@v4
with:
fetch-depth: 0 # Required for GitVersion to behave

- name: GitVersion
id: gitversion
uses: PoshCode/Actions/gitversion@v1
uses: PoshCode/Actions/gitversion@v1.0.1

- name: Install-RequiredModules
uses: PoshCode/Actions/install-requiredmodules@v1

- name: Build Module
run: .\build.ps1 -Build -SemVer "${{ steps.gitversion.outputs.LegacySemVerPadded }}"
run: .\build.ps1 -Build -SemVer "${{ steps.gitversion.outputs.SemVer }}"
shell: pwsh

- name: Upload Built Module
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: NexuShell.${{ steps.gitversion.outputs.LegacySemVerPadded }}
name: NexuShell.${{ steps.gitversion.outputs.SemVer }}
path: .\Output

- name: Test Module
run: .\build.ps1 -TestPrePublish -SemVer "${{ steps.gitversion.outputs.LegacySemVerPadded }}"
run: .\build.ps1 -TestPrePublish -SemVer "${{ steps.gitversion.outputs.SemVer }}"
shell: pwsh

- name: Upload Test Results
Expand All @@ -66,4 +68,4 @@ jobs:

- name: Publish Module
if: ${{ github.ref == 'refs/heads/main' }}
run: .\build.ps1 -DeployToGallery -SemVer "${{ steps.gitversion.outputs.LegacySemVerPadded }}"
run: .\build.ps1 -DeployToGallery -SemVer "${{ steps.gitversion.outputs.SemVer }}"
17 changes: 17 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build Module",
"type": "shell",
"command": "${workspaceFolder}\\Build.ps1 -Build",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
1 change: 1 addition & 0 deletions Build.psd1
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@{
ModuleManifest = ".\src\NexuShell.psd1"
SourceDirectories = "private", "public"
Suffix = "Suffix.ps1"
}
14 changes: 10 additions & 4 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ param(
[string]
$SemVer = $(
if (Get-Command gitversion -ErrorAction SilentlyContinue) {
(gitversion | ConvertFrom-Json).LegacySemVerPadded
if (([version]((gitversion /version).Split('+')[0])).Major -gt 5) {
gitversion /showvariable SemVer
} else {
gitversion /showvariable LegacySemVerPadded
}
}
)
)
Expand Down Expand Up @@ -92,13 +96,15 @@ process {
Compress-Archive -Path $root\Output\* -DestinationPath $PackageSource\tools\NexuShell.zip -Force #Added force to allow local testing without shenanigans

if (Test-Path "$PackageSource\tools\NexuShell.zip") {
choco pack $Nuspec.FullName --output-directory $root
choco pack $Nuspec.FullName --version $SemVer --output-directory $root
} else {
throw "Welp, ya need the zip in the tools folder, dumby"
}

Get-ChildItem $PackageSource -recurse -filter *.nupkg | ForEach-Object {
choco push $_.FullName -s https://push.chocolatey.org --api-key="'$($env:ChocoApiKey)'"
if ($env:ChocoApiKey) {
Get-ChildItem $PackageSource -Recurse -Filter *.nupkg | ForEach-Object {
choco push $_.FullName -s https://push.chocolatey.org --api-key="'$($env:ChocoApiKey)'"
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/NexuShell.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
FunctionsToExport = @()
CmdletsToExport = @()
VariablesToExport = @()
AliasesToExport = @('Get-NexusLifecycle','New-NexusLifecycle')
AliasesToExport = @('Get-NexusLifecycle','New-NexusLifecycle','Get-NexusUri')

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{
Expand Down
1 change: 1 addition & 0 deletions src/Suffix.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$script:InstalledNexusService = Get-NexusRepositoryServiceInstall -ErrorAction SilentlyContinue
6 changes: 5 additions & 1 deletion src/nuget/tools/chocolateyInstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ $PathSegment = @{
Core = "PowerShell\Modules\"

AllUsers = $env:ProgramFiles
CurrentUser = $PROFILE | Split-Path | Split-Path
CurrentUser = try {
$PROFILE | Split-Path | Split-Path
} catch {
Write-Warning "Profile is set to '$($PROFILE)'. Current user '$($env:USERNAME)' may not be able to install at user level."
}
}

$Parameters = Get-PackageParameters
Expand Down
40 changes: 40 additions & 0 deletions src/private/Get-NexusCertificateDomain.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function Get-NexusCertificateDomain {
<#
.SYNOPSIS
Returns the Certificate domain for the specified Nexus configuration, if present.

.DESCRIPTION
Uses the KeyTool to open the currently used KeyStore and grab the domain.

.PARAMETER DataDir
The path to the Sonatype Nexus data directory, e.g. C:\ProgramData\sonatype-work\nexus3.

.PARAMETER ProgramDir
The path to the Sonatype Nexus program files, e.g. C:\ProgramData\nexus.

.EXAMPLE
Get-NexusCertificateDomain -DataDir C:\ProgramData\sonatype-work\nexus3 -ProgramDir C:\ProgramData\nexus
#>
param(
[string]$DataDir = $script:InstalledNexusService.DataFolder,

[string]$ProgramDir = $script:InstalledNexusService.ProgramFolder
)
$Config = Get-NexusConfiguration -Path $DataDir\etc\nexus.properties
if ($Config.'nexus-args'.Split(',') -contains '${jetty.etc}/jetty-https.xml') {
[xml]$HttpsConfig = Get-Content $ProgramDir\etc\jetty\jetty-https.xml
$KeyToolPath = Join-Path $ProgramDir "jre/bin/keytool.exe"
$KeyStorePath = Join-Path (Join-Path $ProgramDir "etc/ssl") $HttpsConfig.SelectSingleNode("//Set[@name='KeyStorePath']").'#text'
$KeyStorePassword = $HttpsConfig.SelectSingleNode("//Set[@name='KeyStorePassword']").'#text'

if ((Test-Path $KeyToolPath) -and (Test-Path $KeyStorePath)) {
# Running in a job, as otherwise KeyTool fails when run without input
Start-Job {
$KeyToolOutput = $using:KeyStorePassword | & "$using:KeyToolPath" -list -v -keystore "$using:KeyStorePath" -J"-Duser.language=en" 2>$null
if ($KeyToolOutput -join "`n" -match "(?smi)Certificate\[1\]:\nOwner: CN=(?<Domain>.+?)(\n|,)") {
$Matches.Domain
}
} | Receive-Job -Wait
}
}
}
27 changes: 27 additions & 0 deletions src/private/Get-NexusConfiguration.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function Get-NexusConfiguration {
<#
.SYNOPSIS
Returns a list of settings configured in nexus.properties.

.DESCRIPTION
Retrieves the specified file (defaulting to the default install location) and returns each active setting.

.PARAMETER Path
The path to the properties file to return.

.EXAMPLE
Get-NexusConfiguration
# Returns all properties and values

.EXAMPLE
(Get-NexusConfiguration).'application-port-ssl'
# Returns the value for a single property, 'application-port-ssl'
#>
[CmdletBinding()]
param(
$Path = (Join-Path $script:InstalledNexusService.DataFolder "etc\nexus.properties")
)
Get-Content $Path | Where-Object {
$_ -and $_ -notmatch "^\W*#"
} | ConvertFrom-StringData
}
41 changes: 41 additions & 0 deletions src/private/Get-NexusRepositoryServiceInstall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function Get-NexusRepositoryServiceInstall {
<#
.SYNOPSIS
If found, returns the name of the Nexus service and the install and data directories it uses.

.DESCRIPTION
Checks all current services for services that run "nexus.exe", then returns the program and data directories for one/each.

.PARAMETER AllResults
To speed up execution, by default we check services until we find the first one running nexus.exe.
If you have more than one instance installed, you can use this switch to check all services.

.EXAMPLE
Get-NexusRepositoryInstallValues
#>
[CmdletBinding()]
param(
# By default, we assume there is only one service. This searches for all installed services.
[switch]$AllResults
)
# If you have a lot of services, searching them all may take longer -
# so we can stop searching when we find the first service matching nexus.exe.
$ResultCount = @{}
if (-not $AllResults) { $ResultCount.First = 1 }

$NexusService = Get-ChildItem HKLM:\System\CurrentControlSet\Services\ | Where-Object {
($ImagePath = Get-ItemProperty -Path $_.PSPath -Name ImagePath -ErrorAction SilentlyContinue) -and
$ImagePath.ImagePath.Trim('"''').EndsWith('\nexus.exe')
} | Select-Object @ResultCount

foreach ($Service in $NexusService) {
$ServiceName = $Service.PSChildName
$TargetFolder = (Get-ItemProperty -Path $Service.PSPath).ImagePath.Trim('"''') | Split-Path | Split-Path
$DataFolder = Convert-Path (Join-Path $TargetFolder "$((Get-Content $TargetFolder\bin\nexus.vmoptions) -match '^-Dkaraf.data=(?<RelativePath>.+)$' -replace '^-Dkaraf.data=')")
[PSCustomObject]@{
ServiceName = $ServiceName
ProgramFolder = $TargetFolder
DataFolder = $DataFolder
}
}
}
27 changes: 25 additions & 2 deletions src/private/Invoke-Nexus.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,21 @@ function Invoke-Nexus {

[Parameter(Mandatory)]
[String]
$Method
$Method,

[Parameter()]
[hashtable]
$Headers
)
process {
$UriBase = "$($protocol)://$($Hostname):$($port)$($ContextPath)"
$Uri = $UriBase + $UriSlug
if ($Headers) {
$local:header = $script:header.Clone()
$Headers.Keys.ForEach{
$header[$_] = $Headers[$_]
}
}
$Params = @{
Headers = $header
ContentType = $ContentType
Expand Down Expand Up @@ -70,6 +80,19 @@ function Invoke-Nexus {
$Params.Add('InFile',$File)
}

Invoke-RestMethod @Params
try {
Invoke-RestMethod @Params -ErrorAction Stop
} catch {
try {
$JsonMessage = $_.ErrorDetails.Message | ConvertFrom-Json -ErrorAction Stop
} catch {<# Not a Json message #>}
if ($JsonMessage) {
# If there's a message from the server, display it appropriately
Write-Error -Message $Message -Exception $_.Exception
} else {
# Otherwise, rethrow
throw
}
}
}
}
3 changes: 1 addition & 2 deletions src/public/Asset/Get-NexusAsset.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ function Get-NexusAsset {

$result.items

if ($($result.continuationToken)) {

while ($($result.continuationToken)) {
$urislug = "/service/rest/v1/assets?continuationToken=$($result.continuationToken)&repository=$($RepositoryName)"
$result = Invoke-Nexus -Urislug $urislug -Method GET
$result.items
Expand Down
53 changes: 53 additions & 0 deletions src/public/Asset/Remove-NexusRepositoryFolder.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
function Remove-NexusRepositoryFolder {
<#
.SYNOPSIS
Removes a given folder from a repository from the Nexus instance

.PARAMETER RepositoryName
The repository to remove from

.PARAMETER Name
The name of the folder to remove

.EXAMPLE
Remove-NexusRepositoryFolder -RepositoryName MyNuGetRepo -Name 'v3'
# Removes the v3 folder in the MyNuGetRepo repository
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$RepositoryName,

[Parameter(Mandatory)]
[string]$Name
)
end {
if (-not $header) {
throw "Not connected to Nexus server! Run Connect-NexusServer first."
}

$ApiParameters = @{
UriSlug = "/service/extdirect"
Method = "POST"
Body = @{
action = "coreui_Component"
method = "deleteFolder"
data = @(
$Name,
$RepositoryName
)
type = "rpc"
tid = Get-Random -Minimum 1 -Maximum 100
}
Headers = @{
"X-Nexus-UI" = "true"
}
}

$Result = Invoke-Nexus @ApiParameters

if (-not $Result.result.success) {
throw "Failed to delete folder: $($Result.result.message)"
}
}
}
Loading
Loading