about summary refs log tree commit diff
path: root/pkgs/build-support/dotnet/build-dotnet-module/fetch-deps.sh
blob: de134162dc845459b1ebe4a86094348d5420bb14 (plain) (blame)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#! @runtimeShell@
# shellcheck shell=bash
set -euo pipefail

export PATH="@path@"

for arg in "$@"; do
    case "$arg" in
        --keep-sources|-k)
            keepSources=1
            shift
            ;;
        --help|-h)
            echo "usage: $0 [--keep-sources] [--help] <output path>"
            echo "    <output path>   The path to write the lockfile to. A temporary file is used if this is not set"
            echo "    --keep-sources  Dont remove temporary directories upon exit, useful for debugging"
            echo "    --help          Show this help message"
            exit
            ;;
    esac
done

if [[ ${TMPDIR:-} == /run/user/* ]]; then
   # /run/user is usually a tmpfs in RAM, which may be too small
   # to store all downloaded dotnet packages
   unset TMPDIR
fi

export tmp=$(mktemp -td "deps-@pname@-XXXXXX")
HOME=$tmp/home

exitTrap() {
    test -n "${ranTrap-}" && return
    ranTrap=1

    if test -n "${keepSources-}"; then
        echo -e "Path to the source: $tmp/src\nPath to the fake home: $tmp/home"
    else
        rm -rf "$tmp"
    fi

    # Since mktemp is used this will be empty if the script didnt succesfully complete
    if ! test -s "$depsFile"; then
      rm -rf "$depsFile"
    fi
}

trap exitTrap EXIT INT TERM

dotnetRestore() {
    local -r project="${1-}"
    local -r rid="$2"

    dotnet restore ${project-} \
        -p:ContinuousIntegrationBuild=true \
        -p:Deterministic=true \
        --packages "$tmp/nuget_pkgs" \
        --runtime "$rid" \
        --no-cache \
        --force \
        @disableParallel@ \
        @flags@
}

declare -a projectFiles=( @projectFileStr@ )
declare -a testProjectFiles=( @testProjectFileStr@ )

export DOTNET_NOLOGO=1
export DOTNET_CLI_TELEMETRY_OPTOUT=1

depsFile=$(realpath "${1:-@defaultDepsFile@}")
echo Will write lockfile to "$depsFile"
mkdir -p "$tmp/nuget_pkgs"

storeSrc="@storeSrc@"
src=$tmp/src
cp -rT "$storeSrc" "$src"
chmod -R +w "$src"

cd "$src"
echo "Restoring project..."

"@dotnetSdkPath@/bin/dotnet" tool restore
cp -r $HOME/.nuget/packages/* $tmp/nuget_pkgs || true

runtimeIds=(@runtimeIds@)

for rid in "${runtimeIds[@]}"; do
    (( ${#projectFiles[@]} == 0 )) && dotnetRestore "" "$rid"

    for project in ${projectFiles[@]-} ${testProjectFiles[@]-}; do
        dotnetRestore "$project" "$rid"
    done
done
# Second copy, makes sure packages restored by ie. paket are included
cp -r $HOME/.nuget/packages/* $tmp/nuget_pkgs || true

echo "Succesfully restored project"

echo "Writing lockfile..."

excluded_sources=( @excludedSources@ )
for excluded_source in ${excluded_sources[@]}; do
  ls "$excluded_source" >> "$tmp/excluded_list"
done
tmpFile="$tmp"/deps.nix
echo -e "# This file was automatically generated by passthru.fetch-deps.\n# Please dont edit it manually, your changes might get overwritten!\n" > "$tmpFile"
nuget-to-nix "$tmp/nuget_pkgs" "$tmp/excluded_list" >> "$tmpFile"
mv "$tmpFile" "$depsFile"
echo "Succesfully wrote lockfile to $depsFile"