Developing an Add-on in a Git Repository

Version control systems (VCS) have become a crucial part of collaborating on projects. A VCS keeps track of the changes to the project, allowing dozens or even hundreds of developers to work together. Services like GitHub can gather full-fledged developer communities around projects.

You can make the development and testing of an add-on with various CS-Cart versions easier by using a VCS and storing the add-on files in a separate directory. In this article we offer the best practices of developing an add-on in repository, separately from CS-Cart installation. We’ll be speaking about Git repositories, as Git is probably the most popular VCS.

The general idea of the workflow is as follows: create an add-on called sample_git_module in a separate directory, initialize a Git repository in that directory, and create symbolic links in the directory with CS-Cart installation. These links will contain the path to the files of the add-on in the repository. So, let’s begin.

Note

The examples provided here use Unix commands. You can run them on MacOS or Linux. We will also be using cscart-sdk—a console-based tool that facilitates routine tasks. We recommend installing the SDK before you read further. Check cscart-sdk repository for details.

1. Separate Add-on Files from CS-Cart Files

Due to CS-Cart’s architecture, the files of an add-on are stored in different directories:

  • app/addons—PHP files
  • design/backend/templates/addons—templates of the administration panel
  • var/themes_repository/*/templates/addons—templates of the customer area
  • var/langs/*/addons—translation files

We need to recreate this directory structure somewhere outside our CS-Cart installation. That way we’ll be able to store the add-on files separately.

Let’s create a directory called sample_git_module and put all the files of our example add-on there:

  • app/addons/sample_git_module/addon.xml—every add-on must have this file;
  • var/langs/en/addons/sample_git_module.po—the translations of the add-on’s language variables;
  • app/addons/sample_git_module/controllers/frontend/sample_git.php—a controller;
  • var/themes_repository/responsive/templates/addons/sample_git_module/views/sample_git_module/view.tpl—the controller’s template.

In this example we’ve put sample_git_module into ~/dev/vagrant/repos, but you can store the directory with your add-on anywhere.

The files of the add-on are structured the same way as in CS-Cart or Multi-Vendor.

If you have already developed an add-on that is stored with the CS-Cart installation, you can easily copy or move all the files of the add-on to a separate directory with cscart-sdk. Run the addon:export command:

$ cscart-sdk addon:export addon_name /path/to/addon_directory /path/to/cscart

Important

This command will copy the add-on’s templates from the design directory to var/themes_repository.

Hint

Learn more about addon:export in the cscart-sdk documentation.

Now that we have the add-on files stored in a separate directory, we can initialize a Git repository there. But wouldn’t we have to create an archive with the add-on and install it in the Administration panel every time we want to test it? Luckily, there is a way to make development and debugging convenient and transparent.

3. Synchronize Files between Directories

Cscart-sdk has another useful command—addon:sync, which runs addon:export and then addon:symlink. The addon:sync command comes useful when you create a new file for your add-on in the CS-Cart installation directory. The command will move the file to the add-on’s repository and create a symbolic link in its place.

Hint

Learn more about addon:sync in the cscart-sdk documentation.


Big CS-Cart Update is Here Experience the New Admin Panel with Dark Theme