Alien-xmake

 view release on metacpan or  search on metacpan

builder/xmake.pm  view on Meta::CPAN

package builder::xmake {
    use strict;
    use warnings;
    use parent 'Module::Build';
    use HTTP::Tiny            qw[];
    use File::Spec            qw[];
    use File::Basename        qw[];
    use Env                   qw[@PATH];              # Windows
    use File::Temp            qw[tempdir tempfile];
    use File::Spec::Functions qw[rel2abs];
    use File::Which           qw[which];
    use Archive::Tar          qw[];

    #~ use File::ShareDir qw[];
    #
    my $version = '2.8.8';    # Target install version

    # If false, a complete archive is downloaded (quickly) via http
    #~ my $installer_sh = 'https://xmake.io/shget.text';
    my $installer_exe
        = "https://github.com/xmake-io/xmake/releases/download/v${version}/xmake-v${version}.win64.exe";
    my $installer_tar
        = "https://github.com/xmake-io/xmake/releases/download/v${version}/xmake-v${version}.tar.gz";
    my $share = rel2abs 'share';
    #
    sub http {
        CORE::state $http //= HTTP::Tiny->new(
            agent => 'Alien::xmake/' .
                shift->dist_version() . '; '    # space at the end asks HT to appended default UA
        );
        $http;
    }
    #
    sub locate_xmake {
        my ($s) = @_;
        my $path = which('xmake');
        $path ? rel2abs($path) : ();
    }
    sub install_with_exe { my ($s) = @_; }
    sub install_via_bash { my ($s) = @_; }

    #~ use File::ShareDir::Install;
    #~ warn File::ShareDir::Install::install_share( module => 'Alien::xmake');
    #~ warn File::Spec->rel2abs(
    #~ File::Basename::dirname(__FILE__), 'share'
    #~ );
    sub download {
        my ( $s, $url, $path ) = @_;
        my $local    = File::Spec->rel2abs( File::Spec->catfile( $s->cwd, $path ) );
        my $response = $s->http->mirror( $url, $local );
        if ( $response->{success} ) {
            $s->log_debug( 'Install executable mirrored at ' . $local );
            $s->make_executable($local);    # get it ready to run
            return $local;
        }
        $s->log_debug( 'Status: [' . $response->{status} . '] ' . $response->{content} );
        $s->log_warn( 'Failed to download ' . $response->{url} );
        return ();
    }

    #~ sub download_shget {
    #~ my ($s)      = @_;
    #~ my $local    = File::Spec->rel2abs( File::Spec->catfile( $s->cwd, 'xmake_installer.sh' ) );
    #~ my $response = $s->http->mirror( $installer_sh, $local );
    #~ if ( $response->{success} ) {
    #~ $s->log_debug( 'Install script mirrored at ' . $local );
    #~ $s->make_executable($local);    # get it ready to run
    #~ return $local;
    #~ }
    #~ $s->log_debug( 'Status: [' . $response->{status} . '] ' . $response->{content} );
    #~ $s->log_warn( 'Failed to download installer script from ' . $response->{url} );
    #~ exit 1;
    #~ }
    sub gather_info {
        my ( $s, $xmake ) = @_;
        $s->config_data( xmake_exe => $xmake );
        $s->config_data( xmake_dir => File::Basename::dirname($xmake) );
        my $run = `$xmake --version`;
        my ($ver) = $run =~ m[xmake (v.+?), A cross-platform build utility based on Lua];
        $s->config_data( xmake_ver       => $ver );
        $s->config_data( xmake_installed => 1 );
    }

    sub slurp {
        my ( $s, $file ) = @_;
        open my $fh, '<', $file or die;
        local $/ = undef;
        my $cont = <$fh>;
        close $fh;
        return $cont;
    }

    # Module::Build subclass
    sub ACTION_xmake_install {
        my ($s) = @_;

        #~ ddx $s->config_data;
        return 1 if $s->config_data('xmake_install');
        #
        my $os = $s->os_type;    # based on Perl::OSType
        if ( !defined $os ) {
            $s->log_warn(
                q[Whoa. Perl has no idea what this OS is so... let's try installing with a shell script and hope for the best!]
            );
            exit 1;
        }
        elsif ( $os eq 'Windows' ) {
            $s->config_data( xmake_type => 'share' );
            my $installer = $s->download( $installer_exe, 'xmake_installer.exe' );
            my $dest      = File::Spec->rel2abs(
                File::Spec->catdir( $s->base_dir, @{ $s->share_dir->{dist} } ) );
            $s->log_info(qq[Running installer [$installer]...\n]);
            $s->do_system( $installer, '/NOADMIN', '/S', '/D=' . $dest );
            $s->log_info(qq[Installed to $dest\n]);
            push @PATH, $dest;
            my $xmake = $s->locate_xmake();
            $s->config_data( xmake_type => 'share' );
            $s->gather_info($xmake);
            $s->config_data( xmake_install => $dest );

# D:\a\_temp\1aa1c77c-ff7b-41bc-8899-98e4cd421618.exe /NOADMIN /S /D=C:\Users\RUNNER~1\AppData\Local\Temp\xmake-15e5f277191e8a088998d0f797dd1f44b5491e17
#~ $s->warn_info('Windows is on the todo list');
#~ exit 1;
        }
        else {
            unshift @PATH, 'share/bin';
            my $xmake = $s->locate_xmake();
            if ($xmake) {
                $s->config_data( xmake_type => 'system' );
            }



( run in 1.839 second using v1.01-cache-2.11-cpan-02777c243ea )