Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build Mindustry from source #39

Merged
merged 8 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Mindustry flatpak

## Generator help
The `gendeps.sh` script can (for the most part) automatically generate the
gradle-sources.json file.

### How to use
1. Adjust the `$SOURCES_FILE` variable to where you want the json file to be
generated.
2. Clone the [Mindustry repo](https://github.com/Anuken/Mindustry) and cd into it.
3. Run the `gendeps.sh` script from there.

### The dependency `com.github.Anuken.Arc:backend-headless:???` isn't found
For some reason, the script doesn't automatically recognize the dependency on
com.github.Anuken.Arc:backend-headless. Thus it is manually added in the generator
script. Whenever the required version changes, the version written in the
generator needs to be adjusted (the failed gradle build will tell you the
expected version).

### Many dependencies are missing
Make sure gradle won't pull any dependencies from `mavenLocal` (`~/.m2/`).
Either remove the `mavenLocal()` source from the `build.gradle` or delete
`~/.m2/`.

If you need more help, ping [@TobTobXX](https://github.com/TobTobXX).

66 changes: 40 additions & 26 deletions com.github.Anuken.Mindustry.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
app-id: com.github.Anuken.Mindustry
runtime: org.freedesktop.Platform
runtime-version: '20.08'
runtime-version: '21.08'
sdk: org.freedesktop.Sdk
sdk-extensions:
- org.freedesktop.Sdk.Extension.openjdk11
- org.freedesktop.Sdk.Extension.openjdk17

command: mindustry.sh
finish-args:
Expand All @@ -18,43 +18,57 @@ finish-args:

build-options:
env:
JAVA_HOME: /usr/lib/sdk/openjdk11/
JAVA_HOME: /usr/lib/sdk/openjdk17/
modules:
- name: openjdk
buildsystem: simple
build-commands:
- /usr/lib/sdk/openjdk11/install.sh
- /usr/lib/sdk/openjdk17/install.sh


# We don't have a gradle generator just yet, so we don't build from source
- name: mindustry-binary
- name: mindustry
buildsystem: simple
build-commands:
- install -D Mindustry.jar /app/bin/Mindustry.jar
- install -D mindustry.sh /app/bin/mindustry.sh
- install -D com.github.Anuken.Mindustry.desktop /app/share/applications/com.github.Anuken.Mindustry.desktop
- install -D com.github.Anuken.Mindustry.metainfo.xml /app/share/metainfo/com.github.Anuken.Mindustry.metainfo.xml
- install -D com.github.Anuken.Mindustry.png /app/share/icons/hicolor/512x512/apps/com.github.Anuken.Mindustry.png

sources:
# Main repo
- type: git
url: 'https://github.com/Anuken/Mindustry'
tag: 'v135'
x-checker-data:
type: git
tag-pattern: '^v([0-9.]+)$'
# Use externally downloaded gradle wrapper
- type: patch
path: local-gradle-wrapper.patch
- type: file
url: https://f-droid.org/repo/io.anuke.mindustry/en-US/icon_Eno3XvqCZUcHRm3eMjiUleAxgzLopPe6-hkI7BHx1lU=.png
dest-filename: com.github.Anuken.Mindustry.png
sha256: 127a375efa82654707466dde32389495e0318332e8a4f7bafa1908ec11f1d655
url: 'https://services.gradle.org/distributions/gradle-7.3-bin.zip'
sha256: de8f52ad49bdc759164f72439a3bf56ddb1589c4cde802d3cec7d6ad0e0ee410
dest: 'gradle/wrapper/'
# Use externally downloaded dependencies
- type: patch
path: local-maven-repo.patch
- gradle-sources.json
# Build script
- type: script
commands:
- 'set -xe'
- 'version=$(git describe --tags | sed -E "s/^v([0-9.]+)$/\1/")'
- './gradlew desktop:dist --no-daemon -Pbuildversion=$version'
dest-filename: build.sh
build-commands:
- 'bash build.sh'
- 'install -D desktop/build/libs/Mindustry.jar /app/bin/Mindustry.jar'
- 'install -D android/ic_launcher-web.png /app/share/icons/hicolor/512x512/apps/com.github.Anuken.Mindustry.png'

- name: misc
buildsystem: simple
sources:
- type: file
path: com.github.Anuken.Mindustry.metainfo.xml

- type: file
path: com.github.Anuken.Mindustry.desktop

- type: file
path: mindustry.sh

- type: file
url: https://github.com/Anuken/Mindustry/releases/download/v135/Mindustry.jar
sha256: 62b3c1f99b4a86e81482e4c8148af44eaabe3b031b3315649414b208ad296ff0
x-checker-data:
type: anitya
project-id: 147357
url-template: https://github.com/Anuken/Mindustry/releases/download/v$version/Mindustry.jar
build-commands:
- 'install -D mindustry.sh /app/bin/mindustry.sh'
- 'install -D com.github.Anuken.Mindustry.desktop /app/share/applications/com.github.Anuken.Mindustry.desktop'
- 'install -D com.github.Anuken.Mindustry.metainfo.xml /app/share/metainfo/com.github.Anuken.Mindustry.metainfo.xml'
3 changes: 2 additions & 1 deletion flathub.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"automerge-flathubbot-prs": true
"automerge-flathubbot-prs": true,
"only-arches": ["x86_64"]
}
139 changes: 139 additions & 0 deletions gendeps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#!/bin/bash

set -e
# set -x

# This script generates Flatpak sources for a gradle project.

# It works by running the gradle build (--dry-run is usually enough) with
# a separate, new gradle user home (instead of ~/.gradle). All needed
# dependencies will be cached in this gradle user home. The script then moves
# all these cached artifacts into a directory structure of a maven repo.
# It also computes the hashes and checks the repos for where this artifact is
# available to generate the url.
# Finally, it writes the json file which can be used in the flatpak build
# manifest.

# You may want to modify these variables
# Where the sources json file should be generated:
SOURCES_FILE="../gradle-sources.json"
# The target to generate the dependencies for:
TARGET="desktop:dist"
# The maven repos:
REPO_BASEURL=(
'https://repo1.maven.org/maven2/'
'https://jitpack.io/'
'https://plugins.gradle.org/m2/'
)
# Manually specify dependencies that aren't getting cached for some reason:
MANUAL_ARTIFACTS=(
'com/github/Anuken/Arc/backend-headless/916c5a77/backend-headless-916c5a77.pom'
'com/github/Anuken/Arc/backend-headless/916c5a77/backend-headless-916c5a77.jar'
)


# These directories should be empty and will get `rm -r`d afterwards.
gradle_user_home="$(mktemp -d)"
maven_repo="$(mktemp -d)"

wd="$(pwd)"

# Let gradle fetch all the dependencies into a new clean gradle user home:
echo "Downloading all dependencies..."
./gradlew -g "$gradle_user_home" "$TARGET" --no-daemon -q
# --dry-run seems to miss some dependendencies
# ./gradlew -g "$gradle_user_home" "$TARGET" --no-daemon --dry-run -q


cd "$gradle_user_home/caches/modules-2/files-2.1" || exit 1

# Following two blocks are adapted from here:
# https://gist.github.com/danieldietrich/76e480f3fb903bdeaac5b1fb007ab5ac
# Thank you Daniel Dietrich!

# Transforms gradle cache paths to maven repo paths
function mavenize {
IFS='/' read -r -a paths <<< "$1"
groupId=$(echo "${paths[1]}" | tr . /)
artifactId="${paths[2]}"
version="${paths[3]}"
echo "$groupId/$artifactId/$version"
}

# Copy every file from the cache to it's maven repo location
find . -type f -print0 | while IFS= read -r -d '' file; do
filename=$(basename "$file")
source_dir=$(dirname "$file")
target_dir="$maven_repo/$(mavenize "$file")"
mkdir -p "$target_dir" && cp "$source_dir/$filename" "$target_dir/"
done

# All interesting files are now in the maven repo
cd "$wd"
rm -r "$gradle_user_home"

# Create the json sources file
cd "$maven_repo"

json_file="$wd/$SOURCES_FILE"
echo '[' > "$json_file"

# Download the $MANUAL_ARTIFACTS manually
for dep in "${MANUAL_ARTIFACTS[@]}"; do
mkdir -p "$(dirname "$dep")"

success=0
for repo in "${REPO_BASEURL[@]}"; do
url="${repo}${dep}"
if curl "$url" --fail --output "$dep" -L &> /dev/null; then
success=1
break
fi
done
if [ $success -eq 0 ]; then
echo "ERROR: No repo contains manual dependency $dep"
exit 1
fi
done

# `find *` to not use the ./ prefix when appending to $REPO_BASEURL
# shellcheck disable=SC2035
find * -type f -print0 | while IFS= read -r -d '' file; do
# Every repo if the ressource exists there
url=''
for repo in "${REPO_BASEURL[@]}"; do
url_to_try="${repo}${file}"
if curl --HEAD "$url_to_try" --fail -L &> /dev/null; then
url="$url_to_try"
break
fi
done
if [ -z "$url" ]; then
echo "ERROR: No repo contains $file"
exit 1
fi

hash="$(sha256sum "$file" | cut -f 1 -d ' ')"
cat << HERE >> "$json_file"
{
"type": "file",
"url": "$url",
"sha256": "$hash",
"dest": "maven-local/$(dirname "$file")",
"dest-filename": "$(basename "$file")"
},
HERE
done


# Remove last line in json file and relpace with closing braces without comma
head -n -1 "$json_file" > temp.json && mv temp.json "$json_file"
echo ' }' >> "$json_file"
# And close the json array
echo ']' >> "$json_file"

# Clean up maven repo too
cd "$wd"
rm -r "$maven_repo"

echo 'Finished. Success is unknown until observed.'
Loading