开发 Discourse 插件 - 第 4 部分 - 设置 git

上一篇教程:Developing Discourse Plugins - Part 3 - Add custom site settings


现在您的插件变得越来越复杂,是时候在开发方式上也变得更复杂一些了。

我们建议您使用 git 作为插件的版本控制工具。我们也建议您使用 github 与他人共享您的插件代码。

创建您的 Git 仓库

创建您的 Github 账户后,请访问 此网址 创建一个新的仓库。您可以随意命名,但通常以 discourse- 开头是一个好习惯。确保仓库是公开的。我的屏幕看起来是这样的:

创建本地工作文件夹

此时,我在我的电脑上创建了一个本地目录来存放插件。我通常将我的放在 ~/code 中,但您可以将它放在电脑上的任何您喜欢的位置:

mkdir -p ~/code/discourse-plugin-test
cd ~/code/discourse-plugin-test

现在让我们按照 Github 上的说明,用一个 README 初始化仓库:

echo "# discourse-plugin-test" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:eviltrout/discourse-plugin-test.git
git push -u origin master

最后,按照 第一部分 中的说明,为您的插件创建一个 plugin.rb 文件。对于这个例子,我只是创建了一个虚拟文件:

plugin.rb

# name: discourse-plugin-test
# about: Shows how to set up Git
# version: 0.0.1
# authors: Robin Ward

创建一个符号链接

因为您遵循了我们的 开发者指南,您的电脑上应该在某个地方检出了 Discourse 的副本。我将其检出到了 ~/code/discourse,但同样,您可以将其放在任何地方,如果相应地调整以下代码,它仍然可以工作:

cd ~/code/discourse/plugins
ln -s ~/code/discourse-plugin-test .

上面的代码在您的 Discourse 代码和您的插件文件夹之间创建了一个符号链接。重启您的开发服务器,您应该会发现您的插件正在工作!

这种设置的好处是,您可以将插件提交到 github,而无需担心它所在的 Discourse 代码库。您的更改将仅限于插件本身。如果您需要编辑 Discourse 的代码,仍然可以这样做,但 git 会单独跟踪这些更改!

我建议使用一个编辑器窗口用于您的插件代码库,另一个用于 Discourse 本身。将它们视为两个不同的事物会更容易。


系列中的更多内容

第一部分:插件基础知识
第二部分:插件插槽
第三部分:站点设置
第四部分:本主题
第五部分:管理界面
第六部分:验收测试
第七部分:发布您的插件


本文档已进行版本控制 - 请在 github 上建议更改。

23 个赞
  • After many frustrating attempts, found out that apparently ln -s does not work in a Windows environment, or atleast not how it should.
  • ln -s essentially just copy-pasted the plugin folder into the discourse/plugins folder
  • Apparently, in Windows the way to create symbolic links is to use the mklink command in command prompt (run as administrator, and this command does not natively run in Windows PowerShell either).
  • Using the mklink command (with both arguments /d and /h), although the created symbolic link could be seen present in the directory, the plugin was not working with discourse (and also not showing in /admin/plugins).
  • I tried this multiple times with restarting the rails server, deleting the tmp folder, but to no avail.

@eviltrout, any idea what could I be doing wrong?

2 个赞

I assume you are using Vagrant on windows? If you can’t get the symbolic links sent over, I think the only way you can do it is to copy the plugin into discourse/plugins manually and work from there. It should work as long as you are not making changes to the core discourse app at the same time, which confuses git.

When your plugin is ready, you’ll want to copy it to another directory to package it up for git.

1 个赞

Yeah, OK this should be fine too.
Although the OCD side of me, much preferred the comparatively “cleaner” symbolic links method.

Anyway, Thanks.

@AhmadF.Cheema I had similar problems with the symlinking using Vagrant 1.9.8 on Linux, and a completely standard Discourse Vagrant development environment as per the docs.

The problem is simple when you look into it. From the scope of inside the Vagrant VM, the destination of the symlink is not a valid path. Try executing the command ls -al in the plugins directory inside your VM (in a standard install this is at /vagrant/plugins)

