Skip to content

Commit

Permalink
Install the .NET Core SDK into the repo root instead of UserProfile a…
Browse files Browse the repository at this point in the history
…nd 'install' copy of AspNetCore shared framework (dotnet#7293)

This is required to workaround several limitations in the way the .NET Core SDK finds shared frameworks and targeting packs. It allow tests to use shared frameworks and targeting packs.

It also matches the patterns established in other aspnet and dotnet repos. This should reduce the friction required to adopt Arcade SDK.

## Changes

* This moves the default location of the .NET Core SDK installation into `$repoRoot/.dotnet`. This location was already in use for CI builds. 
* Update the build step for Microsoft.AspNetCore.App to install the shared framework into the local copy of the .NET Core SDK

## Recommendations

* Use the "startvs.cmd" script to launch Visual Studio. This will set required environment variables to make VS happier than if you just double click the .sln file.
* Use "activate.sh/ps1" if you want to run `dotnet build`, `dotnet test` and other dotnet commands. These will set required environment variables, including PATH.
* I recommend removing %USERPROFILE%/.dotnet to your PATH variable if you had added it manually before. This will no longer match what build tools will install.
* `git clean -xfd -e .dotnet/` preserves the folder so you don’t have to re-download the SDK again.
  • Loading branch information
natemcmaster authored Feb 6, 2019
1 parent 94a4293 commit 075612b
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 63 deletions.
8 changes: 8 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@
<IntermediateOutputPath Condition=" '$(PlatformName)' != 'AnyCPU' ">$(BaseIntermediateOutputPath)$(PlatformName)\$(Configuration)\</IntermediateOutputPath>
</PropertyGroup>

<!-- The location of the local installation of the .NET Core shared framework. -->
<PropertyGroup>
<LocalDotNetRoot Condition="'$(TargetOSName)' == 'win'">$(RepositoryRoot).dotnet\$(TargetArchitecture)\</LocalDotNetRoot>
<LocalDotNetRoot Condition="'$(TargetOSName)' != 'win'">$(RepositoryRoot).dotnet\</LocalDotNetRoot>
<!-- Override the SDK default and point to local .dotnet folder. -->
<NetCoreTargetingPackRoot>$(LocalDotNetRoot)packs\</NetCoreTargetingPackRoot>
</PropertyGroup>

<!-- Defines project type conventions. -->
<PropertyGroup>
<RepoRelativeProjectDir>$([MSBuild]::MakeRelative($(RepositoryRoot), $(MSBuildProjectDirectory)))</RepoRelativeProjectDir>
Expand Down
57 changes: 57 additions & 0 deletions activate.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#
# This file must be used by invoking ". .\activate.ps1" from the command line.
# You cannot run it directly.
# To exit from the environment this creates, execute the 'deactivate' function.
#

function deactivate ([switch]$init) {

# reset old environment variables
if (Test-Path variable:_OLD_PATH) {
$env:PATH = $_OLD_PATH
Remove-Item variable:_OLD_PATH
}

if (test-path function:_old_prompt) {
Set-Item Function:prompt -Value $function:_old_prompt -ea ignore
remove-item function:_old_prompt
}

Remove-Item env:DOTNET_ROOT -ea ignore
Remove-Item env:DOTNET_MULTILEVEL_LOOKUP -ea ignore
if (-not $init) {
# Remove the deactivate function
Remove-Item function:deactivate
}
}

# Cleanup the environment
deactivate -init

$_OLD_PATH = $env:PATH
# Tell dotnet where to find itself
$env:DOTNET_ROOT = "$PSScriptRoot\.dotnet\x64"
# Tell dotnet not to look beyond the DOTNET_ROOT folder for more dotnet things
$env:DOTNET_MULTILEVEL_LOOKUP = 0
# Put dotnet first on PATH
$env:PATH = "${env:DOTNET_ROOT};${env:PATH}"

# Set the shell prompt
if (-not $env:DISABLE_CUSTOM_PROMPT) {
$function:_old_prompt = $function:prompt
function dotnet_prompt {
# Add a prefix to the current prompt, but don't discard it.
write-host "($( split-path $PSScriptRoot -leaf )) " -nonewline
& $function:_old_prompt
}

Set-Item Function:prompt -Value $function:dotnet_prompt -ea ignore
}

Write-Host -f Magenta "Enabled the .NET Core environment. Execute 'deactivate' to exit."
if (-not (Test-Path "${env:DOTNET_ROOT}\dotnet.exe")) {
Write-Host -f Yellow ".NET Core has not been installed yet. Run $PSScriptRoot\restore.cmd to install it."
}
else {
Write-Host "dotnet = ${env:DOTNET_ROOT}\dotnet.exe"
}
69 changes: 69 additions & 0 deletions activate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#
# This file must be used by invoking "source activate.sh" from the command line.
# You cannot run it directly.
# To exit from the environment this creates, execute the 'deactivate' function.

_MAGENTA="\033[0;95m"
_YELLOW="\033[0;33m"
_RESET="\033[0m"

deactivate () {

# reset old environment variables
if [ ! -z "${_OLD_PATH:-}" ] ; then
export PATH="$_OLD_PATH"
unset _OLD_PATH
fi

if [ ! -z "${_OLD_PS1:-}" ] ; then
export PS1="$_OLD_PS1"
unset _OLD_PS1
fi

# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH:-}" ] || [ -n "${ZSH_VERSION:-}" ] ; then
hash -r 2>/dev/null
fi

unset DOTNET_ROOT
unset DOTNET_MULTILEVEL_LOOKUP
if [ ! "${1:-}" = "init" ] ; then
# Remove the deactivate function
unset -f deactivate
fi
}

# Cleanup the environment
deactivate init

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
_OLD_PATH="$PATH"
# Tell dotnet where to find itself
export DOTNET_ROOT="$DIR/.dotnet"
# Tell dotnet not to look beyond the DOTNET_ROOT folder for more dotnet things
export DOTNET_MULTILEVEL_LOOKUP=0
# Put dotnet first on PATH
export PATH="$DOTNET_ROOT:$PATH"

# Set the shell prompt
if [ -z "${DISABLE_CUSTOM_PROMPT:-}" ] ; then
_OLD_PS1="$PS1"
export PS1="(`basename \"$DIR\"`) $PS1"
fi

# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH:-}" ] || [ -n "${ZSH_VERSION:-}" ] ; then
hash -r 2>/dev/null
fi

echo "${_MAGENTA}Enabled the .NET Core environment. Execute 'deactivate' to exit.${_RESET}"

if [ ! -f "$DOTNET_ROOT/dotnet" ]; then
echo "${_YELLOW}.NET Core has not been installed yet. Run $DIR/restore.sh to install it.${_RESET}"
else
echo "dotnet = $DOTNET_ROOT/dotnet"
fi
8 changes: 2 additions & 6 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,7 @@ if (Test-Path $ConfigFile) {
}
}

