Développement de plugins Discourse - Partie 4 - Configuration de git

Tutoriel précédent : Developing Discourse Plugins - Part 3 - Add custom site settings


Maintenant que votre plugin devient plus sophistiqué, il est temps de devenir plus sophistiqué quant à la manière dont vous le développez.

Nous vous suggérons d’utiliser git comme système de contrôle de version pour votre plugin. Nous vous recommandons également d’utiliser github pour partager le code de votre plugin avec d’autres.

Création de votre dépôt Git

Une fois que vous avez créé votre compte Github, visitez cette URL pour créer un nouveau dépôt. Vous pouvez l’appeler comme vous voulez, mais généralement un nom commençant par discourse- est une bonne pratique. Assurez-vous que le dépôt est public. Voici à quoi ressemblait mon écran :

Création de votre dossier de travail local

À ce stade, je crée un répertoire local sur mon ordinateur pour contenir le plugin. Je mets généralement le mien dans ~/code, mais vous pouvez le mettre où vous voulez sur votre ordinateur :

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

Suivons maintenant les instructions de github pour initialiser le dépôt avec un fichier 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

Enfin, créez un fichier plugin.rb pour votre plugin comme expliqué dans la partie 1. Pour cet exemple, j’ai juste créé un fichier factice :

plugin.rb

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

Création d’un lien symbolique

Parce que vous avez suivi notre guide du développeur, vous devriez avoir une copie de discourse extraite sur votre ordinateur quelque part. J’ai extrait la mienne dans ~/code/discourse, mais encore une fois, vous auriez pu la mettre n’importe où et cela devrait toujours fonctionner si vous ajustez le code suivant en conséquence :

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

Le code ci-dessus a créé un lien symbolique entre votre code discourse et votre dossier de plugin. Redémarrez votre serveur de développement et vous devriez voir votre plugin fonctionner !

La beauté de cette configuration est que vous pouvez simplement enregistrer votre plugin sur github et ne pas vous soucier de la base de code discourse dans laquelle il se trouve. Vos modifications seront isolées au plugin lui-même. Si vous avez besoin de modifier le code de discourse, vous le pouvez toujours, mais git suivra les modifications séparément !

Je recommande d’utiliser une fenêtre d’éditeur pour la base de code de votre plugin et une autre pour Discourse lui-même. Il est plus facile de les considérer comme deux choses différentes.


Plus dans la série

Partie 1 : Bases des plugins
Partie 2 : Sorties de plugin
Partie 3 : Paramètres du site
Partie 4 : Ce sujet
Partie 5 : Interfaces d’administration
Partie 6 : Tests d’acceptation
Partie 7 : Publiez votre plugin


Ce document est contrôlé par version - suggérez des modifications sur github.

23 « J'aime »
  • 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 « J'aime »

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 « J'aime »

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 « J'aime »

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

4 « J'aime »

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 « J'aime »

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!

Impossible de faire fonctionner le lien symbolique dans la configuration Docker de macOS. Les plugins ne fonctionnent que s’ils sont copiés directement dans le dossier des plugins.