Skip to content

Commit

Permalink
! Use a remote repository by default
Browse files Browse the repository at this point in the history
Add a `POG_REMOTE_REPOSITORY_URL` environment variable to switch back to a local repository for development.
  • Loading branch information
MatejKafka committed Apr 8, 2024
1 parent d991fe3 commit 4bb6113
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 60 deletions.
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ Get-PogPackage # list installed packages
Get-PogRepositoryPackage # list available packages
```

Update package list (apologies, there's no automatic way to do this for now):

```powershell
cd "directory\where\pog\is\installed\Pog"
rm -Force -Recurse .\data\manifests\
# rerun setup to download the latest manifests
# (this should not break/overwrite anything)
.\setup.cmd
```

## Building

Pog is composed of 4 parts:
Expand Down
12 changes: 9 additions & 3 deletions app/Pog/Paths.psm1
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using module .\lib\Utils.psm1
. $PSScriptRoot\lib\header.ps1

if (Test-Path Env:POG_REMOTE_REPOSITORY_URL) {
$Initiated = [Pog.InternalState]::InitRepository({[Pog.RemoteRepository]::new($env:POG_REMOTE_REPOSITORY_URL)})
if (Test-Path Env:POG_LOCAL_REPOSITORY_PATH) {
$RepoPath = Resolve-VirtualPath $env:POG_LOCAL_REPOSITORY_PATH
if (-not (Test-Path -Type Container $RepoPath)) {
throw "Local repository path passed in `$env:POG_LOCAL_REPOSITORY_PATH does not exist or is not a directory: '$env:POG_LOCAL_REPOSITORY_PATH'"
}

