Skip to content

Commit 5f0f5bd

Browse files
authored
Merge pull request #100 from banchan86/environment-article
Update Environments article with template method
2 parents ebe5faa + 1ff1c7c commit 5f0f5bd

File tree

1 file changed

+46
-47
lines changed

1 file changed

+46
-47
lines changed

articles/environments.md

+46-47
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ If you have many projects, you might notice that older projects require specific
1111

1212
Bonsai addresses these problems by supporting the creation of reproducible package environments. An environment is a self-contained, portable, installation of Bonsai that records a *snapshot* of all the packages required to run the workflows in your project. This makes it much easier to share a project with other people, or keep track of multiple separate projects in our local machine, and be assured you always have everything you need in the right place.
1313

14-
## Creating an environment
15-
14+
## Environment Basics
1615
The key to creating and updating environments is the `Bonsai.config` file, which keeps a record of all currently installed dependencies for a specific Bonsai setup. You can find this file in the same location of the Bonsai executable (`Bonsai.exe`). Anytime you install or update a package, Bonsai will automatically modify the config file.
1716

1817
The contents of the `Bonsai.config` file are compared with the current state of the `Packages` folder when Bonsai starts. If there are any missing packages the Bonsai bootstrapper will download them automatically to recover the expected state of the installation folder.
1918

20-
The easiest way to create a self-contained Bonsai environment is then to download the portable version of Bonsai and install the packages which are necessary for a specific project. For example, to share a project that depends on the [`Vision`](xref:Bonsai.Vision) package:
19+
There is a second configuration file located next to the `Bonsai.config` file called `NuGet.config`. This file stores a list of all the remote, and local, NuGet package sources where the [`Package Manager`](xref:packages) should look for new packages. While this file needs to be included in the environment, most users do not need to modify it.
20+
21+
## Portable Method
22+
> [!TIP]
23+
> We recommend this approach for the casual user.
24+
25+
The easiest way to create a self-contained Bonsai environment is to download the portable version of Bonsai and install the packages which are necessary for a specific project. For example, to share a project that depends on the [`Vision`](xref:Bonsai.Vision) package:
2126