$DotNetHome = if ($env:DOTNET_HOME) { $env:DOTNET_HOME } `
elseif ($CI) { Join-Path $PSScriptRoot '.dotnet' } `
elseif ($env:USERPROFILE) { Join-Path $env:USERPROFILE '.dotnet'} `
elseif ($env:HOME) {Join-Path $env:HOME '.dotnet'}`
else { Join-Path $PSScriptRoot '.dotnet'}

$DotNetHome = Join-Path $PSScriptRoot '.dotnet'
$env:DOTNET_HOME = $DotNetHome

# Execute
Expand Down Expand Up @@ -310,4 +305,5 @@ try {
}
finally {
Remove-Module 'KoreBuild' -ErrorAction Ignore
Remove-Item env:DOTNET_HOME
}
6 changes: 1 addition & 5 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,6 @@ while [[ $# -gt 0 ]]; do
;;
--ci|-[Cc][Ii])
ci=true
if [[ -z "${DOTNET_HOME:-}" ]]; then
DOTNET_HOME="$DIR/.dotnet"
fi
;;
--verbose|-[Vv]erbose)
verbose=true
Expand Down Expand Up @@ -286,8 +283,7 @@ if [ -f "$config_file" ]; then
[ ! -z "${config_tools_source:-}" ] && tools_source="$config_tools_source"
fi

