Scripting with PowerShell - part II

In my last entry I showed you some ways to get data from the user in different ways. We are now going to use this data to ready the environment for Qt programming.

To be able to build with Qt, or any other programming library, Windows needs to know the whereabouts of all libraries and tools that will be used. This is included in the environment variables. To list all the environment settings in PS type: Get-ChildItem env:

In the section below I will include the complete script and comment the code inside the script. When you comment your code in PS you prefix the lines with #. This will cause PS to ignore this line while running. Also note that I am organizing my code in three functions. One to clean the environment, one to set the environment and one main function that calls the other classes.


# We fetch the input
$firstArg = $args[0]

# Clean any QTDIR from the path
function cleanPath(){

# We use the net function split() to split the Windows 
# path into an array using the ; character as end-of-line
 $splitPath = $Env:path.split(';')
 
# Then for each index in the array looks at the content
# and either keep it or lose it 
 foreach($line in $splitPath){
 
# If the string contains "Qt" or "qt", then we simply tell
# the user that we found it - and ignore it
  if ($line -match "Qt" -or $line -match "qt"){
    #echo "Removed $line from the path"
  }
 
# Else - we add the string (as an item to a new array) to
# the variable $newPathArray
  else{
   $newPathArray  += , $line
  }
 }

# We use the .net Join() method to join the array items
# into a new string that will be set to be the new 
# Windows PATH
 $joinedPath = [string]::Join(';',$newPathArray)
 $Env:Path = $joinedPath

# Then we output the new path to the screen (not needed). 
 echo "New path is now: "
 $Env:Path.split(';')
}

# setPath() does what it says, it sets the path.
function setPath(){

# We split the path to analyze it
 $splitPath = $Env:path.split(';')
 foreach($line in $splitPath){
  if ($line -match "Qt" -or $line -match "qt"){
  
# If Qt version is already in the path, we exit the script
# telling the user to clean it first - or leave it as it is.
    echo "Qt version is already set."
    echo "To reset run: qtp clean "
    echo "Current Qt version is set to $Env:qtdir "
    exit
  }
  else{
  
# If Qt is not set, then we use a preset Windows variable - 
# simply the path to where I keep my libraries, and 
# merge it with the argument given to the script.
   $Env:QTDIR = $Env:QTDEPOT + "\" + $firstArg
   $Env:Path = $Env:QTDIR + "\bin;" + $Env:Path
  }
 }
}

# This is the main function executing the other functions.
function main(){

# If the argument given to the script is "clean", we want to 
# call cleanPath()
 if($firstArg -match "clean"){
  echo "Cleaning path"
  . cleanPath
  echo "The path is cleaned. Please run: qtp [QtDir] again."
 }
# Else, we want to set the PATH and QTDIR
 else{
  . setPath
  echo "Qt version is set to $firstArg "
  exit
 }
}

# And this is how you call functions in a script.
. main

Now, this is only an example on how to do this. Play around with the functionality and find out how you want your script to be. I will continue later with more PS stuff, bet this is it for now. As usual I would love to have your comments and questions. So until next time - happy scripting.

Comments

Popular posts from this blog

Designing and programming - Part 2

Filtering Dropdown choices in a Power Pages form using Dataverse Relations

Exploring the Power of Variables in Liquid and Power Pages