第2章 入門

原文連接  https://erlang.mk/guide/getting_started.htmlhtml

 

Chapter 2. Getting startednode

 

This chapter explains how to get started using Erlang.mk.git

本章將介紹如何開始使用Erlang.mkgithub

 

2.1. Creating a folder for your projectshell

2.1. 爲您的項目建立一個文件夾bootstrap

 

The first step is always to create a new folder that will contain your project.app

第一步始終是爲你的項目建立一個新的工做目錄。curl

$ mkdir hello_joe
$ cd hello_joeasync

 

Most people tend to put all their projects side by side in a common folder. We recommend keeping an organization similar to your remote repositories. For example, for GitHub users, put all your projects in a common folder with the same name as your username. For example $HOME/ninenines/cowboy for the Cowboy project.編輯器

大多數人傾向於把他們的全部項目放在一個公有的文件夾中,對此,咱們建議保持一個相似於你的遠程存儲庫的組織方式。例如,對於github用戶,將你的全部項目放在一個與你用戶名相同的公共文件夾中。例如,對於Cowboy項目,路徑是 $HOME/ninenines/cowboy

 

2.2. Downloading Erlang.mk

2.2. 下載Erlang.mk

 

At the time of writing, Erlang.mk is unlikely to be present in your Erlang distribution, or even in your OS packages.

在寫這篇文章時,Erlang.mk不太可能出如今你的Erlang分發中,甚至不在你的操做系統軟件包中。

 

The next step is therefore to download it:

所以下一步是下載它:

 

$ wget https://erlang.mk/erlang.mk

Or:

$ curl -O https://erlang.mk/erlang.mk

 

Alternatively, just click on this link.

或者,只須要點擊此連接

 

Make sure you put the file inside the folder we created previously.

確保你剛剛下載的文件被放在了咱們以前建立的工程文件夾中。

 

2.3. Getting started with OTP applications

2.3. 開始使用OTP應用程序

 

An OTP application is an Erlang application that has a supervision tree. In other words, it will always have processes running.

一個OTP應用程序就是一個具備監督樹的Erlang應用程序。換句話說,它始終有進程運行

 

This kind of project can be automatically generated by Erlang.mk. All you need to do is use the bootstrap target:

這種項目能夠經過Erlang.mk自動生成。你只須要使用‘bootstrap’做爲編譯目標: 

$ make -f erlang.mk bootstrap

 

Something similar to the following snippet will then appear on your screen:

你的屏幕上將出現相似於以下內容:

 

git clone https://github.com/ninenines/erlang.mk .erlang.mk.build
Cloning into '.erlang.mk.build'...
remote: Counting objects: 4035, done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 4035 (delta 8), reused 4 (delta 4), pack-reused 4019
Receiving objects: 100% (4035/4035), 1.10 MiB | 784.00 KiB/s, done.
Resolving deltas: 100% (2442/2442), done.
Checking connectivity... done.
if [ -f build.config ]; then cp build.config .erlang.mk.build; fi
cd .erlang.mk.build && make
make[1]: Entering directory '/home/essen/tmp/hello_joe/.erlang.mk.build'
awk 'FNR==1 && NR!=1{print ""}1' core/core.mk index/*.mk core/index.mk core/deps.mk plugins/protobuffs.mk core/erlc.mk core/docs.mk core/test.mk plugins/asciidoc.mk plugins/bootstrap.mk plugins/c_src.mk plugins/ci.mk plugins/ct.mk plugins/dialyzer.mk plugins/edoc.mk plugins/elvis.mk plugins/erlydtl.mk plugins/escript.mk plugins/eunit.mk plugins/relx.mk plugins/shell.mk plugins/triq.mk plugins/xref.mk plugins/cover.mk \
    | sed 's/^ERLANG_MK_VERSION = .*/ERLANG_MK_VERSION = 1.2.0-642-gccd2b9f/' > erlang.mk
make[1]: Leaving directory '/home/essen/tmp/hello_joe/.erlang.mk.build'
cp .erlang.mk.build/erlang.mk ./erlang.mk
rm -rf .erlang.mk.build

 

