mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
Merge pull request #6013 from Snapcrafter/master
translated by Snapcrafter
This commit is contained in:
commit
526f93b7f4
@ -1,795 +0,0 @@
|
||||
[translating by Snapcraft]
|
||||
How to create a snap for timg with snapcraft on Ubuntu
|
||||
============================================================
|
||||
|
||||
In this post we are going to see how to create a snap for a utility called [timg][14]. If this is the very first time you heard about snap installation packages, see [How to create your first snap][15].
|
||||
|
||||
Today we learn the following items about creating snaps with snapcraft,
|
||||
|
||||
* the source of [timg][9] comes with a handmade Makefile, and requires to tinker a bit the [make plugin parameters.][10]
|
||||
|
||||
* the program is written in C and requires a couple of external libraries. We make sure their relevant code has been added to the snap.
|
||||
|
||||
* should we select [strict confinement][11] or [classic confinement][12]? We discuss how to choose between these two.
|
||||
|
||||
So, what does this [timg][16] do?
|
||||
|
||||
### Background
|
||||
|
||||
The Linux terminal emulators have become quite cool and they have colors!
|
||||
|
||||
[![](https://i1.wp.com/blog.simos.info/wp-content/uploads/2017/02/linux-terminal-standard-colors.png?resize=732%2C438&ssl=1)][17]
|
||||
|
||||
Apart from the standard colors, most terminal emulators (like the GNOME Terminal depicted above) support true color (16 million colors!).
|
||||
|
||||
[![](https://i1.wp.com/blog.simos.info/wp-content/uploads/2017/02/gnome-terminal-true-color.png?resize=732%2C285&ssl=1)][18]
|
||||
|
||||
Yes! True color in the terminal emulators! Get the AWK code from the page [True Colour (16 million colours) support in various terminal applications and terminals][19]
|
||||
and test it yourself. You may notice in the code that it uses some [escape sequences][20] to specify the RGB value (256*256*256 ~= 16 million colors).
|
||||
|
||||
### What is timg?
|
||||
|
||||
Alright, back to the point of the post. What does [timg][21] do? Well, it takes an image as input and resizes it down to the character dimensions of your terminal window (for example, 80×25). And shows the image in color, just by using characters in color, in whatever resolution the terminal window is in!
|
||||
|
||||
[![](https://i0.wp.com/blog.simos.info/wp-content/uploads/2017/02/timg-ubuntu-logo.png?resize=732%2C608&ssl=1)][22]
|
||||
|
||||
This one is [the Ubuntu logo][23], a PNG image file, shown as colored block characters.
|
||||
|
||||
[![](https://i0.wp.com/blog.simos.info/wp-content/uploads/2017/02/timg-flower.png?resize=732%2C608&ssl=1)][24]
|
||||
|
||||
And here is [a flower, by @Doug8888][25].
|
||||
|
||||
[timg][26] would be especially helpful if you are connecting remotely to your server, minding your own business, and want to see what an image file looks like. Bam!
|
||||
|
||||
Apart from static images, [timg][27] can also display animated gifs! Let’s start snapping!
|
||||
|
||||
### Getting familiar with the timg source
|
||||
|
||||
The source of [timg][28] can be found at [https://github.com/hzeller/timg][29] Let’s try to compile it manually in order to get an idea what requirements it may have.
|
||||
|
||||
[![](https://i1.wp.com/blog.simos.info/wp-content/uploads/2017/02/timg-install-from-source.png?resize=741%2C591&ssl=1)][30]
|
||||
|
||||
The _**Makefile**_ is in the _**src/**_ subdirectory and not in the root directory of the project. At the github page they say to install these two development packages (GraphicsMagic++, WebP) and then _**make**_ would simply work and generate the executable. In the screenshot I show for brevity that I have them already installed (after I read the Readme.md file about this).
|
||||
|
||||
Therefore, four mental notes when authoring the snapcraft.yaml file:
|
||||
|
||||
1. The Makefile is in a subdirectory, src/, and not in the root directory of the project.
|
||||
|
||||
2. The program requires two development libraries in order to compile.
|
||||
|
||||
3. In order for timg to run as a snap, we need to somehow bundle these two libraries inside the snap (or link them statically).
|
||||
|
||||
4. [timg][13] is written in C++ and requires g++. Let’s instruct the snapcraft.yaml file to check that the _**build-essential**_ metapackage is installed before compiling.
|
||||
|
||||
### Starting off with snapcraft
|
||||
|
||||
Let’s create a directory _**timg-snap/**_ , and run _**snapcraft init**_ in there in order to create a skeleton _**snapcraft.yaml**_ to work on.
|
||||
|
||||
```
|
||||
ubuntu@snaps:~$ mkdir timg-snap
|
||||
ubuntu@snaps:~$ cd timg-snap/
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft init
|
||||
Created snap/snapcraft.yaml.
|
||||
Edit the file to your liking or run `snapcraft` to get started
|
||||
ubuntu@snaps:~/timg-snap$ cat snap/snapcraft.yaml
|
||||
name: my-snap-name # you probably want to 'snapcraft register <name>'
|
||||
version: '0.1' # just for humans, typically '1.2+git' or '1.3.2'
|
||||
summary: Single-line elevator pitch for your amazing snap # 79 char long summary
|
||||
description: |
|
||||
This is my-snap's description. You have a paragraph or two to tell the
|
||||
most important story about your snap. Keep it under 100 words though,
|
||||
we live in tweetspace and your description wants to look good in the snap
|
||||
store.
|
||||
|
||||
grade: devel # must be 'stable' to release into candidate/stable channels
|
||||
confinement: devmode # use 'strict' once you have the right plugs and slots
|
||||
|
||||
parts:
|
||||
my-part:
|
||||
# See 'snapcraft plugins'
|
||||
plugin: nil
|
||||
```
|
||||
|
||||
### Filling in the metadata
|
||||
|
||||
The upper part of a snapcraft.yaml configuration file is the metadata. We fill them in in one go, and they are the easy part. The metadata consist of the following fields
|
||||
|
||||
1. _**name**_ , the name of the snap as it will be known publicly at the Ubuntu Store.
|
||||
|
||||
2. _**version**_ , the version of the snap. Can be an appropriate branch or tag in the source code repository, or the current date if no branch or tag exist.
|
||||
|
||||
3. _**summary**_ , a short description under 80 characters.
|
||||
|
||||
4. _**description**_ , a bit longer description, under 100 words.
|
||||
|
||||
5. _**grade**_ , either _**stable**_ or _**devel**_ . We want to release the snap in the stable channel of the Ubuntu Store. We make sure the snap works, and write _**stable**_ here.
|
||||
|
||||
6. _**confinement**_ , initially we put _**devmode**_ so as not to restrict the snap in any way. Once it works as _**devmode**_ , we later consider whether to select _**strict**_ or _**classic**_ confinement.
|
||||
|
||||
For the name, we are going to use _**timg,**_
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft register timg
|
||||
Registering timg.
|
||||
You already own the name 'timg'.
|
||||
```
|
||||
|
||||
Yeah, I registered the name the other day :-).
|
||||
|
||||
Next, which version of timg?
|
||||
|
||||
[![](https://i1.wp.com/blog.simos.info/wp-content/uploads/2017/02/timg-tag-095.png?resize=1005%2C480&ssl=1)][31]
|
||||
|
||||
When we look for a branch or a tag in the repository, we find that there is a v0.9.5 tag, with the latest commit in 27th Jun 2016.
|
||||
|
||||
[![](https://i1.wp.com/blog.simos.info/wp-content/uploads/2017/02/timg-master-commits.png?resize=992%2C466&ssl=1)][32]
|
||||
|
||||
However, in the main branch ( _**master**_ ) there are two commits that look significant. Therefore, instead of using _**tag v0.9.5**_ , we are snapping the _**master**_ branch. We are using today’s date as the version, _**20170226**_ .
|
||||
|
||||
We glean the summary and description from the repository. Therefore, the summary is _A terminal image viewer_ , and the description is _A viewer that uses 24-Bit color capabilities and unicode character blocks to display images in the terminal_ .
|
||||
|
||||
Finally, the _**grade**_ will be _**stable**_ and the _**confinement**_ will be _**devmode**_ (initially, until the snap actually works).
|
||||
|
||||
Here is the updated snapcraft.yaml with the completed metadata section:
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ cat snap/snapcraft.yaml
|
||||
name: timg
|
||||
version: '20170226'
|
||||
summary: A terminal image viewer
|
||||
description: |
|
||||
A viewer that uses 24-Bit color capabilities and unicode character blocks
|
||||
to display images in the terminal.
|
||||
|
||||
grade: stable
|
||||
confinement: devmode
|
||||
|
||||
parts:
|
||||
my-part:
|
||||
# See 'snapcraft plugins'
|
||||
plugin: nil
|
||||
```
|
||||
|
||||
### Figuring out the “parts:”
|
||||
|
||||
This is the moment where we want to replace the stub _**parts:**_ section shown above with a real _**parts:**_ .
|
||||
|
||||
<dl class="gallery-item" style="margin-right: 10px; margin-bottom: 15px; float: left; text-align: center; position: relative; outline: rgb(221, 221, 221) solid 1px; border: 4px solid rgb(255, 255, 255); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow: hidden;">
|
||||
|
||||
<dt class="gallery-icon landscape" style="line-height: 20px;"> [![](https://i0.wp.com/blog.simos.info/wp-content/uploads/2017/02/timg-git-url.png?fit=420%2C284&ssl=1)][7] </dt>
|
||||
|
||||
<dd class="wp-caption-text gallery-caption" id="gallery-1-1732" style="font-size: 12px; line-height: 1.5; font-family: Oxygen, Verdana, Geneva, sans-serif; word-wrap: break-word; color: rgb(255, 255, 255); background-color: rgba(0, 0, 0, 0.7); max-height: 50%; padding-top: 6px; padding-bottom: 6px; bottom: 0px; position: absolute; width: 420px; opacity: 0; transition: all 0.3s ease;">The URL for the git repository.</dd>
|
||||
|
||||
</dl>
|
||||
|
||||
<dl class="gallery-item" style="margin-right: 10px; margin-bottom: 15px; float: left; text-align: center; position: relative; outline: rgb(221, 221, 221) solid 1px; border: 4px solid rgb(255, 255, 255); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow: hidden;">
|
||||
|
||||
<dt class="gallery-icon landscape" style="line-height: 20px;"> [![](https://i0.wp.com/blog.simos.info/wp-content/uploads/2017/02/timg-makefile.png?fit=374%2C353&ssl=1)][8] </dt>
|
||||
|
||||
<dd class="wp-caption-text gallery-caption" id="gallery-1-1731" style="font-size: 12px; line-height: 1.5; font-family: Oxygen, Verdana, Geneva, sans-serif; word-wrap: break-word; color: rgb(255, 255, 255); background-color: rgba(0, 0, 0, 0.7); max-height: 50%; padding-top: 6px; padding-bottom: 6px; bottom: 0px; position: absolute; width: 374px; opacity: 0; transition: all 0.3s ease;">There is an existing Makefile, therefore the make plugin.</dd>
|
||||
|
||||
</dl>
|
||||
|
||||
We know the URL to the git repository and we have seen that there is an existing Makefile. The existing Makefile commands for [the make Snapcraft plugin][33]. As the documentation says, _This plugin always runs ‘make’ followed by ‘make install’_ . In order to identify the _**make**_ plugin, we went through [the list of available Snapcraft plugins][34].
|
||||
|
||||
Therefore, we initially change from the stub
|
||||
|
||||
```
|
||||
parts:
|
||||
my-part:
|
||||
# See 'snapcraft plugins'
|
||||
plugin: nil
|
||||
```
|
||||
|
||||
to
|
||||
|
||||
```
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
plugin: make
|
||||
```
|
||||
|
||||
Here is the current snapcraft.yaml,
|
||||
|
||||
```
|
||||
name: timg
|
||||
version: '20170226'
|
||||
summary: A terminal image viewer
|
||||
description: |
|
||||
A viewer that uses 24-Bit color capabilities and unicode character blocks
|
||||
to display images in the terminal.
|
||||
|
||||
grade: stable
|
||||
confinement: devmode
|
||||
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
plugin: make
|
||||
```
|
||||
|
||||
Let’s run the _**snapcraft prime**_ command and see what happens!
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft prime
|
||||
Preparing to pull timg
|
||||
Pulling timg
|
||||
Cloning into '/home/ubuntu/timg-snap/parts/timg/src'...
|
||||
remote: Counting objects: 144, done.
|
||||
remote: Total 144 (delta 0), reused 0 (delta 0), pack-reused 144
|
||||
Receiving objects: 100% (144/144), 116.00 KiB | 0 bytes/s, done.
|
||||
Resolving deltas: 100% (89/89), done.
|
||||
Checking connectivity... done.
|
||||
Preparing to build timg
|
||||
Building timg
|
||||
make -j4
|
||||
make: *** No targets specified and no makefile found. Stop.
|
||||
Command '['/bin/sh', '/tmp/tmpem97fh9d', 'make', '-j4']' returned non-zero exit status 2
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
_**snapcraft**_ could not find a _**Makefile**_ in the source and as we hinted earlier, the _**Makefile**_ is only located inside the _**src/**_ subdirectory. Can we instruct _**snapcraft**_ to look into _**src/**_ of the source for the _**Makefile**_ ?
|
||||
|
||||
Each snapcraft plugin has its own options, and there are general shared options that relate to all plugins. In this case, we want to look into [the snapcraft options relating to the source code][35]. Here we go,
|
||||
|
||||
* _`source-subdir`: pathSnapcraft will checkout the repository or unpack the archive referred to by the`source`keyword into`parts/<part-name>/src/`but it will only copy the specified subdirectory into`parts/<part-name>/build/`_
|
||||
|
||||
We have the appropriate option, let’s update the _**parts:**_
|
||||
|
||||
```
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
source-subdir: src
|
||||
plugin: make
|
||||
```
|
||||
|
||||
And run _**snapcraft**_ again!
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft prime
|
||||
The 'pull' step of 'timg' is out of date:
|
||||
|
||||
The 'source-subdir' part property appears to have changed.
|
||||
|
||||
Please clean that part's 'pull' step in order to continue
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft clean
|
||||
Cleaning up priming area
|
||||
Cleaning up staging area
|
||||
Cleaning up parts directory
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft prime
|
||||
Skipping pull timg (already ran)
|
||||
Preparing to build timg
|
||||
Building timg
|
||||
make -j4
|
||||
g++ `GraphicsMagick++-config --cppflags --cxxflags` -Wall -O3 -fPIC -c -o timg.o timg.cc
|
||||
g++ -Wall -O3 -fPIC -c -o terminal-canvas.o terminal-canvas.cc
|
||||
/bin/sh: 1: GraphicsMagick++-config: not found
|
||||
timg.cc:33:22: fatal error: Magick++.h: No such file or directory
|
||||
compilation terminated.
|
||||
Makefile:10: recipe for target 'timg.o' failed
|
||||
make: *** [timg.o] Error 1
|
||||
make: *** Waiting for unfinished jobs....
|
||||
Command '['/bin/sh', '/tmp/tmpeeyxj5kw', 'make', '-j4']' returned non-zero exit status 2
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
Here it tells us that it cannot find the development library GraphicsMagick++. According to [the Snapcraft common keywords][36], we need to specify this development library so that Snapcraft can install it:
|
||||
|
||||
* `build-packages`: [deb, deb, deb…]A _**list**_ of Ubuntu packages to install on the build host before building the part. The files from these packages typically will not go into the final snap unless they contain libraries that are direct dependencies of binaries within the snap (in which case they’ll be discovered via`ldd`), or they are explicitly described in stage-packages.
|
||||
|
||||
Therefore, let’s find the name of the development package,
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ apt-cache search graphicsmagick++ | grep dev
|
||||
graphicsmagick-libmagick-dev-compat/xenial 1.3.23-1build1 all
|
||||
libgraphicsmagick++1-dev/xenial 1.3.23-1build1 amd64
|
||||
format-independent image processing - C++ development files
|
||||
libgraphicsmagick1-dev/xenial 1.3.23-1build1 amd64
|
||||
format-independent image processing - C development files
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
The package name is a _**lib**_ + _**graphicsmagick++**_ and ends with _**-dev**_ . We got it! Here is the updated _**parts:**_
|
||||
|
||||
```
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
source-subdir: src
|
||||
plugin: make
|
||||
build-packages:
|
||||
- libgraphicsmagick++1-dev
|
||||
```
|
||||
|
||||
Let’s run _**snapcraft prime**_ again!
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft
|
||||
Installing build dependencies: libgraphicsmagick++1-dev
|
||||
[...]
|
||||
The following NEW packages will be installed:
|
||||
libgraphicsmagick++-q16-12 libgraphicsmagick++1-dev libgraphicsmagick-q16-3
|
||||
libgraphicsmagick1-dev libwebp5
|
||||
[...]
|
||||
Building timg
|
||||
make -j4
|
||||
g++ `GraphicsMagick++-config --cppflags --cxxflags` -Wall -O3 -fPIC -c -o timg.o timg.cc
|
||||
g++ -Wall -O3 -fPIC -c -o terminal-canvas.o terminal-canvas.cc
|
||||
g++ -o timg timg.o terminal-canvas.o `GraphicsMagick++-config --ldflags --libs`
|
||||
/usr/bin/ld: cannot find -lwebp
|
||||
collect2: error: ld returned 1 exit status
|
||||
Makefile:7: recipe for target 'timg' failed
|
||||
make: *** [timg] Error 1
|
||||
Command '['/bin/sh', '/tmp/tmptma45jzl', 'make', '-j4']' returned non-zero exit status 2
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
Simply by specifying the development library _**libgraphicsmagick++1-dev**_ , Ubuntu also installs the code libraries, including _**libgraphicsmagick++-q16-12**_ , along with the (dynamic) code library _**libwebp.**_
|
||||
|
||||
We still got an error, and the error is related to the _**webp**_ library. The development version of the _**webp**_ library (a static library) is missing. Let’s find it!
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ apt-cache search libwebp | grep dev
|
||||
libwebp-dev - Lossy compression of digital photographic images.
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
Bingo! The _**libwebp5 package**_ that was installed further up, provides only a dynamic (.so) library. By requiring the _**libwebp-dev**_ package, we get the static (.a) library as well. Let’s update the **parts:** section!
|
||||
|
||||
```
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
source-subdir: src
|
||||
plugin: make
|
||||
build-packages:
|
||||
- libgraphicsmagick++1-dev
|
||||
- libwebp-dev
|
||||
```
|
||||
|
||||
Here is the updated snapcraft.yaml file,
|
||||
|
||||
```
|
||||
name: timg
|
||||
version: '20170226'
|
||||
summary: A terminal image viewer
|
||||
description: |
|
||||
A viewer that uses 24-Bit color capabilities and unicode character blocks
|
||||
to display images in the terminal.
|
||||
|
||||
grade: stable
|
||||
confinement: devmode
|
||||
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
source-subdir: src
|
||||
plugin: make
|
||||
build-packages:
|
||||
- libgraphicsmagick++1-dev
|
||||
- libwebp-dev
|
||||
```
|
||||
|
||||
Let’s run _**snapcraft**_ !
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft prime
|
||||
Skipping pull timg (already ran)
|
||||
Preparing to build timg
|
||||
Building timg
|
||||
make -j4
|
||||
g++ `GraphicsMagick++-config --cppflags --cxxflags` -Wall -O3 -fPIC -c -o timg.o timg.cc
|
||||
g++ -Wall -O3 -fPIC -c -o terminal-canvas.o terminal-canvas.cc
|
||||
g++ -o timg timg.o terminal-canvas.o `GraphicsMagick++-config --ldflags --libs`
|
||||
make install DESTDIR=/home/ubuntu/timg-snap/parts/timg/install
|
||||
install timg /usr/local/bin
|
||||
install: cannot create regular file '/usr/local/bin/timg': Permission denied
|
||||
Makefile:13: recipe for target 'install' failed
|
||||
make: *** [install] Error 1
|
||||
Command '['/bin/sh', '/tmp/tmptq_s1itc', 'make', 'install', 'DESTDIR=/home/ubuntu/timg-snap/parts/timg/install']' returned non-zero exit status 2
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
We have a new problem. The _**Makefile**_ was hand-crafted and does not obey the parameters of [the Snapcraft make plugin][37] to install into the _**prime/**_ directory. The _**Makefile**_ tries to install in _**/usr/local/bin/ **_ !
|
||||
|
||||
We need to instruct [the Snapcraft make plugin][38] not to run _**make install**_ but instead pick the generated executable _**timg**_ and place it into the _**prime/**_ directory. According to the documentation,
|
||||
|
||||
```
|
||||
- artifacts:
|
||||
(list)
|
||||
Link/copy the given files from the make output to the snap
|
||||
installation directory. If specified, the 'make install'
|
||||
step will be skipped.
|
||||
```
|
||||
|
||||
So, we need to put something in _**artifacts:**_ . But what?
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap/parts/timg$ ls build/src/
|
||||
Makefile terminal-canvas.h timg* timg.o
|
||||
terminal-canvas.cc terminal-canvas.o timg.cc
|
||||
ubuntu@snaps:~/timg-snap/parts/timg$
|
||||
```
|
||||
|
||||
In the _**build/**_ subdirectory we can find the _**make**_ output. Since we specified _**source-subdir: src**_ , our base directory for _**artifacts:**_ is _**build/src/**_ . And in there we can find the executable _**timg**_ , which will be the parameter for _**artifacts:**_ . With _**artifacts:**_ we specify the files from the _**make**_ output that will be copied to the snap installation directory (in _**prime/**_ ).
|
||||
|
||||
Here is the updated _**parts:**_ of snapcraft.yaml,
|
||||
|
||||
```
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
source-subdir: src
|
||||
plugin: make
|
||||
build-packages:
|
||||
- libgraphicsmagick++1-dev
|
||||
- libwebp-dev
|
||||
artifacts: [timg]
|
||||
```
|
||||
|
||||
Let’s run _**snapcraft prime**_ !
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft prime
|
||||
Preparing to pull timg
|
||||
Pulling timg
|
||||
Cloning into '/home/ubuntu/timg-snap/parts/timg/src'...
|
||||
remote: Counting objects: 144, done.
|
||||
remote: Total 144 (delta 0), reused 0 (delta 0), pack-reused 144
|
||||
Receiving objects: 100% (144/144), 116.00 KiB | 207.00 KiB/s, done.
|
||||
Resolving deltas: 100% (89/89), done.
|
||||
Checking connectivity... done.
|
||||
Preparing to build timg
|
||||
Building timg
|
||||
make -j4
|
||||
g++ `GraphicsMagick++-config --cppflags --cxxflags` -Wall -O3 -fPIC -c -o timg.o timg.cc
|
||||
g++ -Wall -O3 -fPIC -c -o terminal-canvas.o terminal-canvas.cc
|
||||
g++ -o timg timg.o terminal-canvas.o `GraphicsMagick++-config --ldflags --libs`
|
||||
Staging timg
|
||||
Priming timg
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
We are rolling!
|
||||
|
||||
### Exposing the command
|
||||
|
||||
Up to now, snapcraft generated the executable but did not expose a command for the users to run. We need to expose a command and this is done in the _**apps:**_ section.
|
||||
|
||||
First of all, where is the command located in the _**prime/**_ subdirectory?
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ ls prime/
|
||||
meta/ snap/ timg* usr/
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
It is in the root of the _**prime/**_ subdirectory. We are ready to add the _**apps:**_ section in snapcraft.yaml,
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ cat snap/snapcraft.yaml
|
||||
name: timg
|
||||
version: '20170226'
|
||||
summary: A terminal image viewer
|
||||
description: |
|
||||
A viewer that uses 24-Bit color capabilities and unicode character blocks
|
||||
to display images in the terminal.
|
||||
|
||||
grade: stable
|
||||
confinement: devmode
|
||||
|
||||
apps:
|
||||
timg:
|
||||
command: timg
|
||||
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
source-subdir: src
|
||||
plugin: make
|
||||
build-packages:
|
||||
- libgraphicsmagick++1-dev
|
||||
- libwebp-dev
|
||||
artifacts: [timg]
|
||||
```
|
||||
|
||||
Let’s run _**snapcraft prime**_ again and then try the snap!
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft prime
|
||||
Skipping pull timg (already ran)
|
||||
Skipping build timg (already ran)
|
||||
Skipping stage timg (already ran)
|
||||
Skipping prime timg (already ran)
|
||||
ubuntu@snaps:~/timg-snap$ snap try --devmode prime/
|
||||
timg 20170226 mounted from /home/ubuntu/timg-snap/prime
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
[![](https://i1.wp.com/blog.simos.info/wp-content/uploads/2017/02/timg-car.png?resize=741%2C455&ssl=1)][39]
|
||||
|
||||
Image source: https://www.flickr.com/photos/mustangjoe/6091603784/
|
||||
|
||||
We used _**snap try –devmode prime/**_ as a way to enable the snap and try the command. It is an efficient way for testing and avoids the alternative of generating .snap files, installing them, then uninstalling them. With _**snap try prime/**_ , it uses directly the directory (in this case, _**prime/**_ ) which has the snap content.
|
||||
|
||||
### Restricting the snap
|
||||
|
||||
Up to now, the snap has been running in _**devmode**_ (developer mode), which is unrestricted. Let’s see how it runs in a confinement,
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ snap list
|
||||
Name Version Rev Developer Notes
|
||||
core 16-2 1337 canonical -
|
||||
timg 20170226 x1 devmode,try
|
||||
ubuntu@snaps:~/timg-snap$ snap try --jailmode prime
|
||||
timg 20170226 mounted from /home/ubuntu/timg-snap/prime
|
||||
ubuntu@snaps:~/timg-snap$ snap list
|
||||
Name Version Rev Developer Notes
|
||||
core 16-2 1337 canonical -
|
||||
timg 20170226 x2 jailmode,try
|
||||
ubuntu@snaps:~/timg-snap$ timg pexels-photo-149813.jpeg
|
||||
Trouble loading pexels-photo-149813.jpeg (Magick: Unable to open file (pexels-photo-149813.jpeg) reported by magick/blob.c:2828 (OpenBlob))
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
Here we quickly switch from _**devmode**_ to _**jailmode**_ ( _**confinement: strict**_ ) without having to touch the snapcraft.yaml file. As expected, _**timg**_ could not read the image because we did not permit any access to the filesystem.
|
||||
|
||||
At this stage we need to make a decision. With _**jailmode**_ , we can easily specify that a command has access to the files of the user’s $HOME directory, and only there. If an image file is located elsewhere, we can always copy of the $HOME directory and run _**timg**_ on the copy in $HOME. If we are happy with this, we can set up snapcraft.yaml as follows:
|
||||
|
||||
```
|
||||
name: timg
|
||||
version: '20170226'
|
||||
summary: A terminal image viewer
|
||||
description: |
|
||||
A viewer that uses 24-Bit color capabilities and unicode character blocks
|
||||
to display images in the terminal.
|
||||
|
||||
grade: stable
|
||||
confinement: strict
|
||||
|
||||
apps:
|
||||
timg:
|
||||
command: timg
|
||||
plugs: [home]
|
||||
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
source-subdir: src
|
||||
plugin: make
|
||||
build-packages:
|
||||
- libgraphicsmagick++1-dev
|
||||
- libwebp-dev
|
||||
artifacts: [timg]
|
||||
```
|
||||
|
||||
On the other hand, if we want the _**timg**_ snap to have read-access to all the filesystem, we can use _**confinement: classic**_ and be done with it. Here is how snapcraft.yaml would look in that case,
|
||||
|
||||
```
|
||||
name: timg
|
||||
version: '20170226'
|
||||
summary: A terminal image viewer
|
||||
description: |
|
||||
A viewer that uses 24-Bit color capabilities and unicode character blocks
|
||||
to display images in the terminal.
|
||||
|
||||
grade: stable
|
||||
confinement: classic
|
||||
|
||||
apps:
|
||||
timg:
|
||||
command: timg
|
||||
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
source-subdir: src
|
||||
plugin: make
|
||||
build-packages:
|
||||
- libgraphicsmagick++1-dev
|
||||
- libwebp-dev
|
||||
artifacts: [timg]
|
||||
```
|
||||
|
||||
In the following we are selecting the option of the _**strict**_ confinement. Therefore, images should be in the $HOME only.
|
||||
|
||||
### Packaging and testing
|
||||
|
||||
Let’s package the snap, that is, create the .snap file and try it out on a brand-new installation of Ubuntu!
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft
|
||||
Skipping pull timg (already ran)
|
||||
Skipping build timg (already ran)
|
||||
Skipping stage timg (already ran)
|
||||
Skipping prime timg (already ran)
|
||||
Snapping 'timg' \
|
||||
Snapped timg_20170226_amd64.snap
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
How do we get a brand new installation of Ubuntu in seconds so that we can test the snap?
|
||||
|
||||
See [Trying out LXD containers on our Ubuntu][40] and set up LXD on your system. Then, come back here and try the following commands,
|
||||
|
||||
```
|
||||
$ lxc launch ubuntu:x snaptesting
|
||||
Creating snaptesting
|
||||
Starting snaptesting
|
||||
$ lxc file push timg_20170226_amd64.snap snaptesting/home/ubuntu/
|
||||
$ lxc exec snaptesting -- sudo su - ubuntu
|
||||
To run a command as administrator (user "root"), use "sudo <command>".
|
||||
See "man sudo_root" for details.
|
||||
|
||||
ubuntu@snaptesting:~$ ls
|
||||
timg_20170226_amd64.snap
|
||||
ubuntu@snaptesting:~$ snap install timg_20170226_amd64.snap
|
||||
error: access denied (try with sudo)
|
||||
ubuntu@snaptesting:~$ sudo snap install timg_20170226_amd64.snap
|
||||
error: cannot find signatures with metadata for snap "timg_20170226_amd64.snap"
|
||||
ubuntu@snaptesting:~$ sudo snap install timg_20170226_amd64.snap --dangerous
|
||||
error: cannot perform the following tasks:
|
||||
- Mount snap "core" (1337) ([start snap-core-1337.mount] failed with exit status 1: Job for snap-core-1337.mount failed. See "systemctl status snap-core-1337.mount" and "journalctl -xe" for details.
|
||||
)
|
||||
ubuntu@snaptesting:~$ sudo apt install squashfuse
|
||||
[...]
|
||||
Setting up squashfuse (0.1.100-0ubuntu1~ubuntu16.04.1) ...
|
||||
ubuntu@snaptesting:~$ sudo snap install timg_20170226_amd64.snap --dangerous
|
||||
timg 20170226 installed
|
||||
ubuntu@snaptesting:~$ wget https://farm7.staticflickr.com/6187/6091603784_d6960c8be2_z_d.jpg
|
||||
[...]
|
||||
2017-02-26 22:12:18 (636 KB/s) - ‘6091603784_d6960c8be2_z_d.jpg’ saved [240886/240886]
|
||||
ubuntu@snaptesting:~$ timg 6091603784_d6960c8be2_z_d.jpg
|
||||
[it worked!]
|
||||
ubuntu@snaptesting:~$
|
||||
```
|
||||
|
||||
So, we launched an LXD container called _**snaptesting**_ , then copied in there the .snap file. Then, we connected to the container as a normal user and tried to install the snap. Initially, the installation failed because we need _**sudo**_ when we install snaps in unprivileged LXD containers. It again failed because the .snap was unsigned (we need the _**–dangerous**_ parameter). Then, it further failed because we need to install the _**squashfuse**_ package (not preinstalled in the Ubuntu 16.04 images). Eventually, the snap was installed and we managed to view the image.
|
||||
|
||||
It is important to test a snap in a brand new installation of Linux in order to make sure whether we need to _**stage**_ any code library inside the snap. In this case, static libraries were used and all went well!
|
||||
|
||||
### Publishing to the Ubuntu Store
|
||||
|
||||
Here are [the instructions to publish a snap to the Ubuntu Store][41]. We have already published a few snaps in the previous tutorials. For _**timg**_ , we got _**confinement: strict**_ , and _**grade: stable**_ . We are therefore publishing in the stable channel.
|
||||
|
||||
```
|
||||
$ snapcraft push timg_20170226_amd64.snap
|
||||
Pushing 'timg_20170226_amd64.snap' to the store.
|
||||
Uploading timg_20170226_amd64.snap [ ] 0%
|
||||
Uploading timg_20170226_amd64.snap [=======================================] 100%
|
||||
Ready to release!|
|
||||
Revision 6 of 'timg' created.
|
||||
$ snapcraft release timg 6 stable
|
||||
Track Arch Series Channel Version Revision
|
||||
latest amd64 16 stable 20170226 6
|
||||
candidate ^ ^
|
||||
beta 0.9.5 5
|
||||
edge 0.9.5 5
|
||||
The 'stable' channel is now open.
|
||||
```
|
||||
|
||||
We pushed the .snap file to the Ubuntu Store and we got a _**revision number 6**_ . Then, we released the _**timg revision 6**_ to the _**stable**_ channel of the Ubuntu Store.
|
||||
|
||||
There was no released snap in the _**candidate**_ channel, therefore, it inherits the package from the _**stable**_ channel. Hence, the _**^**_ characters.
|
||||
|
||||
In previous tests I uploaded some older versions of the snap to the _**beta**_ and _**edge**_ channels. These older versions refer to the old _**tag 0.9.****5**_ of the _**timg**_ source code.
|
||||
|
||||
Let’s knock down the old 0.9.5 by releasing the stable version to the _**beta**_ and _**edge**_ channels as well.
|
||||
|
||||
```
|
||||
$ snapcraft release timg 6 beta
|
||||
Track Arch Series Channel Version Revision
|
||||
latest amd64 16 stable 20170226 6
|
||||
candidate ^ ^
|
||||
beta 20170226 6
|
||||
edge 0.9.5 5
|
||||
$ snapcraft release timg 6 edge
|
||||
Track Arch Series Channel Version Revision
|
||||
latest amd64 16 stable 20170226 6
|
||||
candidate ^ ^
|
||||
beta 20170226 6
|
||||
edge 20170226 6
|
||||
```
|
||||
|
||||
### Playing with timg
|
||||
|
||||
Let’s run timg without parameters,
|
||||
|
||||
```
|
||||
ubuntu@snaptesting:~$ timg
|
||||
Expected image filename.
|
||||
usage: /snap/timg/x1/timg [options] <image> [<image>...]
|
||||
Options:
|
||||
-g<w>x<h> : Output pixel geometry. Default from terminal 80x48
|
||||
-s[<ms>] : Scroll horizontally (optionally: delay ms (60)).
|
||||
-d<dx:dy> : delta x and delta y when scrolling (default: 1:0).
|
||||
-w<seconds>: If multiple images given: Wait time between (default: 0.0).
|
||||
-t<seconds>: Only animation or scrolling: stop after this time.
|
||||
-c<num> : Only Animation or scrolling: number of runs through a full cycle.
|
||||
-C : Clear screen before showing image.
|
||||
-F : Print filename before showing picture.
|
||||
-v : Print version and exit.
|
||||
If both -c and -t are given, whatever comes first stops.
|
||||
If both -w and -t are given for some animation/scroll, -t takes precedence
|
||||
ubuntu@snaptesting:~$
|
||||
```
|
||||
|
||||
Here it says that for the current zoom level of our terminal emulator, our resolution is a mere 80×48.
|
||||
|
||||
Let’s zoom out a bit and maximize the GNOME Terminal window.
|
||||
|
||||
```
|
||||
-g<w>x<h> : Output pixel geometry. Default from terminal 635x428
|
||||
```
|
||||
|
||||
It is a better resolution but I can hardly see the characters because they are too small. Let’s invoke the old command to show this car again.
|
||||
|
||||
[![](https://i1.wp.com/blog.simos.info/wp-content/uploads/2017/02/timg-zoomout-max.png?resize=449%2C304&ssl=1)][42]
|
||||
|
||||
What you are seeing is the resized image (from 1080p). Looks great, even if it is made of colored text characters!
|
||||
|
||||
What next? _**timg**_ can play animated gifs as well!
|
||||
|
||||
```
|
||||
$ wget https://m.popkey.co/9b7141/QbAV_f-maxage-0.gif -O JonahHillAmazed.gif$ timg JonahHillAmazed.gif
|
||||
```
|
||||
|
||||
Try to install the _**timg**_ snap yourself in order to experience the animated gif! Failing that, watch the asciinema recording (if the video looks choppy, run it a second time), [https://asciinema.org/a/dezbe2gpye84e0pjndp8t0pvh][43]
|
||||
|
||||
Thanks for reading!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://blog.simos.info/how-to-create-a-snap-for-timg-with-snapcraft-on-ubuntu/
|
||||
|
||||
作者:[Mi blog lah ][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://blog.simos.info/
|
||||
[1]:https://blog.simos.info/category/general/
|
||||
[2]:https://blog.simos.info/category/open-source/linux/
|
||||
[3]:https://blog.simos.info/category/open-source/
|
||||
[4]:https://blog.simos.info/category/planet-ubuntu/
|
||||
[5]:https://blog.simos.info/category/ubuntu/
|
||||
[6]:https://blog.simos.info/category/ubuntu-gr-2/
|
||||
[7]:https://blog.simos.info/how-to-create-a-snap-for-timg-with-snapcraft-on-ubuntu/timg-git-url/
|
||||
[8]:https://blog.simos.info/how-to-create-a-snap-for-timg-with-snapcraft-on-ubuntu/timg-makefile/
|
||||
[9]:https://github.com/hzeller/timg
|
||||
[10]:https://snapcraft.io/docs/reference/plugins/make
|
||||
[11]:https://snapcraft.io/docs/reference/confinement
|
||||
[12]:https://snapcraft.io/docs/reference/confinement
|
||||
[13]:https://github.com/hzeller/timg
|
||||
[14]:https://github.com/hzeller/timg
|
||||
[15]:https://tutorials.ubuntu.com/tutorial/create-first-snap
|
||||
[16]:https://github.com/hzeller/timg
|
||||
[17]:https://i1.wp.com/blog.simos.info/wp-content/uploads/2017/02/linux-terminal-standard-colors.png?ssl=1
|
||||
[18]:https://i1.wp.com/blog.simos.info/wp-content/uploads/2017/02/gnome-terminal-true-color.png?ssl=1
|
||||
[19]:https://gist.github.com/XVilka/8346728
|
||||
[20]:https://en.wikipedia.org/wiki/Escape_sequence
|
||||
[21]:https://github.com/hzeller/timg
|
||||
[22]:https://i0.wp.com/blog.simos.info/wp-content/uploads/2017/02/timg-ubuntu-logo.png?ssl=1
|
||||
[23]:http://design.ubuntu.com/wp-content/uploads/ubuntu-logo112.png
|
||||
[24]:https://i0.wp.com/blog.simos.info/wp-content/uploads/2017/02/timg-flower.png?ssl=1
|
||||
[25]:https://www.flickr.com/photos/doug88888/5776072628/in/photolist-9WCiNQ-7U3Trc-7YUZBL-5DwkEQ-6e1iT8-a372aS-5F75aL-a1gbow-6eNayj-8gWK2H-5CtH7P-6jVqZv-86RpwN-a2nEnB-aiRmsc-6aKvwK-8hmXrN-5CWDNP-62hWM8-a9smn1-ahQqHw-a22p3w-a36csK-ahN4Pv-7VEmnt-ahMSiT-9NpTa7-5A3Pon-ai7DL7-9TKCqV-ahr7gN-a1boqP-83ZzpH-9Sqjmq-5xujdi-7UmDVb-6J2zQR-5wAGNR-5eERar-5KVDym-5dL8SZ-5S2Uut-7RVyHg-9Z6MAt-aiRiT4-5tLesw-aGLSv6-5ftp6j-5wAVBq-5T2KAP
|
||||
[26]:https://github.com/hzeller/timg
|
||||
[27]:https://github.com/hzeller/timg
|
||||
[28]:https://github.com/hzeller/timg
|
||||
[29]:https://github.com/hzeller/timg
|
||||
[30]:https://i1.wp.com/blog.simos.info/wp-content/uploads/2017/02/timg-install-from-source.png?ssl=1
|
||||
[31]:https://i1.wp.com/blog.simos.info/wp-content/uploads/2017/02/timg-tag-095.png?ssl=1
|
||||
[32]:https://i1.wp.com/blog.simos.info/wp-content/uploads/2017/02/timg-master-commits.png?ssl=1
|
||||
[33]:https://snapcraft.io/docs/reference/plugins/make
|
||||
[34]:https://snapcraft.io/docs/reference/plugins/
|
||||
[35]:https://snapcraft.io/docs/reference/plugins/source
|
||||
[36]:https://snapcraft.io/docs/reference/plugins/common
|
||||
[37]:https://snapcraft.io/docs/reference/plugins/make
|
||||
[38]:https://snapcraft.io/docs/reference/plugins/make
|
||||
[39]:https://i1.wp.com/blog.simos.info/wp-content/uploads/2017/02/timg-car.png?ssl=1
|
||||
[40]:https://blog.simos.info/trying-out-lxd-containers-on-our-ubuntu/
|
||||
[41]:https://snapcraft.io/docs/build-snaps/publish
|
||||
[42]:https://i1.wp.com/blog.simos.info/wp-content/uploads/2017/02/timg-zoomout-max.png?ssl=1
|
||||
[43]:https://asciinema.org/a/dezbe2gpye84e0pjndp8t0pvh
|
@ -0,0 +1,787 @@
|
||||
详解 Ubuntu snap 包的制作过程
|
||||
============================================================
|
||||
|
||||
|
||||
> 如果你看过译者以前翻译的 snappy 文章,不知有没有感觉相关主题都是浅尝辄止,讲得不够透彻,看得也不太过瘾?如果有的话,相信这篇详细讲解如何从零开始制作一个 snap 包的文章应该不会让你失望。
|
||||
|
||||
在这篇文章中,我们将看到如何为名为 [timg][1] 的实用程序制作对应的 snap 包。如果这是你第一次听说 snap 安装包,你可以先看看 [如何创建你的第一个 snap 包][2]。
|
||||
|
||||
今天我们将学习以下有关使用 snapcraft 制作 snap 包的内容:
|
||||
|
||||
* [timg][3] 源码中的 Makefile 文件是手工编写,我们需要修改一些 [make 插件参数][4]。
|
||||
|
||||
* 这个程序是用 C++ 语言写的,依赖几个额外的库文件。我们需要把相关的代码添加到 snap 包中。
|
||||
|
||||
* 严格限制还是传统限制?我们将会讨论如何在它们之间进行选择。
|
||||
|
||||
首先,我们了解下 [timg][5] 有什么用?
|
||||
|
||||
### 背景
|
||||
|
||||
Linux 终端模拟器已经变得非常炫酷,并且还能显示颜色!
|
||||
|
||||
![1.png-19.9kB][6]
|
||||
|
||||
除了标准的颜色,大多数终端模拟器都支持真彩色(1600 万种颜色)。
|
||||
|
||||
![图片.png-61.9kB][7]
|
||||
|
||||
是的!终端模拟器已经支持真彩色了!从这个页面 [多个终端和终端应用程序已经支持真彩色(1600 万种颜色)][8] 可以获取 AWK 代码进行测试。你可以看到在代码中使用了一些 [转义序列][9] 来指定 RGB 的值(256 * 256 * 256 ~= 1600 万种颜色)。
|
||||
|
||||
### timg 是什么?
|
||||
|
||||
好了,言归正传,[timg][10] 有什么用?它能将输入的图片重新调整为终端窗口字符能显示范围的大小(比如:80 x 25),然后在终端窗口的任何分辨率用彩色字符显示图像。
|
||||
|
||||
![图片.png-37.3kB][11]
|
||||
|
||||
这幅图用彩色块字符显示了 [Ubuntu 的 logo][12],原图是一个 PNG 格式的文件。
|
||||
|
||||
![图片.png-165kB][13]
|
||||
|
||||
这是 [@Doug8888 拍摄的花][14]。
|
||||
|
||||
如果你通过远程连接服务器来管理自己的业务,并希望查看图像文件,那么 [timg][15] 将会特别有用。
|
||||
|
||||
除了静态图片,[timg][16] 同样也可以显示 gif 动图。让我们开始 snap 之旅吧!
|
||||
|
||||
### 熟悉 timg 的源码
|
||||
|
||||
[timg][17] 的源码可以在 [这里][18] 找到。让我们试着手动编译它,以了解它有什么需求。
|
||||
|
||||
![图片.png-128.4kB][19]
|
||||
|
||||
`Makefile` 在 `src/` 子文件夹中而不是项目的根文件夹中。在 github 页面上,他们说需要安装这两个开发包(GraphicsMagic++ 和 WebP),然后使用 `make` 就能生成可执行文件。在截图中可以看到我已经将它们安装好了(在我读完相关的 Readme.md 文件后)。
|
||||
|
||||
|
||||
因此,在编写 snapcraft.yaml 文件时已经有了四条腹稿:
|
||||
|
||||
1. Makefile 在 src/ 子文件夹中而不是项目的根文件夹中。
|
||||
|
||||
2. 这个程序编译时需要两个开发库。
|
||||
|
||||
3. 为了让 timg 以 snap 包形式运行,我们需要将这两个库捆绑在 snap 包中(或者静态链接它们)。
|
||||
|
||||
4. [timg][20] 是用 C++ 编写的,所以需要安装 g++。在编译之前,让我们通过 snapcraft.yaml 文件来检查 `build-essential` 元包是否已经安装。
|
||||
|
||||
### 从 snapcraft 开始
|
||||
|
||||
让我们新建一个名为 `timg-snap/` 的文件夹,并在其中运行 `snapcraft init` 这条命令来创建 `snapcraft.yaml` 工作的框架。
|
||||
|
||||
```
|
||||
ubuntu@snaps:~$ mkdir timg-snap
|
||||
ubuntu@snaps:~$ cd timg-snap/
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft init
|
||||
Created snap/snapcraft.yaml.
|
||||
Edit the file to your liking or run `snapcraft` to get started
|
||||
ubuntu@snaps:~/timg-snap$ cat snap/snapcraft.yaml
|
||||
name: my-snap-name # you probably want to 'snapcraft register <name>'
|
||||
version: '0.1' # just for humans, typically '1.2+git' or '1.3.2'
|
||||
summary: Single-line elevator pitch for your amazing snap # 79 char long summary
|
||||
description: |
|
||||
This is my-snap's description. You have a paragraph or two to tell the most important story about your snap. Keep it under 100 words though, we live in tweetspace and your description wants to look good in the snap store.
|
||||
|
||||
grade: devel # must be 'stable' to release into candidate/stable channels
|
||||
confinement: devmode # use 'strict' once you have the right plugs and slots
|
||||
|
||||
parts:
|
||||
my-part:
|
||||
# See 'snapcraft plugins'
|
||||
plugin: nil
|
||||
```
|
||||
|
||||
### 填充元数据
|
||||
|
||||
snapcraft.yaml 配置文件的上半部分是元数据。我们需要一个一个把它们填满,这算是比较容易的部分。元数据由以下字段组成:
|
||||
|
||||
1. `名字` —— snap 包的名字,它将公开在 Ubuntu 商店中。
|
||||
|
||||
2. `版本` —— snap 包的版本号。可以是源代码存储库中一个适当的分支或者标记,如果没有分支或标记的话,也可以是当前日期。
|
||||
|
||||
3. `摘要` —— 不超过 80 个字符的简短描述。
|
||||
|
||||
4. `描述` —— 长一点的描述, 100 个字以下.
|
||||
|
||||
5. `等级` —— 稳定或者开发。因为我们想要在 Ubuntu 商店的稳定通道中发布这个 snap 包,所以在 snap 包能正常工作后,就把它设置成稳定。
|
||||
|
||||
6. `限制` —— 我们首先设置为开发模式,这样系统将不会以任何方式限制 snap 包。一旦它在开发模式 下能正常工作,我们再考虑选择严格还是传统限制。
|
||||
|
||||
我们将使用 `timg` 这个名字:
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft register timg
|
||||
Registering timg.
|
||||
You already own the name 'timg'.
|
||||
```
|
||||
|
||||
是的,这个名字我已经注册了 :-)。
|
||||
|
||||
接下来,我们应该选择哪个版本的 timg?
|
||||
|
||||
![图片.png-72.7kB][21]
|
||||
|
||||
当在仓库中寻找分支或标记时,我们会发现有一个 v0.9.5 标签,其中有 2016 年 6 月 27 日最新提交的代码。
|
||||
|
||||
![图片.png-71.4kB][22]
|
||||
|
||||
然而主分支(`master`)中有两个看起来很重要的提交。因此我们使用主分支而不用 `v0.9.5` 标签的那个。我们使用今天的日期—— `20170226` 做为版本号。
|
||||
|
||||
我们从仓库中搜集了摘要和描述。其中摘要的内容为 `一个终端图像查看器`,描述的内容为 `一个能用 24 位颜色和 unicode 字符块来在终端中显示图像的查看器`。
|
||||
|
||||
最后,将等级设置为稳定,将限制设置为开发模式(一直到 snap 包真正起作用)。
|
||||
|
||||
这是更新后的 snapcraft.yaml,带有所有的元数据:
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ cat snap/snapcraft.yaml
|
||||
name: timg
|
||||
version: '20170226'
|
||||
summary: A terminal image viewer
|
||||
description: |
|
||||
A viewer that uses 24-Bit color capabilities and unicode character blocks to display images in the terminal.
|
||||
|
||||
grade: stable
|
||||
confinement: devmode
|
||||
|
||||
parts:
|
||||
my-part:
|
||||
# See 'snapcraft plugins'
|
||||
plugin: nil
|
||||
```
|
||||
|
||||
### 弄清楚 "parts:" 是什么
|
||||
|
||||
现在我们需要将上面已经存在的 `parts:` 部分替换成真实的 `parts:`。
|
||||
|
||||
![timg-git-url.png-8kB][23]
|
||||
|
||||
<dd> Git 仓库的 URL。 </dd>
|
||||
|
||||
![图片.png-28.7kB][24]
|
||||
|
||||
<dd> 存在 Makefile,因此我们需要 make 插件。</dd>
|
||||
|
||||
(这两张图在原文中是并排显示的,在 markdown 中不知道怎么设置。。)
|
||||
|
||||
|
||||
我们已经知道 git 仓库的 URL 链接,并且 timg 源码中存在 Makefile 文件。至于 [snapcraft make 插件][25] 的 Makefile 命令,正如文档所言,这个插件总是会运行 `make` 后再运行 `make install`。为了确认 `make` 插件的用法,我查看了 [snapcraft 可用插件列表][26]。
|
||||
|
||||
因此,我们将最初的配置:
|
||||
|
||||
```
|
||||
parts:
|
||||
my-part:
|
||||
# See 'snapcraft plugins'
|
||||
plugin: nil
|
||||
```
|
||||
|
||||
修改为:
|
||||
|
||||
```
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
plugin: make
|
||||
```
|
||||
|
||||
这是当前 snapcraft.yaml 文件的内容:
|
||||
|
||||
```
|
||||
name: timg
|
||||
version: '20170226'
|
||||
summary: A terminal image viewer
|
||||
description: |
|
||||
A viewer that uses 24-Bit color capabilities and unicode character blocks
|
||||
to display images in the terminal.
|
||||
|
||||
grade: stable
|
||||
confinement: devmode
|
||||
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
plugin: make
|
||||
```
|
||||
|
||||
让我们运行下 `snapcraft prime` 命令看看会发生什么:
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft prime
|
||||
Preparing to pull timg
|
||||
Pulling timg
|
||||
Cloning into '/home/ubuntu/timg-snap/parts/timg/src'...
|
||||
remote: Counting objects: 144, done.
|
||||
remote: Total 144 (delta 0), reused 0 (delta 0), pack-reused 144
|
||||
Receiving objects: 100% (144/144), 116.00 KiB | 0 bytes/s, done.
|
||||
Resolving deltas: 100% (89/89), done.
|
||||
Checking connectivity... done.
|
||||
Preparing to build timg
|
||||
Building timg
|
||||
make -j4
|
||||
make: *** No targets specified and no makefile found. Stop.
|
||||
Command '['/bin/sh', '/tmp/tmpem97fh9d', 'make', '-j4']' returned non-zero exit status 2
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
我们可以看到 `snapcraft` 无法在源代码中找到 `Makefile` 文件,正如我们之前所暗示的,`Makefile` 只位于 `src/` 子文件夹中。那么,我们可以让 `snapcraft` 使用 `src/` 文件夹中的 `Makefile` 文件吗?
|
||||
|
||||
每个 snapcraft 插件都有自己的选项,并且有一些通用选项是所有插件共享的。在本例中,我们希望研究那些[与源代码相关的 snapcraft 选项][27]。我们开始吧:
|
||||
|
||||
* source-subdir:path
|
||||
|
||||
snapcraft 会检出(checkout) `source` 关键字所引用的仓库或者解压归档文件到 `parts/<part-name>/src/` 中,但是它只会将特定的子目录复制到 `parts/<part-name>/build/` 中。
|
||||
|
||||
我们已经有了适当的选项,下面更新下 `parts`:
|
||||
|
||||
```
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
source-subdir: src
|
||||
plugin: make
|
||||
```
|
||||
|
||||
然后再次运行 snapcraft prime:
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft prime
|
||||
The 'pull' step of 'timg' is out of date:
|
||||
|
||||
The 'source-subdir' part property appears to have changed.
|
||||
|
||||
Please clean that part's 'pull' step in order to continue
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft clean
|
||||
Cleaning up priming area
|
||||
Cleaning up staging area
|
||||
Cleaning up parts directory
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft prime
|
||||
Skipping pull timg (already ran)
|
||||
Preparing to build timg
|
||||
Building timg
|
||||
make -j4
|
||||
g++ `GraphicsMagick++-config --cppflags --cxxflags` -Wall -O3 -fPIC -c -o timg.o timg.cc
|
||||
g++ -Wall -O3 -fPIC -c -o terminal-canvas.o terminal-canvas.cc
|
||||
/bin/sh: 1: GraphicsMagick++-config: not found
|
||||
timg.cc:33:22: fatal error: Magick++.h: No such file or directory
|
||||
compilation terminated.
|
||||
Makefile:10: recipe for target 'timg.o' failed
|
||||
make: *** [timg.o] Error 1
|
||||
make: *** Waiting for unfinished jobs....
|
||||
Command '['/bin/sh', '/tmp/tmpeeyxj5kw', 'make', '-j4']' returned non-zero exit status 2
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
从错误信息我们可以得知 snapcraft 找不到 GraphicsMagick++ 这个开发库文件。根据 [snapcraft 常见关键字][29] 可知,我们需要在 snapcraft.yaml 中指定这个库文件,这样 snapcraft 才能安装它。
|
||||
|
||||
* `build-packages`:[deb, deb, deb…]
|
||||
|
||||
构建 part 前需要在主机中安装的 Ubuntu 包列表。这些包通常不会进入最终的 snap 包中,除非它们含有 snap 包中二进制文件直接依赖的库文件(在这种情况下,可以通过 `ldd` 发现他们),或者在 `stage-package` 中显式地指定了它们。
|
||||
|
||||
让我们寻找下这个开发包的名字:
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ apt-cache search graphicsmagick++ | grep dev
|
||||
graphicsmagick-libmagick-dev-compat/xenial 1.3.23-1build1 all
|
||||
libgraphicsmagick++1-dev/xenial 1.3.23-1build1 amd64
|
||||
format-independent image processing - C++ development files
|
||||
libgraphicsmagick1-dev/xenial 1.3.23-1build1 amd64
|
||||
format-independent image processing - C development files
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
可以看到包名为 `libgraphicsmagick++1-dev`,下面是更新后的 `parts`:
|
||||
|
||||
```
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
source-subdir: src
|
||||
plugin: make
|
||||
build-packages:
|
||||
- libgraphicsmagick++1-dev
|
||||
```
|
||||
|
||||
再次运行 `snapcraft`:
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft
|
||||
Installing build dependencies: libgraphicsmagick++1-dev
|
||||
[...]
|
||||
The following NEW packages will be installed:
|
||||
libgraphicsmagick++-q16-12 libgraphicsmagick++1-dev libgraphicsmagick-q16-3
|
||||
libgraphicsmagick1-dev libwebp5
|
||||
[...]
|
||||
Building timg
|
||||
make -j4
|
||||
g++ `GraphicsMagick++-config --cppflags --cxxflags` -Wall -O3 -fPIC -c -o timg.o timg.cc
|
||||
g++ -Wall -O3 -fPIC -c -o terminal-canvas.o terminal-canvas.cc
|
||||
g++ -o timg timg.o terminal-canvas.o `GraphicsMagick++-config --ldflags --libs`
|
||||
/usr/bin/ld: cannot find -lwebp
|
||||
collect2: error: ld returned 1 exit status
|
||||
Makefile:7: recipe for target 'timg' failed
|
||||
make: *** [timg] Error 1
|
||||
Command '['/bin/sh', '/tmp/tmptma45jzl', 'make', '-j4']' returned non-zero exit status 2
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
虽然只指定了开发库 `libgraphicsmagick+1-dev`,但 Ubuntu 还安装了一些代码库,包括 `libgraphicsmagick ++-q16-12`,以及动态代码库 `libwebp`。
|
||||
|
||||
这里仍然有一个错误,这个是因为缺少开发版本的 `webp` 库(一个静态库)。我们可以通过下面的命令找到它:
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ apt-cache search libwebp | grep dev
|
||||
libwebp-dev - Lossy compression of digital photographic images.
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
上面安装的 `libwebp5` 包只提供了一个动态库(.so)。通过 `libwebp-dev` 包,我们可以得到相应的静态库(.a)。好了,让我们更新下 `parts:` 部分:
|
||||
|
||||
```
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
source-subdir: src
|
||||
plugin: make
|
||||
build-packages:
|
||||
- libgraphicsmagick++1-dev
|
||||
- libwebp-dev
|
||||
```
|
||||
|
||||
下面是更新后 snapcraft.yaml 文件的内容:
|
||||
|
||||
```
|
||||
name: timg
|
||||
version: '20170226'
|
||||
summary: A terminal image viewer
|
||||
description: |
|
||||
A viewer that uses 24-Bit color capabilities and unicode character blocks
|
||||
to display images in the terminal.
|
||||
|
||||
grade: stable
|
||||
confinement: devmode
|
||||
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
source-subdir: src
|
||||
plugin: make
|
||||
build-packages:
|
||||
- libgraphicsmagick++1-dev
|
||||
- libwebp-dev
|
||||
```
|
||||
|
||||
让我们运行下 `snapcraft prime`:
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft prime
|
||||
Skipping pull timg (already ran)
|
||||
Preparing to build timg
|
||||
Building timg
|
||||
make -j4
|
||||
g++ `GraphicsMagick++-config --cppflags --cxxflags` -Wall -O3 -fPIC -c -o timg.o timg.cc
|
||||
g++ -Wall -O3 -fPIC -c -o terminal-canvas.o terminal-canvas.cc
|
||||
g++ -o timg timg.o terminal-canvas.o `GraphicsMagick++-config --ldflags --libs`
|
||||
make install DESTDIR=/home/ubuntu/timg-snap/parts/timg/install
|
||||
install timg /usr/local/bin
|
||||
install: cannot create regular file '/usr/local/bin/timg': Permission denied
|
||||
Makefile:13: recipe for target 'install' failed
|
||||
make: *** [install] Error 1
|
||||
Command '['/bin/sh', '/tmp/tmptq_s1itc', 'make', 'install', 'DESTDIR=/home/ubuntu/timg-snap/parts/timg/install']' returned non-zero exit status 2
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
我们遇到了一个新问题。由于 `Makefile` 文件是手工编写的,不符合 [snapcraft make 插件][30] 的参数设置,所以不能正确安装到 `prime/` 文件夹中。`Makefile` 会尝试安装到 `usr/local/bin` 中。
|
||||
|
||||
我们需要告诉 [snapcraft make 插件][31] 不要运行 `make install`,而是找到 `timg` 可执行文件然后把它放到 `prime/` 文件夹中。根据文档的描述:
|
||||
|
||||
```
|
||||
- artifacts:
|
||||
(列表)
|
||||
将 make 生成的指定文件复制或者链接到 snap 包安装目录。如果使用,则 `make install` 这步操作将被忽略。
|
||||
```
|
||||
|
||||
所以,我们需要将一些东西放到 `artifacts:` 中。但是具体是哪些东西?
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap/parts/timg$ ls build/src/
|
||||
Makefile terminal-canvas.h timg* timg.o
|
||||
terminal-canvas.cc terminal-canvas.o timg.cc
|
||||
ubuntu@snaps:~/timg-snap/parts/timg$
|
||||
```
|
||||
|
||||
在 `build/` 子目录中,我们可以找到 `make` 的输出结果。由于我们设置了 `source-subdir:` 为 `src`,所以 `artifacts:` 的基目录为 `build/src`。在这里我们可以找到可执行文件 `timg`,我们需要将它设置为 `artifacts:` 的一个参数:。通过 `artifacts:`,我们可以把 `make` 输出的某些文件复制到 snap 包的安装目录(在 `prime/` 中)。
|
||||
|
||||
下面是更新后 snapcraft.yaml 文件 parts: 部分的内容:
|
||||
|
||||
```
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
source-subdir: src
|
||||
plugin: make
|
||||
build-packages:
|
||||
- libgraphicsmagick++1-dev
|
||||
- libwebp-dev
|
||||
artifacts: [timg]
|
||||
```
|
||||
让我们运行 `snapcraft prime`:
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft prime
|
||||
Preparing to pull timg
|
||||
Pulling timg
|
||||
Cloning into '/home/ubuntu/timg-snap/parts/timg/src'...
|
||||
remote: Counting objects: 144, done.
|
||||
remote: Total 144 (delta 0), reused 0 (delta 0), pack-reused 144
|
||||
Receiving objects: 100% (144/144), 116.00 KiB | 207.00 KiB/s, done.
|
||||
Resolving deltas: 100% (89/89), done.
|
||||
Checking connectivity... done.
|
||||
Preparing to build timg
|
||||
Building timg
|
||||
make -j4
|
||||
g++ `GraphicsMagick++-config --cppflags --cxxflags` -Wall -O3 -fPIC -c -o timg.o timg.cc
|
||||
g++ -Wall -O3 -fPIC -c -o terminal-canvas.o terminal-canvas.cc
|
||||
g++ -o timg timg.o terminal-canvas.o `GraphicsMagick++-config --ldflags --libs`
|
||||
Staging timg
|
||||
Priming timg
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
我们还将继续迭代。
|
||||
|
||||
### 导出命令
|
||||
|
||||
到目前为止,snapcraft 生成了可执行文件,但没有导出给用户使用的命令。接下来我们需要通过 `apps:` 导出一个命令。
|
||||
|
||||
首先我们需要知道命令在 `prime/` 的哪个子文件夹中:
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ ls prime/
|
||||
meta/ snap/ timg* usr/
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
它在 `prime/` 子文件夹的根目录中。现在,我们已经准备好要在 snapcaft.yaml 中增加 `apps:` 的内容:
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ cat snap/snapcraft.yaml
|
||||
name: timg
|
||||
version: '20170226'
|
||||
summary: A terminal image viewer
|
||||
description: |
|
||||
A viewer that uses 24-Bit color capabilities and unicode character blocks
|
||||
to display images in the terminal.
|
||||
|
||||
grade: stable
|
||||
confinement: devmode
|
||||
|
||||
apps:
|
||||
timg:
|
||||
command: timg
|
||||
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
source-subdir: src
|
||||
plugin: make
|
||||
build-packages:
|
||||
- libgraphicsmagick++1-dev
|
||||
- libwebp-dev
|
||||
artifacts: [timg]
|
||||
```
|
||||
|
||||
让我们再次运行 `snapcraft prime`,然后测试下生成的 snap 包:
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft prime
|
||||
Skipping pull timg (already ran)
|
||||
Skipping build timg (already ran)
|
||||
Skipping stage timg (already ran)
|
||||
Skipping prime timg (already ran)
|
||||
ubuntu@snaps:~/timg-snap$ snap try --devmode prime/
|
||||
timg 20170226 mounted from /home/ubuntu/timg-snap/prime
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
![图片.png-42.3kB][32]
|
||||
|
||||
图片来源: https://www.flickr.com/photos/mustangjoe/6091603784/
|
||||
|
||||
我们可以通过 `snap try --devmode prime/ ` 使能 snap 包然后测试 timg 命令。这是一种高效的测试方法,可以避免生成 .snap 文件,并且无需安装和卸载它们,因为 `snap try prime/` 直接使用了 `prime/` 文件夹中的内容。
|
||||
|
||||
### 限制 snap
|
||||
|
||||
到目前为止,snap 包一直是在不受限制的开发模式下运行的。让我们看看如何限制它的运行:
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ snap list
|
||||
Name Version Rev Developer Notes
|
||||
core 16-2 1337 canonical -
|
||||
timg 20170226 x1 devmode,try
|
||||
ubuntu@snaps:~/timg-snap$ snap try --jailmode prime
|
||||
timg 20170226 mounted from /home/ubuntu/timg-snap/prime
|
||||
ubuntu@snaps:~/timg-snap$ snap list
|
||||
Name Version Rev Developer Notes
|
||||
core 16-2 1337 canonical -
|
||||
timg 20170226 x2 jailmode,try
|
||||
ubuntu@snaps:~/timg-snap$ timg pexels-photo-149813.jpeg
|
||||
Trouble loading pexels-photo-149813.jpeg (Magick: Unable to open file (pexels-photo-149813.jpeg) reported by magick/blob.c:2828 (OpenBlob))
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
通过这种方式,我们可以无需修改 snapcraft.yaml 文件就从开发模式切换到限制模式(confinement: strict)。正如预期的那样,timg 无法读取图像,因为我们没有开放访问文件系统的权限。
|
||||
|
||||
现在,我们需要作出决定。使用限制模式,我们可以很容易授予某个命令访问用户 `$HOME` 目录中文件的权限,但是只能访问那里。如果图像文件位于其它地方,我们总是需要复制到 `$HOME` 目录并在 `$HOME` 的副本上运行 timg。如果我们对此感到满意,那我们可以设置 snapcraf.yaml 为:
|
||||
|
||||
```
|
||||
name: timg
|
||||
version: '20170226'
|
||||
summary: A terminal image viewer
|
||||
description: |
|
||||
A viewer that uses 24-Bit color capabilities and unicode character blocks
|
||||
to display images in the terminal.
|
||||
|
||||
grade: stable
|
||||
confinement: strict
|
||||
|
||||
apps:
|
||||
timg:
|
||||
command: timg
|
||||
plugs: [home]
|
||||
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
source-subdir: src
|
||||
plugin: make
|
||||
build-packages:
|
||||
- libgraphicsmagick++1-dev
|
||||
- libwebp-dev
|
||||
artifacts: [timg]
|
||||
```
|
||||
|
||||
另一方面,如果希望 timg snap 包能访问整个文件系统,我们可以设置 传统限制来实现。对应的 snapcraft.yaml 内容如下:
|
||||
|
||||
```
|
||||
name: timg
|
||||
version: '20170226'
|
||||
summary: A terminal image viewer
|
||||
description: |
|
||||
A viewer that uses 24-Bit color capabilities and unicode character blocks
|
||||
to display images in the terminal.
|
||||
|
||||
grade: stable
|
||||
confinement: classic
|
||||
|
||||
apps:
|
||||
timg:
|
||||
command: timg
|
||||
|
||||
parts:
|
||||
timg:
|
||||
source: https://github.com/hzeller/timg.git
|
||||
source-subdir: src
|
||||
plugin: make
|
||||
build-packages:
|
||||
- libgraphicsmagick++1-dev
|
||||
- libwebp-dev
|
||||
artifacts: [timg]
|
||||
```
|
||||
|
||||
接下来我们将选择严格约束选项。因此,图像应该只能放在 $HOME 中。
|
||||
|
||||
### 打包和测试
|
||||
|
||||
让我们打包这个 snap,也就是制作 .snap 文件,然后在新安装的 Ubuntu 系统上对它进行测试。
|
||||
|
||||
```
|
||||
ubuntu@snaps:~/timg-snap$ snapcraft
|
||||
Skipping pull timg (already ran)
|
||||
Skipping build timg (already ran)
|
||||
Skipping stage timg (already ran)
|
||||
Skipping prime timg (already ran)
|
||||
Snapping 'timg' \
|
||||
Snapped timg_20170226_amd64.snap
|
||||
ubuntu@snaps:~/timg-snap$
|
||||
```
|
||||
|
||||
我们如何在几秒钟内得到一个全新安装的 Ubuntu 系统来对 snap 包进行测试?
|
||||
|
||||
请查看 [尝试在 Ubuntu 上使用 LXD 容器][33],并在你的系统上设置 LXD。然后回到这里,尝试运行下面的命令:
|
||||
|
||||
```
|
||||
$ lxc launch ubuntu:x snaptesting
|
||||
Creating snaptesting
|
||||
Starting snaptesting
|
||||
$ lxc file push timg_20170226_amd64.snap snaptesting/home/ubuntu/
|
||||
$ lxc exec snaptesting -- sudo su - ubuntu
|
||||
To run a command as administrator (user "root"), use "sudo <command>".
|
||||
See "man sudo_root" for details.
|
||||
|
||||
ubuntu@snaptesting:~$ ls
|
||||
timg_20170226_amd64.snap
|
||||
ubuntu@snaptesting:~$ snap install timg_20170226_amd64.snap
|
||||
error: access denied (try with sudo)
|
||||
ubuntu@snaptesting:~$ sudo snap install timg_20170226_amd64.snap
|
||||
error: cannot find signatures with metadata for snap "timg_20170226_amd64.snap"
|
||||
ubuntu@snaptesting:~$ sudo snap install timg_20170226_amd64.snap --dangerous
|
||||
error: cannot perform the following tasks:
|
||||
- Mount snap "core" (1337) ([start snap-core-1337.mount] failed with exit status 1: Job for snap-core-1337.mount failed. See "systemctl status snap-core-1337.mount" and "journalctl -xe" for details.
|
||||
)
|
||||
ubuntu@snaptesting:~$ sudo apt install squashfuse
|
||||
[...]
|
||||
Setting up squashfuse (0.1.100-0ubuntu1~ubuntu16.04.1) ...
|
||||
ubuntu@snaptesting:~$ sudo snap install timg_20170226_amd64.snap --dangerous
|
||||
timg 20170226 installed
|
||||
ubuntu@snaptesting:~$ wget https://farm7.staticflickr.com/6187/6091603784_d6960c8be2_z_d.jpg
|
||||
[...]
|
||||
2017-02-26 22:12:18 (636 KB/s) - ‘6091603784_d6960c8be2_z_d.jpg’ saved [240886/240886]
|
||||
ubuntu@snaptesting:~$ timg 6091603784_d6960c8be2_z_d.jpg
|
||||
[it worked!]
|
||||
ubuntu@snaptesting:~$
|
||||
```
|
||||
|
||||
我们启动了一个名为 `snaptesting` 的 LXD 容器,并将 .snap 文件复制进去。然后,通过普通用户连接到容器,并尝试安装 snap 包。最初,我们安装失败了,因为在无特权的 LXD 容器中安装 snap 包需要使用 `sudo` 。接着又失败了,因为 .snap 没有经过签名(我们需要使用 `--dangerous` 参数)。然而还是失败了,这次是因为我们需要安装 `squashfuse` 包(Ubuntu 16.04 镜像中没有预装)。最后,我们成功安装了snap,并设法查看了图像。
|
||||
|
||||
在一个全新安装的 Linux 系统中测试 snap 包是很重要的,因为这样才能确保 snap 包中包含所有必须的代码库。在这个例子中,我们使用了静态库并运行良好。
|
||||
|
||||
### 发布到 Ubuntu 商店
|
||||
|
||||
这是 [发布 snap 包到 Ubuntu 商店的说明][34]。 在之前的教程中,我们已经发布了一些 snap 包。对于 `timg` 来说,我们设置了严格限制和稳定等级。因此,我们会将它发布到稳定通道。
|
||||
|
||||
```
|
||||
$ snapcraft push timg_20170226_amd64.snap
|
||||
Pushing 'timg_20170226_amd64.snap' to the store.
|
||||
Uploading timg_20170226_amd64.snap [ ] 0%
|
||||
Uploading timg_20170226_amd64.snap [=======================================] 100%
|
||||
Ready to release!|
|
||||
Revision 6 of 'timg' created.
|
||||
$ snapcraft release timg 6 stable
|
||||
Track Arch Series Channel Version Revision
|
||||
latest amd64 16 stable 20170226 6
|
||||
candidate ^ ^
|
||||
beta 0.9.5 5
|
||||
edge 0.9.5 5
|
||||
The 'stable' channel is now open.
|
||||
```
|
||||
|
||||
我们把 .snap 包推送到 Ubuntu 商店后,得到了一个 `修订版本号 6`。然后,我们将 timg `修订版本 6` 发布到了 Ubuntu 商店的稳定通道。
|
||||
|
||||
在候选通道中没有已发布的 snap 包,他继承的是稳定通道的包,所以显示 `^` 字符。
|
||||
|
||||
在之前的测试中,我将一些较老版本的 snap 包上传到了测试和边缘通道。这些旧版本使用了 timg 标签为 `0.9.5` 的源代码。
|
||||
|
||||
我们可以通过将稳定版本发布到测试和边缘通道来移除旧的 0.9.5 版本的包。
|
||||
|
||||
```
|
||||
$ snapcraft release timg 6 beta
|
||||
Track Arch Series Channel Version Revision
|
||||
latest amd64 16 stable 20170226 6
|
||||
candidate ^ ^
|
||||
beta 20170226 6
|
||||
edge 0.9.5 5
|
||||
$ snapcraft release timg 6 edge
|
||||
Track Arch Series Channel Version Revision
|
||||
latest amd64 16 stable 20170226 6
|
||||
candidate ^ ^
|
||||
beta 20170226 6
|
||||
edge 20170226 6
|
||||
```
|
||||
|
||||
### 使用 timg
|
||||
|
||||
让我们不带参数运行 timg:
|
||||
|
||||
```
|
||||
ubuntu@snaptesting:~$ timg
|
||||
Expected image filename.
|
||||
usage: /snap/timg/x1/timg [options] <image> [<image>...]
|
||||
Options:
|
||||
-g<w>x<h> : Output pixel geometry. Default from terminal 80x48
|
||||
-s[<ms>] : Scroll horizontally (optionally: delay ms (60)).
|
||||
-d<dx:dy> : delta x and delta y when scrolling (default: 1:0).
|
||||
-w<seconds>: If multiple images given: Wait time between (default: 0.0).
|
||||
-t<seconds>: Only animation or scrolling: stop after this time.
|
||||
-c<num> : Only Animation or scrolling: number of runs through a full cycle.
|
||||
-C : Clear screen before showing image.
|
||||
-F : Print filename before showing picture.
|
||||
-v : Print version and exit.
|
||||
If both -c and -t are given, whatever comes first stops.
|
||||
If both -w and -t are given for some animation/scroll, -t takes precedence
|
||||
ubuntu@snaptesting:~$
|
||||
```
|
||||
|
||||
这里说,当前我们终端模拟器的缩放级别,即分辨率为:80 × 48。
|
||||
|
||||
让我们缩小一点,并最大化 GNOME 终端窗口。
|
||||
|
||||
```
|
||||
-g<w>x<h> : Output pixel geometry. Default from terminal 635x428
|
||||
```
|
||||
|
||||
这是一个更好的解决方案,但我几乎看不到字符,因为他们太小了。让我们调用前面的命令再次显示这辆车。
|
||||
|
||||
![图片.png-904.9kB][35]
|
||||
|
||||
你所看到的是调整后的图像(1080p)。虽然它是用彩色文本字符显示的,但看起来依旧很棒。
|
||||
|
||||
接下来呢?timg 其实也可以播放 gif 动画哦!
|
||||
|
||||
```
|
||||
$ wget https://m.popkey.co/9b7141/QbAV_f-maxage-0.gif -O JonahHillAmazed.gif$ timg JonahHillAmazed.gif
|
||||
```
|
||||
|
||||
你可以试着安装 timg 来体验 gif 动画。要是不想自己动手,可以在 [asciinema][36] 上查看相关记录 (如果视频看上去起伏不定的,请重新运行它)。
|
||||
|
||||
谢谢阅读!
|
||||
|
||||
-----
|
||||
译者简介:
|
||||
|
||||
经常混迹于 snapcraft.io,对 Ubuntu Core、Snaps 和 Snapcraft 有着浓厚的兴趣,并致力于将这些还在快速发展的新技术通过翻译或原创的方式介绍到中文世界。有兴趣的小伙伴也可以关注译者个人公众号: `Snapcraft`
|
||||
|
||||
-----
|
||||
via:https://blog.simos.info/how-to-create-a-snap-for-timg-with-snapcraft-on-ubuntu/
|
||||
|
||||
作者:[Mi blog lah!][37]
|
||||
译者:[Snapcrafter](https://github.com/Snapcrafter)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
|
||||
[1]: https://github.com/hzeller/timg
|
||||
[2]:https://tutorials.ubuntu.com/tutorial/create-first-snap
|
||||
[3]: https://github.com/hzeller/timg
|
||||
[4]: https://snapcraft.io/docs/reference/plugins/make
|
||||
[5]:https://github.com/hzeller/timg
|
||||
[6]: http://static.zybuluo.com/apollomoon/ynm5k5urc7idb037ahca2s93/%E5%9B%BE%E7%89%87.png
|
||||
[7]: http://static.zybuluo.com/apollomoon/h2ynj68axdqiy7dwszgw5z1f/%E5%9B%BE%E7%89%87.png
|
||||
[8]:https://gist.github.com/XVilka/8346728
|
||||
[9]:https://en.wikipedia.org/wiki/Escape_sequence
|
||||
[10]:https://github.com/hzeller/timg
|
||||
[11]: http://static.zybuluo.com/apollomoon/nzlqpq3xn4rs72h4r96k4xlw/%E5%9B%BE%E7%89%87.png
|
||||
[12]:http://design.ubuntu.com/wp-content/uploads/ubuntu-logo112.png
|
||||
[13]: http://static.zybuluo.com/apollomoon/vo1nxnu4xfaghyib03fnkvq4/%E5%9B%BE%E7%89%87.png
|
||||
[14]:https://www.flickr.com/photos/doug88888/5776072628/in/photolist-9WCiNQ-7U3Trc-7YUZBL-5DwkEQ-6e1iT8-a372aS-5F75aL-a1gbow-6eNayj-8gWK2H-5CtH7P-6jVqZv-86RpwN-a2nEnB-aiRmsc-6aKvwK-8hmXrN-5CWDNP-62hWM8-a9smn1-ahQqHw-a22p3w-a36csK-ahN4Pv-7VEmnt-ahMSiT-9NpTa7-5A3Pon-ai7DL7-9TKCqV-ahr7gN-a1boqP-83ZzpH-9Sqjmq-5xujdi-7UmDVb-6J2zQR-5wAGNR-5eERar-5KVDym-5dL8SZ-5S2Uut-7RVyHg-9Z6MAt-aiRiT4-5tLesw-aGLSv6-5ftp6j-5wAVBq-5T2KAP
|
||||
[15]: https://github.com/hzeller/timg
|
||||
[16]: https://github.com/hzeller/timg
|
||||
[17]:https://github.com/hzeller/timg
|
||||
[18]: https://github.com/hzeller/timg
|
||||
[19]: http://static.zybuluo.com/apollomoon/hovu73yqx08pdhm8qmdg6f6a/%E5%9B%BE%E7%89%87.png
|
||||
[20]:https://github.com/hzeller/timg
|
||||
[21]: http://static.zybuluo.com/apollomoon/o64i7jm65u3o12wg3fqqcn7x/%E5%9B%BE%E7%89%87.png
|
||||
[22]: http://static.zybuluo.com/apollomoon/t4w1uak9j4h6rfn4ghc8q15k/%E5%9B%BE%E7%89%87.png
|
||||
[23]: http://static.zybuluo.com/apollomoon/cvuetj2rzd5nee7pgfcp7wr3/timg-git-url.png
|
||||
[24]: http://static.zybuluo.com/apollomoon/dxtl628r1qavphhzu70jiw1n/%E5%9B%BE%E7%89%87.png
|
||||
[25]: https://snapcraft.io/docs/reference/plugins/make
|
||||
[26]:https://snapcraft.io/docs/reference/plugins/
|
||||
[27]: https://snapcraft.io/docs/reference/plugins/source
|
||||
[28]: https://snapcraft.io/docs/reference/plugins/source
|
||||
[29]:https://snapcraft.io/docs/reference/plugins/common
|
||||
[30]:https://snapcraft.io/docs/reference/plugins/make
|
||||
[31]:https://snapcraft.io/docs/reference/plugins/make
|
||||
[32]: http://static.zybuluo.com/apollomoon/v9y3vutt8li4wwaxeigwr4yz/%E5%9B%BE%E7%89%87.png
|
||||
[33]:https://blog.simos.info/trying-out-lxd-containers-on-our-ubuntu/
|
||||
[34]:https://snapcraft.io/docs/build-snaps/publish
|
||||
[35]: http://static.zybuluo.com/apollomoon/clnv44g3bwhaqog7o1jpvpcd/%E5%9B%BE%E7%89%87.png
|
||||
[36]: https://asciinema.org/a/dezbe2gpye84e0pjndp8t0pvh
|
||||
[37]: https://blog.simos.info/
|
Loading…
Reference in New Issue
Block a user