Skip to content

Commit

Permalink
Merge pull request #1 from dfinke/add-build-prompt-from-repo
Browse files Browse the repository at this point in the history
Add build prompt from repo
  • Loading branch information
dfinke authored Sep 23, 2024
2 parents 2788826 + 494893b commit 742b216
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 4 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
on:
push:
paths-ignore:
- "README.md"
- "changelog.md"
- ".github/workflows/docs.yml"
- "docs/**"
- "mkdocs.yml"
- "examples/**"

branches:
- main

pull_request:

jobs:
validate:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest]
steps:
- uses: actions/checkout@v4
- name: Run Continuous Integration
shell: pwsh
run: |
cd ./__tests__
Invoke-Pester -Output Detailed
11 changes: 8 additions & 3 deletions BuildPromptFromFiles.psd1
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
@{
@{
CompatiblePSEditions = @('Desktop')

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

# Version number of this module.
ModuleVersion = '0.2.0'
ModuleVersion = '0.2.1'

# ID used to uniquely identify this module
GUID = 'fa761a01-55b9-436d-8928-9dfd0ffdf978'
Expand All @@ -25,7 +27,10 @@ Combine files from a directory into a single prompt, ready for use with large la
# Functions to export from this module
FunctionsToExport = @(
'Build-PromptFromFiles'
'Build-PromptFromGitHubRepo'
'Get-GHBranch'
'Get-GitIgnoreContent'
'Test-RepoExists'
)

# Aliases to export from this module
Expand Down Expand Up @@ -70,7 +75,7 @@ Combine files from a directory into a single prompt, ready for use with large la
}

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

# Name of the Windows PowerShell host required by this module
# PowerShellHostName = ''
Expand Down
6 changes: 5 additions & 1 deletion BuildPromptFromFiles.psm1
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
foreach ($directory in @('Private', 'Public')) {
if ($IsWindows -eq $false) {
throw "This module is only supported on Windows operating systems."
}

foreach ($directory in @('Private', 'Public')) {
Get-ChildItem -Path "$PSScriptRoot\$directory\*.ps1" | ForEach-Object { . $_.FullName }
}
File renamed without changes.
35 changes: 35 additions & 0 deletions Public/Build-PromptFromGitHubRepo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function Build-PromptFromGitHubRepo {
[CmdletBinding()]
param(
$slug
)

if (!(Test-RepoExists -slug $slug)) {
throw "Repo $slug does not exist"
}

$branch = Get-GHBranch -slug $slug

# test if github repo exists

$url = "https://api.github.com/repos/{0}/zipball/{1}" -f $slug, $branch

Write-Verbose "[$(Get-Date)] Retrieving $url"

$fileName = $slug.replace("/", "-")
$folderName = [System.IO.Path]::GetTempPath()
$OutFile = "$folderName\$($fileName).zip"

$DestinationPath = "$folderName\$fileName"

Invoke-RestMethod $url -OutFile $OutFile
Expand-Archive -Path $OutFile -DestinationPath $DestinationPath -Force
$targetFullName = (Get-ChildItem $folderName -Recurse $fileName -Directory | Get-ChildItem).FullName

Write-Verbose "[$(Get-Date)] $OutFile"
Write-Verbose "[$(Get-Date)] $DestinationPath"
Build-PromptFromFiles $targetFullName

$null = Remove-Item $OutFile -Force -Verbose:$Verbose
$null = Remove-Item $DestinationPath -Recurse -Force -Verbose:$Verbose
}
11 changes: 11 additions & 0 deletions Public/Get-GHBranch.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function Get-GHBranch {
[CmdletBinding()]
param(
[string]$slug
)

$branches = Invoke-RestMethod "https://api.github.com/repos/$slug/branches"

$branchName = $branches.name
return ($branchName -eq 'main') ? 'main' : ($branchName -eq 'master') ? 'master' : $branches[0].name
}
18 changes: 18 additions & 0 deletions Public/Test-RepoExists.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function Test-RepoExists {
[CmdletBinding()]
param(
$slug
)

$repoExists = $false

try {
$null = Invoke-RestMethod "https://api.github.com/repos/$slug"
$repoExists = $true
}
catch {
Write-Verbose "Repo $slug does not exist"
}

$repoExists
}
35 changes: 35 additions & 0 deletions __tests__/TestFunctionsExist.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Describe "TestFunctionsExist" -Tag "TestFunctionsExist" {
BeforeAll {
Import-Module "$PSScriptRoot/../BuildPromptFromFiles.psd1" -Force
}

It "Should have Build-PromptFromGitHubRepo" {
$actual = Get-Command -module BuildPromptFromFiles Build-PromptFromGitHubRepo -ErrorAction SilentlyContinue

$actual | Should -Not -BeNullOrEmpty
}

It "Should have Build-PromptFromFiles" {
$actual = Get-Command -module BuildPromptFromFiles Build-PromptFromFiles -ErrorAction SilentlyContinue

$actual | Should -Not -BeNullOrEmpty
}

It "Should have Get-GHBranch" {
$actual = Get-Command -module BuildPromptFromFiles Get-GHBranch -ErrorAction SilentlyContinue

$actual | Should -Not -BeNullOrEmpty
}

It "Should have Get-GitIgnoreContent" {
$actual = Get-Command -module BuildPromptFromFiles Get-GitIgnoreContent -ErrorAction SilentlyContinue

$actual | Should -Not -BeNullOrEmpty
}

It "Should have Test-RepoExists" {
$actual = Get-Command -module BuildPromptFromFiles Test-RepoExists -ErrorAction SilentlyContinue

$actual | Should -Not -BeNullOrEmpty
}
}
7 changes: 7 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# v0.2.1

- Added `Build-PromptFromGitHubRepo`
- Downloads the zip of a GitHub repository to the temporary directory and extracts the contents. Then it uses `Build-PromptFromFIles` to build the prompt.

- This only works on Windows with PowerShell 7.0 or later.

# v0.2.0

- Added `gitIgnore` switch to include patterns from .gitignore files in the ignore list.
Expand Down
4 changes: 4 additions & 0 deletions examples/basics.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Import-Module "$PSScriptRoot/../BuildPromptFromFiles.psd1" -Force

$prompt = Build-PromptFromGitHubRepo dfinke/psai #-Verbose
$prompt

0 comments on commit 742b216

Please sign in to comment.