This is Erlang.mk bootstrapping itself. Indeed, the file you initially downloaded contains nothing more than the code needed to bootstrap. This operation is done only once. Consult the Updating Erlang.mk chapter for more information.

這是Erlang.mk的自我引導。事實上,你最初下載的文件只包含引導所需的代碼。這個操做只作一次。更多信息,請參考 Updating Erlang.mk章節。

 

Of course, the generated project can now be compiled:

固然,剛剛生成的項目如今已經能夠編譯了:

$ make

 

Cheers!

乾杯!

 

2.4. Getting started with OTP libraries

2.4. 開始使用OTP庫

 

An OTP library is an Erlang application that has no supervision tree. In other words, it is nothing but modules.

一個OTP庫是一個沒有監督樹的Erlang應用程序。換句話說,它只是個模塊。

 

This kind of project can also be generated by Erlang.mk, using the bootstrap-lib target:

這種項目也能夠由Erlang.mk使用 ‘bootstrap-lib’ 做爲編譯目標而生成: 

$ make -f erlang.mk bootstrap-lib

 

Erlang.mk will once again bootstrap itself and generate all the files for your project. You can now compile it:

Erlang.mk將再次自我引導併爲你的項目生成全部文件。你如今能夠編譯它:

$ make

 

Enjoy!

請享用!

 

2.5. Getting started with OTP releases

2.5. 開始使用OTP發行版

An OTP release is the combination of the Erlang RunTime System (ERTS) along with all the libraries and files that your node will need to run. It is entirely self contained, and can often be sent as-is to your production system and run without any extra setup.

OTP發行版由Erlang運行時系統(ERTS)以及節點運行須要的全部庫和文件的組合。它徹底獨立,一般能夠按原樣發送到你的生產環境,並沒有需任何額外的設置便可運行。

 

Erlang.mk can of course bootstrap your project to generate releases. You can use the bootstrap-rel target for this purpose:

Erlang.mk固然能夠引導你的項目來生成版本。你可使用 ‘bootstrap-rel’ 做爲編譯目標來達到這個目的:

$ make bootstrap-rel

 

This target can be combined with bootstrap or bootstrap-lib to create a project that will build a release:

bootstrap-rel 能夠連同 bootstrap 或者 bootstrap-lib 一塊兒來建立一個項目並構建出一個版本:

$ make -f erlang.mk bootstrap-lib bootstrap-rel

 

It is often very useful to keep the top-level project for commands useful during operations, and put the components of the system in separate applications that you will then depend on. Consult the Packages and dependencies chapter for more information.

在項目設計的過程當中,爲命令保持頂級項目,並將(項目須要的)系統組件放在單獨的應用程序中,而後依賴它們,一般很是有用。更多信息,請參閱軟件包和依賴項章節。

 

 

When you run make from now on, Erlang.mk will compile your project and build the release:

當你如今開始執行make命令時,Erlang.mk將會編譯你的項目並構建出版本:

$ make
 APP    hello_joe.app.src
 GEN    distclean-relx-rel
 GEN    /home/essen/tmp/hello_joe/relx
===> Starting relx build process ...
===> Resolving OTP Applications from directories:
          /home/essen/tmp/hello_joe/ebin
          /usr/lib/erlang/lib
          /home/essen/tmp/hello_joe/deps
===> Resolved hello_joe_release-1
===> Including Erts from /usr/lib/erlang
===> release successfully created!

 

The first time you run this command, Erlang.mk will download relx, the release building tool. So don’t worry if you see more output than above.

第一次運行這個命令時,Erlang.mk會下載發行版本的編譯工具relx。因此當你看到更多輸出時,請不用擔憂。

 

If building the release is slow, no need to upgrade your hardware just yet. Just consult the Releases chapter for various tips to speed up build time during development.

若是構建發行版很慢,剛好如今又不能升級硬件。你能夠先查閱發行版本章節中的各類提示,來嘗試加快開發過程當中的構建時間。

 

You can start the release using the ./_rel/hello_joe_release/bin/hello_joe_releasescript, or simply run make run. The latter will also compile your project and build the release if it wasn’t already:

