运用snapcraft 2.25+提供的hooks来实现对snap的设置

来源:互联网 发布:世界征服者3 mac 编辑:程序博客网 时间:2024/05/17 09:20

在先前的文章"如何为我们的Ubuntu Core应用进行设置 "中,我们通过copy plugin的方法把我们想要的congfigure文件拷入到我们所需要的目录中.具体的实现是这样的:

snapcraft.yaml

parts:   hello:    plugin: copy    files:      ./bin: bin   config:    plugin: dump    source: .    organize:      configure: meta/hooks/configure  

由于在snapcraft 2.25版本以后,它提供了对hook的支持,所有,我们只需要要在我们的项目的根目录中建立一个叫做snap/hooks的目录,并把我们的configure文件拷入即可:

liuxg@liuxg:~/snappy/desktop/helloworld-hook$ tree -L 4.├── bin│   ├── createfile│   ├── createfiletohome│   ├── echo│   ├── env│   ├── evil│   └── sh├── setup│   ├── gui│   │   ├── helloworld.desktop│   │   └── helloworld.png│   └── license.txt├── snap│   └── hooks│       └── configure└── snapcraft.yaml

有了这样的文件架构后,snapcraft会自动帮我们把configure文件考入到meta/hooks文件目录下.下面是我们的prime目录里的内容:

liuxg@liuxg:~/snappy/desktop/helloworld-hook/prime$ tree -L 3.├── bin│   ├── createfile│   ├── createfiletohome│   ├── echo│   ├── env│   ├── evil│   └── sh├── command-createfiletohome.wrapper├── command-createfile.wrapper├── command-env.wrapper├── command-evil.wrapper├── command-hello-world.wrapper├── command-sh.wrapper├── meta│   ├── gui│   │   ├── helloworld.desktop│   │   └── helloworld.png│   ├── hooks│   │   └── configure│   └── snap.yaml└── snap    └── hooks        └── configure

我们必须记住这个功能只是在snapcraft 2.25以上的版本中才有的.我们可以看到在meta/hooks/中有一个叫做configure的文件.

我们安装好这个snap应用,并执行如下的命令:

$ sudo snap set hello username=foo password=bar

我们可以通过如下的命令来获得这个值:

$ sudo snap get hello usernamefoo

显然,我们得到我们设置的值.整个源码在:https://github.com/liu-xiao-guo/helloworld-hook.另外一个例程也可以在我们的snapcraft项目中的hooks找到.

更多阅读:https://github.com/snapcore/snapcraft/blob/master/docs/hooks.md.就想文章中介绍的那样,我们也可以利用另外一种方法来实现.具体的例子见pyhooks.这种方法的好处是可以使用python语言来进行设置.运行结果如下:

liuxg@liuxg:~$ sudo snap set pyhooks fail=trueerror: cannot perform the following tasks:- Run configure hook of "pyhooks" snap (Failing as requested.)liuxg@liuxg:~$ sudo snap set pyhooks fail=false


更多阅读:https://snapcraft.io/docs/build-snaps/hooks




0 0
原创粉丝点击