pulsar/docs/your-first-package.md

159 lines
5.8 KiB
Markdown
Raw Normal View History

2014-01-25 06:07:37 +04:00
# Create Your First Package
2014-01-27 22:49:09 +04:00
This tutorial will guide you though creating a simple command that replaces the
selected text with [ascii art](http://en.wikipedia.org/wiki/ASCII_art). When you
run our new command with the word "cool" selected, it will be replaced with:
```
___
2014-01-25 06:07:37 +04:00
/\_ \
___ ___ ___\//\ \
/'___\ / __`\ / __`\\ \ \
/\ \__//\ \L\ \/\ \L\ \\_\ \_
\ \____\ \____/\ \____//\____\
\/____/\/___/ \/___/ \/____/
```
2014-01-25 06:07:37 +04:00
The final package can be viewed at
2014-01-27 20:46:27 +04:00
[https://github.com/atom/ascii-art](https://github.com/atom/ascii-art).
2014-01-27 20:46:27 +04:00
To begin, press `cmd-shift-P` to bring up the [Command
Palette](https://github.com/atom/command-palette). Type "generate package" and
select the "Package Generator: Generate Package" command. Now we need to name
2014-03-06 20:50:18 +04:00
the package. Try to avoid naming your package with the *atom-* prefix, for
2014-04-25 00:56:14 +04:00
example we are going to call this package _ascii-art_.
Atom will open a new window with the contents of our new _ascii-art_ package
displayed in the Tree View. Because this window is opened **after** the package
is created, the ASCII Art package will be loaded and available in our new
window. To verify this, toggle the Command Palette (`cmd-shift-P`) and type
"ASCII Art". You'll see a new `ASCII Art: Toggle` command. When triggered, this
command displays a default message.
Now let's edit the package files to make our ASCII Art package do something
interesting. Since this package doesn't need any UI, we can remove all
view-related code. Start by opening up _lib/ascii-art.coffee_. Remove all view
2014-02-04 02:54:40 +04:00
code, so the `module.exports` section looks like this:
```coffeescript
2014-02-12 23:45:00 +04:00
module.exports =
activate: ->
```
## Create a Command
Now let's add a command. We recommend that you namespace your commands with the
package name followed by a `:`, so we'll call our command `ascii-art:convert`.
Register the command in _lib/ascii-art.coffee_:
```coffeescript
2014-01-25 06:07:37 +04:00
module.exports =
activate: ->
atom.workspaceView.command "ascii-art:convert", => @convert()
2014-01-25 06:07:37 +04:00
convert: ->
# This assumes the active pane item is an editor
2014-01-27 20:53:47 +04:00
editor = atom.workspace.activePaneItem
editor.insertText('Hello, World!')
```
2014-01-27 20:46:27 +04:00
The `atom.workspaceView.command` method takes a command name and a callback. The
2014-01-25 06:07:37 +04:00
callback executes when the command is triggered. In this case, when the command
is triggered the callback will call the `convert` method and insert 'Hello,
World!'.
## Reload the Package
Before we can trigger `ascii-art:convert`, we need to load the latest code for
2014-01-28 23:32:31 +04:00
our package by reloading the window. Run the command `window:reload` from the
command palette or by pressing `ctrl-alt-cmd-l`.
## Trigger the Command
2014-01-27 20:46:27 +04:00
Now open the command panel and search for the `ascii-art:convert` command. But
it's not there! To fix this, open _package.json_ and find the property called
`activationEvents`. Activation Events speed up load time by allowing Atom to
delay a package's activation until it's needed. So remove the existing command
and add `ascii-art:convert` to the `activationEvents` array:
2014-01-28 23:33:07 +04:00
```json
2014-01-25 06:07:37 +04:00
"activationEvents": ["ascii-art:convert"],
```
First, reload the window by running the command `window:reload`. Now when you
run the `ascii-art:convert` command it will output 'Hello, World!'
## Add a Key Binding
2014-01-25 06:07:37 +04:00
Now let's add a key binding to trigger the `ascii-art:convert` command. Open
_keymaps/ascii-art.cson_ and add a key binding linking `ctrl-alt-a` to the
`ascii-art:convert` command. You can delete the pre-existing key binding since
you don't need it anymore. When finished, the file will look like this:
```coffeescript
2014-01-25 06:07:37 +04:00
'.editor':
'cmd-alt-a': 'ascii-art:convert'
```
Notice `.editor` on the first line. Just like CSS, keymap selectors *scope* key
bindings so they only apply to specific elements. In this case, our binding is
only active for elements matching the `.editor` selector. If the Tree View has
focus, pressing `cmd-alt-a` won't trigger the `ascii-art:convert` command. But
if the editor has focus, the `ascii-art:convert` method *will* be triggered.
2014-01-28 23:11:11 +04:00
More information on key bindings can be found in the
[keymaps](advanced/keymaps.html) documentation.
Now reload the window and verify that the key binding works! You can also verify
that it **doesn't** work when the Tree View is focused.
## Add the ASCII Art
Now we need to convert the selected text to ASCII art. To do this we will use
2014-01-28 23:11:11 +04:00
the [figlet](https://npmjs.org/package/figlet) [node](http://nodejs.org/) module
2014-01-28 23:27:06 +04:00
from [npm](https://npmjs.org/). Open _package.json_ and add the latest version of
2014-01-28 23:21:58 +04:00
figlet to the dependencies:
```json
2014-02-13 04:30:03 +04:00
"dependencies": {
"figlet": "1.0.8"
}
```
After saving the file, run the command 'update-package-dependencies:update' from
the Command Palette. This will install the package's node module dependencies,
2014-01-28 23:23:50 +04:00
only figlet in this case. You will need to run
'update-package-dependencies:update' whenever you update the dependencies field
in your _package.json_ file.
Now require the figlet node module in _lib/ascii-art.coffee_ and instead of
inserting 'Hello, World!' convert the selected text to ASCII art.
```coffeescript
2014-01-25 06:07:37 +04:00
convert: ->
# This assumes the active pane item is an editor
editor = atom.workspace.activePaneItem
selection = editor.getSelection()
2013-10-30 01:26:56 +04:00
2014-01-25 06:07:37 +04:00
figlet = require 'figlet'
figlet selection.getText(), {font: "Larry 3D 2"}, (error, asciiArt) ->
if error
console.error(error)
else
2014-01-28 05:30:14 +04:00
selection.insertText("\n#{asciiArt}\n")
2013-10-30 01:26:56 +04:00
```
2014-02-04 04:48:33 +04:00
Select some text in an editor window and hit `cmd-alt-a`. :tada: You're now an
ASCII art professional!
2013-10-31 01:34:50 +04:00
## Further reading
* [Getting your project on GitHub guide](http://guides.github.com/overviews/desktop)
2014-05-22 01:47:30 +04:00
* [Writing specs](writing-specs.md) for your package
2014-02-14 02:17:58 +04:00
* [Creating a package guide](creating-a-package.html) for more information
on the mechanics of packages
2014-04-25 00:56:14 +04:00
* [Publishing a package guide](publishing-a-package.html) for more information
on publishing your package to [atom.io](https://atom.io)