Skip to content

Commit

Permalink
srcdir detection
Browse files Browse the repository at this point in the history
  • Loading branch information
turboMaCk committed May 23, 2019
1 parent aaebde0 commit 505750c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
8 changes: 4 additions & 4 deletions data/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let
mkDerivation =
{ elmSrcs ? ./elm-srcs.nix
, srcs
, srcDir ? "./src"
, srcdir ? "./src"
, extension ? ".elm"
, name
, targets ? []
Expand All @@ -26,12 +26,12 @@ let
};

installPhase = let
elmfile = module: "\${srcDir}/\${builtins.replaceStrings ["."] ["/"] module}\${extension}";
elmfile = module: "\${srcdir}/\${builtins.replaceStrings ["."] ["/"] module}\${extension}";
# TODO: review naming
elmJson = pkgs.lib.importJSON ./elm.json;
elmJsonFile = pkgs.writeText "elm.json" (builtins.toJSON generatedElmJson);
# TODO: hacky.. there might be a better way
genSrcs = xs: map (path: if path == "." then srcDir else builtins.replaceStrings [".." "/"] ["" ""] path) xs;
genSrcs = xs: map (path: if path == "." then srcdir else builtins.replaceStrings [".." "/"] ["" ""] path) xs;
generatedElmJson = with pkgs.lib;
if hasAttrByPath ["source-directories"] elmJson then
# TODO: check if there isn't better function
Expand Down Expand Up @@ -62,5 +62,5 @@ in mkDerivation {
# - consult with maitainer
srcs = ${srcs};
targets = ["Main"];
srcDir = "${srcDir}";
srcdir = "${srcdir}";
}
1 change: 1 addition & 0 deletions elm2nix.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ library
, transformers
, unordered-containers
, vector
, directory
exposed-modules:
Elm2Nix
Elm2Nix.FixedOutput
Expand Down
24 changes: 21 additions & 3 deletions src/Elm2Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import Data.Vector (Vector)
import System.Exit (exitFailure)
import System.IO (hPutStrLn, stderr)

import qualified System.Directory
import qualified Data.HashMap.Strict as HM
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Aeson as Json
import qualified Data.Text as Text
import qualified Data.Vector as Vector

import Elm2Nix.FixedOutput (FixedDerivation(..), prefetch)
import Elm2Nix.PackagesSnapshot (snapshot)
Expand Down Expand Up @@ -123,6 +125,7 @@ initialize = runCLI $ do
liftIO (hPutStrLn stderr $ "Using source directories:")
liftIO (mapM (hPutStrLn stderr) srcs')
let srcs = stringifySrcs srcs'
srcdir <- liftIO (getSrcDir srcs')

liftIO (putStrLn [template|data/default.nix|])
where
Expand All @@ -135,10 +138,25 @@ initialize = runCLI $ do
toNixName = Text.replace "/" "-"
name :: String
name = Text.unpack (toNixName baseName <> "-" <> version)
srcDir :: String
srcDir = "./src" -- TODO: get from elm.json
getSrcDir :: Vector FilePath -> IO FilePath
getSrcDir dirs =
case Vector.toList dirs of
[] -> pure "./src" -- in theory redundant
[ dir ] -> pure dir
xs@(h:t) ->
if "./." `elem` xs then do
-- Nix creates dir named after current directory
-- if `srcs` contains `./.`
path <- liftIO System.Directory.getCurrentDirectory
pure (lastDir path)
else
-- We can't really tell which one to choose
-- so we guess it's the first one
pure h
lastDir :: FilePath -> FilePath
lastDir = foldl (\path c -> if c == '/' then "" else path <> [c]) ""
-- TODO: Improve
stringifySrcs :: Vector String -> String
stringifySrcs :: Vector FilePath -> String
stringifySrcs xs =
"[\n"
<> foldr (\i acc -> " " <> i <> "\n" <> acc) "" xs
Expand Down

0 comments on commit 505750c

Please sign in to comment.