...
 
Commits (102)
......@@ -2,14 +2,14 @@ unit-test:
image: ubuntu:18.04
script:
- apt-get --quiet -qq update
- apt-get --quiet -qq install perl-modules
- apt-get --quiet -qq install perl-modules libmojolicious-perl libio-socket-ssl-perl libyaml-perl
- prove -I modules -r t/unit
smoke-test:
image: ubuntu:18.04
script:
- apt-get --quiet -qq update
- apt-get --quiet -qq install perl-modules git cmake g++ libio-socket-ssl-perl libyaml-perl
- apt-get --quiet -qq install perl-modules libmojolicious-perl libio-socket-ssl-perl libyaml-perl git cmake g++
- prove -I modules -r t/smoke
# Verifies that kdesrc-build itself can get to a basic install from a supported
......
......@@ -32,63 +32,7 @@ set(KDESRC_BUILD_MODULE_INSTALL_PREFIX "${KDE_INSTALL_DATADIR}/kdesrc-build/modu
if (KDESRC_BUILD_INSTALL_MODULES)
message(STATUS "Installing component modules to ${KDESRC_BUILD_MODULE_INSTALL_PREFIX}")
install(FILES
modules/ksb/Application.pm
modules/ksb/BuildContext.pm
modules/ksb/BuildException.pm
modules/ksb/BuildSystem.pm
modules/ksb/Debug.pm
modules/ksb/DependencyResolver.pm
modules/ksb/FirstRun.pm
modules/ksb/IPC.pm
modules/ksb/KDEProjectsReader.pm
modules/ksb/Module.pm
modules/ksb/ModuleResolver.pm
modules/ksb/ModuleSet.pm
modules/ksb/OptionsBase.pm
modules/ksb/OSSupport.pm
modules/ksb/PhaseList.pm
modules/ksb/RecursiveFH.pm
modules/ksb/StatusView.pm
modules/ksb/Updater.pm
modules/ksb/Util.pm
modules/ksb/Version.pm
modules/ksb/l10nSystem.pm
DESTINATION ${KDESRC_BUILD_MODULE_INSTALL_PREFIX}/ksb)
install(FILES
modules/ksb/BuildSystem/Autotools.pm
modules/ksb/BuildSystem/Meson.pm
modules/ksb/BuildSystem/CMakeBootstrap.pm
modules/ksb/BuildSystem/KDE4.pm
modules/ksb/BuildSystem/QMake.pm
modules/ksb/BuildSystem/Qt4.pm
modules/ksb/BuildSystem/Qt5.pm
DESTINATION ${KDESRC_BUILD_MODULE_INSTALL_PREFIX}/ksb/BuildSystem)
install(FILES
modules/ksb/Module/BranchGroupResolver.pm
DESTINATION ${KDESRC_BUILD_MODULE_INSTALL_PREFIX}/ksb/Module)
install(FILES
modules/ksb/ModuleSet/KDEProjects.pm
modules/ksb/ModuleSet/Null.pm
modules/ksb/ModuleSet/Qt.pm
DESTINATION ${KDESRC_BUILD_MODULE_INSTALL_PREFIX}/ksb/ModuleSet)
install(FILES
modules/ksb/IPC/Null.pm
modules/ksb/IPC/Pipe.pm
DESTINATION ${KDESRC_BUILD_MODULE_INSTALL_PREFIX}/ksb/IPC)
install(FILES
modules/ksb/Updater/Bzr.pm
modules/ksb/Updater/Git.pm
modules/ksb/Updater/KDEProject.pm
modules/ksb/Updater/KDEProjectMetadata.pm
modules/ksb/Updater/Svn.pm
modules/ksb/Updater/Qt5.pm
DESTINATION ${KDESRC_BUILD_MODULE_INSTALL_PREFIX}/ksb/Updater)
install(DIRECTORY modules/ DESTINATION ${KDESRC_BUILD_MODULE_INSTALL_PREFIX})
endif()
install(PROGRAMS ${CMAKE_CURRENT_SOURCE_DIR}/kdesrc-build DESTINATION ${KDE_INSTALL_BINDIR})
......
......@@ -9,6 +9,11 @@ doc/*.docbook - Licensed per KDE Documentation policy, i.e. under the
GNU FDL v1.2 (or any later version), with no Invariant
Sections, no Front-Cover Texts, and no Back-Cover Texts.
Full text available as doc/COPYING.DOC
modules/web/public/sakura.css - Licensed under MIT License as noted in file.
modules/Mojo* (files and directories)
- Licensed under Artistic License 2.0, see
https://github.com/mojolicious/mojo/raw/master/LICENSE
and modules/LICENSE.mojo
License follows -------------------------------------------------------------
......
#!/bin/sh
# This is a very simple script to build the documentation used on the
# kdesrc-build.kde.org website. The /common files themselves came from
# kdelibs/doc/common IIRC
# kdesrc-build.kde.org website. The /common files themselves are part
# of kdoctools.
#
# By Michael Pyne <mpyne@kde.org>
#
......@@ -26,21 +26,28 @@
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
PATH_TO_DOCS="../doc/index.docbook"
COMMON_DIR="kdoctools5-common"
COMMON_FILES=$(printf "'%s/'" "$COMMON_DIR") # The single quotes are exceedingly important
PATH_TO_DOCS="/kdesvn/src/extragear/utils/kdesrc-build/doc/index.docbook"
COMMON_FILES="'common/'" # The single quotes are exceedingly important
DATA_DIR=$(kf5-config --install data)
DATA_DIR=${DATA_DIR%/} # Remove trailing /
# The kdelibs install process installs support files for DocBook handling to
# the ksgmltools2 application data dir. A kdelibs checkout is not sufficient
# since some of the xsl files are input to a CMake script which converts them
# to final form.
DATA_DIR=$(kde4-config --install data)
DOC_SRCDIR="$DATA_DIR/ksgmltools2"
# The kdoctools install process installs support files for DocBook handling to
# the kf5/kdoctools application data dir. A kdoctools checkout (i.e. not
# installed already) is probably not sufficient but I haven't tested this
DOC_SRCDIR="$DATA_DIR/kf5/kdoctools"
XSLT="$DOC_SRCDIR/customization/kde-chunk-online.xsl"
# We need to run this from right directory, i.e. the directory we want generated
# output to go to (www/kdesrc-build/documentation)
meinproc4 --srcdir $DOC_SRCDIR \
# We need to run this from the directory we want generated output to go
if [ ! -e "$COMMON_DIR" ]; then
ln -s "$DATA_DIR/doc/HTML/en/$COMMON_DIR" .
fi
meinproc5 --srcdir "$DOC_SRCDIR" \
--param kde.common="$COMMON_FILES" \
--stylesheet $XSLT \
$PATH_TO_DOCS
--param chunker.output.encoding="UTF-8" \
--param chunker.output.indent="yes" \
--stylesheet "$XSLT" \
"$PATH_TO_DOCS"
This diff is collapsed.
......@@ -44,7 +44,13 @@
<arg rep="repeat"><replaceable>OPTIONS</replaceable></arg>
<arg rep="repeat"><replaceable>Module name | Module set name</replaceable></arg>
</cmdsynopsis>
</refsynopsisdiv>
<cmdsynopsis>
<command>kdesrc-build</command>
<arg choice='plain'>--query</arg>
<arg choice='req'><replaceable>VALUE TO QUERY</replaceable></arg>
<arg rep="repeat"><replaceable>Module name</replaceable></arg>
</cmdsynopsis>
<refsect1>
<title>DESCRIPTION</title>
......
......@@ -72,7 +72,7 @@ use lib "$modPath"; # Make ksb:: modules available
sub dumpError
{
my $err = $@;
my $err = shift;
open my $fh, '>>', "error-$$.log" or return;
my $time = localtime;
say $fh $time;
......@@ -92,7 +92,7 @@ eval {
};
if ($@) {
dumpError();
dumpError($@);
say STDERR <<DONE;
* Unable to even load the simplistic initial setup support for some reason??
......@@ -119,13 +119,20 @@ eval {
require ksb::Version;
require ksb::Application;
require ksb::BuildException;
require ksb::UserInterface::TTY;
require web::BackendServer;
require Mojo::IOLoop;
require Mojo::Server::Daemon;
};
if ($@) {
dumpError();
my $err = $@;
dumpError($err);
say STDERR <<DONE;
Couldn't load the base platform for kdesrc-build!
More detail might be available in error-$$.log
$err
DONE
if (! -e "kdesrc-buildrc" && ! -e "$ENV{HOME}/.kdesrc-buildrc") {
......@@ -144,6 +151,11 @@ ksb::Util->import();
ksb::BuildException->import();
ksb::Version->import(qw(scriptVersion));
ksb::Application->import();
ksb::UserInterface::TTY->import();
web::BackendServer->import();
Mojo::IOLoop->import();
Mojo::Server::Daemon->import();
# Make Perl 'plain die' exceptions use Carp::confess instead of their core
# support. This is not supported by the Perl 5 authors but assuming it works
......@@ -164,8 +176,6 @@ ksb::Version->path($RealBin);
# similar fashion to how the XFree/X.org lndir() program does it. This is
# reimplemented here since some systems lndir doesn't seem to work right.
#
# Used from ksb::l10nSystem
#
# As a special exception to the GNU GPL, you may use and redistribute this
# function however you would like (i.e. consider it public domain).
#
......@@ -291,7 +301,10 @@ sub findMissingModules
my @requiredModules = (
'HTTP::Tiny',
'IO::Socket::SSL',
[qw(JSON::XS JSON::PP)],
# Assume if Mojolicious::Lite is present, that whole framework is properly installed
'Mojolicious::Lite',
'Mojo::Promise', # That was a bad assumption
'Mojo::JSON',
[qw(YAML::XS YAML::PP YAML::Syck)]
);
my @missingModules;
......@@ -317,6 +330,34 @@ sub findMissingModules
return @missingModules;
}
# Rather than running an interactive build, launches a web server that can be
# interacted with by and outside user interface, printing the URL to the server
# on stdout and then remaining in the foreground until killed.
sub launchBackend
{
# Manually setup the daemon so that we can figure out what port it
# ends up on.
my $daemon = Mojo::Server::Daemon->new(
app => web::BackendServer->new,
listen => ['http://localhost'],
silent => 1,
);
$daemon->start; # Grabs the socket to listen on
my $port = $daemon->ports->[0] or do {
say STDERR "Can't autodetect which TCP port was assigned!";
exit 1;
};
say "http://localhost:$port";
Mojo::IOLoop->start
unless Mojo::IOLoop->is_running;
exit 0;
}
# Script starts.
# Ensure some critical Perl modules are available so that the user isn't surprised
......@@ -338,17 +379,10 @@ EOF
exit 1;
}
# Adding in a way to load all the functions without running the program to
# enable some kind of automated QA testing.
if (defined caller && caller eq 'test')
{
my $scriptVersion = scriptVersion();
say "kdesrc-build being run from testing framework, BRING IT.";
say "kdesrc-build is version $scriptVersion";
return 1;
}
# Drop here if we're in backend-only mode.
launchBackend()
if (scalar @ARGV == 1 && $ARGV[0] eq '--backend');
my $app;
our @atexit_subs;
END {
......@@ -358,39 +392,59 @@ END {
}
}
# TODO: Need to split up option reading here to exit early for some options
# (--help, --version, etc) and only continue on with creating a U/I if there's
# then still work to do. But this still needs to follow reading the rc-file!
# Until fixed this will fail t/install-and-run.t (which will handle --version in
# ui->start() and then as an atexit handler app->ksb->finish)
# Use some exception handling to avoid ucky error messages
eval
{
$app = ksb::Application->new(@ARGV);
my $app = web::BackendServer->new(@ARGV);
my $ui = ksb::UserInterface::TTY->new($app);
push @atexit_subs, sub { $app->finish(99) };
my $result = $app->runAllModulePhases();
push @atexit_subs, sub { $app->ksb->finish(99) };
# TODO: Reimplement --print-modules, --query modes, which wouldn't go through ->start
my $result = $ui->start();
@atexit_subs = (); # Clear exit handlers
$app->finish($result);
# env driver is just the ~/.config/kde-env-*.sh, session driver is that + ~/.xsession
my $ctx = $app->context;
if ($ctx->getOption('install-environment-driver') ||
$ctx->getOption('install-session-driver'))
{
ksb::Application::_installCustomSessionDriver($ctx);
}
# Exits the script
my $logdir = $app->context()->getLogDir();
note ("Your logs are saved in y[$logdir]");
exit $result;
};
if (my $err = $@)
{
if (had_an_exception()) {
print "kdesrc-build encountered an exceptional error condition:\n";
print " ========\n";
print " $err\n";
print " ========\n";
print "\tCan't continue, so stopping now.\n";
if ($err->{'exception_type'} eq 'Internal') {
print "\nPlease submit a bug against kdesrc-build on https://bugs.kde.org/\n"
}
say "kdesrc-build encountered an exceptional error condition:";
say " ========";
say " $err";
say " ========";
say "\tCan't continue, so stopping now.";
say "\nPlease submit a bug against kdesrc-build on https://bugs.kde.org/"
if ($err->{exception_type} eq 'Internal');
}
else {
# We encountered an error.
print "Encountered an error in the execution of the script.\n";
print "The error reported was $err\n";
print "Please submit a bug against kdesrc-build on https://bugs.kde.org/\n";
# An exception was raised, but not one that kdesrc-build generated
say "Encountered an error in the execution of the script.";
say "The error reported was $err";
say "Please submit a bug against kdesrc-build on https://bugs.kde.org/";
}
exit 99;
}
# vim: set et sw=4 ts=4 fdm=marker:
# vim: set et sw=4 ts=4:
This diff is collapsed.
The Artistic License 2.0
Copyright (c) 2000-2006, The Perl Foundation.
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
This license establishes the terms under which a given free software
Package may be copied, modified, distributed, and/or redistributed.
The intent is that the Copyright Holder maintains some artistic
control over the development of that Package while still keeping the
Package available as open source and free software.
You are always permitted to make arrangements wholly outside of this
license directly with the Copyright Holder of a given Package. If the
terms of this license do not permit the full use that you propose to
make of the Package, you should contact the Copyright Holder and seek
a different licensing arrangement.
Definitions
"Copyright Holder" means the individual(s) or organization(s)
named in the copyright notice for the entire Package.
"Contributor" means any party that has contributed code or other
material to the Package, in accordance with the Copyright Holder's
procedures.
"You" and "your" means any person who would like to copy,
distribute, or modify the Package.
"Package" means the collection of files distributed by the
Copyright Holder, and derivatives of that collection and/or of
those files. A given Package may consist of either the Standard
Version, or a Modified Version.
"Distribute" means providing a copy of the Package or making it
accessible to anyone else, or in the case of a company or
organization, to others outside of your company or organization.
"Distributor Fee" means any fee that you charge for Distributing
this Package or providing support for this Package to another
party. It does not mean licensing fees.
"Standard Version" refers to the Package if it has not been
modified, or has been modified only in ways explicitly requested
by the Copyright Holder.
"Modified Version" means the Package, if it has been changed, and
such changes were not explicitly requested by the Copyright
Holder.
"Original License" means this Artistic License as Distributed with
the Standard Version of the Package, in its current version or as
it may be modified by The Perl Foundation in the future.
"Source" form means the source code, documentation source, and
configuration files for the Package.
"Compiled" form means the compiled bytecode, object code, binary,
or any other form resulting from mechanical transformation or
translation of the Source form.
Permission for Use and Modification Without Distribution
(1) You are permitted to use the Standard Version and create and use
Modified Versions for any purpose without restriction, provided that
you do not Distribute the Modified Version.
Permissions for Redistribution of the Standard Version
(2) You may Distribute verbatim copies of the Source form of the
Standard Version of this Package in any medium without restriction,
either gratis or for a Distributor Fee, provided that you duplicate
all of the original copyright notices and associated disclaimers. At
your discretion, such verbatim copies may or may not include a
Compiled form of the Package.
(3) You may apply any bug fixes, portability changes, and other
modifications made available from the Copyright Holder. The resulting
Package will still be considered the Standard Version, and as such
will be subject to the Original License.
Distribution of Modified Versions of the Package as Source
(4) You may Distribute your Modified Version as Source (either gratis
or for a Distributor Fee, and with or without a Compiled form of the
Modified Version) provided that you clearly document how it differs
from the Standard Version, including, but not limited to, documenting
any non-standard features, executables, or modules, and provided that
you do at least ONE of the following:
(a) make the Modified Version available to the Copyright Holder
of the Standard Version, under the Original License, so that the
Copyright Holder may include your modifications in the Standard
Version.
(b) ensure that installation of your Modified Version does not
prevent the user installing or running the Standard Version. In
addition, the Modified Version must bear a name that is different
from the name of the Standard Version.
(c) allow anyone who receives a copy of the Modified Version to
make the Source form of the Modified Version available to others
under
(i) the Original License or
(ii) a license that permits the licensee to freely copy,
modify and redistribute the Modified Version using the same
licensing terms that apply to the copy that the licensee
received, and requires that the Source form of the Modified
Version, and of any works derived from it, be made freely
available in that license fees are prohibited but Distributor
Fees are allowed.
Distribution of Compiled Forms of the Standard Version
or Modified Versions without the Source
(5) You may Distribute Compiled forms of the Standard Version without
the Source, provided that you include complete instructions on how to
get the Source of the Standard Version. Such instructions must be
valid at the time of your distribution. If these instructions, at any
time while you are carrying out such distribution, become invalid, you
must provide new instructions on demand or cease further distribution.
If you provide valid instructions or cease distribution within thirty
days after you become aware that the instructions are invalid, then
you do not forfeit any of your rights under this license.
(6) You may Distribute a Modified Version in Compiled form without
the Source, provided that you comply with Section 4 with respect to
the Source of the Modified Version.
Aggregating or Linking the Package
(7) You may aggregate the Package (either the Standard Version or
Modified Version) with other packages and Distribute the resulting
aggregation provided that you do not charge a licensing fee for the
Package. Distributor Fees are permitted, and licensing fees for other
components in the aggregation are permitted. The terms of this license
apply to the use and Distribution of the Standard or Modified Versions
as included in the aggregation.
(8) You are permitted to link Modified and Standard Versions with
other works, to embed the Package in a larger work of your own, or to
build stand-alone binary or bytecode versions of applications that
include the Package, and Distribute the result without restriction,
provided the result does not expose a direct interface to the Package.
Items That are Not Considered Part of a Modified Version
(9) Works (including, but not limited to, modules and scripts) that
merely extend or make use of the Package, do not, by themselves, cause
the Package to be a Modified Version. In addition, such works are not
considered parts of the Package itself, and are not subject to the
terms of this license.
General Provisions
(10) Any use, modification, and distribution of the Standard or
Modified Versions is governed by this Artistic License. By using,
modifying or distributing the Package, you accept this license. Do not
use, modify, or distribute the Package, if you do not accept this
license.
(11) If your Modified Version has been derived from a Modified
Version made by someone other than you, you are nevertheless required
to ensure that your Modified Version complies with the requirements of
this license.
(12) This license does not grant you the right to use any trademark,
service mark, tradename, or logo of the Copyright Holder.
(13) This license includes the non-exclusive, worldwide,
free-of-charge patent license to make, have made, use, offer to sell,
sell, import and otherwise transfer the Package with respect to any
patent claims licensable by the Copyright Holder that are necessarily
infringed by the Package. If you institute patent litigation
(including a cross-claim or counterclaim) against any party alleging
that the Package constitutes direct or contributory patent
infringement, then this Artistic License to you shall terminate on the
date that such litigation is filed.
(14) Disclaimer of Warranty:
THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS
IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL
LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package Mojo;
use Mojo::Base -strict;
# "Professor: These old Doomsday devices are dangerously unstable. I'll rest
# easier not knowing where they are."
1;
=encoding utf8
=head1 NAME
Mojo - Web development toolkit
=head1 SYNOPSIS
# HTTP/WebSocket user agent
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
say $ua->get('www.mojolicious.org')->result->headers->server;
# HTML/XML DOM parser with CSS selectors
use Mojo::DOM;
my $dom = Mojo::DOM->new('<div><b>Hello Mojo!</b></div>');
say $dom->at('div > b')->text;
# Perl-ish templates
use Mojo::Template;
my $mt = Mojo::Template->new(vars => 1);
say $mt->render('Hello <%= $what %>!', {what => 'Mojo'});
# HTTP/WebSocket server
use Mojo::Server::Daemon;
my $daemon = Mojo::Server::Daemon->new(listen => ['http://*:8080']);
$daemon->unsubscribe('request')->on(request => sub {
my ($daemon, $tx) = @_;
$tx->res->code(200);
$tx->res->body('Hello Mojo!');
$tx->resume;
});
$daemon->run;
# Event loop
use Mojo::IOLoop;
for my $seconds (1 .. 5) {
Mojo::IOLoop->timer($seconds => sub { say $seconds });
}
Mojo::IOLoop->start;
=head1 DESCRIPTION
A powerful web development toolkit, with all the basic tools and helpers needed
to write simple web applications and higher level web frameworks, such as
L<Mojolicious>. Some of the most commonly used tools are L<Mojo::UserAgent>,
L<Mojo::DOM>, L<Mojo::JSON>, L<Mojo::Server::Daemon>, L<Mojo::Server::Prefork>,
L<Mojo::IOLoop> and L<Mojo::Template>.
See L<Mojolicious::Guides> for more!
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
=cut
package Mojo::Asset;
use Mojo::Base 'Mojo::EventEmitter';
use Carp 'croak';
has 'end_range';
has start_range => 0;
sub add_chunk { croak 'Method "add_chunk" not implemented by subclass' }
sub contains { croak 'Method "contains" not implemented by subclass' }
sub get_chunk { croak 'Method "get_chunk" not implemented by subclass' }
sub is_file {undef}
sub is_range { !!($_[0]->end_range || $_[0]->start_range) }
sub move_to { croak 'Method "move_to" not implemented by subclass' }
sub mtime { croak 'Method "mtime" not implemented by subclass' }
sub size { croak 'Method "size" not implemented by subclass' }
sub slurp { croak 'Method "slurp" not implemented by subclass' }
sub to_file { croak 'Method "to_file" not implemented by subclass' }
1;
=encoding utf8
=head1 NAME
Mojo::Asset - HTTP content storage base class
=head1 SYNOPSIS
package Mojo::Asset::MyAsset;
use Mojo::Base 'Mojo::Asset';
sub add_chunk {...}
sub contains {...}
sub get_chunk {...}
sub move_to {...}
sub mtime {...}
sub size {...}
sub slurp {...}
sub to_file {...}
=head1 DESCRIPTION
L<Mojo::Asset> is an abstract base class for HTTP content storage backends,
like L<Mojo::Asset::File> and L<Mojo::Asset::Memory>.
=head1 EVENTS
L<Mojo::Asset> inherits all events from L<Mojo::EventEmitter>.
=head1 ATTRIBUTES
L<Mojo::Asset> implements the following attributes.
=head2 end_range
my $end = $asset->end_range;
$asset = $asset->end_range(8);
Pretend file ends earlier.
=head2 start_range
my $start = $asset->start_range;
$asset = $asset->start_range(3);
Pretend file starts later.
=head1 METHODS
L<Mojo::Asset> inherits all methods from L<Mojo::EventEmitter> and implements
the following new ones.
=head2 add_chunk
$asset = $asset->add_chunk('foo bar baz');
Add chunk of data to asset. Meant to be overloaded in a subclass.
=head2 contains
my $position = $asset->contains('bar');
Check if asset contains a specific string. Meant to be overloaded in a
subclass.
=head2 get_chunk
my $bytes = $asset->get_chunk($offset);
my $bytes = $asset->get_chunk($offset, $max);
Get chunk of data starting from a specific position, defaults to a maximum
chunk size of C<131072> bytes (128KiB). Meant to be overloaded in a subclass.
=head2 is_file
my $bool = $asset->is_file;
False, this is not a L<Mojo::Asset::File> object.
=head2 is_range
my $bool = $asset->is_range;
Check if asset has a L</"start_range"> or L</"end_range">.
=head2 move_to
$asset = $asset->move_to('/home/sri/foo.txt');
Move asset data into a specific file. Meant to be overloaded in a subclass.
=head2 mtime
my $mtime = $asset->mtime;
Modification time of asset. Meant to be overloaded in a subclass.
=head2 size
my $size = $asset->size;
Size of asset data in bytes. Meant to be overloaded in a subclass.
=head2 slurp
my $bytes = $asset->slurp;
Read all asset data at once. Meant to be overloaded in a subclass.
=head2 to_file
my $file = $asset->to_file;
Convert asset to L<Mojo::Asset::File> object. Meant to be overloaded in a
subclass.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
=cut
package Mojo::Asset::File;
use Mojo::Base 'Mojo::Asset';
use Carp 'croak';
use Fcntl 'SEEK_SET';
use File::Spec::Functions ();
use Mojo::File 'tempfile';
has [qw(cleanup path)];
has handle => sub {
my $self = shift;
# Open existing file
my $path = $self->path;
return Mojo::File->new($path)->open('<') if defined $path && -e $path;
$self->cleanup(1) unless defined $self->cleanup;
# Create a specific file
return Mojo::File->new($path)->open('+>>') if defined $path;
# Create a temporary file
my $template = 'mojo.tmp.XXXXXXXXXXXXXXXX';
my $file = tempfile DIR => $self->tmpdir, TEMPLATE => $template, UNLINK => 0;
$self->path($file->to_string);
return $file->open('+>>');
};
has tmpdir => sub { $ENV{MOJO_TMPDIR} || File::Spec::Functions::tmpdir };
sub DESTROY {
my $self = shift;
return unless $self->cleanup && defined(my $path = $self->path);
if (my $handle = $self->handle) { close $handle }
# Only the process that created the file is allowed to remove it
Mojo::File->new($path)->remove if -w $path && ($self->{pid} // $$) == $$;
}
sub add_chunk {
my ($self, $chunk) = @_;
($self->handle->syswrite($chunk) // -1) == length $chunk
or croak "Can't write to asset: $!";
return $self;
}
sub contains {
my ($self, $str) = @_;
my $handle = $self->handle;
$handle->sysseek($self->start_range, SEEK_SET);
# Calculate window size
my $end = $self->end_range // $self->size;
my $len = length $str;
my $size = $len > 131072 ? $len : 131072;
$size = $end - $self->start_range if $size > $end - $self->start_range;
# Sliding window search
my $offset = 0;
my $start = $handle->sysread(my $window, $len);
while ($offset < $end) {
# Read as much as possible
my $diff = $end - ($start + $offset);
my $read = $handle->sysread(my $buffer, $diff < $size ? $diff : $size);
$window .= $buffer;
# Search window
my $pos = index $window, $str;
return $offset + $pos if $pos >= 0;
return -1 if $read == 0 || ($offset += $read) == $end;
# Resize window
substr $window, 0, $read, '';
}
return -1;
}
sub get_chunk {
my ($self, $offset, $max) = @_;
$max //= 131072;
$offset += $self->start_range;
my $handle = $self->handle;
$handle->sysseek($offset, SEEK_SET);
my $buffer;
if (defined(my $end = $self->end_range)) {
return '' if (my $chunk = $end + 1 - $offset) <= 0;
$handle->sysread($buffer, $chunk > $max ? $max : $chunk);
}
else { $handle->sysread($buffer, $max) }
return $buffer;
}
sub is_file {1}
sub move_to {
my ($self, $to) = @_;
# Windows requires that the handle is closed
close $self->handle;
delete $self->{handle};
# Move file and prevent clean up
Mojo::File->new($self->path)->move_to($to);
return $self->path($to)->cleanup(0);
}
sub mtime { (stat shift->handle)[9] }
sub new {
my $file = shift->SUPER::new(@_);
$file->{pid} = $$;
return $file;
}
sub size { -s shift->handle }
sub slurp {
my $handle = shift->handle;
$handle->sysseek(0, SEEK_SET);
my $ret = my $content = '';
while ($ret = $handle->sysread(my $buffer, 131072, 0)) { $content .= $buffer }
return defined $ret ? $content : croak "Can't read from asset: $!";
}
sub to_file {shift}
1;
=encoding utf8
=head1 NAME
Mojo::Asset::File - File storage for HTTP content
=head1 SYNOPSIS
use Mojo::Asset::File;
# Temporary file
my $file = Mojo::Asset::File->new;
$file->add_chunk('foo bar baz');
say 'File contains "bar"' if $file->contains('bar') >= 0;
say $file->slurp;
# Existing file
my $file = Mojo::Asset::File->new(path => '/home/sri/foo.txt');
$file->move_to('/yada.txt');
say $file->slurp;
=head1 DESCRIPTION
L<Mojo::Asset::File> is a file storage backend for HTTP content.
=head1 EVENTS
L<Mojo::Asset::File> inherits all events from L<Mojo::Asset>.
=head1 ATTRIBUTES
L<Mojo::Asset::File> inherits all attributes from L<Mojo::Asset> and implements
the following new ones.
=head2 cleanup
my $bool = $file->cleanup;
$file = $file->cleanup($bool);
Delete L</"path"> automatically once the file is not used anymore.
=head2 handle
my $handle = $file->handle;
$file = $file->handle(IO::File->new);
Filehandle, created on demand for L</"path">, which can be generated
automatically and safely based on L</"tmpdir">.
=head2 path
my $path = $file->path;
$file = $file->path('/home/sri/foo.txt');
File path used to create L</"handle">.
=head2 tmpdir
my $tmpdir = $file->tmpdir;
$file = $file->tmpdir('/tmp');
Temporary directory used to generate L</"path">, defaults to the value of the
C<MOJO_TMPDIR> environment variable or auto-detection.
=head1 METHODS
L<Mojo::Asset::File> inherits all methods from L<Mojo::Asset> and implements
the following new ones.
=head2 add_chunk
$file = $file->add_chunk('foo bar baz');
Add chunk of data.
=head2 contains
my $position = $file->contains('bar');
Check if asset contains a specific string.
=head2 get_chunk
my $bytes = $file->get_chunk($offset);
my $bytes = $file->get_chunk($offset, $max);
Get chunk of data starting from a specific position, defaults to a maximum
chunk size of C<131072> bytes (128KiB).
=head2 is_file
my $bool = $file->is_file;
True, this is a L<Mojo::Asset::File> object.
=head2 move_to
$file = $file->move_to('/home/sri/bar.txt');
Move asset data into a specific file and disable L</"cleanup">.
=head2 mtime
my $mtime = $file->mtime;
Modification time of asset.
=head2 new
my $file = Mojo::Asset::File->new;
my $file = Mojo::Asset::File->new(path => '/home/sri/test.txt');
my $file = Mojo::Asset::File->new({path => '/home/sri/test.txt'});
Construct a new L<Mojo::Asset::File> object.
=head2 size
my $size = $file->size;
Size of asset data in bytes.
=head2 slurp
my $bytes = $file->slurp;
Read all asset data at once.
=head2 to_file
$file = $file->to_file;
Does nothing but return the invocant, since we already have a
L<Mojo::Asset::File> object.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
=cut
package Mojo::Asset::Memory;
use Mojo::Base 'Mojo::Asset';
use Mojo::Asset::File;
use Mojo::File 'path';
has 'auto_upgrade';
has max_memory_size => sub { $ENV{MOJO_MAX_MEMORY_SIZE} || 262144 };
has mtime => sub {$^T};
sub add_chunk {
my ($self, $chunk) = @_;
# Upgrade if necessary
$self->{content} .= $chunk;
return $self if !$self->auto_upgrade || $self->size <= $self->max_memory_size;
$self->emit(upgrade => my $file = $self->to_file);
return $file;
}
sub contains {
my ($self, $str) = @_;
my $start = $self->start_range;
my $pos = index $self->{content} // '', $str, $start;
$pos -= $start if $start && $pos >= 0;
my $end = $self->end_range;
return $end && ($pos + length $str) >= $end ? -1 : $pos;
}
sub get_chunk {
my ($self, $offset, $max) = @_;
$max //= 131072;
$offset += $self->start_range;
if (my $end = $self->end_range) {
$max = $end + 1 - $offset if ($offset + $max) > $end;
}
return substr shift->{content} // '', $offset, $max;
}
sub move_to { path($_[1])->spurt($_[0]{content} // '') and return $_[0] }
sub size { length(shift->{content} // '') }
sub slurp { shift->{content} // '' }
sub to_file { Mojo::Asset::File->new->add_chunk(shift->slurp) }
1;
=encoding utf8
=head1 NAME
Mojo::Asset::Memory - In-memory storage for HTTP content
=head1 SYNOPSIS
use Mojo::Asset::Memory;
my $mem = Mojo::Asset::Memory->new;
$mem->add_chunk('foo bar baz');
say $mem->slurp;
=head1 DESCRIPTION
L<Mojo::Asset::Memory> is an in-memory storage backend for HTTP content.
=head1 EVENTS
L<Mojo::Asset::Memory> inherits all events from L<Mojo::Asset> and can emit the
following new ones.
=head2 upgrade
$mem->on(upgrade => sub {
my ($mem, $file) = @_;
...
});
Emitted when asset gets upgraded to a L<Mojo::Asset::File> object.
$mem->on(upgrade => sub {
my ($mem, $file) = @_;
$file->tmpdir('/tmp');
});
=head1 ATTRIBUTES
L<Mojo::Asset::Memory> inherits all attributes from L<Mojo::Asset> and
implements the following new ones.
=head2 auto_upgrade
my $bool = $mem->auto_upgrade;
$mem = $mem->auto_upgrade($bool);
Try to detect if content size exceeds L</"max_memory_size"> limit and
automatically upgrade to a L<Mojo::Asset::File> object.
=head2 max_memory_size
my $size = $mem->max_memory_size;
$mem = $mem->max_memory_size(1024);
Maximum size in bytes of data to keep in memory before automatically upgrading
to a L<Mojo::Asset::File> object, defaults to the value of the
C<MOJO_MAX_MEMORY_SIZE> environment variable or C<262144> (256KiB).
=head2 mtime
my $mtime = $mem->mtime;
$mem = $mem->mtime(1408567500);
Modification time of asset, defaults to the value of C<$^T>.
=head1 METHODS
L<Mojo::Asset::Memory> inherits all methods from L<Mojo::Asset> and implements
the following new ones.
=head2 add_chunk
$mem = $mem->add_chunk('foo bar baz');
my $file = $mem->add_chunk('abc' x 262144);
Add chunk of data and upgrade to L<Mojo::Asset::File> object if necessary.
=head2 contains
my $position = $mem->contains('bar');
Check if asset contains a specific string.
=head2 get_chunk
my $bytes = $mem->get_chunk($offset);
my $bytes = $mem->get_chunk($offset, $max);
Get chunk of data starting from a specific position, defaults to a maximum
chunk size of C<131072> bytes (128KiB).
=head2 move_to
$mem = $mem->move_to('/home/sri/foo.txt');
Move asset data into a specific file.
=head2 size
my $size = $mem->size;
Size of asset data in bytes.
=head2 slurp
my $bytes = mem->slurp;
Read all asset data at once.
=head2 to_file
my $file = $mem->to_file;
Convert asset to L<Mojo::Asset::File> object.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
=cut
This diff is collapsed.
package Mojo::ByteStream;
use Mojo::Base -strict;
use overload bool => sub {1}, '""' => sub { ${$_[0]} }, fallback => 1;
use Exporter 'import';
use Mojo::Collection;
use Mojo::Util;
our @EXPORT_OK = ('b');
# Turn most functions from Mojo::Util into methods
my @UTILS = (
qw(b64_decode b64_encode camelize decamelize gunzip gzip hmac_sha1_sum),
qw(html_unescape md5_bytes md5_sum punycode_decode punycode_encode quote),
qw(sha1_bytes sha1_sum slugify term_escape trim unindent unquote url_escape),
qw(url_unescape xml_escape xor_encode)
);
for my $name (@UTILS) {
my $sub = Mojo::Util->can($name);
Mojo::Util::monkey_patch __PACKAGE__, $name, sub {
my $self = shift;
$$self = $sub->($$self, @_);
return $self;
};
}
sub b { __PACKAGE__->new(@_) }
sub clone { $_[0]->new(${$_[0]}) }
sub decode { shift->_delegate(\&Mojo::Util::decode, @_) }
sub encode { shift->_delegate(\&Mojo::Util::encode, @_) }
sub new {
my $class = shift;
return bless \(my $dummy = join '', @_), ref $class || $class;
}
sub say {
my ($self, $handle) = @_;
$handle ||= \*STDOUT;
say $handle $$self;
return $self;
}
sub secure_compare { Mojo::Util::secure_compare ${shift()}, shift }
sub size { length ${$_[0]} }
sub split {
my ($self, $pat, $lim) = (shift, shift, shift // 0);
return Mojo::Collection->new(map { $self->new($_) } split $pat, $$self, $lim);
}
sub tap { shift->Mojo::Base::tap(@_) }
sub to_string { ${$_[0]} }
sub with_roles { shift->Mojo::Base::with_roles(@_) }
sub _delegate {
my ($self, $sub) = (shift, shift);
$$self = $sub->(shift || 'UTF-8', $$self);
return $self;
}
1;
=encoding utf8
=head1 NAME
Mojo::ByteStream - ByteStream
=head1 SYNOPSIS
use Mojo::ByteStream;
# Manipulate bytestream
my $stream = Mojo::ByteStream->new('foo_bar_baz');
say $stream->camelize;
# Chain methods
my $stream = Mojo::ByteStream->new('foo bar baz')->quote;
$stream = $stream->unquote->encode('UTF-8')->b64_encode('');
say "$stream";
# Use the alternative constructor
use Mojo::ByteStream 'b';
my $stream = b('foobarbaz')->b64_encode('')->say;
=head1 DESCRIPTION
L<Mojo::ByteStream> is a scalar-based container for bytestreams that provides a
more friendly API for many of the functions in L<Mojo::Util>.
# Access scalar directly to manipulate bytestream
my $stream = Mojo::ByteStream->new('foo');
$$stream .= 'bar';
=head1 FUNCTIONS
L<Mojo::ByteStream> implements the following functions, which can be imported
individually.
=head2 b
my $stream = b('test123');
Construct a new scalar-based L<Mojo::ByteStream> object.
=head1 METHODS
L<Mojo::ByteStream> implements the following methods.
=head2 b64_decode
$stream = $stream->b64_decode;
Base64 decode bytestream with L<Mojo::Util/"b64_decode">.
=head2 b64_encode
$stream = $stream->b64_encode;
$stream = $stream->b64_encode("\n");
Base64 encode bytestream with L<Mojo::Util/"b64_encode">.
# "Zm9vIGJhciBiYXo="
b('foo bar baz')->b64_encode('');
=head2 camelize
$stream = $stream->camelize;
Camelize bytestream with L<Mojo::Util/"camelize">.
=head2 clone
my $stream2 = $stream->clone;
Return a new L<Mojo::ByteStream> object cloned from this bytestream.
=head2 decamelize
$stream = $stream->decamelize;
Decamelize bytestream with L<Mojo::Util/"decamelize">.
=head2 decode
$stream = $stream->decode;
$stream = $stream->decode('iso-8859-1');
Decode bytestream with L<Mojo::Util/"decode">, defaults to using C<UTF-8>.
# "♥"
b('%E2%99%A5')->url_unescape->decode;
=head2 encode
$stream = $stream->encode;
$stream = $stream->encode('iso-8859-1');
Encode bytestream with L<Mojo::Util/"encode">, defaults to using C<UTF-8>.
# "%E2%99%A5"
b('♥')->encode->url_escape;
=head2 gunzip
$stream = $stream->gunzip;
Uncompress bytestream with L<Mojo::Util/"gunzip">.
=head2 gzip
stream = $stream->gzip;
Compress bytestream with L<Mojo::Util/"gzip">.
=head2 hmac_sha1_sum
$stream = $stream->hmac_sha1_sum('passw0rd');
Generate HMAC-SHA1 checksum for bytestream with L<Mojo::Util/"hmac_sha1_sum">.
# "7fbdc89263974a89210ea71f171c77d3f8c21471"
b('foo bar baz')->hmac_sha1_sum('secr3t');
=head2 html_unescape
$stream = $stream->html_unescape;
Unescape all HTML entities in bytestream with L<Mojo::Util/"html_unescape">.
# "%3Chtml%3E"
b('&lt;html&gt;')->html_unescape->url_escape;
=head2 md5_bytes
$stream = $stream->md5_bytes;
Generate binary MD5 checksum for bytestream with L<Mojo::Util/"md5_bytes">.
=head2 md5_sum
$stream = $stream->md5_sum;
Generate MD5 checksum for bytestream with L<Mojo::Util/"md5_sum">.
=head2 new
my $stream = Mojo::ByteStream->new('test123');
Construct a new scalar-based L<Mojo::ByteStream> object.
=head2 punycode_decode
$stream = $stream->punycode_decode;
Punycode decode bytestream with L<Mojo::Util/"punycode_decode">.
=head2 punycode_encode
$stream = $stream->punycode_encode;
Punycode encode bytestream with L<Mojo::Util/"punycode_encode">.
=head2 quote
$stream = $stream->quote;
Quote bytestream with L<Mojo::Util/"quote">.
=head2 say
$stream = $stream->say;
$stream = $stream->say(*STDERR);
Print bytestream to handle and append a newline, defaults to using C<STDOUT>.
=head2 secure_compare
my $bool = $stream->secure_compare($str);
Compare bytestream with L<Mojo::Util/"secure_compare">.