$Initiated = [Pog.InternalState]::InitRepository({[Pog.LocalRepository]::new($RepoPath)})
if ($Initiated) {
Write-Information "Using remote repository: $([Pog.InternalState]::Repository.Url)"
Write-Information "Using local repository: $([Pog.InternalState]::Repository.Path)"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,12 @@ public sealed class ConfirmPogRepositoryPackageCommand : PogCmdlet {
[ArgumentCompleter(typeof(PSAttributes.RepositoryPackageVersionCompleter))]
public PackageVersion? Version;

private readonly LocalRepository _repo;
private LocalRepository _repo = null!;
private bool _noIssues = true;

private static readonly Regex QuoteHighlightRegex = new(@"'([^']+)'",
RegexOptions.Compiled | RegexOptions.CultureInvariant);

public ConfirmPogRepositoryPackageCommand() {
if (InternalState.Repository is LocalRepository r) {
_repo = r;
} else {
// TODO: implement
throw new RuntimeException("Validation of remote repositories is not yet supported");
}
}

private void AddIssue(string message) {
_noIssues = false;
var aligned = message.Replace("\n", "\n ");
Expand All @@ -66,6 +57,15 @@ private void AddIssue(string message) {
protected override void BeginProcessing() {
base.BeginProcessing();

// place this check here; if we throw an exception in the constructor, XmlDoc2CmdletDoc fails,
// because it needs to create instances of all commands to get default parameter values
if (InternalState.Repository is LocalRepository r) {
_repo = r;
} else {
// TODO: implement
throw new RuntimeException("Validation of remote repositories is not yet supported.");
}

if (Version != null) {
if (MyInvocation.ExpectingInput) {
ThrowTerminatingArgumentError(Version, "VersionWithPipelineInput",
Expand Down
4 changes: 2 additions & 2 deletions app/Pog/lib_compiled/Pog/src/Pog.InternalState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static bool InitDataRoot(string dataRootDirPath) {
return false;
}

/// Debug method, used for testing. Not thread-safe.
/// Configure the package repository used to retrieve package manifests. Not thread-safe.
[PublicAPI]
public static bool InitRepository(Func<IRepository> repositoryGenerator) {
if (_repository == null) {
Expand All @@ -61,7 +61,7 @@ public static bool InitRepository(Func<IRepository> repositoryGenerator) {

private static IRepository? _repository;
public static IRepository Repository => LazyInitializer.EnsureInitialized(ref _repository,
() => new LocalRepository(PathConfig.ManifestRepositoryDir))!;
() => new RemoteRepository(PathConfig.DefaultRemoteRepositoryUrl))!;

private static GeneratorRepository? _generatorRepository;
public static GeneratorRepository GeneratorRepository => LazyInitializer.EnsureInitialized(ref _generatorRepository,
Expand Down
4 changes: 2 additions & 2 deletions app/Pog/lib_compiled/Pog/src/Pog.PathConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public static class PackagePaths {
internal const string TmpDeleteDirName = ".POG_INTERNAL_delete_tmp";
}

public const string DefaultRemoteRepositoryUrl = "https://matejkafka.github.io/PogPackages/";

/// Directory where exported shortcuts from packages are copied (system-wide).
public static readonly string StartMenuSystemExportDir =
Path.Combine(GetFolderPath(SpecialFolder.CommonStartMenu), "Pog");
Expand All @@ -77,7 +79,6 @@ public static class PackagePaths {
public readonly string VcRedistDir;

public readonly string ExportedCommandDir;
public readonly string ManifestRepositoryDir;
public readonly string ManifestGeneratorDir;

/// Directory where package files with known hash are cached.
Expand Down Expand Up @@ -110,7 +111,6 @@ public PathConfig(string appRootDirPath, string dataRootDirPath, string cacheRoo
VcRedistDir = $"{CompiledLibDir}\\vc_redist";

ExportedCommandDir = $"{dataRootDirPath}\\package_bin";
ManifestRepositoryDir = $"{dataRootDirPath}\\manifests";
ManifestGeneratorDir = $"{dataRootDirPath}\\manifest_generators";

DownloadCacheDir = $"{cacheRootDirPath}\\download_cache";
Expand Down
5 changes: 5 additions & 0 deletions app/Pog/tests/TestEnvironmentSetup.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,10 @@ Export function SetupNewPogTestDir {
throw "TEST SETUP ERROR: Pog data root already configured"
}

# use local repository
if (-not [Pog.InternalState]::InitRepository({[Pog.LocalRepository]::new("$TestDir\data\manifests")})) {
throw "TEST SETUP ERROR: Pog data root already configured"
}

return $TestDir
}
35 changes: 2 additions & 33 deletions setup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ Write-Host "Setting up all directories required by Pog..."

newdir "./data"
newdir "./cache"
# local manifest repository
# ./data/manifests is created below
newdir "./data/manifest_generators"
# directory where commands are exported; is added to PATH
newdir "./data/package_bin"
Expand All @@ -64,6 +62,8 @@ if (-not (Test-Path -PathType Leaf $ROOT_FILE_PATH)) {
Set-Content $ROOT_FILE_PATH -Value $DefaultContentRoot
}

Write-Host ""

# ====================================================================================
# now, we should be ready to import Pog
Import-Module $PSScriptRoot\app\Pog
Expand All @@ -90,37 +90,6 @@ if (-not (Test-Path ([Pog.InternalState]::PathConfig.Path7Zip))) {
}
}

Write-Host ""


# we download the package repository after enabling 7zip to be able to use it for extraction
# TODO: when a more comprehensive support for remote repository is implemented, update this
$MANIFEST_REPO_PATH = Resolve-VirtualPath "$PSScriptRoot/data/manifests"
if (-not (Test-Path $MANIFEST_REPO_PATH)) {
# manifest repository is not initialized, download it
Write-Host "Downloading package manifests from 'https://github.com/MatejKafka/PogPackages'..."
if (Get-Command git -ErrorAction Ignore) {
Write-Host "Cloning using git..."
git clone https://github.com/MatejKafka/PogPackages $MANIFEST_REPO_PATH --depth 1 --quiet
} else {
$TmpDir = "${env:TEMP}\PogPackages-$(New-Guid)"
$ExtractedPath = "$TmpDir\PogPackages"
try {
# use internal functions for download and extraction
Import-Module $PSScriptRoot\app\Pog\lib_compiled\Pog.dll

$ArchivePath = Invoke-FileDownload "https://github.com/MatejKafka/PogPackages/archive/refs/heads/main.zip" $TmpDir
# Expand-Archive in powershell.exe is incredibly slow for small files, so we use 7zip
Expand-Archive7Zip $ArchivePath $ExtractedPath
Move-Item $ExtractedPath\* $MANIFEST_REPO_PATH -Force
} finally {
Remove-Item $TmpDir -Recurse -Force -ErrorAction Ignore
}
}
Write-Host "Downloaded '$(@(ls -Directory $MANIFEST_REPO_PATH).Count)' package manifests."
}


if (-not (Test-Path ([Pog.InternalState]::PathConfig.PathOpenedFilesView))) {
try {
pog OpenedFilesView
Expand Down

0 comments on commit 4bb6113

Please sign in to comment.