oh-my-fish/docs/en-US/Packages.md

94 lines
3.0 KiB
Markdown
Raw Normal View History

2015-09-11 20:23:05 +08:00
<div align="center">
<a href="http://github.com/oh-my-fish/oh-my-fish">
<img width=120px src="https://cloud.githubusercontent.com/assets/8317250/8510172/f006f0a4-230f-11e5-98b6-5c2e3c87088f.png">
</a>
</div>
<br>
<p align="center">
<b>English</b> &bull;
2016-01-18 00:44:26 +08:00
<a href="docs/zh-CN/Packages.md">简体中文</a> &bull;
<a href="docs/ru-RU/Packages.md">Русский</a>
2015-09-11 20:23:05 +08:00
</p>
2015-09-06 13:45:21 +08:00
# Creating
2015-09-06 13:45:21 +08:00
To learn package creation let's create a new package that will provide a `hello_world` command for your shell. Package names may only contain lowercase letters and hyphens to separate words.
2015-09-06 13:45:21 +08:00
Oh My Fish can scaffold a package structure for you. Use the command `omf new`:
```fish
2015-09-06 13:45:21 +08:00
$ omf new pkg hello_world
```
2015-09-06 13:45:21 +08:00
> Use `omf new theme my_theme_name` for themes.
2015-09-06 13:45:21 +08:00
The utily changes the current directory to the newly created package:
2015-09-06 13:45:21 +08:00
```
$ ls -l
README.md
hello_world.fish
completions/hello_world.fish
```
2016-01-14 09:07:57 +08:00
Always describe how your package works in the `README.md`. Also read more about [auto completion](http://fishshell.com/docs/current/commands.html#complete) and take care to provide it for your utilities when applicable.
2015-09-06 13:45:21 +08:00
`hello_world.fish` defines a single function:
```fish
2015-09-06 13:45:21 +08:00
function hello_world -d "Prints hello world"
echo "Hello World!"
end
```
2015-09-06 13:45:21 +08:00
Each function in your package must be declared in its own file. This is required by fish autoloading mechanism, which loads functions on demand, avoiding loading unused functions.
2015-09-06 13:45:21 +08:00
Bear in mind that fish lacks a private scope, so if you need to split your package into functions, consider prefixing private functions like this: `__hello_world.my_extra_function`, to avoid both name clashes and global namespace pollution.
2015-09-06 13:45:21 +08:00
# Events
2015-09-06 13:45:21 +08:00
Packages were designed to take advantages of fish events. There are currently two events that Oh My Fish will emit to your package:
## Initialization
2015-09-06 13:45:21 +08:00
If you want to be [notified](http://fishshell.com/docs/current/commands.html#emit) when your package loads, declare the following function in your `hello_world.fish`:
```fish
2015-09-06 13:45:21 +08:00
function init -a path --on-event init_hello_world
echo "hello_world initialized"
end
```
Use this event to modify the environment, load resources, autoload functions, etc. If your package does not export any functions, you can still use this event to add functionality to your package.
## Uninstall
Oh My Fish emits `uninstall_<pkg>` events before a package is removed via `omf remove <pkg>`. Subscribers can use the event to clean up custom resources, etc.
```fish
2015-09-06 13:45:21 +08:00
function uninstall --on-event uninstall_hello_world
end
```
2015-09-06 13:45:21 +08:00
# Make it public
Oh My Fish keeps a registry of public packages under `$OMF_PATH/db/`.
To add your package to the registry you need to:
```fish
# For packages:
omf submit pkg/hello_world .../hello_world.git
# For themes
omf submit theme/my_theme .../my_theme_name.git
```
This will add a new entry to your local copy of the registry. Now you just need to [send us a PR][omf-pulls-link] to update the global registry.
[omf-pulls-link]: https://github.com/oh-my-fish/oh-my-fish/pulls