dsa-ou / m269-installer

Software installation script and instructions for M269
https://dsa-ou.github.io/m269-installer/
BSD 3-Clause "New" or "Revised" License
0 stars 1 forks source link

Bespoke Windows script #4

Closed mwermelinger closed 1 year ago

mwermelinger commented 1 year ago

Instead of requiring WSL to then reuse the bash script, it's easier for Windows users to have their own batch or PowerShell script to install the software directly in Windows. The script would be similar in functionality to the bash script:

Finally, the script should create an easy way for the user to activate the environment, launch the notebooks and use allowed. The bash script does this by creating aliases in the startup shell files.

In Windows, one possibility is for the installation script to put another a script (let's call it m269-23j.ps1) in the M269 folder. When double-clicked, that script opens a command prompt, cds to the M269 folder, activates the environment and uses the Set-Alias cmdlet to create aliases nb and allowed like the bash script.

As I'm not a Windows user, I don't know if this is feasible or whether there's a better way to do it.

mwermelinger commented 1 year ago

The Windows script is done, apart from creating aliases like the Unix script. I believe the script must do:

  1. Check if the file given by variable $PROFILE.CurrentUserCurrentHost exists; if not, create it. (That file is the equivalent of ~/.bashrc. Any functions and aliases defined by it can be typed into the PowerShell prompt.)
  2. Add to the end of that file:
function m269-23j {
    cd $FOLDER
    $VENV\Scripts\Activate.ps1
}
function nb {
    Start-Job jupyter notebook $FOLDER
}
function allowed {
    python $FOLDER\allowed.py -c $FOLDER\m269.json
}

where variables $FOLDER and $VENV have been defined in the install.ps1 script.

Unfortunately I got into syntax problems with the above in PowerShell 5.1: something about only accepting one argument or whatever. I couldn't figure out what the problem was. I also tried Set-Alias instead of defining functions, but couldn't get it to work. Furthermore, I don't know how the allowed function can pass any remaining arguments to the python allowed.py ... command, so that the user can type allowed TMA01\23J_TMA01.ipynb, for example.

Someone knowledgeable in PowerShell thus needs to do this. Maybe one has to assume PowerShell Core 6.0+, but then the instructions must tell students to enter $PSVersionTable to find out the version and how to install PowerShell Core.

densnow commented 1 year ago

I have had some success by adding the following to install.ps1. (VirtualBox, Windows 10 Home, PowerShell 5.1)

Write-Host "Adding shortcut commands to the PowerShell config file..." 

$CONFIG_FILE = $Profile.CurrentUserCurrentHost

$ALIASES = @"
function m269-23j {
    cd $FOLDER
    $VENV\Scripts\Activate.ps1
}
function nb {
    jupyter notebook $FOLDER
}
function allowed {
    param(
        [string]`$FilePath
    )

    python $FOLDER\allowed.py -c $FOLDER\m269.json `$FilePath
}
"@  

# Check if the config file exists
if (-not (Test-Path -Path $CONFIG_FILE)) {
    # File doesn't exist, create it
    New-Item -Path $CONFIG_FILE -ItemType File -Force
}

# Append aliases to the file
Add-Content -Path $CONFIG_FILE -Value $ALIASES -NoNewline

I could not get the Start-Job command to work in the nb alias/function, so have just left it off for now.