Skip to content

Commit

Permalink
Update To Module.Template 2.0.1 and Fix Assert workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
ili101 authored Aug 6, 2019
1 parent 6dda4dd commit 70e90fd
Show file tree
Hide file tree
Showing 12 changed files with 490 additions and 338 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
*.history.zip
217 changes: 100 additions & 117 deletions Install.ps1
Original file line number Diff line number Diff line change
@@ -1,153 +1,141 @@
# Remove BOM from the file
[CmdLetBinding()]
<#
.SYNOPSIS
Installs module from Git clone or directly from GitHub.
File must not have BOM for GitHub deploy to work.
#>
[CmdletBinding(DefaultParameterSetName = 'Default')]
Param (
[ValidateNotNullOrEmpty()]
[String]$ModuleName,

# Path to install the module to, if not provided -Scope used.
[Parameter(Mandatory, ParameterSetName = 'ModulePath')]
[ValidateNotNullOrEmpty()]
[String]$ModulePath,

[ValidateNotNullOrEmpty()]
[Uri]$FromGitHub,

# Path to install the module to, PSModulePath "CurrentUser" or "AllUsers", if not provided "CurrentUser" used.
[Parameter(Mandatory, ParameterSetName = 'Scope')]
[ValidateSet('CurrentUser', 'AllUsers')]
[string]
$Scope = 'CurrentUser'
$Scope = 'CurrentUser',

# Get module from GitHub instead of local Git clone, for example "https://raw.githubusercontent.com/ili101/Module.Template/master/Install.ps1"
[ValidateNotNullOrEmpty()]
[Uri]$FromGitHub
)
# Set Files and Folders patterns to Include/Exclude.
$IncludeFiles = @(
'*.dll',
'*.psd1',
'*.psm1',
'*.ps1',
'morelinq*'
)
$ExcludeFiles = @(
'Install.ps1'
)


function Convert-LikeToMatch
{
function Invoke-MultiLike {
[alias("LikeAny")]
[CmdletBinding()]
param
(
[Parameter(Mandatory, ValueFromPipeLine)]
[String]$Filters
$InputObject,
[Parameter(Mandatory)]
[String[]]$Filters,
[Switch]$Not
)
begin
{
$Output = @()
}
process
{
$Filters = [regex]::Escape($Filters)
if ($Filters -match "^\\\*")
{
$Filters = $Filters.Remove(0, 2)
$FiltersRegex = foreach ($Filter In $Filters) {
$Filter = [regex]::Escape($Filter)
if ($Filter -match "^\\\*") {
$Filter = $Filter.Remove(0, 2)
}
else
{
$Filters = '^' + $Filters
else {
$Filter = '^' + $Filter
}
if ($Filters -match "\\\*$")
{
$Filters = $Filters.Substring(0, $Filters.Length - 2)
if ($Filter -match "\\\*$") {
$Filter = $Filter.Substring(0, $Filter.Length - 2)
}
else
{
$Filters = $Filters + '$'
else {
$Filter = $Filter + '$'
}
$Output += $Filters
$Filter
}
if ($Not) {
$InputObject -notmatch ($FiltersRegex -join '|').replace('\*', '.*').replace('\?', '.')
}
end
{
($Output -join '|').replace('\*', '.*').replace('\?', '.')
else {
$InputObject -match ($FiltersRegex -join '|').replace('\*', '.*').replace('\?', '.')
}
}

Try
{
Try {
Write-Verbose -Message 'Module installation started'

if (!$ModulePath)
{
if ($Scope -eq 'CurrentUser')
{
$ModulePath = ($Env:PSModulePath -split ';')[0]
if (!$ModulePath) {
if ($Scope -eq 'CurrentUser') {
$ModulePathIndex = 0
}
else
{
$ModulePath = ($Env:PSModulePath -split ';')[1]
else {
$ModulePathIndex = 1
}
if ($IsLinux) {
$ModulePathSeparator = ':'
}
else {
$ModulePathSeparator = ';'
}
$ModulePath = ($env:PSModulePath -split $ModulePathSeparator)[$ModulePathIndex]
}

$Files = @(
'*.dll',
'*.psd1',
'*.psm1',
'*.ps1',
'morelinq*'
)
$ExcludeFiles = @(
'Install.ps1'
)

if ($FromGitHub)
{
# Get $ModuleName, $TargetPath, [$Links]
if ($FromGitHub) {
# Fix Could not create SSL/TLS secure channel
$SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#$SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol
#[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$WebClient = [System.Net.WebClient]::new()
#$GitUri = ($FromGitHub -Split '/raw/')[0]
$GitUri = $FromGitHub.AbsolutePath.Split('/')[1, 2] -join '/'
$GitBranch = $FromGitHub.AbsolutePath.Split('/')[3]
#$Links = ((Invoke-WebRequest -Uri $GitUri).Links | Where-Object {$_.innerText -match '^.'+($Files -join '$|^.')+'$' -and $_.innerText -notmatch '^'+($ExcludeFiles -join '$|^.')+'$' -and $_.class -eq 'js-navigation-open'}).innerText
$Links = (Invoke-RestMethod -Uri "https://api.github.com/repos/$GitUri/contents" -Body @{ref = $GitBranch}) | Where-Object {$_.name -match ($Files | Convert-LikeToMatch) -and $_.name -notmatch ($ExcludeFiles | Convert-LikeToMatch)}
$Links = (Invoke-RestMethod -Uri "https://api.github.com/repos/$GitUri/contents" -Body @{ref = $GitBranch }) | Where-Object { (LikeAny $_.name $IncludeFiles) -and (LikeAny $_.name $ExcludeFiles -Not) }

$ModuleName = [System.IO.Path]::GetFileNameWithoutExtension(($Links | Where-Object {$_.name -like '*.psm1'}))
$ModuleVersion = (. ([Scriptblock]::Create((Invoke-WebRequest -Uri ($Links | Where-Object {$_.name -eq "$ModuleName.psd1"}).download_url)))).ModuleVersion
$ModuleName = [System.IO.Path]::GetFileNameWithoutExtension(($Links | Where-Object { $_.name -like '*.psm1' }).name)
$ModuleVersion = (. ([Scriptblock]::Create((Invoke-WebRequest -Uri ($Links | Where-Object { $_.name -eq "$ModuleName.psd1" }).download_url)))).ModuleVersion
}
else
{
else {
$ModuleName = [System.IO.Path]::GetFileNameWithoutExtension((Get-ChildItem -File -Filter *.psm1 -Name -Path $PSScriptRoot))
$ModuleVersion = (. ([Scriptblock]::Create((Get-Content -Path "$PSScriptRoot\$ModuleName.psd1" | Out-String)))).ModuleVersion
$ModuleVersion = (. ([Scriptblock]::Create((Get-Content -Path (Join-Path $PSScriptRoot "$ModuleName.psd1") | Out-String)))).ModuleVersion
}

$TargetPath = Join-Path -Path $ModulePath -ChildPath $ModuleName
$TargetPath = Join-Path -Path $TargetPath -ChildPath $ModuleVersion

# Create Directory
if (-not (Test-Path -Path $TargetPath))
{
if (-not (Test-Path -Path $TargetPath)) {
$null = New-Item -Path $TargetPath -ItemType Directory -ErrorAction Stop
Write-Verbose -Message "$ModuleName created module folder '$TargetPath'"
Write-Verbose -Message ('Created module folder: "{0}"' -f $TargetPath)
}

# Copy Files
if ($FromGitHub)
{

foreach ($Link in $Links)
{
if ($FromGitHub) {
foreach ($Link in $Links) {
$TargetPathItem = Join-Path -Path $TargetPath -ChildPath $Link.name
if ($Link.type -ne 'dir')
{
if ($Link.type -ne 'dir') {
$WebClient.DownloadFile($Link.download_url, $TargetPathItem)
#$File = Get-Content "$TargetPath\$_"
#$File | Set-Content "$TargetPath\$_"
Write-Verbose -Message ("{0} installed module file '{1}'" -f $ModuleName, $Link.name)
Write-Verbose -Message ('Installed module file: "{0}"' -f $Link.name)
}
else
{
if (-not (Test-Path -Path $TargetPathItem))
{
else {
if (-not (Test-Path -Path $TargetPathItem)) {
$null = New-Item -Path $TargetPathItem -ItemType Directory -ErrorAction Stop
Write-Verbose -Message "$ModuleName created module folder '$TargetPathItem'"
Write-Verbose -Message 'Created module folder: "{0}"' -f $TargetPathItem
}
$SubLinks = (Invoke-RestMethod -Uri $Link.git_url -Body @{recursive = '1'}).tree #| Where-Object 'type' -EQ 'blob'
foreach ($SubLink in $SubLinks)
{
$SubLinks = (Invoke-RestMethod -Uri $Link.git_url -Body @{recursive = '1' }).tree
foreach ($SubLink in $SubLinks) {
$TargetPathSub = Join-Path -Path $TargetPathItem -ChildPath $SubLink.path
if ($SubLink.'type' -EQ 'tree')
{
if (-not (Test-Path -Path $TargetPathSub))
{
if ($SubLink.'type' -EQ 'tree') {
if (-not (Test-Path -Path $TargetPathSub)) {
$null = New-Item -Path $TargetPathSub -ItemType Directory -ErrorAction Stop
Write-Verbose -Message "$ModuleName created module folder '$TargetPathSub'"
Write-Verbose -Message 'Created module folder: "{0}"' -f $TargetPathSub
}
}
else
{
else {
$WebClient.DownloadFile(
('https://raw.githubusercontent.com/{0}/{1}/{2}/{3}' -f $GitUri, $GitBranch, $Link.name, $SubLink.path),
$TargetPathSub
Expand All @@ -157,35 +145,30 @@ Try
}
}
}
else
{
Get-ChildItem -Path $PSScriptRoot -Exclude $ExcludeFiles | Where-Object -Property Name -Match ($Files | Convert-LikeToMatch) | ForEach-Object {
if ($_.Attributes -ne 'Directory')
{
else {
Get-ChildItem -Path $PSScriptRoot -Exclude $ExcludeFiles | Where-Object { LikeAny $_.Name $IncludeFiles } | ForEach-Object {
if ($_.Attributes -ne 'Directory') {
Copy-Item -Path $_ -Destination $TargetPath
Write-Verbose -Message ("{0} installed module file '{1}'" -f $ModuleName, $_)
Write-Verbose -Message ('Installed module file "{0}"' -f $_)
}
else
{
else {
Copy-Item -Path $_ -Destination $TargetPath -Recurse -Force
Write-Verbose -Message ("{0} installed module file '{1}'" -f $ModuleName, $_)
Write-Verbose -Message ('Installed module folder "{0}"' -f $_)
}
}
}

# Import Module
Import-Module -Name $ModuleName -Force
Write-Verbose -Message "$ModuleName module installation successful to $TargetPath"
Import-Module -Name $ModuleName -Force
Write-Verbose -Message "Module installed"
}
Catch
{
throw ("Failed installing the module '{0}': {1} in Line {2}" -f $ModuleName, $_, $_.InvocationInfo.ScriptLineNumber)
Catch {
throw ('Failed installing module "{0}". Error: "{1}" in Line {2}' -f $ModuleName, $_, $_.InvocationInfo.ScriptLineNumber)
}
finally
{
if ($FromGitHub)
{
[Net.ServicePointManager]::SecurityProtocol = $SecurityProtocol
}
finally {
#if ($FromGitHub) {
# [Net.ServicePointManager]::SecurityProtocol = $SecurityProtocol
#}
Write-Verbose -Message 'Module installation end'
}
88 changes: 0 additions & 88 deletions Tests/AppVeyor.ps1

This file was deleted.

Loading

0 comments on commit 70e90fd

Please sign in to comment.