Skip to content

Commit

Permalink
Convert to use PSModule framework
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusStorhaug committed Sep 17, 2023
1 parent b0b690a commit c6440f8
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 287 deletions.
60 changes: 21 additions & 39 deletions .github/workflows/Publish.PublicIP.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,30 @@ name: Publish [PublicIP]

on:
push:
paths:
- scripts/**
- src/PublicIP/**
- .github/workflows/Publish.PublicIP.yml
branches:
- '*'
workflow_dispatch:

defaults:
run:
shell: pwsh

env:
ModuleName: 'PublicIP'
APIKey: ${{ secrets.apikey }}

jobs:
test:
runs-on: 'ubuntu-latest'
steps:
- name: Checkout repo
uses: actions/checkout@v3

- name: Install Pester
run: |
Get-Module -ListAvailable
- name: Run linting
run: |
Invoke-ScriptAnalyzer -Path .\src\PublicIP -Recurse -Verbose
prepare:
runs-on: 'ubuntu-latest'
steps:
- name: Checkout repo
uses: actions/checkout@v3

publish:
runs-on: 'ubuntu-latest'
BuildModule:
name: Build Module
runs-on: ubuntu-latest
steps:
- name: Checkout repo
- name: Checkout Code
uses: actions/checkout@v3

- name: Publish module
if: github.ref == 'refs/heads/main'
run: |
.\scripts\Publish-Module.ps1 -ModuleName $env:ModuleName -APIKey $env:APIKey -Verbose
- name: Build Module
uses: PSModule/Build-Module@main
with:
Verbose: true

- name: Test Module
uses: PSModule/Test-Module@main
with:
Verbose: true

- name: Release Module
uses: PSModule/Release-Module@main
with:
APIKey: ${{ secrets.APIKEY }}
Verbose: true
32 changes: 0 additions & 32 deletions scripts/Publish-Module.ps1

This file was deleted.

36 changes: 0 additions & 36 deletions scripts/Set-ModuleVersion.ps1

This file was deleted.

114 changes: 9 additions & 105 deletions src/PublicIP/PublicIP.psd1
Original file line number Diff line number Diff line change
@@ -1,131 +1,35 @@
#
# Module manifest for module 'PublicIP'
#
# Generated by: Marius Storhaug
#
# Generated on: 16.12.2021
#

@{

# Script module or binary module file associated with this manifest.
RootModule = 'PublicIP.psm1'

# Version number of this module.
ModuleVersion = '0.0.1'

# Supported PSEditions
CompatiblePSEditions = @('Core')

# ID used to uniquely identify this module
GUID = 'dcbfe52d-b875-4f8b-ac39-0f958f57f9ae'

# Author of this module
Author = 'Marius Storhaug'
Author = 'Marius Storhaug'

# Company or vendor of this module
CompanyName = 'Marius Storhaug'

# Copyright statement for this module
Copyright = '(c) Marius Storhaug. All rights reserved.'
CompanyName = 'Marius Storhaug'

# Description of the functionality provided by this module
Description = 'PowerShell Module for Public IP'

# Minimum version of the PowerShell engine required by this module
PowerShellVersion = '7.0'

# Name of the PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# ClrVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = '*'

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = '*'

# Variables to export from this module
VariablesToExport = '*'

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = '*'

# DSC resources to export from this module
# DscResourcesToExport = @()

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @()
Description = 'PowerShell Module for Public IP'

# 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 = @{
PrivateData = @{

PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
Tags = @('PublicIP', 'IP', 'Public', 'IPConfig', 'Config')
Tags = @('PublicIP', 'IP', 'Public', 'IPConfig', 'Config')

# A URL to the license for this module.
LicenseUri = 'https://github.com/MariusStorhaug/PublicIP/blob/main/LICENSE'
LicenseUri = 'https://github.com/MariusStorhaug/PublicIP/blob/main/LICENSE'

# A URL to the main website for this project.
ProjectUri = 'https://github.com/MariusStorhaug/PublicIP'
ProjectUri = 'https://github.com/MariusStorhaug/PublicIP'

# A URL to an icon representing this module.
IconUri = 'https://raw.githubusercontent.com/MariusStorhaug/PublicIP/main/icon/PowerShell_Core_6.0_icon.png'

# ReleaseNotes of this module
# ReleaseNotes = ''

# Prerelease string of this module
# Prerelease = ''

# Flag to indicate whether the module requires explicit user acceptance for install/update/save
RequireLicenseAcceptance = $false

# External dependent modules of this module
# ExternalModuleDependencies = @()
IconUri = 'https://raw.githubusercontent.com/MariusStorhaug/PublicIP/main/icon/PowerShell_Core_6.0_icon.png'

} # End of PSData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
HelpInfoURI = 'https://mariusstorhaug.github.io/PublicIP'

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''
HelpInfoURI = 'https://mariusstorhaug.github.io/PublicIP'

}
100 changes: 25 additions & 75 deletions src/PublicIP/PublicIP.psm1
Original file line number Diff line number Diff line change
@@ -1,82 +1,32 @@
$script:providerMap = @{
MyIP = 'https://api.myip.com/'
IPInfo = 'https://ipinfo.io/json'
}

Function Get-PublicIP {
param(
[Parameter(Mandatory = $false)]
[ValidateScript({$providerMap.Keys})]
$Provider = 'IPInfo'
)

Invoke-RestMethod -Uri $providerMap[$Provider]
}

Function Get-IPConfig {
$PublicIP = Get-PublicIP
$Time = Get-Date -Format yyyyMMdd-hhmmss.fffff
$Location = Get-GeoLocation
$LocalIPConfig = Get-NetIPAddress -AddressFamily IPv4 -Type Unicast | Where-Object PrefixOrigin -Match dhcp
$IPObj = [ordered]@{
PublicIP = $PublicIP.IP
PrivateIP = $LocalIPConfig.IPv4Address
PCName = $ENV:COMPUTERNAME
Time = $Time
Latitude = $Location.Latitude
Longitude = $Location.Longitude
}
return $IPObj
}

$IPConfigFilePath = "$([Environment]::GetFolderPath('MyDocuments'))\IPConfig.json"

Function Save-IPConfig {
$IPConfig = @()
$RestoredIPConfig = Restore-IPConfig
if ($null -ne $RestoredIPConfig) {
$IPConfig += $RestoredIPConfig
}
$CurrentIPConfig = Get-IPConfig

# Check if recent and current ip is the same
if ($IPConfig[-1].PublicIP -eq $CurrentIPConfig.PublicIP) {

} else {
"Public IP has changed since $($IPConfig[-1].Time)"
"Old PIP: $($IPConfig[-1].PublicIP)"
"New PIP: $($CurrentIPConfig.PublicIP)"
$IPConfig += $CurrentIPConfig

if (-not (Test-Path -Path $IPConfigFilePath)) {
New-Item -Path $IPConfigFilePath -Force | Out-Null
[Cmdletbinding()]
param()

Write-Verbose 'Importing subcomponents'
$Folders = 'classes', 'private', 'public'
# Import everything in these folders
Foreach ($Folder in $Folders) {
$Root = Join-Path -Path $PSScriptRoot -ChildPath $Folder
Write-Verbose "Processing folder: $Root"
if (Test-Path -Path $Root) {
Write-Verbose "Getting all files in $Root"
$Files = $null
$Files = Get-ChildItem -Path $Root -Include '*.ps1', '*.psm1' -Recurse
# dot source each file
foreach ($File in $Files) {
Write-Verbose "Importing $($File)"
Import-Module $File
Write-Verbose "Importing $($File): Done"
}

$IPConfig | ConvertTo-Json -AsArray | Set-Content -Path $IPConfigFilePath -Force
}
}

Function Restore-IPConfig {
if (Test-Path -Path $IPConfigFilePath) {
return Get-Content -Path $IPConfigFilePath | ConvertFrom-Json
}
$Param = @{
Function = (Get-ChildItem -Path "$PSScriptRoot\public" -Include '*.ps1' -Recurse).BaseName
Variable = '*'
Cmdlet = '*'
Alias = '*'
}

Function Get-GeoLocation {
Add-Type -AssemblyName System.Device #Required to access System.Device.Location namespace
$GeoWatcher = New-Object System.Device.Location.GeoCoordinateWatcher #Create the required object
$GeoWatcher.Start() #Begin resolving current locaton

while (($GeoWatcher.Status -ne 'Ready') -and ($GeoWatcher.Permission -ne 'Denied')) {
Start-Sleep -Milliseconds 100 #Wait for discovery.
}
if ($GeoWatcher.Permission -eq 'Denied') {
Write-Error 'Access Denied for Location Information'
} else {
$Location = $GeoWatcher.Position.Location | Select-Object Latitude, Longitude #Select the relevent results.
}
$GeoWatcher.Stop()
return $Location
}
Write-Verbose 'Exporting module members'

Export-ModuleMember -Function '*' -Alias '*' -Variable '*' -Cmdlet '*'
Export-ModuleMember @Param -Verbose
6 changes: 6 additions & 0 deletions src/PublicIP/private/common.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
$script:providerMap = @{
MyIP = 'https://api.myip.com/'
IPInfo = 'https://ipinfo.io/json'
}

$script:IPConfigFilePath = "$([Environment]::GetFolderPath('MyDocuments'))\IPConfig.json"
Loading

0 comments on commit c6440f8

Please sign in to comment.