你可使用 ./_rel/hello_joe_release/bin/hello_joe_releasescript 來啓動發行版,或者直接運行 make run。若是項目尚未編譯發佈,後者將編譯你的項目並創建發行版:

 

$ make run
 APP    hello_joe.app.src
 GEN    distclean-relx-rel
===> Starting relx build process ...
===> Resolving OTP Applications from directories:
          /home/essen/tmp/hello_joe/ebin
          /usr/lib/erlang/lib
          /home/essen/tmp/hello_joe/deps
===> Resolved hello_joe_release-1
===> Including Erts from /usr/lib/erlang
===> release successfully created!
Exec: /home/essen/tmp/hello_joe/_rel/hello_joe_release/erts-7.0/bin/erlexec -boot /home/essen/tmp/hello_joe/_rel/hello_joe_release/releases/1/hello_joe_release -boot_var ERTS_LIB_DIR /home/essen/tmp/hello_joe/_rel/hello_joe_release/erts-7.0/../lib -env ERL_LIBS /home/essen/tmp/hello_joe/_rel/hello_joe_release/releases/1/lib -config /home/essen/tmp/hello_joe/_rel/hello_joe_release/releases/1/sys.config -args_file /home/essen/tmp/hello_joe/_rel/hello_joe_release/releases/1/vm.args -- console
Root: /home/essen/tmp/hello_joe/_rel/hello_joe_release
/home/essen/tmp/hello_joe/_rel/hello_joe_release
heart_beat_kill_pid = 16389
Erlang/OTP 18 [erts-7.0] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V7.0  (abort with ^G)
(hello_joe@127.0.0.1)1>

 

Simple as that!

就是這麼簡單!

 

2.6. Getting started from scratch

2.6. 在已有程序中開始使用Erlang.mk

 

If you already have an application, or you want to have full control over what files will be created, you can setup Erlang.mk manually.

若是你已經有了一個應用程序,或者你但願徹底控制什麼文件會被建立,你能夠手動設置Erlang.mk.

 

Erlang.mk is very easy to setup: all that you need to do is to create a folder, put Erlang.mk in it, and write a one line Makefile containing:

Erlang.mk很是容易設置:你只須要建立一個文件夾,把Erlang.mk文件放在裏面,而且在Makefile寫一行:

include erlang.mk

 

For a step by step:

完整流程以下:

 

$ mkdir hello_joe
$ cd hello_joe
$ curl https://erlang.mk/erlang.mk -o erlang.mk
$ echo "include erlang.mk" > Makefile
$ make

 

From that point onward you can create an src/ folder or start using templates.

如今開始,你能夠建立一個src/folder或者開始使用模版。

 

2.7. Using spaces instead of tabs

2.7. 使用空格而不是tab鍵

 

Erlang.mk defaults to tabs when creating files from templates. This is in part because of a personal preference, and in part because it is much easier to convert tabs to spaces than the opposite.

經過模版建立文件時,Erlang.mk默認使用tab。使用tab而不用空格,部分是由於我的喜愛,部分是由於相比於將空格轉換爲tab,將tab轉換爲空格會更容易。

 

Use the SP variable if you prefer spaces. Set it to the number of spaces per indentation level you want.

若是你喜歡使用空格,請使用SP變量,設置你須要的縮進空格數。

 

For example, if you prefer two spaces per indentation level:

例如,若是你但願兩個空格的縮進:

 

$ make -f erlang.mk bootstrap SP=2

 

When you bootstrap the project initially, the variable automatically gets added to the Makefile, so you only need to provide it when you get started.

當你開始引導項目時,這個變量將會自動添加到Makefile中,因此你只須要在開始時提供便可。

 

2.8. Using templates

2.8. 使用模版

 

It is no secret that Erlang’s OTP behaviors tend to have some boilerplate. It is rarely an issue of course, except when creating new modules. That’s why Erlang.mk not only comes with templates for generating projects, but also individual modules!

