📜 ⬆️ ⬇️

Mars rover Initialization



In this series of articles, we build the rover software in accordance with the following specifications . This will allow us to practice the following approaches:



First we need to initialize our project.
')

Creating a repository


Let's start by creating a new git repository:

mkdir rover cd rover git init 

Since we are going to use Composer , let's create composer.json :

 { "name": "mars-rover/mars-rover", "license": "MIT", "type": "project", "description": "Mars Rover", "require": { "php": "^7.0" } } 

Add .gitignore to ignore third-party library files in the repository:

 # Third Party libraries /vendor/ 

Having finished with creation of a repository, we will start composer :

 composer install --optimize-autoloader 

This will be enough for the first commit:

 git add composer.json .gitignore git commit -m '0: Created project' 

Creating a navigation package


Looking at the decomposition of the task , we will see that the tasks can be distinguished in write-only and read-only :

  1. Mars Rover Landing - write-only
  2. Rover driving - write-only
  3. Location request - read-only

Since we want to adhere to the principle of CQRS , we put the write-only logic separately from read-only . Landing and driving are all about navigation, so let's start with it:

 git checkout -b 1-navigation mkdir -p packages/navigation cd packages/navigation 

Create composer.json for the new package:

 { "name": "mars-rover/navigation", "license": "MIT", "type": "library", "description": "Mars Rover - Navigation", "autoload": { "psr-4": { "MarsRover\\Navigation\\": "src/MarsRover/Navigation" } }, "require": { "php": "^7.0" }, "require-dev": { "memio/spec-gen": "^0.6" } } 

As a test platform, take phpspec , and for most tests we will use its SpecGen extension. To do this, create in the root of the project phpspec.yml.dist :

 extensions: Memio\SpecGen\MemioSpecGenExtension: ~ 

Note : For more information on phpspec, see the article .
Finally, we need to configure git for this package by creating a .gitignore file:

 # Configuration /phpspec.yml # Third Party libraries /vendor/ /composer.lock 

This is where we’ve finished configuring our package, and now we can run Composer :

 composer install --optimize-autoloader 

We fix our actions in the second commit:

 git add -A git commit -m '1: Created Navigation package' 

Adding a navigation package to a project


Let's return to the project root:

 cd ../../ 

One of the advantages of MonoRepo is the ability to perform tests of all packages with one command. To do this, we need to register the dependence on navigation in the composer.json file of our main project:

 { "name": "mars-rover/mars-rover", "license": "MIT", "type": "project", "description": "Mars Rover", "repositories": [ { "type": "path", "url": "./packages/*" } ], "require": { "mars-rover/navigation": "*@dev", "php": "^7.0" } } 

By default, Composer only searches for Packagist packages . Having added a new section repositories we tell it to check the availability of packages also locally in ./packages , which allows us to use the found packages in the require section.

We also need to tell the package manager what version we would like, but in the mono-repository all packages have the same version, so we just use * (any). But in order to be able to use the latest changes, not only with version tags, we need to specify the preferred stability ( @dev ).

Well, since we use phpspec for our tests, we put the dependency in the main project and on it:

 composer require --dev phpspec/phpspec:^3.0 

phpspec will look for tests at the root of the project. We need to create phpspec.yml.dist to report the presence of a navigation package:

 suites: navigation: namespace: 'MarsRover\Navigation' src_path: packages/navigation/src spec_path: packages/navigation 

To ignore the local configuration, also update the .gitignore :

 # Configuration /phpspec.yml # Third Party libraries /vendor/ 

Here it is! Now we can run Composer and then phpspec :

 composer update --optimize-autoloader ./vendor/bin/phpspec run 

Let's close it all up:

 git add -A git commit -m '1: Added navigation package to main project' 

And stink with the master branch:

 git checkout master git merge --no-ff 1-navigation 

Conclusion


With Composer we can create many layouts within one repository, and using the MonoRepo approach, we can run all tests with one command.

What's next


In the next article, we will deal with the interior of the task “Landing a rover on Mars”, demonstrating an example of how Event Sourcing and TDD work .

Previous part: Rover, Introduction
Next part: Rover, Landing

Source: https://habr.com/ru/post/314544/


All Articles