JSEDLAK » PowerShell

Posts Tagged ‘PowerShell’

PowerShell Script for Copying Libraries

Here is a nice little PowerShell script I have created for copying my FGF libraries to the projects that use them. I also hot-keyed the PowerShell window which makes distributing new FGF builds really easy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function fgfDist
{
    # An array of platforms that need to be copied
    $platforms = "x86","Xbox 360","Zune"
 
    if($args.count -ne 0){
        $platforms = $args
    }
 
    # An array of destinations for copying
    $destinations = "M:\Code\Games\Galactic Defense\trunk\Libraries\",
                    "M:\Code\AIR\trunk\Libraries\"
 
    $source = "M:\Code\FGF\trunk\Bin\"
    $sourceSuffix = "\Debug"
 
    # Loop through all the destinations and all the platforms
    # For each platform, copy some files.
    foreach($destination in $destinations)
    {
        write-host "Starting copy to"$destination
        foreach($platform in $platforms)
        {
            $tempSource = $source + $platform + $sourceSuffix
            $tempDestination = $destination + $platform
 
            # Test if the folder exists
            if( Test-Path $tempSource ){
                if( !(Test-Path $tempDestination) ){
                    write-host "Creating directory."
                    new-item -path $destination -name $platform -type directory
                }
            }
            else{
                write-host "Could not find the source files for"$platform
                continue
            }
 
            # Get a list of DLL files that contains thrust as well as the FocusedGames.dll file.
            $thrustLibs = get-childItem $tempSource -force | ? {$_.extension -eq ".dll"}|where {$_.name -match "Thrust"}
            $coreLibs = get-childItem $tempSource -force | ? {$_.extension -eq ".dll"}|where {$_.name -match "FocusedGames\.dll"}
 
            if($thrustLibs){
                foreach($dll in $thrustLibs){
                    if( $dll -ne ""){
                        copy-item $tempSource\$dll $tempDestination
                        write-host "  Copied"$platform\$dll
                    }
                }
            }
 
            if($coreLibs){
                foreach($dll in $coreLibs){
                    if( $dll -ne ""){
                        copy-item $tempSource\$dll $tempDestination
                        write-host "  Copied"$platform\$dll
                    }
                }
            }
        }
    }
}

My Introduction to PowerShell

Wow! I can’t believe I have been missing this for so long. It is the answer to all my command line woes in Windows. I have only touched the surface but love it already. Today I created my profile to change the prompt and add a command for copying my FGF binaries to my current working directories:

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Profile PowerShell Script
# Author: John Sedlak
# Site: http://focusedgames.com
 
$a = (Get-Host).UI.RawUI
$a.ForegroundColor = "White"
$a.WindowTitle = "WPS - Focused Games"
 
# This function changes the prompt.
function prompt{
	$path = get-location
	$b = Get-Date -format "HH:mm"
	return "$b $path ?> "
}
 
# This function copies a bunch of FGF libraries to current working directories.
function fgfCopy{
	# These are the platforms that need to be copied
	$platforms = "x86","Zune","Xbox 360"
 
	# An array of the destinations for copying
	$destinations = "E:\Users\John Sedlak\Code\Games\GW3\trunk\Libraries\"
 
	# The source directory and the suffix.
	$source = "E:\Users\John Sedlak\Code\FGF\trunk\Bin\"
	$sourceSuffix = "\Release"
 
	# Loop through all the destinations and all the platforms
	# For each platform, copy some files.
	foreach($destination in $destinations){
		write-host "Starting copy to"$destination
		foreach($platform in $platforms){
			# Build up the source and destination paths
			$tempSource = $source + $platform + $sourceSuffix
			$tempDestination = $destination + $platform
 
			# Get a list of all DLL files that contain the word Thrust
			$files = get-childItem $tempSource -force | ? {$_.extension -eq ".dll"}|where {$_.name -match "Thrust"}
 
			# For each file, copy it!
			foreach($dll in $files){
				copy-item $tempSource\$dll $tempDestination
				write-host "Copied"$platform\$dll
			}
		}
	}
}
PowerShell - fgfCopy Command