Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WAU with mods enabled #59

Merged
merged 10 commits into from
Apr 23, 2022
Merged

WAU with mods enabled #59

merged 10 commits into from
Apr 23, 2022

Conversation

KnifMelti
Copy link
Contributor

@KnifMelti KnifMelti commented Apr 20, 2022

Have tested a lot, it seems all good and nothing broken as far as I can tell.
Some mistakes were reverted.

@KnifMelti
Copy link
Contributor Author

@Romanitho User "GAJ-san" is "KnifMelti@Work" :=)

@Romanitho
Copy link
Owner

maybe few optimisations ? :p

function Test-Mods ($app){

    $Mods = "$WorkingDir\mods"
    if (Test-Path "$Mods\$app-*"){
        if (Test-Path "$Mods\$app-install.ps1"){
            $ModsInstall = "$Mods\$app-install.ps1"
            $ModsUpgrade = "$Mods\$app-install.ps1"
        }
        if (Test-Path "$Mods\$app-upgrade.ps1"){
            $ModsUpgrade = "$Mods\$app-upgrade.ps1"
        }
        return $ModsInstall,$ModsUpgrade
    }
    else {
        #Takes care of a null situation
        $ModsInstall = $null
        $ModsUpgrade = $null
        return $ModsInstall,$ModsUpgrade
    }

}

and then

$ModsInstall, $ModsUpgrade = Test-Mods $($app.Id)
if ($ModsUpgrade){
    Write-Log "Modifications for $($app.Id) during upgrade are being applied..." "Yellow"
    & "$ModsUpgrade"
}

@KnifMelti
Copy link
Contributor Author

Ah, you can do that...
...if it works then of course!
It was the $ModsTest.split() that caused a problem if $null

@Romanitho
Copy link
Owner

Yes. In this way we don't have to handle the split

@KnifMelti
Copy link
Contributor Author

KnifMelti commented Apr 21, 2022

But, if there's only an upgrade file then the install variable doesn't get cleared and may contain the previous content from the loop if the previous app had an install mod.
Or...?
I cleared the variables before testing.
Or return does this by default?

@Romanitho
Copy link
Owner

In PowerShell, as in other programming languages, functions define the scope of variables. The values of variables defined within a function block are not available outside the function. However, to avoid undesirable side effects, you should not use the same name for local and global variables. source
So, just to be sure, we could change the name of variables inside the function, but each time you call the function, the variables are "reset".
that's why when I want to reuse a variable outside of a function, I declare the scope: $Script:Variable for instance

@KnifMelti
Copy link
Contributor Author

KnifMelti commented Apr 21, 2022

Then, if variables are reset each time a function is called there's no issue at all.

Another thing; in this I modified (then deleted the modifications) in Update-WAU.ps1 and Winget-AutoUpdate-Install.ps1 (the mods directory).
I should have Undone the Commits, but I reverted (edited) them back to original instead...
...I can't find a way to Undo what I did, so now I guess my layman way of working in GitHub is going to overwrite your description of the file Winget-AutoUpdate-Install.ps1; "Using preview version to resolve issue as system" if this Pull request is merged...
...sorry!
To avoid that you could implement the changes yourself!

@Romanitho
Copy link
Owner

@KnifMelti quick question. If you want to run a script with your Mods feature, but don't want to copy it in the WAU Mods folder. Is it possible ? Let's imagine an app you want to install with specific config that you apply with your mods, but then, not necessary to run it on each new version.

@KnifMelti
Copy link
Contributor Author

KnifMelti commented Apr 22, 2022

You mean in the winget install...
...well, one could check if there's a install-once or something then run that in the winget install and exclude it in copying.
The WAU mods only runs if it's called $AppID-install.ps1 or $AppID-upgrade.ps1
Should I Commit something from my Winget-Install fork?

#Check if modifications exist in "mods" directory
function Test-ModsInstall ($AppID){
    if (Test-Path "$PSScriptRoot\mods\$AppID-install-once.ps1"){
        $ModsInstallOnce = "$PSScriptRoot\mods\$AppID-install-once.ps1"
        return $ModsInstallOnce
    }
    elseif (Test-Path "$PSScriptRoot\mods\$AppID-install.ps1"){
        $ModsInstall = "$PSScriptRoot\mods\$AppID-install.ps1"
        return $ModsInstall
    }
    elseif (Test-Path "$PSScriptRoot\mods\$AppID-upgrade.ps1"){
        $ModsUpgrade = "$PSScriptRoot\mods\$AppID-upgrade.ps1"
        return $ModsUpgrade
    }
    else{
        return 0
    }
}
../
        #Check if mods exist
        $ModsInstall = Test-ModsInstall $AppID
        if ($ModsInstall -like "*$AppID-install*"){
            Write-Log "Modifications for $AppID during install are being applied..." "Yellow"
            & "$ModsInstall"
        }
        #Check if install is ok
        $IsInstalled = Confirm-Install $AppID
        if ($IsInstalled){
            Write-Log "$AppID successfully installed." "Green"
            #Add to WAU mods if exists
            if (($ModsInstall -like "*$AppID-install*") -or ($ModsInstall -like "*$AppID-upgrade*")){
                Add-WAUMods $AppID
            }
            #Add to WAU White List if set
            if ($WAUWhiteList){
                Add-WAUWhiteList $AppID
            }
        }
        else{
            Write-Log "$AppID installation failed!" "Red"
        }
/..
#Function to Add Mods to WAU "mods"
function Add-WAUMods ($AppID){
    #Check if WAU default install path exists
    $Mods = "$env:ProgramData\Winget-AutoUpdate\mods"
    if (Test-Path $Mods){
        #Add mods
        if ((Test-Path "$PSScriptRoot\mods\$AppID-install.ps1") -or (Test-Path "$PSScriptRoot\mods\$AppID-upgrade.ps1")){
            Write-Log "Add modifications for $AppID to WAU 'mods'"
            Copy-Item "$PSScriptRoot\mods\$AppID-*" -Destination "$Mods" -Exclude "*-install-once*","*-uninstall*" -Force
        }
    }
}

@KnifMelti
Copy link
Contributor Author

KnifMelti commented Apr 22, 2022

I haven't tested it yet, but it feels/looks right ;)
The only thing I don't take care of is if there's an "$AppID-install-once.ps1" AND an "$AppID-install.ps1"...
...ah, a simple priority change did it, now changed above (I've also tested it and it behaves OK)!

@Romanitho
Copy link
Owner

Romanitho commented Apr 23, 2022

I like the idea :)
dont hesitate to PR if you implemented it :)
thanks :)

@Romanitho Romanitho merged commit d0c47c3 into Romanitho:main Apr 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants