omy / OMyTar

# ==========================
# OMyTar
# ==========================
# Handling tar archives, such as .tar, .tar.gz, .tar.bz2, .tgz, .tbz

TarCompressSwitch(f) =
  match $(f)
  case $'tar$'
      return $"tar"
  case $'gz$'
      return $"z"
  case $'bz2?$'
      return $"j"

#| Extract tar archive $(f). 
TarExtract(f) =
  ext = $(TarCompressSwitch $(f))
  tar $(ext)xf $(f)

#| Extract $(f) to directory $(dir)
TarExtractAt(f, dir) =
  ext = $(TarCompressSwitch $(f))
  tar $(ext)xf $(f) -C $(dir) 

#| Obtain the list of archived files in $(f)
TarList(f) =
  ext=$(TarCompressSwitch $(f))
  cmd=tar $(ext)tf $(f)
  return $(shella $(cmd))

# CR : not finished
TopDir(f) =
  match $(f)
  case $'^\([^/]+\)/'
    return $1
    return yyy

TarExtractDir(f) =
  files=$(TarList $(f))
  return $(set $(foreach $(fun x => $(TopDir $(x))), $(files)))

#| Extract tar archive $(f) to directory $(dir).
#  If the archive has more than one files or directories in its top directory structure,
#  they are expanded into a newly created directory, whose name is from $(f). 
TarAutoExtract(f, dir) =
  extdirs = $(TarExtractDir $(f))
  extdir = $(dir)$(DIRSEP)$(if $(eq 1, $(length $(extdirs))), $(nth 0, $(extdirs)), $(rootname $(basename $(f))))
  atdir = $(if $(eq 1, $(length $(extdirs))), $(dir), $(extdir))
  println(EXTDIR $(extdir))
  println(ATDIR $(atdir))
  TarExtractAt($(f), $(atdir))
  return $(extdir)

# TarAutoExtract($"external/findlib/findlib-1.2.1.tar.gz", $"hoge")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.