Skip to content

Commit

Permalink
Simplified loading modules
Browse files Browse the repository at this point in the history
  • Loading branch information
whut committed Oct 23, 2011
1 parent 42eb994 commit 98f110b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 51 deletions.
30 changes: 10 additions & 20 deletions psake-config.ps1
Expand Up @@ -2,30 +2,20 @@
-------------------------------------------------------------------
Defaults
-------------------------------------------------------------------
$config.buildFileName="default.ps1";
$config.framework = "3.5";
$config.taskNameFormat="Executing {0}";
$config.verboseError=$false;
$config.coloredOutput = $true;
$config.modules=(new-object psobject -property @{ autoload=$false })
$config.buildFileName="default.ps1"
$config.framework = "3.5"
$config.taskNameFormat="Executing {0}"
$config.verboseError=$false
$config.coloredOutput = $true
$config.modules=$null
-------------------------------------------------------------------
Auto-load modules from .\modules folder
Load modules from .\modules folder and from file my_module.psm1
-------------------------------------------------------------------
$config.modules=(new-object psobject -property @{ autoload=$true})
$config.modules=(".\modules\*.psm1",".\my_module.psm1")
-------------------------------------------------------------------
Auto-load modules from .\my_modules folder
Use scriptblock for taskNameFormat
-------------------------------------------------------------------
$config.modules=(new-object psobject -property @{ autoload=$true; directory=".\my_modules" })
-------------------------------------------------------------------
Explicitly load module(s)
-------------------------------------------------------------------
$config.modules=(new-object psobject -property @{
autoload=$false;
module=(new-object psobject -property @{path="c:\module1dir\module1.ps1"}),
(new-object psobject -property @{path="c:\module1dir\module2.ps1"})
})
}
$config.taskNameFormat= { param($taskName) "Executing $taskName at $(get-date)" }
#>
39 changes: 9 additions & 30 deletions psake.psm1
Expand Up @@ -414,32 +414,15 @@ function Write-ColoredOutput {
}

function Load-Modules {
$modules = $null

$currentConfig = $psake.context.peek().config
if ($currentConfig.modules.autoload) {
if ($currentConfig.modules.directory) {
Assert (test-path $currentConfig.modules.directory) ($msgs.error_invalid_module_dir -f $currentConfig.modules.directory)
$modules = get-item(join-path $currentConfig.modules.directory "*.psm1")
}
elseif (test-path (join-path $PSScriptRoot "modules")) {
$modules = get-item (join-path (join-path $PSScriptRoot "modules") "*.psm1")
}
} else {
if ($currentConfig.modules.module) {
$modules = $currentConfig.modules.module | % {
Assert (test-path $_.path) ($msgs.error_invalid_module_path -f $_.path);
get-item $_.path
}
}
}

if ($modules) {
$modules | % {
"loading module: $_";
$module = import-module $_ -passthru;
if (!$module) {
throw ($msgs.error_loading_module -f $_.Name)
if ($currentConfig.modules) {
$currentConfig.modules | foreach {
resolve-path $_ | foreach {
"Loading module: $_"
$module = import-module $_ -passthru
if (!$module) {
throw ($msgs.error_loading_module -f $_.Name)
}
}
}
""
Expand Down Expand Up @@ -662,8 +645,6 @@ convertfrom-stringdata @'
error_invalid_include_path = Unable to include {0}. File not found.
error_build_file_not_found = Could not find the build file, {0}.
error_no_default_task = default task required
error_invalid_module_dir = Unable to load modules from directory: {0}
error_invalid_module_path = Unable to load module at path: {0}
error_loading_module = Error loading module: {0}
warning_deprecated_framework_variable = Warning: Using global variable $framework to set .NET framework version used is deprecated. Instead use Framework function or configuration file psake-config.ps1
postcondition_failed = Postcondition failed for {0}
Expand All @@ -685,9 +666,7 @@ $psake.config_default = new-object psobject -property @{
taskNameFormat = "Executing {0}";
verboseError = $false;
coloredOutput = $true;
modules = (new-object PSObject -property @{
autoload = $false
})
modules = $null;
} # contains default configuration, can be overriden in psake-config.ps1 in directory with psake.psm1 or in directory with current build script

$psake.build_success = $false # indicates that the current build was successful
Expand Down
2 changes: 1 addition & 1 deletion specs/writing_psake_variables_should_pass.ps1
Expand Up @@ -30,5 +30,5 @@ task Verify -description "This task verifies psake's variables" {
Assert ($config.taskNameFormat -eq "Executing {0}") '$psake.context.peek().config.taskNameFormat not equal to "Executing {0}"'
Assert (!$config.verboseError) '$psake.context.peek().config.verboseError should be $false'
Assert ($config.coloredOutput) '$psake.context.peek().config.coloredOutput should be $false'
Assert ($config.modules) '$psake.context.peek().config.modules is $null'
Assert ($config.modules -eq $null) '$psake.context.peek().config.modules is not $null'
}

0 comments on commit 98f110b

Please sign in to comment.