Weavy Tools

A tool is very similar to a daemon, but instead of running on a schedule, tools are executed on demand from the management UI.

Tool class

To add a tool you should first add a class that inherits from Weavy.Core.Models.Tool. In the example below we add a simple (and not very useful) tool that writes a message to the log file when it’s executed.

using NLog;
using System;
using System.ComponentModel.DataAnnotations;
using System.Runtime.InteropServices;
using System.Threading;
using Weavy.Core.Models;

namespace Wvy.Models {

    [Serializable]
    [Guid("F276B5A9-AA94-4ACC-B2DA-A5206B7ECF4F")]
    [Plugin(Name = "Log message", Description = "A simple function that just writes a message to the log file.")]
    public class LogMessageFunction : Function, ICommand, ITool {

        private static readonly Logger _log = LogManager.GetCurrentClassLogger();

        [Required]
        [Display(Description = "The message to write to the log file.")]
        public string Message { get; set; }

        /// <summary>
        /// Writes a message to the log file.
        /// </summary>
        /// <returns></returns>
        public override bool Run(CancellationToken token, params string[] args) {
            try {
                if (Message == null) {
                    _log.Warn("No message specified");
                    return false;
                }

                _log.Info(Message);
                return true;
            } catch (Exception ex) {
                Output.WriteLine(ex.Message);
                return false;
            }
        }
    }
}

Class attributes

As shown above, tools in Weavy must be decorated with the [Serializable] and [Guid] attributes.

Make sure to give your class a unique Guid, otherwise it will not be recognized by Weavy.

To further customize your tool you can also decorate it with the [Plugin] attribute. This attribute has the following properties:

  • Icon - name of an icon to use when displaying the tool in the manage UI.
  • Color - color to use for the icon.
  • Name - display name for the tool
  • Description - a description to use for the tool, e.g “A simple function that just writes a message to the log file”.

Fields

Tool fields are pieces of information that can be added to a tool. This can be useful if your tool requires some kind of configuration or settings. Tool fields have a name and a type. In the example above, we added a field for the message to write to the log file.

For a property to be considered a tool field, it must be declared as public read-write, i.e. the property has the public access modifier and have both a get and a set accessor.

Tool fields must also have one of the following supported types:

  • enum
  • byte
  • short
  • int
  • long
  • bool
  • double
  • float
  • string
  • Guid
  • DateTime
  • TimeSpan

Nullables and Lists of the above types are also supported, e.g. int?, List<string> and List<DateTime?>.

Field attributes

By decorating your fields with one, or more, of the following attributes you can customize how the field is displayed, edited and/or validated.

Datatype

By default, Weavy will look at the property type (in this case string) when deciding which editor to use for the field. By decorating a field with the [DataType] attribute you can specify an additional type to associate with the field. This value will then be used by the UI to determine which editor to use for the field.

Here we do not specify a [Datatype] attribute since we are happy to treat the Message filed as a simple string.

Display

The [Display] attribute lets you specify a name and description to use for the field.

[Display(Description = "The message to write to the log file.")]
public string Message { get; set; }

Validation

By adding one, or more, [Validation] attributes to your fields you can control how they are validated.

[Required]
public string Message { get; set; }

Here we specify that the Message property is required.

To control validation even more you can also let your class implement IValidatableObject and add your own custom validation logic.