Jump to content

Help:Lua development environment

From Wikipedia

Even with CodeEditor and syntax checking at preview time, advanced Lua scripting in MediaWiki is still a tedious process. Fortunately, you can set up a development environment on your own computer to help with debugging.

Install Lua

[edit]

This wiki runs Lua 5.1, which you can download from lua.org. If you use Linux, you can get a Lua package from your system's package manager; likewise from MacPorts for Mac OS X users.

Lua comes with a built-in command-line interpreter (lua -i), which makes it easy to learn Lua syntax by trial and error.

Use a Lua-savvy editor

[edit]

Many code editors and IDEs support syntax highlighting for Lua code, including programs based on Scintilla. They can even help you wrap your lines at column 80, if you're into that sort of thing. The free, cross-platform Komodo Edit is one such program that also supports as-you-type syntax checking via the Lua Language Extension.

Organize your scripts

[edit]

You can emulate some of Scribunto's environment on your computer, so that each time you come up with a new version of a script, you can just copy and paste it wholesale into a MediaWiki module and save:

  1. Create a dedicated "modules" folder anywhere on your file system to hold in-development modules. Each module you work on should be saved with the extension ".lua" in the "modules" folder.
  2. Before working on your local copy, always make sure to copy and paste the module's current revision from the wiki to the local copy, to avoid overwriting others' changes.
  3. If your script requires a helper script, say Module:Unicode, copy its contents into a separate file (with the extension ".lua") and put it in the same folder as the main script.
    • On Linux and Mac OS X, if you need a helper script that's a subpage, for instance, Module:Vi2ipa 2/dialects, create a folder called "Module:Vi2ipa 2" and place "dialects.lua" in it. If the module you're working on is Module:Vi2ipa 2, you won't be able to name it "Module:Vi2ipa 2.lua" (because there would be a folder of the same name), but you can name it anything else instead.

For short-term projects, it may be easier to set up an external editor to quickly switch between the wiki and your favorite text editor.

Speed up development with test cases

[edit]

Like the many module talk pages here, separate test scripts allow you to quickly verify that each little change you make won't break something else in the module:

  1. Copy Module:TestUtils to a file in your "modules" folder named "TestUtils.lua".
  2. Create a separate test script in the same folder as your main script. For example, if you're working on a script you've saved as "MyMod.lua", call the test script "MyMod_test.lua".
  3. At the top of the test script, add these two lines:
    dofile("TestUtils.lua") -- for toframe()
    local MyMod = require("MyMod") -- where "MyMod.lua" is the script being tested
    
  4. In your test script, you can use the TestUtils function toframe() to invoke module functions the way Scribunto would:
    assert(MyMod.doSomething(toframe({"Antartica", size = "small"})) == "<small>Antarctica</small>")
    print("SUCCESS")
    




This help article is a stub. Please help improve it.