aboutsummaryrefslogtreecommitdiff
path: root/scripts/package.sh
blob: 21a8cc91a14782342083721aab469d92e8b3786a (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/bin/bash
set -e

if [ ! -e "src/api/api.h" ]; then
  echo "Please run this script from the root directory of Lite XL."; exit 1
fi

source scripts/common.sh

show_help() {
  echo
  echo "Usage: $0 <OPTIONS>"
  echo
  echo "Available options:"
  echo
  echo "-b --builddir DIRNAME     Sets the name of the build directory (not path)."
  echo "                          Default: '$(get_default_build_dir)'."
  echo "-d --destdir DIRNAME      Set the name of the package directory (not path)."
  echo "                          Default: 'lite-xl'."
  echo "-h --help                 Show this help and exit."
  echo "-p --prefix PREFIX        Install directory prefix. Default: '/'."
  echo "-v --version VERSION      Sets the version on the package name."
  echo "   --addons               Install 3rd party addons (currently RXI colors)."
  echo "   --debug                Debug this script."
  echo "-A --appimage             Create an AppImage (Linux only)."
  echo "-B --binary               Create a normal / portable package or macOS bundle,"
  echo "                          depending on how the build was configured. (Default.)"
  echo "-D --dmg                  Create a DMG disk image with AppDMG (macOS only)."
  echo "-I --innosetup            Create a InnoSetup package (Windows only)."
  echo "-S --source               Create a source code package,"
  echo "                          including subprojects dependencies."
  echo
}

# Addons installation: some distributions forbid external downloads
# so make it as optional module.
install_addons() {
  local build_dir="$1"
  local data_dir="$2"

  if [[ -d "${build_dir}/third/data/colors" ]]; then
    echo "Warning: found previous colors addons installation, skipping."
    return 0
  fi

  # Copy third party color themes
  curl --insecure \
    -L "https://github.com/rxi/lite-colors/archive/master.zip" \
    -o "${build_dir}/rxi-lite-colors.zip"

  mkdir -p "${build_dir}/third/data/colors"
  unzip "${build_dir}/rxi-lite-colors.zip" -d "${build_dir}"
  mv "${build_dir}/lite-colors-master/colors" "${build_dir}/third/data"
  rm -rf "${build_dir}/lite-colors-master"

  for module_name in colors; do
    cp -r "${build_dir}/third/data/$module_name" "${data_dir}"
  done
}

source_package() {
  local build_dir=build-src
  local package_name=$1

  rm -rf ${build_dir}
  rm -rf ${package_name}
  rm -f ${package_name}.tar.gz

  meson subprojects download
  meson setup ${build_dir} -Dsource-only=true

  # Note: not using git-archive(-all) because it can't include subprojects ignored by git
  rsync -arv \
    --exclude /*build*/ \
    --exclude *.git* \
    --exclude lhelper \
    --exclude lite-xl* \
    --exclude submodules \
    . ${package_name}

  cp "${build_dir}/start.lua" "${package_name}/data/core"

  tar rf ${package_name}.tar ${package_name}
  gzip -9 ${package_name}.tar
}

main() {
  local arch="$(uname -m)"
  local platform="$(get_platform_name)"
  local build_dir="$(get_default_build_dir)"
  local dest_dir=lite-xl
  local prefix=/
  local version
  local addons=false
  local appimage=false
  local binary=false
  local dmg=false
  local innosetup=false
  local source=false

  for i in "$@"; do
    case $i in
      -b|--builddir)
        build_dir="$2"
        shift
        shift
        ;;
      -d|--destdir)
        dest_dir="$2"
        shift
        shift
        ;;
      -h|--help)
        show_help
        exit 0
        ;;
      -p|--prefix)
        prefix="$2"
        shift
        shift
        ;;
      -v|--version)
        if [[ -n $2 ]]; then version="-$2"; fi
        shift
        shift
        ;;
      -A|--appimage)
        if [[ "$platform" != "linux" ]]; then
          echo "Warning: ignoring --appimage option, works only under Linux."
        else
          appimage=true
        fi
        shift
        ;;
      -B|--binary)
        binary=true
        shift
        ;;
      -D|--dmg)
        if [[ "$platform" != "macos" ]]; then
          echo "Warning: ignoring --dmg option, works only under macOS."
        else
          dmg=true
        fi
        shift
        ;;
      -I|--innosetup)
        if [[ "$platform" != "windows" ]]; then
          echo "Warning: ignoring --innosetup option, works only under Windows."
        else
          innosetup=true
        fi
        shift
        ;;
      -S|--source)
        source=true
        shift
        ;;
      --addons)
        addons=true
        shift
        ;;
      --debug)
        set -x
        shift
        ;;
      *)
        # unknown option
        ;;
    esac
  done

  if [[ -n $1 ]]; then show_help; exit 1; fi

  # The source package doesn't require a previous build,
  # nor the following install step, so run it now.
  if [[ $source == true ]]; then source_package "lite-xl$version-src"; fi

  # No packages request
  if [[ $appimage == false && $binary == false && $dmg == false && $innosetup == false ]]; then
    # Source only, return.
    if [[ $source == true ]]; then return 0; fi
    # Build the binary package as default instead doing nothing.
    binary=true
  fi

  rm -rf "${dest_dir}"

  DESTDIR="$(pwd)/${dest_dir}" meson install --skip-subprojects -C "${build_dir}"

  local data_dir="$(pwd)/${dest_dir}/data"
  local exe_file="$(pwd)/${dest_dir}/lite-xl"
  local package_name=lite-xl$version-$platform-$arch
  local bundle=false
  local portable=false
  local stripcmd="strip"

  if [[ -d "${data_dir}" ]]; then
    echo "Creating a portable, compressed archive..."
    portable=true
    exe_file="$(pwd)/${dest_dir}/lite-xl"
    if [[ $platform == "windows" ]]; then
      exe_file="${exe_file}.exe"
      stripcmd="strip --strip-all"
    else
      # Windows archive is always portable
      package_name+="-portable"
    fi
  elif [[ $platform == "macos" && ! -d "${data_dir}" ]]; then
    data_dir="$(pwd)/${dest_dir}/Contents/Resources"
    if [[ -d "${data_dir}" ]]; then
      echo "Creating a macOS bundle application..."
      bundle=true
      # Specify "bundle" on compressed archive only, implicit on images
      if [[ $dmg == false ]]; then package_name+="-bundle"; fi
      rm -rf "Lite XL.app"; mv "${dest_dir}" "Lite XL.app"
      dest_dir="Lite XL.app"
      exe_file="$(pwd)/${dest_dir}/Contents/MacOS/lite-xl"
    fi
  fi

  if [[ $bundle == false && $portable == false ]]; then
    echo "Creating a compressed archive..."
    data_dir="$(pwd)/${dest_dir}/$prefix/share/lite-xl"
    exe_file="$(pwd)/${dest_dir}/$prefix/bin/lite-xl"
  fi

  mkdir -p "${data_dir}"

  if [[ $addons == true ]]; then install_addons "${build_dir}" "${data_dir}"; fi

  # TODO: use --skip-subprojects when 0.58.0 will be available on supported
  # distributions to avoid subprojects' include and lib directories to be copied.
  # Install Meson with PIP to get the latest version is not always possible.
  pushd "${dest_dir}"
  find . -type d -name 'include' -prune -exec rm -rf {} \;
  find . -type d -name 'lib' -prune -exec rm -rf {} \;
  find . -type d -empty -delete
  popd

  $stripcmd "${exe_file}"

  if [[ $binary == true ]]; then
    rm -f "${package_name}".tar.gz
    rm -f "${package_name}".zip

    if [[ $platform == "windows" ]]; then
      zip -9rv ${package_name}.zip ${dest_dir}/*
    else
      tar czvf "${package_name}".tar.gz "${dest_dir}"
    fi
  fi

  if [[ $appimage == true ]]; then source scripts/appimage.sh; fi
  if [[ $bundle == true && $dmg == true ]]; then source scripts/appdmg.sh "${package_name}"; fi
  if [[ $innosetup == true ]]; then source scripts/innosetup/innosetup.sh -b "${build_dir}"; fi
}

main "$@"