你們都知道Erlang OTP的特性是傾向於提供一些樣板供用戶使用。除非建立新的模版,通常來講使用已有的模版極少有問題。這也是爲何Erlang.mk不只帶有OTP生成項目的模版,並且還有本身特有的模版!(好比下面執行make list-templates命令時顯示的cowboy_http模版?!)

 

You can list all available templates with the list-templates target:

你可使用list-templates編譯目標列出全部可用的模版:

 

$ make list-templates
Available templates: cowboy_http cowboy_loop cowboy_rest cowboy_ws gen_fsm gen_server ranch_protocol supervisor

 

To generate a module, let’s say a gen_server, all you need to do is to call make new with the appropriate arguments:

假設要生成gen_server模版,你所要作的只是用適當的參數調用make new:

$ make new t=gen_server n=my_server

 

This will create a module located in src/my_server.erl using the gen_server template.

這將在src/my_server.erl中建立一個使用gen_server的模版。

 

This module is automatically compiled the next time you run make:

這個模版在下次運行make時自動自動編譯:

 

$ make
 ERLC   my_server.erl
 APP    hello_joe.app.src

 

All that’s left to do is to open it in your favorite editor and make it do something!

剩下的事就是用你喜歡的編輯器打開它,並讓它作一些事!

 

2.9. Hiding Erlang.mk from git

2.9. 在git版本庫中隱藏Erlang.mk

 

Erlang.mk is a large text file. It can easily take a large part of a git diff or a git grep command. You can avoid this by telling Git that erlang.mk is a binary file.

Erlang.mk是一個很大的文本文件,因此它能夠很容易的在大部分git diff和git grep命令中顯示,你能夠經過告訴git Erlang.mk是一個二進制文件來避免這種狀況。

 

Add this to your .gitattributes file. This is a file that you can create at the root of your repository:

在.gitattributes 文件中指定Erlang.mk是二進制文件。.gitattributes是一個能夠在你的存儲庫根目錄建立的文件:

 

erlang.mk -diff

 

The erlang.mk file will still appear in diffs and greps, but as a binary file, meaning its contents won’t be shown by default anymore.

(執行上面的命令發現),Erlang.mk文件仍然會出如今diffs和greps命令中,可是做爲二進制文件,意味着它的內容將不會被默認顯示。

 

2.10. Getting help

2.10. 得到幫助

 

During development, if you don’t remember the name of a target, you can always run make help:

在開發過程當中,若是你不記得某個target的名字,你始終能夠經過運行make help來獲取幫助:

$ make help
erlang.mk (version 1.2.0-642-gccd2b9f) is distributed under the terms of the ISC License.
Copyright (c) 2013-2016 Loïc Hoguin <essen@ninenines.eu>

Usage: [V=1] make [target]...

Core targets:
  all           Run deps, app and rel targets in that order
  app           Compile the project
  deps          Fetch dependencies (if needed) and compile them
  search q=...  Search for a package in the built-in index
  rel           Build a release for this project, if applicable
  docs          Build the documentation for this project
  install-docs  Install the man pages for this project
  check         Compile and run all tests and analysis for this project
  tests         Run the tests for this project
  clean         Delete temporary and output files from most targets
  distclean     Delete all temporary and output files
  help          Display this help and exit
  erlang-mk     Update erlang.mk to the latest version

Bootstrap targets:
  bootstrap          Generate a skeleton of an OTP application
  bootstrap-lib      Generate a skeleton of an OTP library
  bootstrap-rel      Generate the files needed to build a release
  new t=TPL n=NAME   Generate a module NAME based on the template TPL
  list-templates     List available templates
...

 

This guide should provide any other answer. If not, please open a ticket on the official repository and we will work on improving the guide.

若是這份指南還有任何你認爲沒有提供的答案,請在官方存儲庫提交一個ticket,咱們將努力改進這份指南。

 

Commercial support is available through Nine Nines. Please contact Loïc Hoguin by sending an email to contact@ninenines.eu.

同時Nine Nines也提供商業支持,若是有須要,請發送郵件至contact@ninenines.eu.聯繫Loïc Hoguin。

相關文章
相關標籤/搜索