2227
1. Start by downloading the [latest Bonsai portable release](https://github.com/bonsai-rx/bonsai/releases/latest/download/Bonsai.zip).
2328
2. After extracting all the files from the `Bonsai.zip` file, your folder will look like this:
@@ -28,29 +33,50 @@ The easiest way to create a self-contained Bonsai environment is then to downloa
2833
4. Install the **Bonsai - Vision** package using the [`Package Manager`](xref:packages). The `Bonsai.config` file will be modified to specify this package as a new dependency. Any additional dependencies which might be needed for the package to run will also be added.
2934
5. Close Bonsai.
3035

31-
## Deploying your environment
36+
You now have a local Bonsai environment folder that you can keep separate for experiments or share with anyone else who needs it. Alternatively, once you have a modified `Bonsai.config` file, you can also simply copy the `Bonsai.config` file into a new portable installation, and the Bonsai bootstrapper will download and resolve the missing or inconsistent packages.
3237

33-
Now that you have a `Bonsai.config` file with all the dependencies needed to run your project, how can you reproduce this installation elsewhere? As we saw in the previous step, when we run `Bonsai.exe` for the first time, the bootstrapper will attempt to download and resolve all core dependencies.
38+
## Template Method
39+
> [!TIP]
40+
> We recommend this approach for users who need to deploy and maintain multiple environments. This approach assumes familiarity with the command-line.
3441
35-
However, if a `Bonsai.config` file is found inside the executable folder, the bootstrapper will also resolve those dependencies at startup time. We can test this by doing the following:
42+
We have also provided a command-line tool for "one-click" deployment which installs packages automatically and streamlines the process for creating new environments.
3643

37-
1. Create a new folder and repeat steps 1. and 2. from the previous section.
38-
2. This time, before running `Bonsai.exe`, copy your `Bonsai.config` into the folder:
44+
1. Install [`.NET SDK`](https://dotnet.microsoft.com/en-us/download).
45+
2. Install `Bonsai.Templates` with the following command.
46+
```cmd
47+
dotnet new install Bonsai.Templates
48+
```
49+
3. Navigate to the folder where you want to create a new Bonsai environment and run this command.
50+
```cmd
51+
dotnet new bonsaienv
52+
```
53+
This creates by default a `.bonsai` folder with a minimal bonsai environment in your current directory. It will also prompt to run a powershell script to install any packages based on `Bonsai.config`. Before running the powershell script, you can replace the `Bonsai.config` file with a modified version or run the powershell script after by navigating to the `.bonsai` folder and running:
3954

40-
![Unziped Bonsai release with config](~/images/environments-bonsaiconfig.png)
55+
```cmd
56+
./Setup.cmd
57+
```
58+
4. You can supply additional options to change the default settings. The following command for instance will create a new folder `project1` containing a bonsai environment folder named `env`.
59+
```cmd
60+
dotnet new bonsaienv -o project1 -n .env
61+
```
62+
To install new packages in this environment, simply open the local `Bonsai.exe` and install the packages you need.
4163

42-
2. Run `Bonsai.exe`.
43-
3. Add a node that depends on the Vision package (e.g. [`CameraCapture`](xref:Bonsai.Vision.CameraCapture)) to verify that the package has been successfully installed.
64+
To share this environment with others, all you need are these 4 files.
4465

45-
The new folder is now a copy of your previous environment.
66+
- `Setup.ps1`
67+
- `Setup.cmd`.
68+
- `NuGet.config`
69+
- `Bonsai.config`
4670

47-
## Adding local dependencies
71+
Other users will be able to easily install your current Bonsai dependencies by running the `Setup.cmd` file.
4872

49-
There is a second configuration file located next to the `Bonsai.config` file called `NuGet.config`. This file stores a list of all the remote, and local, NuGet package sources where the [`Package Manager`](xref:packages) should look for new packages.
73+
## Adding Local Dependencies
74+
> [!TIP]
75+
> For [new package](xref:create-package) developers, you may need to install local NuGet packages as dependencies during testing.
5076
51-
The `NuGet.config` file can be modified to specify new package sources. For example, you may want to install local NuGet packages as dependencies (e.g. when testing [your own packages](xref:create-package)).
77+
The `NuGet.config` file can be modified to specify new package sources by adding new entries.
5278

53-
To do this we just need to add new entries in the `NuGet.config` file. For example, to add a new package source named `LocalPackages` pointing to the Desktop folder:
79+
For example, to add a new package source named `LocalPackages` pointing to the Desktop folder:
5480

5581
```xml
5682
<?xml version="1.0" encoding="utf-8"?>
@@ -73,44 +99,17 @@ You can also use relative paths if you want to keep package sources relative to
7399
> [!Note]
74100
> `NuGet.config` files can be deployed hierarchically. You can have other `NuGet.config` files located higher in the file system to specify package sources that should be shared across multiple projects or specific for the local machine. For more information on this and other settings see the [Common NuGet configurations](https://learn.microsoft.com/en-us/nuget/consume-packages/configuring-nuget-behavior) documentation page.
75101
76-
## Version control and "one-click" Bonsai deployment
77-
78-
Now that we know how to create and reuse Bonsai environments, we will show you one way to streamline this whole process.
79-
80-
1. Create the `Bonsai.config` and, if necessary, modify your `NuGet.config` files.
81-
2. Create a new folder and add these two files to it.
82-
3. Add the following `PowerShell` script to the folder (e.g.: open a new text file and copy-paste the code) and name it `Setup.ps1`:
83-
84-
```powershell
85-
if (!(Test-Path "./Bonsai.exe")) {
86-
Invoke-WebRequest "https://github.com/bonsai-rx/bonsai/releases/download/2.7/Bonsai.zip" -OutFile "temp.zip"
87-
Move-Item -Path "NuGet.config" "temp.config"
88-
Expand-Archive "temp.zip" -DestinationPath "." -Force
89-
Move-Item -Path "temp.config" "NuGet.config" -Force
90-
Remove-Item -Path "temp.zip"
91-
Remove-Item -Path "Bonsai32.exe"
92-
}
93-
& .\Bonsai.exe --no-editor
94-
```
95-
96-
When executed, this small script will attempt to download a portable version of a Bonsai release, unzip it, and delete all the unnecessary files.
97-
98-
4. Add a second batch command file `Setup.cmd` that targets and runs `Setup.ps1`. It will look this:
99-
100-
```cmd
101-
powershell -ExecutionPolicy Bypass -File .\setup.ps1
102-
```
102+
Similar to `Bonsai.config` the `NuGet.config` file will be used as part of the bootstrapper process when `Bonsai.exe` starts.
103103

104-
5. Your folder should now contain only four files:
104+
## Version Control
105+
To keep track of environments, all that is needed are these four files from the [template method](#template-method)
105106

106107
- `Setup.ps1`
107108
- `Setup.cmd`.
108109
- `NuGet.config`
109110
- `Bonsai.config`
110111

111-
You can now simply share this folder with another person who will be able to easily install your current Bonsai dependencies by running the `Setup.cmd` or `Setup.ps1` file.
112-
113-
As a final bonus, since all these files are encoded as text, they are easily version controlled using a distributed version control system such as [`git`](https://git-scm.com/). Because of this, we often keep this folder inside our project repository and keep updating it with new dependencies as the project evolves.
112+
Since all these files are encoded as text, they are easily version controlled using a distributed version control system such as [`git`](https://git-scm.com/). Because of this, we often keep this folder inside our project repository and keep updating it with new dependencies as the project evolves.
114113

115114
To prevent installed packages and other binary files from being tracked, you can add the following lines to the `.gitignore` file for convenience:
116115

0 commit comments

Comments
 (0)