From c7395cf22e167bc14f6be18a49be4d3250711636 Mon Sep 17 00:00:00 2001 From: mah0x211 Date: Fri, 5 Apr 2024 14:47:48 +0900 Subject: [PATCH] Remove the implementation in perl --- lenv | 753 ----------------------------------------------------------- 1 file changed, 753 deletions(-) delete mode 100644 lenv diff --git a/lenv b/lenv deleted file mode 100644 index e2579a4..0000000 --- a/lenv +++ /dev/null @@ -1,753 +0,0 @@ -#$ENVBIN -# -# Copyright (C) 2015 Masatoshi Teruya -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -# THE SOFTWARE. -# - -use 5.008; - -use strict; -use warnings; - -binmode STDOUT, ":encoding(UTF-8)"; -use open ":encoding(utf8)"; - -my $VERSION = '0.1.0'; - -# -# directories -# -my $LENV_DIR = "$ENV{HOME}/.lenv"; -my $BIN_DIR = "$LENV_DIR/bin"; -my $SRC_DIR = "$LENV_DIR/src"; -my $TMP_DIR = "$LENV_DIR/tmp"; -my $LUA_DIR = "$LENV_DIR/lua"; -my $LUAJIT_DIR = "$LENV_DIR/luajit"; -my $CURRENT_DIR = "$LENV_DIR/current"; - - -# -# version files for lua, luajit, luarocks -# -my $LUA_VERS_TXT = "$LENV_DIR/lua_vers.txt"; -my $LUAJIT_VERS_TXT = "$LENV_DIR/luajit_vers.txt"; -my $ROCKS_VERS_TXT = "$LENV_DIR/rocks_vers.txt"; - - -# -# lenv path -# -my $LENV="$BIN_DIR/lenv"; - - -# -# required commands -# -my $CURL = `which curl` or die "command curl not found"; -chomp($CURL); - -my $TAR = `which tar` or die "command tar not found"; -chomp($TAR); - -my $OPENSSL = `which openssl` or die "command openssl not found"; -chomp($OPENSSL); - - -# -# helper functions -# -sub shcall -{ - foreach( @_ ){ - die $! if system( "sh -x -e -c '$_'" ); - } -} - - -# -# use -# -sub cmdUse -{ - my ( $dir, $ver ) = @_; - - die "invalid version number $ver" if $ver !~ /\d+(.\d+)*$/; - die "version $ver not installed" if !-d $dir; - chdir $LENV_DIR or die $!; - - # remove current symbolic link - if( -l $CURRENT_DIR ){ - unlink $CURRENT_DIR or die "failed to unlink $!"; - } - - # create new symbolic link - symlink $dir, $CURRENT_DIR or die "failed to symlink: $!"; -} - - -sub cmdUseLua -{ - my $ver = shift @ARGV; - cmdUse( "$LUA_DIR/$ver", $ver ); -} - - -sub cmdUseLuaJIT -{ - my $ver = shift @ARGV; - cmdUse( "$LUAJIT_DIR/$ver", $ver ); -} - - -# -# install -# -sub extract -{ - my $src = shift; - my $dir; - my @list; - my $dh; - - shcall( "mkdir -p $TMP_DIR" ); - - # cleanup dir - opendir( $dh, $TMP_DIR ) or die $!; - @list = grep( /[^.]/, readdir( $dh ) ); - foreach( @list ){ - shcall( "rm -rf $TMP_DIR/$_" ); - } - closedir( $dh ); - - # extract - shcall( "$TAR xzf $src -C $TMP_DIR" ); - - # find dir - opendir( $dh, $TMP_DIR ) or die $!; - @list = grep( /[^.]/, readdir( $dh ) ); - foreach( @list ){ - $dir = "$TMP_DIR/$_"; - last; - } - closedir( $dh ); - - return $dir; -} - - -sub download -{ - my ( $ver, $url, $file, $checksum ) = @_; - my $src = "$SRC_DIR/$file"; - my $sum; - - # download if not exists - if( !-f $src ){ - shcall( "$CURL -o $src -L $url" ); - } - - # verify checksum - if( $checksum ){ - $sum = `$OPENSSL md5 $src`; - chomp( $sum ); - die "checksum of $file does not match $checksum" if $sum !~ /$checksum/; - } - - return $src; -} - - -sub getVerLine -{ - my ( $txt, $ver ) = @_; - my @arr; - - chdir $SRC_DIR or die "failed to chdir $SRC_DIR - $!"; - - # parse version files - open( my $fh, $txt ) or die "could not open $txt: $!"; - while( <$fh> ) - { - @arr = split( /\s/, $_ ); - if( $ver eq 'latest' or $arr[0] eq $ver ){ - last; - } - undef @arr; - } - close( $fh ); - - return @arr; -} - - -# -# compile and install LuaRocks -# -sub installRocks -{ - my $instpath = shift; - my @line = getVerLine( $ROCKS_VERS_TXT, 'latest' ) or die "version 'latest' not found"; - my $dest = extract( download( @line ) ); - my $installer = "$TMP_DIR/install.sh"; - my $args = join( ' ', @ARGV ); - my $regex = "${instpath}/(share|lib)/([^?]+)/"; - my ( $fh, $lpath ); - - # parse version files - open( $fh, '>', $installer ) or die "could not open $installer: $!"; - - print $fh <<_EOS_; -# -# compile and install -# - -set -x -set -e - -rm -f ${installer} -cd ${dest} - -./configure --prefix=${instpath} \\ - --sysconfdir=${instpath}/luarocks \\ - --with-lua=${instpath} \\ - --with-lua-bin=${instpath}/bin \\ - --with-lua-include=${instpath}/include \\ - --with-lua-lib=${instpath}/lib - -make build -make bootstrap - -rm -rf ${dest} - -_EOS_ - - close( $fh ); - shcall("sh $installer"); - - # create symbolic link - chdir $instpath or die $!; - $lpath = `bin/luarocks path` or die $!; - - foreach( split( /\n/, $lpath ) ) - { - if( $_ =~ /LUA_C?PATH='(.+)'/ ) - { - my @arr = split( /;/, $1 ); - - foreach( @arr ) - { - if( $_ =~ $regex ){ - my $dir = "${instpath}/$1/$2"; - my $dst = "${instpath}/luarocks/$1"; - - print "ln -s $dir, $dst\n"; - if( -e $dst ){ - unlink $dst or die $!; - } - symlink $dir, $dst or die $!; - } - } - } - } -} - - -sub cmdInstall -{ - my ( $txt, $ver, $instpath, $prefix, $postflight ) = @_; - my @line = getVerLine( $txt, $ver ) or die "version $ver not found"; - my $dest = extract( download( @line ) ); - my $installer = "$TMP_DIR/install.sh"; - my $args = join( ' ', @ARGV ); - my $fh; - - # parse version files - open( $fh, '>', $installer ) or die "could not open $installer: $!"; - - print $fh <<_EOS_; -# -# compile and install -# - -set -x -set -e - -rm -f ${installer} -cd ${dest} -make ${args} -rm -rf ${instpath} -make install ${prefix} -rm -rf ${dest} - -${postflight} - -_EOS_ - - close( $fh ); - shcall("sh $installer"); - - installRocks( $instpath ); -} - - -sub getPlatform -{ - # aix ansi bsd generic linux macosx mingw posix solaris - my $os = `uname` or die $!; - - $os = lc $os; - return 'aix' if $os =~ /^aix/; - return 'bsd' if $os =~ /^\wbsd/; - return 'linux' if $os =~ /^linux/; - return 'macosx' if $os =~ /^darwin/; - return 'solaris' if $os =~ /^sunos/; - - return 'UNKNOWN_PLATFORM'; -} - - - -sub cmdInstallLua -{ - my $ver = shift @ARGV; - my $instpath = "$LUA_DIR/$ver"; - - unshift( @ARGV, getPlatform() ) if !@ARGV; - cmdInstall( $LUA_VERS_TXT, $ver, $instpath, "INSTALL_TOP=$instpath" ); -} - - -sub cmdInstallLuaJIT -{ - my $ver = shift @ARGV; - my $instpath = "$LUAJIT_DIR/$ver"; - my $postflight = <<_EOS_; - -# -# postflight script for luajit -# - -# create symbolic link if there is luajit -# link: ${instpath}/bin/luajit -> ${instpath}/bin/lua -cd "${instpath}/bin" -ln -s luajit lua - -# link: ${instpath}/include/\${header} -> ${instpath}/include/\${header##*/} -cd '../include' -for header in \$(ls -A */*); do - ln -s \${header} -done - -cd '../lib' -for lib in \$(ls -A liblua*); do - if ! [ -L \${lib} ] && [ -f \${lib} ]; then - # link: ${instpath}/lib/\${lib} -> ${instpath}/lib/liblua.\${lib##*.} - ln -s \${lib} "liblua.\${lib##*.}" - fi -done - -# link: ${instpath}/share/luajit* -> ${instpath}/share/lua -cd '../share' -ln -s \$PWD/luajit-*/* lua/*/ - -_EOS_ - - cmdInstall( $LUAJIT_VERS_TXT, $ver, $instpath, "PREFIX=$instpath", $postflight ); -} - - -# -# uninstall -# -sub cmdUninstall -{ - my ( $dir, $ver ) = @_; - - die "invalid version number $ver" if $ver !~ /\d+(.\d+)*$/; - die "version $ver does not installed\n" if !-d $dir; - - print "uninstall $dir\n"; - shcall( "rm -rf $dir" ); -} - - -sub cmdUninstallLua -{ - my $ver = shift @ARGV; - cmdUninstall( "$LUA_DIR/$ver", $ver ); -} - - -sub cmdUninstallLuaJIT -{ - my $ver = shift @ARGV; - cmdUninstall( "$LUAJIT_DIR/$ver", $ver ); -} - - -# -# list LUA_DIR contents -# -sub cmdList -{ - my @dirs = ( - { name => 'lua', path => "$LUA_DIR" }, - { name => 'luajit', path => "$LUAJIT_DIR" } - ); - - foreach my $dir ( @dirs ) - { - if( -d $dir->{path} ) - { - print "$dir->{name}: $dir->{path}\n"; - - if( opendir( my $dh, $dir->{path} ) ) - { - my @list = grep( /[^.]/, readdir( $dh ) ); - - closedir( $dh ); - - foreach( @list ){ - print "$_\n"; - } - } - else { - print "could not open directory: $dir->{path}\n" - } - print "\n"; - } - } -} - - -# -# list available versions -# -sub cmdVers -{ - my @srcs = ( - { name => 'lua', file => $LUA_VERS_TXT }, - { name => 'luajit', file => $LUAJIT_VERS_TXT }, - { name => 'luarocks', file => $ROCKS_VERS_TXT } - ); - - foreach my $list ( @srcs ) - { - print "$list->{name}\nversion\tsource\n"; - - if( -f $list->{file} ) - { - if( open( my $fh, $list->{file} ) ) - { - while( <$fh> ){ - my @col = split( /\s/, $_ ); - print "$col[0]\t$col[1]\n"; - } - close($fh); - } - else { - print "cannot open $list->{file} : $!\n"; - } - } - else { - print "$list->{file} not found\n"; - } - print "\n"; - } -} - - - -# -# fetch remote versions -# -sub parseRocksVers -{ - local $/ = undef; - my ( $file, $fh ) = @_; - my $html = <$fh>; - my @arr = (); - - # print and save versions - while( $html =~ /href="(luarocks-((\d+\.\d+)(?:\.\d+)*)\.tar\.gz)"/sig ) - { - # version 2.2 or later - if( $3 >= 2.2 ){ - print "$2\t$1\n"; - push @arr, "$2 http://luarocks.github.io/luarocks/releases/$1 $1"; - } - } - @arr = reverse( sort @arr ); - print $file join( "\n", @arr ); -} - - -sub parseLuaJITVers -{ - local $/ = undef; - my ( $file, $fh ) = @_; - my $html = <$fh>; - my @arr = (); - - # print and save versions - while( $html =~ /^([a-f0-9]+)\s+(luajit-((\d+)(?:\.\d+)*)\.tar\.gz)/mig ) - { - # versioin 2 or later - if( $4 >= 2 ){ - print "$3\t$2 $1\n"; - push @arr, "$3 https://luajit.org/download/$2 $2 $1"; - - } - } - @arr = reverse( sort @arr ); - print $file join( "\n", @arr ); -} - - -sub parseLuaVers -{ - local $/ = undef; - my ( $file, $fh ) = @_; - my $html = <$fh>; - my @arr = (); - - # print and save versions - while( $html =~ /href="(lua-((\d+\.\d+)(?:\.\d+)*)\.tar\.gz)".+?md5:\s*([a-f0-9]+)/sig ) - { - # versioin 5.1 or later - if( $3 >= 5.1 ){ - print "$2\t$1 $4\n"; - push @arr, "$2 http://www.lua.org/ftp/$1 $1 $4"; - } - } - @arr = reverse( sort @arr ); - print $file join("\n", @arr ); -} - - - -# -# setup -# -sub cmdUpdate -{ - # create required directories - my @arr = ( - $LENV_DIR, $BIN_DIR, $SRC_DIR, $LUA_DIR, $LUAJIT_DIR, $TMP_DIR - ); - my $envbin = `which env`; - - foreach( @arr ){ - mkdir $_ if !-d $_; - } - - # fetch lenv - shcall( "$CURL -L --compressed 'http://git.io/lenv' > ${LENV}" ); - shcall( "chmod +x ${LENV}" ); - - # replace $ENVBIN string to shebang - chomp( $envbin ); - die $! if system( "sed -i -e 's|^#\$ENVBIN|#!${envbin} perl|' ${LENV}" ); - - shcall( "${LENV} path > ~/.lenvrc" ); -} - - -sub cmdFetch -{ - my @targets = ( - { - name => 'lua', - file => $LUA_VERS_TXT, - url => 'http://www.lua.org/ftp/', - parse => \&parseLuaVers - }, - { - name => 'luajit', - file => $LUAJIT_VERS_TXT, - url => 'https://luajit.org/download.html', - parse => \&parseLuaJITVers - }, - { - name => 'luarocks', - file => $ROCKS_VERS_TXT, - url => 'http://luarocks.github.io/luarocks/releases/', - parse => \&parseRocksVers - } - ); - my $tmp; - - foreach my $target ( @targets ) - { - # clear list files - if( -f $target->{file} ){ - print "remove $target->{file}\n"; - unlink $target->{file} or die $!; - } - - # fetch version list - print "download $target->{url}\n"; - $tmp = "$TMP_DIR/html"; - shcall( "$CURL -L --compressed $target->{url} > $tmp" ); - - # parse version files - open( my $fh, '>', $target->{file} ) or die "could not open $target->{file}: $!"; - open( my $fhtmp, $tmp ) or die "could not open $tmp: $!"; - $target->{parse}( $fh, $fhtmp ); - close( $fh ); - close( $fhtmp ); - unlink $tmp; - - print "\n"; - } - print "\n"; -} - - -# -# show paths -# -sub cmdPath -{ - print <<_EOS_ -# -# please add the following lenv settings to your environment -# -export PATH=${BIN_DIR}:${CURRENT_DIR}/bin:\$PATH -export LUA_PATH='${CURRENT_DIR}/luarocks/share/?.lua;${CURRENT_DIR}/luarocks/share/?/init.lua;;' -export LUA_CPATH='${CURRENT_DIR}/luarocks/lib/?.so;;' - -_EOS_ - -} - - -# -# print usage -# -sub cmdUsage() -{ - print <<_EOS_ -lenv ${VERSION} - -Usage: - lenv help Show this message - lenv path Show the configured paths - lenv update Update lenv - lenv fetch Fetch remote versions - lenv vers List available versions - lenv ls List installed versions - lenv install Download and install a of lua - with luarocks - lenv install-lj Download and install a of - luajit with luarocks - lenv uninstall Uninstall a of lua - lenv uninstall-lj Uninstall a of luajit - lenv use Use a of lua - lenv use-lj Use a of luajit - -_EOS_ - -} - - -# -# check command -# -sub CheckArgs -{ - my %CMDS = ( - help => { - run => \&cmdUsage - }, - path => { - run => \&cmdPath - }, - update => { - run => \&cmdUpdate - }, - fetch => { - run => \&cmdFetch - }, - vers => { - run => \&cmdVers - }, - ls => { - run => \&cmdList - }, - install => { - run => \&cmdInstallLua - }, - "install-lj" => { - run => \&cmdInstallLuaJIT - }, - uninstall => { - run => \&cmdUninstallLua - }, - "uninstall-lj" => { - run => \&cmdUninstallLuaJIT - }, - "use" => { - run => \&cmdUseLua - }, - "use-lj" => { - run => \&cmdUseLuaJIT - } - ); - my $arg = shift @ARGV; - my $cmd = $CMDS{$arg}; - - if( $cmd ){ - $cmd->{run}(); - } - else { - print "unknown command.\n"; - cmdUsage(); - exit(1); - } -} - - -if( !-f $LENV ) -{ - cmdUpdate; - - print <<_EOS_ -lenv has been installed! -please run the following command to enable the lenv; - - source ~/.lenvrc - -if you need to be enabled the lenv on login, please add the following script -into .bashrc or .bash_profile; - -# -# load ~/.lenvrc -# -if [ -f ~/.lenvrc ]; then - source ~/.lenvrc -fi - -_EOS_ - -} -else { - CheckArgs; -}