From c90059f5323f9e45ca81c97d54a34d813513d5af Mon Sep 17 00:00:00 2001 From: Morten Delenk Date: Sat, 13 Aug 2022 17:42:42 +0100 Subject: [PATCH] build the server with nix --- build.gradle | 24 ++++++++++++ flake.nix | 37 +++++++++++++++++++ result | 1 + .../server/db/LocationLogTable.java | 36 ++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 120000 result diff --git a/build.gradle b/build.gradle index 23802a7..2375b4c 100644 --- a/build.gradle +++ b/build.gradle @@ -9,3 +9,27 @@ plugins { task clean(type: Delete) { delete rootProject.buildDir } + +task resolveDependencies { + doLast { + project.rootProject.allprojects.each { subProject -> + subProject.buildscript.configurations.each { configuration -> + resolveConfiguration(subProject, configuration, "buildscript config '${configuration.name}'") + } + subProject.configurations.each { configuration -> + resolveConfiguration(subProject, configuration, "config '${configuration.name}'") + } + } + } +} + +void resolveConfiguration(subProject, configuration, name) { + // TODO: fix this + if (subProject.name == "app") { + return; + } + if (configuration.canBeResolved) { + logger.info("Resolving project {} {}", subProject.name, name) + configuration.resolve() + } +} diff --git a/flake.nix b/flake.nix index eee9b41..e8806da 100644 --- a/flake.nix +++ b/flake.nix @@ -35,5 +35,42 @@ # override the aapt2 that gradle uses with the nix-shipped version GRADLE_OPTS = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${androidSdk}/libexec/android-sdk/build-tools/32.0.0/aapt2"; }; + packages = rec { + invtracker-deps = pkgs.stdenv.mkDerivation { + pname = "invtracker-deps"; + version = self.lastModifiedDate; + src = self; + nativeBuildInputs = with pkgs; [ gradle openjdk_headless perl ]; + buildPhase = '' + export GRADLE_USER_HOME=$(mktemp -d) + # Fetch the maven deps + gradle --no-daemon --info -Dorg.gradle.java.home=${pkgs.openjdk_headless} resolveDependencies + ''; + installPhase = '' + find $GRADLE_USER_HOME/caches/modules-2 -type f -regex '.*\.\(jar\|pom\)$' \ + | perl -pe 's#(.*/([^/]+)/([^/]+)/([^/]+)/[0-9a-f]{30,40}/([^/\s]+))$# ($x = $2) =~ tr|\.|/|; "install -Dm444 $1 \$out/maven/$x/$3/$4/$5" #e' \ + | sh + ''; + outputHashAlgo = "sha256"; + outputHashMode = "recursive"; + outputHash = "sha256-OXNB19WPC5MyLEoDAtnVM9XykY7e0mKGRH5Aw8C/lMo="; + }; + invtracker-server = pkgs.stdenv.mkDerivation { + pname = "invtracker-server"; + version = self.lastModifiedDate; + src = self; + nativeBuildInputs = with pkgs; [ gradle openjdk_headless ]; + buildPhase = '' + sed -i 's#gradlePluginPortal..#maven { url "${invtracker-deps}/maven" }#' settings.gradle + sed -i 's#google..#maven { url "${invtracker-deps}/maven" }#' settings.gradle + + export GRADLE_USER_HOME=$(mktemp -d) + gradle --offline --no-daemon --info -Dorg.gradle.java.home=${pkgs.openjdk_headless} server:installDist + ''; + installPhase = '' + cp -rv server/build/install/server $out/ + ''; + }; + }; }); } diff --git a/result b/result new file mode 120000 index 0000000..b9e28b3 --- /dev/null +++ b/result @@ -0,0 +1 @@ +/nix/store/llwxk8hih9hrw235472f14d2ds4m4qyh-invtracker-server-20220813100940 \ No newline at end of file diff --git a/server/src/main/java/rs/chir/invtracker/server/db/LocationLogTable.java b/server/src/main/java/rs/chir/invtracker/server/db/LocationLogTable.java index 522d9d5..5166d4f 100644 --- a/server/src/main/java/rs/chir/invtracker/server/db/LocationLogTable.java +++ b/server/src/main/java/rs/chir/invtracker/server/db/LocationLogTable.java @@ -25,18 +25,48 @@ import rs.chir.db.DBConnection; import rs.chir.invtracker.model.Cursor; import rs.chir.invtracker.model.GeoLocation; +/** + * A table for storing locations for tracked items. + */ public class LocationLogTable implements Map { + /** + * The page size to use for API cursors + */ private static final int PAGE_SIZE = 50; + /** + * The database connection to use for accessing the table. + */ private final DBConnection dbConnection; + /** + * The user ID to use for accessing the table. + */ private final long userId; + /** + * The item ID to use for accessing the table. + */ private final long itemId; + /** + * Creates a new table. + * + * @param dbConnection the database connection to use for accessing the table. + * @param userId the user ID to use for accessing the table. + * @param itemId the item ID to use for accessing the table. + */ private LocationLogTable(DBConnection dbConnection, long userId, long itemId) { this.dbConnection = dbConnection; this.userId = userId; this.itemId = itemId; } + /** + * Retrieve the table for the given user and item. + * + * @param dbConnection the database connection to use for accessing the table. + * @param userId the user ID to use for accessing the table. + * @param itemId the item ID to use for accessing the table. + * @return the new table. + */ @NonNull public static LocationLogTable getInstance(@NonNull DBConnection dbConnection, long userId, long itemId) { // TODO: add caching maybe @@ -45,6 +75,12 @@ public class LocationLogTable implements Map { return result; } + /** + * Converts a row from the database to a location. + * @param resultSet the result set to convert. + * @return the location. + * @throws SQLException if an error occurs. + */ @NonNull @Contract("_ -> new") private static GeoLocation fromResultSet(@NonNull ResultSet rs) throws SQLException {