From 671a372d87ff8311956f9158e8885ffc254fe1bc Mon Sep 17 00:00:00 2001 From: pacien Date: Tue, 29 Jun 2021 13:14:14 +0200 Subject: compiler: add "portable" target This adds a build flag for generating a portable version of the compiler binary which make it look in its own runtime directory for its assets. This is useful in particular for the portable release tarballs which contain the web viewer at the same location instead of a pre-defined one in the FHS. GitHub: closes #286 --- compiler/app/Main.hs | 29 +++++++++++++---------------- compiler/app/ViewerDist.hs | 39 +++++++++++++++++++++++++++++++++++++++ compiler/package.yaml | 14 +++++++++++++- 3 files changed, 65 insertions(+), 17 deletions(-) create mode 100644 compiler/app/ViewerDist.hs diff --git a/compiler/app/Main.hs b/compiler/app/Main.hs index dc97b38..3e6f254 100644 --- a/compiler/app/Main.hs +++ b/compiler/app/Main.hs @@ -19,7 +19,7 @@ module Main where import GHC.Generics (Generic) -import Paths_ldgallery_compiler (version, getDataFileName) +import Paths_ldgallery_compiler (version) import Control.Monad (when) import Data.Functor ((<&>)) import Data.Maybe (isJust) @@ -31,7 +31,7 @@ import System.Console.CmdArgs import Compiler import Files (readDirectory, copyTo, remove) - +import ViewerDist (viewerDistPath) newtype ViewerConfig = ViewerConfig { galleryRoot :: String @@ -106,10 +106,7 @@ main = do opts <- cmdArgs options buildGallery opts - - when (isJust $ withViewer opts) $ do - viewerDist <- viewerDistPath $ withViewer opts - deployViewer viewerDist opts + deployViewer opts where gallerySubdir :: String @@ -118,11 +115,6 @@ main = viewerConfig :: ViewerConfig viewerConfig = ViewerConfig (gallerySubdir ++ "/") - viewerDistPath :: Maybe FilePath -> IO FilePath - viewerDistPath (Just "") = getDataFileName "viewer" - viewerDistPath (Just dist) = return dist - viewerDistPath Nothing = fail "No viewer distribution" - buildGallery :: Options -> IO () buildGallery opts = checkDistinctPaths (inputDir opts) (outputDir opts) @@ -146,10 +138,11 @@ main = | isJust withViewer = outputDir gallerySubdir | otherwise = outputDir - deployViewer :: FilePath -> Options -> IO () - deployViewer distPath Options{outputDir, cleanOutput} = + deployViewer :: Options -> IO () + deployViewer Options{withViewer = Nothing} = pure () + deployViewer Options{withViewer = Just viewerPath, outputDir, cleanOutput} = when cleanOutput (cleanViewerDir outputDir) - >> copyViewer distPath outputDir + >> viewerDistOr viewerPath >>= deployTo outputDir >> writeJSON (outputDir "config.json") viewerConfig where @@ -159,8 +152,12 @@ main = <&> filter (/= gallerySubdir) >>= mapM_ (remove . (target )) - copyViewer :: FilePath -> FilePath -> IO () - copyViewer dist target = + viewerDistOr :: FilePath -> IO FilePath + viewerDistOr "" = viewerDistPath + viewerDistOr custom = pure custom + + deployTo :: FilePath -> FilePath -> IO () + deployTo target dist = putStrLn "Copying viewer webapp" >> readDirectory dist >>= copyTo target diff --git a/compiler/app/ViewerDist.hs b/compiler/app/ViewerDist.hs new file mode 100644 index 0000000..2b80ffc --- /dev/null +++ b/compiler/app/ViewerDist.hs @@ -0,0 +1,39 @@ +-- ldgallery - A static generator which turns a collection of tagged +-- pictures into a searchable web gallery. +-- +-- Copyright (C) 2021 Pacien TRAN-GIRARD +-- +-- This program is free software: you can redistribute it and/or modify +-- it under the terms of the GNU Affero General Public License as +-- published by the Free Software Foundation, either version 3 of the +-- License, or (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU Affero General Public License for more details. +-- +-- You should have received a copy of the GNU Affero General Public License +-- along with this program. If not, see . + +{-# LANGUAGE CPP #-} + +module ViewerDist where + +#ifndef FLAG_PORTABLE + +import Paths_ldgallery_compiler (getDataFileName) + +viewerDistPath = getDataFileName "viewer" + +#else + +import Data.Functor ((<&>)) +import System.FilePath (takeDirectory, ()) +import System.Environment (getExecutablePath) + +viewerDistPath = fmap takeDirectory getExecutablePath <&> ( "viewer") + +#endif + +viewerDistPath :: IO FilePath diff --git a/compiler/package.yaml b/compiler/package.yaml index faa2174..b02a40a 100644 --- a/compiler/package.yaml +++ b/compiler/package.yaml @@ -5,7 +5,7 @@ github: "pacien/ldgallery" license: AGPL-3 author: "Pacien TRAN-GIRARD, Guillaume FOUET" maintainer: "" -copyright: "2019-2020 Pacien TRAN-GIRARD, Guillaume FOUET" +copyright: "2019-2021 Pacien TRAN-GIRARD, Guillaume FOUET" extra-source-files: - readme.md @@ -54,6 +54,15 @@ data-files: ["**/*"] library: source-dirs: src +flags: + portable: + description: > + Make the output binary portable. + It will look in its own runtime location for its assets instead of + absolute installation paths. + manual: true + default: false + executables: ldgallery: main: Main.hs @@ -64,6 +73,9 @@ executables: - -with-rtsopts=-N dependencies: - ldgallery-compiler + when: + - condition: flag(portable) + cpp-options: -DFLAG_PORTABLE tests: ldgallery-compiler-test: -- cgit v1.2.3