[ -z "${DOTNET_HOME:-}" ] && DOTNET_HOME="$HOME/.dotnet"
export DOTNET_HOME="$DOTNET_HOME"
export DOTNET_HOME="$DIR/.dotnet"

get_korebuild

Expand Down
2 changes: 1 addition & 1 deletion build/repo.props
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
$(RepositoryRoot)src\SignalR\clients\ts\**\node_modules\**\*.*proj;
$(RepositoryRoot)src\Components\Blazor\Templates\src\content\**\*.*proj;
$(RepositoryRoot)src\ProjectTemplates\Web.ProjectTemplates\content\**\*.csproj;
$(RepositoryRoot)src\ProjectTemplates\Web.ProjectTemplates\content\**\*.fsproj;
$(RepositoryRoot)src\ProjectTemplates\Web.Spa.ProjectTemplates\content\**\*.csproj;
" />

Expand Down Expand Up @@ -159,7 +160,6 @@
$(RepositoryRoot)src\SignalR\**\*.csproj;
$(RepositoryRoot)src\Components\**\*.csproj;
$(RepositoryRoot)src\ProjectTemplates\*\*.csproj;
$(RepositoryRoot)src\ProjectTemplates\test\*.csproj;
$(RepositoryRoot)src\ProjectTemplates\testassets\*\*.csproj;
"
Exclude="
Expand Down
46 changes: 27 additions & 19 deletions docs/BuildFromSource.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ To update an existing copy, run:
git submodule update --init --recursive
```

## Building in Visual Studio / Code
## Building in Visual Studio

Before opening our .sln files in Visual Studio or VS Code, you need to perform the following actions.

Expand Down Expand Up @@ -95,30 +95,22 @@ Or you can use this script to automatically traverse the project reference graph

./eng/scripts/AddAllProjectRefsToSolution.ps1 -WorkingDir src/Mvc/

#### PATH
## Building with Visual Studio Code

For VS Code and Visual Studio and `dotnet` commands to work correctly, you must place the following location in your PATH.
Use the following commands to update the PATH variable in a command line window.
Using Visual Studio Code with this repo requires setting environment variables on command line first.
Use these command to launch VS Code with the right settings.

Windows (Command Prompt)

```batch
set PATH=%USERPROFILE%\.dotnet\x64;%PATH%
On Windows (requires PowerShell):
```

Windows (Powershell)

```ps1
$env:PATH="$env:USERPROFILE\.dotnet\x64;$env:PATH"
. activate.ps1
code .
```

Linux/macOS:

```sh
export PATH="$HOME/.dotnet:$PATH"
On macOS/Linux:
```
source activate.sh
code .
```

On Windows, we recommend using the `startvs.cmd` command to launch Visual Studio.

## Building on command-line

Expand All @@ -134,6 +126,22 @@ On macOS/Linux:
./build.sh
```

### Using `dotnet` on command line in this repo

Because we are using pre-release versions of .NET Core, you have to set a handful of environment variables
to make the .NET Core command line tool work well. You can set these environment variables like this

On Windows (requires PowerShell):

```ps1
. .\activate.ps1
```

On macOS/Linux:
```bash
source ./activate.sh
```

## Running tests on command-line

Tests are not run by default. Use the `-test` option to run tests in addition to building.
Expand Down
Loading

0 comments on commit 075612b

Please sign in to comment.