Skip to content

Instantly share code, notes, and snippets.

@ugexe

ugexe/xxx.pl6 Secret

Created August 4, 2017 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ugexe/90b41dbf217c96f03282cc60f621390c to your computer and use it in GitHub Desktop.
Save ugexe/90b41dbf217c96f03282cc60f621390c to your computer and use it in GitHub Desktop.
unit module Zef::Utils::SystemCommands;
## HELPERS
our $*ZEF-USERAGENT = "zef/0.1.* ({$*PERL.compiler}/{$*PERL.compiler.version})";
our sub ZEF-SHELL-DOWNLOAD($uri, |c) is export {
$uri.&run-powershell-webrequest(|c)
// $uri.&run-curl(|c)
// $uri.&run-wget(|c)
}
our sub ZEF-SHELL-UNTAR($tar-file, $path-inside-tar, |c) is export {
$tar-file.&run-tar($path-inside-tar, |c)
// $tar-file.&run-perl5-tar($path-inside-tar, |c)
}
our sub ZEF-SHELL-UNZIP($zip-file, $path-inside-zip, |c) is export {
$zip-file.&run-powershell-unzip($path-inside-zip, |c)
// $zip-file.&run-unzip($path-inside-zip, |c)
}
## [Powershell Invoke-WebRequest]
our proto sub run-powershell-webrequest($uri, |c) is export {
{*} if once { $*DISTRO.is-win && try { shell('cmd /c powershell -help', :!out, :!err).so } }
}
# download
multi sub run-powershell-webrequest($uri, |c) {
my $ps-command = qq|Invoke-WebRequest -UserAgent "{$*ZEF-USERAGENT}" -UseBasicParsing -URI "$uri"|;
shell("cmd /c powershell -executionpolicy bypass -command \$progressPreference='silentlyContinue'; ({$ps-command}).Content", |c, :out, :err)
}
## [curl]
our proto sub run-curl($uri, |c) is export {
{*} if once { try { run('curl', '--help', :!out, :!err).so } }
}
# download
multi sub run-curl($uri, |c) {
run('curl', '-A', $*ZEF-USERAGENT, '--max-time', 60, '-s', '-L', $uri, |c, :out, :err);
}
## [wget]
our proto sub run-wget($uri, |c) is export {
{*} if once { try { run('wget', '--help', :!out, :!err).so } }
}
# download
multi sub run-wget($uri, |c) {
run('wget', qq|--user-agent="{$*ZEF-USERAGENT}"|, '--timeout=60', '-qO-', $uri, |c, :out, :err);
}
## [bsd/gnu tar]
our proto sub run-tar(|) is export {
{*} if once { try { run('tar', '--help', :!out, :!err).so } }
}
# list files
multi sub run-tar($archive-file, Bool :$list! where *.so, |c) {
run('tar', '--list', '-f', $archive-file, |c, :out, :err);
}
# extract file
multi sub run-tar($archive-file, $path-inside-archive, |c) {
run('tar', '--to-stdout', '-zxvf', $archive-file, $path-inside-archive, |c, :out, :err);
}
## [perl5 Archive::Tar script]
our proto sub run-perl5-tar(|) is export {
{*} if once { try { run('perl', '-MArchive::Tar', '-e0', :!out, :!err).so } }
}
# list files
multi sub run-perl5-tar($archive-file, Bool :$list! where *.so, |c) {
my $inline-script = q|use Archive::Tar; my $tar = Archive::Tar->new(); $tar->read($ARGV[0]); say $_ for( $tar->list_files() );|;
run('perl', '-E', $inline-script, $archive-file, |c, :out, :err);
}
# extract file
multi sub run-perl5-tar($archive-file, $path-inside-archive, |c) {
my $inline-script = q|use Archive::Tar; my $tar = Archive::Tar->new(); $tar->read($ARGV[0]); print $tar->get_content($ARGV[1]);|;
run('perl', '-E', $inline-script, $archive-file, $path-inside-archive, :out, :err, |c);
}
## [unzip]
our proto sub run-unzip(|) is export {
{*} if once { try { run('unzip', '--help', :!out, :!err).so } }
}
# list files
multi sub run-unzip($archive-file, Bool :$list! where *.so, |c) {
run('unzip', '-Z', '-1', $archive-file, |c, :out, :err);
}
# extract file
multi sub run-unzip($archive-file, $path-inside-archive, |c) {
run('unzip', '-p', '-o', '-qq', $archive-file, $path-inside-archive, |c, :out, :err);
}
## [unzip via powershell] # XXX: Doesn't work
our proto sub run-powershell-unzip(|) is export {
{*} if once { try { run('powershell', '-help', :!out, :!err).so } }
}
# list files
multi sub run-powershell-unzip($archive-file, Bool :$list! where *.so, |c) {
my $inline-script = join ' ', lines q:to/END/;
function List-ZipFiles {
$ns = $shell.NameSpace($args[0]);
foreach( $item in $ns.Items() ) {
if( $item.IsFolder ) {
List-ZipFiles($item);
} else {
$path = $item | Select -ExpandProperty Path;
Write-Output($path);
}
}
}
$items = $zip.items();
$path = $items | Select -ExpandProperty Path;
Write-Output $path;
List-ZipFiles $path;
END
#:
shell(qq|cmd /c powershell -executionpolicy bypass -command "&\{$inline-script\}"|, |c, :out, :err)
}
# extract file
multi sub run-powershell-unzip($archive-file, $path-inside-archive, |c) {
my $inline-script = join ' ', lines q:to/END/;
param ([string]$file = $(throw "-file is required."));
$data = (Get-content $file -encoding byte);
$ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo;
$ProcessInfo.FileName = $file;
$ProcessInfo.RedirectStandardInput = $true;
$ProcessInfo.RedirectStandardOutput = $true;
$ProcessInfo.UseShellExecute = $false;
$Proc = New-Object System.Diagnostics.Process;
$Proc.StartInfo = $ProcessInfo;
$Proc.Start() | Out-Null;
$Proc.WaitForExit();
$Proc.StandardOutput.ReadToEnd();
END
#:
shell(qq|cmd /c powershell -executionpolicy bypass -command "$inline-script" -f "$archive-file"|, |c, :out, :err)
}
#my sub proc-to-json($proc) { Rakudo::Internals::JSON.from-json($proc.out.slurp) }
#my $url = 'https://httpbin.org/user-agent';
#use Test;
#
#if $*DISTRO.is-win {
# ok proc-to-json( run-powershell-webrequest($url) )<user-agent> ~~ /:i rakudo/;
#}
#else {
# # windows powershell aliases these commands to cmdlets that don't take standard paramters
# ok proc-to-json( run-curl($url) )<user-agent> ~~ /:i rakudo/;
# ok proc-to-json( run-wget($url) )<user-agent> ~~ /:i rakudo/;
#}
#
#done-testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment