Skip to content

Instantly share code, notes, and snippets.

@tomrgio
Created June 19, 2019 14:34
Show Gist options
  • Save tomrgio/e59649f5705f2dc2bd147d8a60e47c2f to your computer and use it in GitHub Desktop.
Save tomrgio/e59649f5705f2dc2bd147d8a60e47c2f to your computer and use it in GitHub Desktop.

movie-database

ASP.NET MVC 1.0 App Repackaged with Habitat https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/movie-database/create-a-movie-database-application-in-15-minutes-with-asp-net-mvc-cs

Setup steps

Let's setup our initial directory

cd C:/
hab setup

Follow the prompts to initialize your habitat origin. You can respond no to the prompts that ask y/n questions.

Now let's create our plan

hab plan init movie-database --windows
cd C:/movie-database
code .

Open plan.ps1

We're building our .NET application, so the first thing we need is our source code. Add the download string to the plan file

$pkg_source="http://download.microsoft.com/download/7/2/8/728F8794-E59A-4D18-9A56-7AD2DB05BD9D/MovieApp_CS.zip"

Save plan.ps1

Open an administrative powershell session, enter the habitat studio, and build the app.

cd C:/movie-database
hab studio enter
build

Your build should now fail, because you have an invalid hash - we haven't provided one! Copy the hash from PowerShell and add it to your plan.ps1.

$pkg_shasum="93bb6654357446cd443d75562949ad62194bac07a589b22cd95f2223292c61d0"

Save plan.ps1. Return to the PowerShell window running your Habitat studio

build

Your build should now succeed! But there is still more work to do We haven't actually built our code yet, we've just pulled in the code.

Let's add a build dependency on core/visual-build-tools-2017

$pkg_build_deps=@("core/visual-build-tools-2017/15/20170802220805")

Add an Invoke-Build section to define how we use msbuild to build our app

function Invoke-Build{
    $csprojPath = "$HAB_CACHE_SRC_PATH\$pkg_dirname\MovieApp\MovieApp\MovieApp.csproj"
    $proj = [xml](get-content $csprojPath)    
    $proj.GetElementsByTagName("Import") | foreach {
        if($_.Project -eq '$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets'){
            $_.Project = '$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets'
        }    
    }
    $proj.Save($csprojPath)
    MSBuild.exe $csprojPath
}

Save plan.ps1. Return to the PowerShell window running your Habitat studio

build

Now we have a new failure- .NET 3.5 is not available! Let's use DSC to resolve this.

In plan.ps1 add a build dependency on core/dsc-core

$pkg_build_deps=@("core/visual-build-tools-2017/15/20170802220805", "core/dsc-core")

Let's add a file to our config directory named `enable-net35.ps1' Add this code to that file, and save it.

Configuration EnableNet35
{
    Node 'localhost' {
        WindowsFeature netfx3 
        { 
            Ensure = "Present"
            Name = "NET-Framework-Core"
        }
    }
}

In plan.ps1, above the CSV modification code, let's run our DSC.

    Import-Module "$(Get-HabPackagePath dsc-core)/Modules/DscCore"
    Start-DscCore $PLAN_CONTEXT\config\enable-net35.ps1 EnableNet35

Save plan.ps1. Let's build again. Return to the PowerShell window running your Habitat studio

build

We now see a new error! We don't have MVC 1.0 available. Let's fix that.

In plan.ps1, let's add a build dependency on core/aspnet-mvc1

$pkg_build_deps=@("core/visual-build-tools-2017/15/20170802220805", "core/dsc-core", "core/aspnet-mvc1")

Save plan.ps1. Let's build again. Return to the PowerShell window running your Habitat studio

build

AH- another issue with our csproj- we have ASP.NET MVC 1.0 available, but the project doesn't know how to find it. Let's replace our code for the csproj XML modification with this.

function Invoke-Build{
    Import-Module "$(Get-HabPackagePath dsc-core)/Modules/DscCore"
    Start-DscCore $PLAN_CONTEXT\config\enable-net35.ps1 EnableNet35

    $csprojPath = "$HAB_CACHE_SRC_PATH\$pkg_dirname\MovieApp\MovieApp\MovieApp.csproj"
    $proj = [xml](get-content $csprojPath)
    $mvcAssemblyHintPathNode = $proj.CreateElement("HintPath", "http://schemas.microsoft.com/developer/msbuild/2003")
    $mvcAssemblyHintPath = "$(Get-HabPackagePath aspnet-mvc1)\Assemblies\System.Web.Mvc.dll"
    $mvcAssemblyHintPathNode.InnerText = $mvcAssemblyHintPath
    $proj.GetElementsByTagName("Reference") | foreach {
       if($_.Include -eq 'System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL'){
          $_.AppendChild($mvcAssemblyHintPathNode)
       }
    }
    $proj.GetElementsByTagName("Import") | foreach {
    if($_.Project -eq '$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets'){
       $_.Project = '$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets'
    }   
 }
 $proj.Save($csprojPath)
 MSBuild.exe $csprojPath
}

Save plan.ps1. Let's build again. Return to the PowerShell window running your Habitat studio

build

Now we're building successfully!

##Install the app

Let's copy our install files into place. Add this code to the bottom of plan.ps1.

function Invoke-Install{
    New-Item -ItemType Directory -Path $pkg_prefix/MovieApp
    Write-Host "$HAB_CACHE_SRC_PATH/$pkg_dirname/MovieApp/MovieApp/bin/"
    Copy-Item "$HAB_CACHE_SRC_PATH/$pkg_dirname/MovieApp/MovieApp/App_Data" "$pkg_prefix/MovieApp" -recurse
    Copy-Item "$HAB_CACHE_SRC_PATH/$pkg_dirname/MovieApp/MovieApp/Scripts" "$pkg_prefix/MovieApp" -recurse
    Copy-Item "$HAB_CACHE_SRC_PATH/$pkg_dirname/MovieApp/MovieApp/Content" "$pkg_prefix/MovieApp" -recurse
    Copy-Item "$HAB_CACHE_SRC_PATH/$pkg_dirname/MovieApp/MovieApp/bin/" "$pkg_prefix/MovieApp" -recurse
    Copy-Item "$HAB_CACHE_SRC_PATH/$pkg_dirname/MovieApp/MovieApp/Views/" "$pkg_prefix/MovieApp" -recurse
    Copy-Item "$HAB_CACHE_SRC_PATH/$pkg_dirname/MovieApp/MovieApp/Default.aspx" "$pkg_prefix/MovieApp" -recurse
    Copy-Item "$HAB_CACHE_SRC_PATH/$pkg_dirname/MovieApp/MovieApp/Global.asax" "$pkg_prefix/MovieApp" -recurse
}

Save plan.ps1. Let's build again. Return to the PowerShell window running your Habitat studio

build

We've copied our files into place, but we haven't added any instructions about how to run it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment