aboutsummaryrefslogtreecommitdiff
path: root/build
blob: 95d5d5c495d4be88adb004544b07a6ea0dfa2cb0 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env bash

# build - build Jellyfin binaries or packages

set -o errexit
set -o pipefail

# The list of possible package actions (except 'clean')
declare -a actions=( 'build' 'package' 'sign' 'publish' )

# The list of possible platforms, based on directories under 'deployment/'
declare -a platforms=( $(
    find deployment/ -maxdepth 1 -mindepth 1 -type d -exec basename {} \; | sort
) )

# The list of standard dependencies required by all build scripts; individual
# action scripts may specify their own dependencies
declare -a dependencies=( 'tar' 'zip' )

usage() {
    echo -e "build - build Jellyfin binaries or packages"
    echo -e ""
    echo -e "Usage:"
    echo -e " $ build --list-platforms"
    echo -e " $ build --list-actions <platform>"
    echo -e " $ build [-k/--keep-artifacts] [-b/--web-branch <web_branch>] <platform> <action>"
    echo -e ""
    echo -e "The 'keep-artifacts' option preserves build artifacts, e.g. Docker images for system package builds."
    echo -e "The web_branch defaults to the same branch name as the current main branch or can be 'local' to not touch the submodule branching."
    echo -e "To build all platforms, use 'all'."
    echo -e "To perform all build actions, use 'all'."
    echo -e "Build output files are collected at '../bin/<platform>'."
}

# Show usage on stderr with exit 1 on argless
if [[ -z $1 ]]; then
    usage >&2
    exit 1
fi
# Show usage if -h or --help are specified in the args
if [[ $@ =~ '-h' || $@ =~ '--help' ]]; then
    usage
    exit 0
fi

# List all available platforms then exit
if [[ $1 == '--list-platforms' ]]; then
    echo -e "Available platforms:"
    for platform in ${platforms[@]}; do
        echo -e "  ${platform}"
    done
    exit 0
fi

# List all available actions for a given platform then exit
if [[ $1 == '--list-actions' ]]; then
    platform="$2"
    if [[ ! " ${platforms[@]} " =~ " ${platform} " ]]; then
        echo "ERROR: Platform ${platform} does not exist."
        exit 1
    fi
    echo -e "Available actions for platform ${platform}:"
    for action in ${actions[@]}; do
        if [[ -f deployment/${platform}/${action}.sh ]]; then
            echo -e "  ${action}"
        fi
    done
    exit 0
fi

# Parse keep-artifacts option
if [[ $1 == '-k' || $1 == '--keep-artifacts' ]]; then
    keep_artifacts="y"
    shift 1
else
    keep_artifacts="n"
fi

# Parse branch option
if [[ $1 == '-b' || $1 == '--web-branch' ]]; then
	web_branch="$2"
	shift 2
else
    web_branch="$( git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' )"
fi

# Parse platform option
if [[ -n $1 ]]; then
    cli_platform="$1"
    shift
else
    echo "ERROR: A platform must be specified. Use 'all' to specify all platforms."
    exit 1
fi
if [[ ${cli_platform} == 'all' ]]; then
    declare -a platform=( ${platforms[@]} )
else
    if [[ ! " ${platforms[@]} " =~ " ${cli_platform} " ]]; then
        echo "ERROR: Platform ${cli_platform} is invalid. Use the '--list-platforms' option to list available platforms."
        exit 1
    else
        declare -a platform=( "${cli_platform}" )
    fi
fi

# Parse action option
if [[ -n $1 ]]; then
    cli_action="$1"
    shift
else
    echo "ERROR: An action must be specified. Use 'all' to specify all actions."
    exit 1
fi
if [[ ${cli_action} == 'all' ]]; then
    declare -a action=( ${actions[@]} )
else
    if [[ ! " ${actions[@]} " =~ " ${cli_action} " ]]; then
        echo "ERROR: Action ${cli_action} is invalid. Use the '--list-actions <platform>' option to list available actions."
        exit 1
    else
        declare -a action=( "${cli_action}" )
    fi
fi

# Verify required utilities are installed
missing_deps=()
for utility in ${dependencies[@]}; do
    if ! which ${utility} &>/dev/null; then
        missing_deps+=( ${utility} )
    fi
done

# Error if we're missing anything
if [[ ${#missing_deps[@]} -gt 0 ]]; then
    echo -e "ERROR: This script requires the following missing utilities:"
    for utility in ${missing_deps[@]}; do
        echo -e "  ${utility}"
    done
    exit 1
fi

# Parse platform-specific dependencies
for target_platform in ${platform[@]}; do
    # Read platform-specific dependencies
    if [[ -f deployment/${target_platform}/dependencies.txt ]]; then
        platform_dependencies="$( grep -v '^#' deployment/${target_platform}/dependencies.txt )"

        # Verify required utilities are installed
        missing_deps=()
        for utility in ${platform_dependencies[@]}; do
            if ! which ${utility} &>/dev/null; then
                missing_deps+=( ${utility} )
            fi
        done

        # Error if we're missing anything
        if [[ ${#missing_deps[@]} -gt 0 ]]; then
            echo -e "ERROR: The ${target_platform} platform requires the following utilities:"
            for utility in ${missing_deps[@]}; do
                echo -e "  ${utility}"
            done
            exit 1
        fi
    fi
done

# Execute each platform and action in order, if said action is enabled
pushd deployment/
for target_platform in ${platform[@]}; do
    echo -e "> Processing platform ${target_platform}"
    date_start=$( date +%s )
    pushd ${target_platform}
    cleanup() {
        echo -e ">> Processing action clean"
        if [[ -f clean.sh && -x clean.sh ]]; then
            ./clean.sh ${keep_artifacts}
        fi
    }
    trap cleanup EXIT INT
    for target_action in ${action[@]}; do
        echo -e ">> Processing action ${target_action}"
        if [[ -f ${target_action}.sh && -x ${target_action}.sh ]]; then
            ./${target_action}.sh web_branch=${web_branch}
        fi
    done
    if [[ -d pkg-dist/ ]]; then
        echo -e ">> Collecting build artifacts"
        target_dir="../../../bin/${target_platform}"
        mkdir -p ${target_dir}
        mv pkg-dist/* ${target_dir}/
    fi
    cleanup
    date_end=$( date +%s )
    echo -e "> Completed platform ${target_platform} in $( expr ${date_end} - ${date_start} ) seconds."
    popd
done
popd