Creating a module that chooses different files

Hi all.
I have written a module to process some data. Depending on the computer where it is running I would like to use different files for the plotting. If we are using a normal computer with a screen I would like to use “vizGL.jl” that runs with GLMakie. In the case he computer is headless I want to call “vizCairo.jl”.

I tried this:


 module DASVader
 
 include("headless2.jl")
 
 if has_graphical_display() == true
     include("vizGL.jl")
     @info "Working on graphical mode"
 else
     include("vizCairo.jl")
     @info "Working on headless mode"
 end
 
 include("tandf.jl")
 include("wmatrix.jl")
 include("filemanagement.jl")
 include("xandw.jl")
 include("detectiontools.jl")
 include("Findpeaks.jl")
 include("myspectogram.jl")
 include("myspectra.jl")
 include("datasrc.jl")
 include("interface.jl")
 
 end

But it does not work :(.

headless2.jl is like this:


function env_has_display()
    if Sys.islinux()
        return !isempty(get(ENV, "DISPLAY", "")) || !isempty(get(ENV, "WAYLAND_DISPLAY", ""))
    elseif Sys.iswindows() || Sys.isapple()
        return true  # Assume GUI unless proven otherwise
    else
        return false
    end
end

function glmakie_screen_available()
    try
        @eval using GLMakie  # Delay loading to avoid errors
        s = GLMakie.Screen()
        close(s)
        return true
    catch
        return false
    end
end

function has_graphical_display()
    env_has_display() && glmakie_screen_available()
end

export has_graphical_display, glmakie_screen_available, env_has_display

Any idea?

You could try adding @static:

@static if has_graphical_display() == true
     include("vizGL.jl")
     @info "Working on graphical mode"
 else
     include("vizCairo.jl")
     @info "Working on headless mode"
 end

This means that the if statement will be run at parse time, effectively just leaving the branch that you want. E.g.

julia> @macroexpand @static if Sys.isapple() ; include("macOS.jl") ; else include("everythingelse.jl") ; end
quote
    #= REPL[4]:1 =#
    include("macOS.jl")
end

But any result will be cached (if this is in a package) and will not change for that machine even if an environment variable changes. (Which may or may not be what you want.)

HI @dawbarton .

I just noticed that my problem is much bigger thatn this. In the Project file, I cannot have GLMakie as a dependency while working on the server :(. I am rather lost here