Difference between revisions of "My First Bazaar Package"

From EPrints Documentation
Jump to: navigation, search
m (links updated)
 
(27 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
=Introduction=
 
=Introduction=
In this tutorial we look at how to create a simple Bazaar Package (epm) ready for distribution via the EPrints Bazaar Store.
 
  
We are going to create this example simple and create a simple screen plug-in which will simply say hello to the user.  
+
In this tutorial you will create a "Hello, World" Screen and package it as a Bazaar Package (.epm).
  
It will read from a config file a variable which it will repeat back to the user.  
+
The page also references using a code versioning tool; [https://git-scm.com Git]. The goal is to have your EPM source files managed through Git and be able to edit them in a live repository.
  
=Remember the steps to create an epm=
+
Tracking files in Git is more complex but allows easier development. All of your package's files are placed under a directory tree then Sym-linked into the correct locations in EPrints. The [[API:tools/epm]] tool provides a ''link_lib'' option that makes this easier to achieve, as long as you follow the directory layout described below.
 +
As an alternative to using the web for building/customising your package you can try [https://github.com/eprintsug/gitaar Gitaar], a tool for building the epmi metadata file and EPM suitable for sharing.
  
* Make it work first locally
+
'''Warning!''' do not edit files and then enable/disable or install/uninstall an EPM. The EPM will detect the changed files and either create backups or refuse to work. You must edit the EPM and do ''Save and Return'' to re-scan the EPM's files (in Admin → EPrints Bazaar, Developer Tools).
* Package up a working version with a spec file and icon.
 
* Install from the "Local Packages" tab in the bazaar to test it.
 
  
=Exercise=
+
=Requirements=
  
By this point you should have an EPrints Bazaar capable version of EPrints installed and have a repository set up of which you are the admin user.  
+
You will need a working EPrints installation on which you have an administrator account. You will need to have access to the command line to create the files for the package.
  
You should also have a command line (or other method) for accessing and creating files within the repository directory.  
+
If using Git, you need to have it installed on your server.
  
In this demo we are assuming that your root directory is always the archive's home folder found at eprints/archives/archive_name (where archive_name is your archive ID)
 
  
==Step 1 - Decide a Package Name==
+
= Create a blank package=
  
I'm going to call my package eprints_test_package which I shall use throughout this exercise, let's hope its unique!
+
In ''Admin'' → ''System Tools'' → ''EPrints Bazaar'', select the ''Developer Tools'' tab.
  
To check it is unique, create a eprints_test_package.spec file with the following content:
+
At the bottom of this screen is a form to create a new EPM. Enter the name (without quotes) "hello_world" and click ''Create''.
  
  package: eprints_test_package
+
You can fill out the metadata if you wish but at this stage you are only required to have a ''version'', which defaults to "1.0.0".
  version: 0.0.1
 
  title: Davetaz's First Package
 
  description: I'll add one later
 
  creator: Dave Tarrant <davetaz@foo.bar.org>
 
  icon: icon.png
 
  
For more on the spec file specification see [[EPM_Specification]]
+
Click ''Save and Return'' to return to the EPrints Bazaar screen.
  
You will have to find a random small icon you can use as well, a package without one isn't accepted!
+
If you make a mistake you can click ''Edit'' on the ''Developer Tools'' tab to re-edit the package.
  
We then package up our spec file and icon only into the root level of a zip file in this case called eprints_test_package.epm
+
The GUI will create the lib/epm/hello_world directory tree for you.
  
Finally using the "Local Packages" tab in the Bazaar screen, upload your package. If you get a green tick back you have a unique name.
 
  
NOTE: This process does not secure you the package name! You can't camp on a name! You secure the name by uploading a working version of your package.
+
= Add blank EPM to Git =
  
==Step 2 - Develop your package==
+
This step is performed on your Eprints server.
  
In this example we shall make 3 files, a screen, a config file and a phrase file at the following locations:
+
Initialise and configure your git repository
 +
cd lib/epm/hello_world
 +
git init
 +
git add .
 +
git commit -a -m 'Blank EPM started via web'
  
* cfg/plugins/EPrints/Plugin/Screen/Admin/EPrintsTestScreen.pm
 
* cfg/cfg.d/eprints_test_package.pl
 
* cfg/lang/en/phrases/eprints_test_package.xml
 
  
In each of these files copy and paste the following:
+
= Develop your package contents=
  
===EPrintsTestScreen.pm===
+
You will need to be in the root directory of your EPrints installation (typically /opt/eprints3).
  
  package EPrints::Plugin::Screen::Admin::EPrintsTestScreen;
+
''gedit'' is the Gnome Text editor but you can use any editor that is capable of creating text files.
 +
 
 +
 
 +
== Add an Icon ==
 +
 
 +
Create an icon, e.g. using [http://bazaar.eprints.org/223/ Bazaar's icon builder].
 +
 
 +
OR
 +
 
 +
Download the hello_world icon from [https://bazaar.eprints.org/393/1/static/images/epm/Hello.png Bazaar].
 +
 
 +
Once you have this icon, name it hello_world.png and move it into lib/epm/hello_world/lib/static/images/epm/ directory.
 +
 
 +
==Hello.pm==
 +
 
 +
Create the directory to contain the ''Screen'' plugin you're going to package:
 +
 
 +
$ mkdir -p lib/plugins/EPrints/Plugin/Screen/
 +
 
 +
Create the screen plugin using your preferred text editor and add the sample content below:
 +
 
 +
$ gedit lib/plugins/EPrints/Plugin/Screen/Hello.pm
 +
 
 +
<source lang="perl">
 +
package EPrints::Plugin::Screen::Hello;
 +
 +
@ISA = ( 'EPrints::Plugin::Screen' );
 +
 +
use strict;
 +
# Make the plug-in
 +
sub new
 +
{
 +
    my( $class, %params ) = @_;
 +
 +
    my $self = $class->SUPER::new(%params);
 +
 +
    # Where the button to access the screen appears if anywhere, and what priority
 +
    $self->{appears} = [
 +
      {
 +
          place => "admin_actions",
 +
          position => 1247,
 +
      },
 +
    ];
 +
 +
    return $self;
 +
}
 +
 +
# Anyone can see this screen
 +
sub can_be_viewed { 1 }
 +
 +
# What to display
 +
sub render
 +
{
 +
    my( $self ) = @_;
 
    
 
    
  @ISA = ( 'EPrints::Plugin::Screen' );
+
    # Get the current repository object (so we can access the users, eprints information about things in this repository)
 
    
 
    
  use strict;
+
    my $repository = $self->{repository};
  # Make the plug-in
 
  sub new
 
  {
 
        my( $class, %params ) = @_;
 
 
    
 
    
        my $self = $class->SUPER::new(%params);
+
    # Create an XML element to return to our screen
 
    
 
    
        # Where the button to access the screen appears if anywhere, and what priority
+
    my $frag = $repository->xml->create_document_fragment();
        $self->{appears} = [
+
                {
+
    # Fill the fragment with stuff
                        place => "admin_actions",
 
                        position => 1247,
 
                },
 
        ];
 
 
    
 
    
        return $self;
+
    $frag->appendChild($repository->xml->create_text_node( "Hello, World!" ));
  }
 
 
    
 
    
  # Can anyone see this screen.
+
    return $frag;
  sub can_be_viewed
+
}
  {
+
        my( $plugin ) = @_;
+
1;
 
+
</source>
        return 1;
+
 
  }
+
==hello_world.pl==
 
+
 
  # What to display
+
Create the package directory that will contain the package's configuration file:
  sub render
+
 
  {
+
$ mkdir -p lib/epm/hello_world/cfg/cfg.d
        my( $self ) = @_;
+
 
 
+
Create a configuration file that enables the plugin - this file is copied into the repository when the package is enabled:
        my $repository = $self->{repository};
+
 
 
+
$ gedit lib/epm/hello_world/cfg/cfg.d/hello_world.pl
        my $ret = $repository->make_doc_fragment();
+
 
        my $br = $repository->make_element("br");
+
$c->{plugins}{"Screen::Hello"}{params}{disable} = 0;
 
 
        my $lang_phrase = $self->html_phrase("eprints_test_phrase");
 
 
 
        my $text_from_config = $repository->make_text($repository->{config}->{eprints_test_package}->{phrase});
 
 
 
        $ret->appendChild($lang_phrase);
 
        $ret->appendChild($br);
 
        $ret->appendChild($text_from_config);
 
 
 
        return $ret;
 
 
 
  }
 
 
 
  1;
 
  
===cfg/cfg.d/eprints_test_package.pl===
+
'''NOTE All plugins in lib/plugins are disabled by default (see EPrints::PluginFactory::new) UNLESS you explicitly set the disable property in the plugin (and it doesn't make sense to set disable = 0 in the plugin because that would make it visible to all of your repositories even if they hadn't explicitly enabled your bazaar package).
  
  $c->{eprints_test_package}->{phrase} = "This is a phrase, but it shouldn't be we are just testing. It could be a version number or some other variable.";
+
Tip: Any files you add below '''cfg/''' and are tracked by the EPM will be added to the repository when the EPM is enabled.
  
===cfg/lang/en/phrases/eprints_test_package.xml===
 
  
Finally we need some phrases. The title and description phrases are required if you are going to allow access to the screen from one of the admin menus.
+
== Commit changes ==
  
  <?xml version="1.0" encoding="utf-8" standalone="no"?>
+
After adding new files its important to record the changes
  <!DOCTYPE phrases SYSTEM "entities.dtd">
 
  <epp:phrases xmlns="http://www.w3.org/1999/xhtml" xmlns:epp="http://eprints.org/ep3/phrase" xmlns:epc="http://eprints.org/ep3/control">
 
 
 
    <epp:phrase id="Plugin/Screen/Admin/EPrintsTestScreen:title">Davetaz's Test Screen</epp:phrase>
 
    <epp:phrase id="Plugin/Screen/Admin/EPrintsTestScreen:description">I really need a description</epp:phrase>
 
 
 
    <epp:phrase id="Plugin/Screen/Admin/EPrintsTestScreen:eprints_test_phrase">This is our test phrase.</epp:phrase>
 
 
 
  </epp:phrases>
 
  
 +
Add all new files
 +
git add .
  
==Step 3 - Does it work locally==
+
Commit them with a message
  
You should now have the 3 files in place and if you reload your config (via the "Reload Configuration" button on the "Config. Tools" tab of the admin interface) you should then be able to go to the "Misc. Tools" tab and find your screen. Clicking on it should show our two phrases, one from the phrase file and one from the config listed on 2 different lines.
+
git commit -a -m "Added new Screen and its configuration. Also included new image"
  
==Step 3b - HELP - It didn't work==
 
  
If you made an error then the error message will likely be in the web servers error log.
+
= Build the Package=
  
Here is a very useful command for full debugging of EPrints and restarting the web server at the same time.
+
Re-Edit your EPM in the "Developer Tools" section of the Bazaar Store.  
  
In a new terminal as root, type and execute the following (on debian based systems):
+
At the bottom of the screen (the ''Files'' selector) you need to add the files you created above:
  
  apache2ctl restart && tail -f /var/apache2/logs/error_log
+
* epm/hello_world/cfg/cfg.d/hello_world.pl
 +
* plugins/EPrints/Plugin/Screen/Hello.pm
 +
* static/images/epm/hello_world.png
  
This will restart the web server and then give you a live view on the error log.
 
  
Attempting to reload your screen again (hit F5) will the cause the error to be displayed instantly in this terminal. It is recommended to leave this terminal open for debugging at all times.
+
= Testing in place =
  
==Step 4 - Package it up and test it==
+
Use the ''link_lib'' argument in [[API:tools/epm]] to sym-link the plugin into your live repository:
  
Now what we want to do is create a directory for our package (even better an svn or other change management system!) and move the files we just created out of the repository and into a directory which we can build out package from.  
+
./tools/epm link_lib hello_world
  
So in your home directory (not the eprints one) make a folder for your package and move the files into it including the spec file and icon.
+
If needed, also include configuration
 +
./tools/epm link_cfg REPOID hello_world
  
IMPORTANT: The directory structure needs to be preserved thus your directory listing should look as follows:
+
Followed by
 +
tools/epm enable REPOID hello_world
  
* icon.png
+
= Enable and test the package=
* eprints_test_package.spec
 
* cfg/plugins/EPrints/Plugin/Screen/Admin/EPrintsTestScreen.pm
 
* cfg/cfg.d/eprints_test_package.pl
 
* cfg/lang/en/phrases/eprints_test_package.xml
 
  
Before we package it up we want to update our spec file to point at the configuration file, so in the spec file add the line:
+
On the EPrints Bazaar ''Installed'' tab click ''Enable'' for the '''hello_world''' package. After a short time you should see a repository configuration reloaded message.
  
  configuration_file: cfg/cfg.d/eprints_test_package.pl
+
In ''Admin'' &rarr; ''System Tools'' &rarr; ''Misc. Tools'' you should now a button-link to your plugin (although with a missing phrase).
  
With this done, reload the config on your repository to make sure the screen has gone! You can even use view config to make sure all the related files have gone.  
+
You can now use ''Uninstall'' to completely remove the package and the source files created. A copy of the package will be saved in '''var/cache/epm/hello_world-1.0.0.epm'''.
  
Finally package up your directory using zip to make an epm called eprints_test_package.epm as before and upload this via the "Local Packages" tab, you may need to remove the old one first! Also did you version your package from the initial 0.1 marker in the spec file. (Get a script to grab the SVN version to go here when you build a propper package)
 
  
With all this done, if you have a green box, click the install button and see if it worked!
+
= Adding the missing phrases=
  
If it didn't you will need to remove it (if it installed) and use the steps outlined in 3b to debug why.  
+
As EPrints is designed to be multi-language, phrases should be used instead of embedded text.
  
==Step 4b - I'm stuck in limbo, package won't remove (a.k.a. using epm via the command line)==
+
In this section we add the phrase file to define a title and a description for our screen.
  
If you get stuck you can use the command line scripts which allow you to force changes.
+
Create the English language directory that will contain the package's English language phrase file:
  
You have to be aware that forcing changes means that any errors (prerm and dataset removals etc) will be ignored!
+
$ mkdir -p lib/lang/en/phrases/
  
The syntax is as follows (and needs to be executed as the eprints user at the root level of the eprints home directory)
+
Create a phrase file:
  
  perl bin/epm archive_name [install|remove] package_name [--force]
+
$ gedit lib/lang/en/phrases/hello_world.xml
  
This basically replicates the graphically functionality except the addition of the --force flag. Search functionality is still to be added... maybe.
+
<source lang="xml">
 +
  <?xml version="1.0" encoding="iso-8859-1" standalone="no" ?>
 +
  <!DOCTYPE phrases SYSTEM "entities.dtd">
 +
 
 +
  <epp:phrases xmlns="http://www.w3.org/1999/xhtml" xmlns:epp="http://eprints.org/ep3/phrase" xmlns:epc="http://eprints.org/ep3/control">
 +
 +
        <epp:phrase id="Plugin/Screen/Hello:title">Hello</epp:phrase>
 +
        <epp:phrase id="Plugin/Screen/Hello:description">My First Bazaar Package</epp:phrase>
 +
       
 +
        <epp:phrase id="Plugin/Screen/Hello:text">Hello World - This is the my first bazaar package calling.</epp:phrase>
 +
 +
  </epp:phrases>
 +
</source>
 +
 +
Don't worry too much about the wrapping here, the important thing is the phrases of which we have created 3.  
  
=Exercise 2=
+
Note that 2 of these (the top 2) will get used instantly by our package however the 3rd needs to be added to the render method of our screen.
  
That the easy exercise done, so how about doing something a little more challenging.
+
In order to do this edit the Hello.pm screen created in Step 1 and add the following in the appropriate place in the render method.
  
How about using our config file to specify an eprint_id or search expression and then using your screen to display something about this item/list.
+
  $frag->appendChild($repository->xml->create_element("br"));
 +
  $frag->appendChild($self->html_phrase("text"));
  
 
For some hints on loading and manipulating EPrints see [[Manipulating eprints in 3.2]].
 
For some hints on loading and manipulating EPrints see [[Manipulating eprints in 3.2]].
 +
 +
'''Don't Forget''' to add the phrase file to your bazaar package using the developer tools tab and commit them to git!
 +
 +
'''WARNING: if your package overrides any existing EPrints phrases then your overrides will be applied to ALL repositories even if they have not enabled your package. In this case you would add your override phrases to a separate file - eg. lib/epm/hello_world/cfg/lang/en/phrases/hello_world_overrides.xml - this file will then only be considered when a repository enables your package.
 +
 +
 +
 +
[[Category:EPrints_Bazaar]]

Latest revision as of 09:53, 6 August 2019

Introduction

In this tutorial you will create a "Hello, World" Screen and package it as a Bazaar Package (.epm).

The page also references using a code versioning tool; Git. The goal is to have your EPM source files managed through Git and be able to edit them in a live repository.

Tracking files in Git is more complex but allows easier development. All of your package's files are placed under a directory tree then Sym-linked into the correct locations in EPrints. The API:tools/epm tool provides a link_lib option that makes this easier to achieve, as long as you follow the directory layout described below. As an alternative to using the web for building/customising your package you can try Gitaar, a tool for building the epmi metadata file and EPM suitable for sharing.

Warning! do not edit files and then enable/disable or install/uninstall an EPM. The EPM will detect the changed files and either create backups or refuse to work. You must edit the EPM and do Save and Return to re-scan the EPM's files (in Admin → EPrints Bazaar, Developer Tools).

Requirements

You will need a working EPrints installation on which you have an administrator account. You will need to have access to the command line to create the files for the package.

If using Git, you need to have it installed on your server.


Create a blank package

In AdminSystem ToolsEPrints Bazaar, select the Developer Tools tab.

At the bottom of this screen is a form to create a new EPM. Enter the name (without quotes) "hello_world" and click Create.

You can fill out the metadata if you wish but at this stage you are only required to have a version, which defaults to "1.0.0".

Click Save and Return to return to the EPrints Bazaar screen.

If you make a mistake you can click Edit on the Developer Tools tab to re-edit the package.

The GUI will create the lib/epm/hello_world directory tree for you.


Add blank EPM to Git

This step is performed on your Eprints server.

Initialise and configure your git repository

cd lib/epm/hello_world
git init
git add .
git commit -a -m 'Blank EPM started via web'


Develop your package contents

You will need to be in the root directory of your EPrints installation (typically /opt/eprints3).

gedit is the Gnome Text editor but you can use any editor that is capable of creating text files.


Add an Icon

Create an icon, e.g. using Bazaar's icon builder.

OR

Download the hello_world icon from Bazaar.

Once you have this icon, name it hello_world.png and move it into lib/epm/hello_world/lib/static/images/epm/ directory.

Hello.pm

Create the directory to contain the Screen plugin you're going to package:

$ mkdir -p lib/plugins/EPrints/Plugin/Screen/

Create the screen plugin using your preferred text editor and add the sample content below:

$ gedit lib/plugins/EPrints/Plugin/Screen/Hello.pm
 package EPrints::Plugin::Screen::Hello;
 
 @ISA = ( 'EPrints::Plugin::Screen' );
 
 use strict;
 # Make the plug-in
 sub new
 {
    my( $class, %params ) = @_;
 
    my $self = $class->SUPER::new(%params);
 
    # Where the button to access the screen appears if anywhere, and what priority
    $self->{appears} = [
       {
           place => "admin_actions",
           position => 1247,
       },
    ];
 
    return $self;
 }
 
 # Anyone can see this screen
 sub can_be_viewed { 1 }
 
 # What to display
 sub render
 {
    my( $self ) = @_;
  
    # Get the current repository object (so we can access the users, eprints information about things in this repository)
  
    my $repository = $self->{repository};
  
    # Create an XML element to return to our screen
  
    my $frag = $repository->xml->create_document_fragment();
 
    # Fill the fragment with stuff
  
    $frag->appendChild($repository->xml->create_text_node( "Hello, World!" ));
  
    return $frag;
 }
 
 1;

hello_world.pl

Create the package directory that will contain the package's configuration file:

$ mkdir -p lib/epm/hello_world/cfg/cfg.d

Create a configuration file that enables the plugin - this file is copied into the repository when the package is enabled:

$ gedit lib/epm/hello_world/cfg/cfg.d/hello_world.pl
$c->{plugins}{"Screen::Hello"}{params}{disable} = 0;

NOTE All plugins in lib/plugins are disabled by default (see EPrints::PluginFactory::new) UNLESS you explicitly set the disable property in the plugin (and it doesn't make sense to set disable = 0 in the plugin because that would make it visible to all of your repositories even if they hadn't explicitly enabled your bazaar package).

Tip: Any files you add below cfg/ and are tracked by the EPM will be added to the repository when the EPM is enabled.


Commit changes

After adding new files its important to record the changes

Add all new files

git add . 

Commit them with a message

git commit -a -m "Added new Screen and its configuration. Also included new image"


Build the Package

Re-Edit your EPM in the "Developer Tools" section of the Bazaar Store.

At the bottom of the screen (the Files selector) you need to add the files you created above:

  • epm/hello_world/cfg/cfg.d/hello_world.pl
  • plugins/EPrints/Plugin/Screen/Hello.pm
  • static/images/epm/hello_world.png


Testing in place

Use the link_lib argument in API:tools/epm to sym-link the plugin into your live repository:

./tools/epm link_lib hello_world

If needed, also include configuration

./tools/epm link_cfg REPOID hello_world

Followed by

tools/epm enable REPOID hello_world

Enable and test the package

On the EPrints Bazaar Installed tab click Enable for the hello_world package. After a short time you should see a repository configuration reloaded message.

In AdminSystem ToolsMisc. Tools you should now a button-link to your plugin (although with a missing phrase).

You can now use Uninstall to completely remove the package and the source files created. A copy of the package will be saved in var/cache/epm/hello_world-1.0.0.epm.


Adding the missing phrases

As EPrints is designed to be multi-language, phrases should be used instead of embedded text.

In this section we add the phrase file to define a title and a description for our screen.

Create the English language directory that will contain the package's English language phrase file:

$ mkdir -p lib/lang/en/phrases/

Create a phrase file:

$ gedit lib/lang/en/phrases/hello_world.xml
  <?xml version="1.0" encoding="iso-8859-1" standalone="no" ?>
  <!DOCTYPE phrases SYSTEM "entities.dtd">
  
  <epp:phrases xmlns="http://www.w3.org/1999/xhtml" xmlns:epp="http://eprints.org/ep3/phrase" xmlns:epc="http://eprints.org/ep3/control">
 
        <epp:phrase id="Plugin/Screen/Hello:title">Hello</epp:phrase>
        <epp:phrase id="Plugin/Screen/Hello:description">My First Bazaar Package</epp:phrase>
        
        <epp:phrase id="Plugin/Screen/Hello:text">Hello World - This is the my first bazaar package calling.</epp:phrase>
 
  </epp:phrases>

Don't worry too much about the wrapping here, the important thing is the phrases of which we have created 3.

Note that 2 of these (the top 2) will get used instantly by our package however the 3rd needs to be added to the render method of our screen.

In order to do this edit the Hello.pm screen created in Step 1 and add the following in the appropriate place in the render method.

 $frag->appendChild($repository->xml->create_element("br"));
 $frag->appendChild($self->html_phrase("text"));

For some hints on loading and manipulating EPrints see Manipulating eprints in 3.2.

Don't Forget to add the phrase file to your bazaar package using the developer tools tab and commit them to git!

WARNING: if your package overrides any existing EPrints phrases then your overrides will be applied to ALL repositories even if they have not enabled your package. In this case you would add your override phrases to a separate file - eg. lib/epm/hello_world/cfg/lang/en/phrases/hello_world_overrides.xml - this file will then only be considered when a repository enables your package.