Skip to content
This repository has been archived by the owner on Nov 23, 2018. It is now read-only.

Commit

Permalink
added #1154: Extract-Views support for localized views in satellite a…
Browse files Browse the repository at this point in the history
…ssemblies
  • Loading branch information
maxtoroq committed Oct 8, 2014
1 parent 0b66ddd commit 820a790
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -6,6 +6,7 @@ v1.3.0
- Added [#1156](https://mvccoderouting.codeplex.com/workitem/1156): Cannot locate dynamically loaded assemblies in Mvc (see [EnableCodeRouting(ControllerBuilder)](docs/api/MvcCodeRouting/CodeRoutingExtensions/EnableCodeRouting.md) extension method)
- Added [#1161](https://mvccoderouting.codeplex.com/workitem/1161): Parent controller reference syntax
- Improved PowerShell module documentation
- Added [#1154](https://mvccoderouting.codeplex.com/workitem/1154): *Extract-Views* support for localized views in satellite assemblies

Web.Http.WebHost.v1.0.0
-----------------------
Expand Down
3 changes: 2 additions & 1 deletion docs/Embedded-Views.md
Expand Up @@ -63,14 +63,15 @@ PM> Extract-Views MvcAccount Account
Usage:

```text
Extract-Views [-AssemblyName] <String> [[-ViewsDirectory] <String>] [[-ProjectName] <String>]
Extract-Views [-AssemblyName] <String> [[-ViewsDirectory] <String>] [[-ProjectName] <String>] [-Culture <String>]
```

Parameters:

- **AssemblyName**: Specifies the assembly that contains embedded views.
- **ViewsDirectory**: Specifies the directory relative to the *Views* directory where you want to save the views. e.g. if *Foo\Bar* views are saved in *Views\Foo\Bar*. If omitted, views are saved directly in *Views*.
- **ProjectName**: Specifies the project to use as context. If omitted, the default project is chosen.
- **Culture**: Specifies the culture of the satellite assembly to extract views from. If omitted, views are extracted from the main assembly.

[1]: http://msdn.microsoft.com/en-us/library/system.web.hosting.virtualpathprovider
[2]: api/MvcCodeRouting/CodeRoutingSettings/EnableEmbeddedViews.md
Expand Down
62 changes: 49 additions & 13 deletions tools/MvcCodeRouting.psm1
Expand Up @@ -12,35 +12,43 @@
# See the License for the specific language governing permissions and
# limitations under the License.

$ErrorActionPreference = "Stop"

function Extract-Views {
<#
.SYNOPSIS
Copies assembly-embedded views to your project.
.DESCRIPTION
Copies assembly-embedded views to your project.
.PARAMETER AssemblyName
Specifies the assembly that contains embedded views.
.PARAMETER ViewsDirectory
Specifies the directory relative to the 'Views' directory where you want to save the views. e.g. if 'Foo\Bar' views are saved in 'Views\Foo\Bar'. If omitted, views are saved directly in 'Views'.
.PARAMETER ProjectName
Specifies the project to use as context. If omitted, the default project is chosen.
.PARAMETER Culture
Specifies the culture of the satellite assembly to extract views from. If omitted, views are extracted from the main assembly.
.EXAMPLE
PM> Extract-Views MvcAccount Account
#>
param(
[Parameter(Mandatory=$true)]
[Parameter(Mandatory=$true, Position=0)]
[string]$AssemblyName,

[Parameter()]
[Parameter(Position=1)]
[string]$ViewsDirectory = $null,

[Parameter(Position=2)]
[string]$ProjectName = $null,

[Parameter()]
[string]$ProjectName = $null
[string]$Culture = $null
)

$project = $null
Expand All @@ -63,13 +71,27 @@ function Extract-Views {
throw "Couldn't find $AssemblyName reference in $ProjectName."
}

$cult = $null

if ($Culture) {
$cult = [Globalization.CultureInfo]::GetCultureInfo($Culture)
}


$assemblyPath = if ($isWebsite) { $assemblyRef.FullPath } else { $assemblyRef.Path }

if ($cult) {
$assemblyPath = [IO.Path]::Combine([IO.Path]::GetDirectoryName($assemblyPath), $cult.Name, [IO.Path]::GetFileNameWithoutExtension($assemblyPath)) + ".resources.dll"
}

$assembly = [Reflection.Assembly]::Load([IO.File]::ReadAllBytes($assemblyPath))

$viewResourceNames = $assembly.GetManifestResourceNames() |
where {[Text.RegularExpressions.Regex]::IsMatch($_, "^$AssemblyName\.Views\.", [Text.RegularExpressions.RegexOptions]::IgnoreCase)}

if ($viewResourceNames -eq $null `
-or $viewResourceNames.Length -eq 0) {

throw "$AssemblyName doesn't have embedded views."
}

Expand All @@ -83,6 +105,11 @@ function Extract-Views {

$resParts = $res.Split(".")
$viewFileName = [string]::Join(".", $resParts[($resParts.Length - 2)..($resParts.Length - 1)])

if ($cult) {
$viewFileName = [IO.Path]::GetFileNameWithoutExtension($viewFileName) + "." + $cult.Name + [IO.Path]::GetExtension($viewFileName)
}

$viewDirParts = New-Object Collections.ArrayList
$viewDirParts.AddRange($resParts[($AssemblyName.Split('.').Length)..($resParts.Length - 3)])

Expand Down Expand Up @@ -116,10 +143,22 @@ function Extract-Views {
if ($createFile) {

$resStream = $assembly.GetManifestResourceStream($res)
$fileStream = [IO.File]::Create($viewPath)
$resStream.CopyTo($fileStream)
$fileStream.Dispose()
$resStream.Dispose()

try {

$fileStream = [IO.File]::Create($viewPath)

try {

$resStream.CopyTo($fileStream)

} finally {
$fileStream.Dispose()
}

} finally {
$resStream.Dispose()
}

$extracted++

Expand All @@ -132,11 +171,8 @@ function Extract-Views {
if ($extracted `
-and $isWebsite) {

# Focus on Solution Explorer
#$dte.Windows.Item("{3AE79031-E1BC-11D0-8F78-00A0C9110057}").Activate()

try {
# Refresh
# Refresh Solution Explorer
$dte.Commands.Raise("{1496A755-94DE-11D0-8C3F-00C04FC2AAE2}", 222, [ref]$null, [ref]$null)
} catch { }
}
Expand Down

0 comments on commit 820a790

Please sign in to comment.