vagrant@discourse:/vagrant/plugins$ ls -al
total 36
drwxr-xr-x 1 vagrant vagrant 4096 Oct 22 09:08 ./
drwxr-xr-x 1 vagrant vagrant 4096 Oct 22 09:10 ../
drwxr-xr-x 1 vagrant vagrant 4096 Sep  7 19:51 discourse-details/
drwxrwxr-x 1 vagrant vagrant 4096 Oct 21 13:56 discourse-narrative-bot/
drwxr-xr-x 1 vagrant vagrant 4096 Oct 21 13:56 discourse-nginx-performance-report/
drwxr-xr-x 1 vagrant vagrant 4096 Sep  7 19:51 discourse-plugin-outlet-locations/
drwxrwxr-x 1 vagrant vagrant 4096 Oct 21 13:56 discourse-presence/
lrwxrwxrwx 1 vagrant vagrant   55 Oct 22 09:08 my-basic-plugin -> /home/marcus/code/discourse/my-basic-plugin
drwxr-xr-x 1 vagrant vagrant 4096 Oct 21 13:56 lazyYT/
drwxr-xr-x 1 vagrant vagrant 4096 Oct 21 13:56 poll/

As you can see, the path /home/marcus/code/discourse/my-basic-plugin cannot possibly be accessible from the VM because it doesn’t exist inside the VM!

The solution is to delete the externally created symlink and set up a separate shared folder in Vagrant, by adding a line to your Vagrantfile:

config.vm.synced_folder "/home/marcus/code/my-basic-plugin",  "/my-basic-plugin"

Then restart the Vagrant VM: vagrant halt && vagrant up so that this change is picked up

Now, when you enter your VM via SSH using vagrant ssh you can create a symlink inside the VM:

cd /vagrant/plugins
ln -s /my-basic-plugin .

Now you can develop in a neatly isolated local folder, and have the neat Git workflow that @eviltrout describes, and the symlinking happens inside the VM. Note that outside the VM, the symlink will be broken - but this shouldn’t matter for our purposes.

3 个赞

If you are developing on Linux using our docker based dev is way simpler

4 个赞

Windows symlinks are different from Unix symlinks, thus your confusion. Windows synlinks are very fussy, requiring particular versions of OS to support and sometimes applications must be written to be aware of this. In other words, the stars must line up perfectly for windows symlinks to work.

A hard link (/H) I dont think work with directories. Your /D makes a symlink on a directory, trumping your /H (which is used to create a hard link to a file, not a directory).

Confusing? Welcome to Windows.

There are four types of links in Windows:

  • MKLINK (no flags) – symbolic link to file
  • MKLINK /H – hard link to file
  • MKLINK /D – symbolic link to directory
  • MKLINK /J – junction (i.e. hard link) to directory

What you need is is a junction which is Windows-speak for hard link to a directory.

Do MKLINK /J to your plugins directory and the system will treat it as a subdirectory. In fact it won’t know otherwise. Beware, it is not common to have a Windows directory (folder in Windows-speak) to point to the same place as another directory, so you’ll get confused very easy and forget that both are the same things.

That’s why you’ll need to run the command in Administrator mode, otherwise Windows won’t let you create the directory junction.

4 个赞

Thanks for the info regarding Windows symlinks @schungx - it should be of help to the OP.

The workaround I described should work fine on any platform, since the symlinks happen inside the (Ubuntu) vagrant box

M

Yup, you’re right. If you can avoid it, avoid messing with Windows. Windows is very picky and may choose to die or go wrong at the most unfortunate moment…

Work for me! 2018-4-29

1. I put discourse & my plugin in Desktop

instead of put 1c7-plugin under discourse/plugin

2. and put a “alias” into discourse/plugins folder

alias is a macOS concept,
it’s the same things as ln -s command

3. Discourse correctly load the plugin

(After reboot server with rails s)

4. Now they are separated, use git to manage code is much easier

Thanks!

无法在 macOS Docker 设置中使用符号链接。插件只能在直接复制到插件文件夹时才有效。