Lazarus Examples Window

From Lazarus wiki
Jump to navigationJump to search


The new Example Window that exists in Lazarus 3.0 and beyond is documented below.

For the end user

Introduction

A Lazarus user gets to the Example Window after clicking the button on the Project Wizard screen or from the Tools Menu, "Example Projects". In either case you see a window offering a list of known examples. The list can be filtered by key words or by category (double click between category checkboxes turns all off). In basic use, it shows you all the examples shipped with Lazarus.

The Example Window will allow you to open, build (and edit) and run any example but note some have specific package requirements. When opened, the examples from the Lazarus Source code are copied to a (user writable) area in the Lazarus Config directory (PCP) because, on some systems Lazarus Source is in read-only disk space.

Feel free to edit or make changes to examples in Lazarus Source, if you totally mess up, its easily refreshed from the Examples Window.

Examples in Packages

When you install a package that has compliant examples, they will also appear in the Lazarus Examples Window. If they don't appear, the most likely reason is that they do not have an example meta data file. See below.

Displaying your own Examples

If you have your own examples in one or more Lazarus projects, you can arrange to have them listed along with the built in examples supplied with Lazarus. Following the same rules as below, you would -

  • Ensure each example is in its own directory and is self contained.
  • Provide meta data files as described below. Perhaps set the category to something like "Private" ?
  • Add an entry to the ./examples/examples.txt file in your Lazarus Source directory. The entry, see the existing ones, should have a relative path from the top of the lazarus source directory to your meta data file. If you are using a packaged version of Lazarus then this examples.txt file might be in read only space and you will need administrative access to it.
  • Examples mentioned in examples.txt are always copied to a work area ensuring write access and allowing you to play without affecting the original.

FPC Examples

FPC also provides a number of examples relating to its packages and RTL. These examples are generally command line driven and don't have the Lazarus project information so are unsuited to working with the Lazarus Examples Window. (It might be nice to find a way to display them however.)

For the Developer

Examples in the Lazarus Source

You can add an example anywhere you like in the Lazarus Source Tree, there are lots under ~/examples but also many associated with different Lazarus sub system. A few rules apply -

  • The Example Name must be unique within Lazarus (when lowercased).
  • The example should be self contained within its own directory. That directory (and any sub directories) will be copied to the work area so don't assume particular files are "just up one dir".
  • The example should have an Example Metadata File, with a file name that corresponds to the project name and an extension of .ex-meta. The content of this file is JSON and must have a name, a category and should have a (multiline) description and keywords. See below.

If you add a new example (or remove one) to the Lazarus Source Tree, the file ~/examples/examples.txt needs to be refreshed. Its plain text, edit it directly or generate a new one with this command (on a *nix )-

$> find . -name "*.ex-meta" > examples/examples.txt

Examples in Third Party Packages

Again, you are free to place your package examples directory where you like but in order for the Lazarus Examples Window to find it, some things must be provided, note, different rules than above !

  • The package must be currently installed in Lazarus (check ? see if its listed in (PCP)/staticpackages.inc)
  • Your example should be in a directory of its own. You should not put more than one example in the same directory.
  • In the case of Package Examples, the example directory is not moved to a work area so it can (but perhaps should not) be dependent on local, relative path files.
  • It must have a Metadata File, see below.
  • The Package file, that is the .lpk file must have entry such as <ExamplesDirectory Value="../demo"/>, typically just below the Author item. The value is the relative path from the .lpk file to a directory containing your example or examples. eg -
[MyPackage]
    [demo]
        mypackage_demo1.ex-meta
        ....
        [demo2]
            mypackage_demo2.ex-meta
            ....
    [package]
        mypackage.lpk

(Personally, I would put demo1 in its own directory too but the above would work !)

Note that, at present, Lazarus, when making a package, does NOT create the ExamplesDirectory entry, it will, but its a work in progress. If you want to test, manually adding that entry is the way to go right now.

The Metadata File

Light bulb  Note: If you add a new example (or remove one) to the Lazarus Source Tree, the file ~/examples/examples.txt needs to be refreshed. Its plain text, edit it directly or generate a new one with this command from the top level Lazarus directory (on a *nix )-

$> find . -name "*.ex-meta" > examples/examples.txt

All examples that appear in the Example Window have a json metadata file with an extension ".ex-meta" in the top level directory of the example (not the package). Without a valid metadata file, the example will not be listed in the Example Window. An individual example project file would look like this -

{ 
"Laz_Hello" : {
    "Category" : "Beginner",
    "Keywords" : ["Lazarus", "Hello World", "TButton", "TMemo"],
    "Description" : "This might be your first Lazarus project, its the traditional Hello World.  Two buttons have been dropped on the form, renamed and their captions set. A memo was then added and each button was double clicked and what they are expected to do was set."
}

In this case, the example project files would be in a subdirectory called laz_hello, same spelling but all lowercase consistent with Lazarus.

The three fields are free form text, there is no dictionary so, you are free to introduce both new Categories and new Key Words. Its desirable that we do not have too many Categories so, perhaps consider posting a message on the forum before using a new one ? At present, the following Categories are in use -

  • Beginner
  • General
  • TAChart
  • DBase
  • LazReport
  • ThirdParty

There is a rough and ready GUI app to make, edit and importantly, validate metadata files available at https://github.com/davidbannon/ExampleMetaData .

Can the IDE find the example ?

If the IDE does not find an example, some things to check for -

  • Does it have a metadata file ?
  • Is there a message about the metadata from console ?
  • If its a Lazarus Source example, is it mentioned in examples/examples.txt ?
  • if a Package Example, is it mentioned in (PCP)/staticpackages.inc ? and in (PCP)/packagefiles.xml ? (Unlike the examples.txt file, do not manually edit these files, they should be updated when a package is installed into the the IDE.)

Notes

The user can view the project content and, if they choose, build the project. Later, if the user users the Example Window to again open that project, they will be asked if they want to refresh it, doing so will erase any false edits they may have made.

Important to note -

  • Example Projects will not be displayed in the Examples Window unless they have a valid metadata file. An error is dropped to the console if a metadata file with bad json is encountered.
  • The original copy of the Example is not altered when the Example Window is used to open an Example. So, play away !
  • New examples can, potentially be added via the normal Gitlab pull request system.
  • When examples are added to or removed from the Lazarus Source, changes need be made to the (eg) ~/lazarus_3_0/examples/examples.txt file. See the Metadata section.
  • Examples demonstrating aspects of third party packages are probably best added to that package rather than to Lazarus itself.

See also