Source

omy / OMyApt

# Tools for apt-get
# =====================

#| A flag to tell that we can use OMyApt functions. True by default. Turn it on if you want to have automatic apt package installation.
WithOMyApt=true

#| Installation check. Return the list of installed apt packages.
#
# .. note :: Users do not need to call this function. This is used to define $(InstalledAptPackages).
ListInstalledAptPackages() =
  println(Listing installed packages...)
  cmd=COLUMNS=200 dpkg -l
  answer[]=
  res=$(shella $(cmd))
  foreach(line, $(res))
    match $(line)
    case $'^ii  \([^ ]+\)'
      answer+=$1
      export
    default
    export
  return $(answer)

.MEMO:
#| Lazy variable of installed apt installed packages.
    InstalledAptPackages=$(ListInstalledAptPackages)
    export

#| Install $(packages) if not installed, using sudo apt-get install -y.
#
# .. note :: User should use RequireAptPackage() function instead.
#            Bug: Currently apt-get install is performed even if it is not requred
InstallAptPackages(packages) =
    required = $(set-diff $(packages), $(InstalledAptPackages))
    if $(not $(equal $(required), $(EMPTY)))
      println(Installing apt package $(string $(required)))
      cmd[]=sudo apt-get install -y
      foreach(r, $(required))
         cmd += $r
         export 
      println(the cmd $(string $(cmd)))
      $(cmd)
      # Reload the set
      public.InstalledAptPackages=$(ListInstalledAptPackages)
      export InstalledAptPackages
    export InstalledAptPackages

##| Add dependencies over the apt $(packages) for the files created in the current directory.
# BUG: This disables omake -P!
# RequireAptPackages(packages) =
#     .apt-installed: :value: $(InstalledAptPackages)
#         section: 
#           println(Installing $(packages) in $(length $(InstalledAptPackages)))
#           InstallAptPackages($(packages))    
#           chan=$(fopen $@, w)
#           fprintln($(chan), $(string $(intersection $(packages), $(InstalledAptPackages))))
#           close($(chan))
#     .SCANNER: scan-%: .apt-installed
#     % : .apt-installed
#     export

#| Installs the apt $(packages).
RequireAptPackages(packages)= 
    InstallAptPackages($(packages))