-
Make sure Strivex\Phing is installed
$ composer require strivexnl/strivex-phing
-
Load the autoloader in your build.xml
<property name="vendor.dir" value="${base.dir}/vendor" /> <includepath classpath="$(vendor.dir}/autoload.php" />
-
Define the custom tasks you want to use
<!-- Define the jsoneditor task --> <taskdef name="jsoneditor" classname="Strivex\Phing\Task\Json\JsonEditorTask" />
-
Use the JsonEditor in your target
<!-- Use the jsoneditor task --> <jsoneditor file="${file.path.to.json}"> <!-- You can use dot notation for the json keys! --> <add key="some.key" value="Wowsers!" overwrite="true" /> <delete key="another.key" /> <get key="name" propertyName="what.is.my.name" /> </jsoneditor>
As seen in the examples above, we define the JsonEditor like jsonedit
.
In the all our examples we will use this task name.
Task (1) | Class (2) | Description |
---|---|---|
jsoneditor | JsonEditorTask | The task to edit your JSON file |
1_We assume this task definition names._
2. The task is in the Strivex\Phing\Task\Json
namespace.
The attributes you can use on the jsoneditor task:
Parameter | Type | Description | Default | Required |
---|---|---|---|---|
file | String | Path to the JSON file | n/a | yes |
save | Boolean | Whether to save the changed JSON file. | true | no |
As seen in the examples above, we define the JsonEditor like jsonedit
.
In the all our examples we will use this task name.
This are sub tasks in the json editor:
Task | Description |
---|---|
add | Adds a value to the key to the json |
delete | Deletes the key from the json |
get | Gets the value from the key into a property |
bumpversion | Bumps a (semantic) version number |
The add task will add the key (with value) to the JSON.
Parameter | Type | Description | Default | Required |
---|---|---|---|---|
key | String | The key (in dot.notation ) in the JSON |
n/a | yes |
value | String | The value as string or JSON string | n/a | yes |
overwrite | Boolean | Whether to overwrite the key when it already exists. | true | no |
Example:
<jsoneditor file="${file.path.to.json}">
<add key="some.key" value="Some Value" overwrite="true" />
<add key="dont.overwrite" value${some.value} overwrite="false" />
</jsoneditor>
The delete task will delete the key from the JSON.
Parameter | Type | Description | Default | Required |
---|---|---|---|---|
key | String | The key (in dot.notation ) to delete |
n/a | yes |
Example:
<jsoneditor file="${file.path.to.json}">
<delete key="some.key" />
</jsoneditor>
The get task will get the key from the JSON.
Parameter | Type | Description | Default | Required |
---|---|---|---|---|
key | String | The key (in dot.notation ) to get |
n/a | yes |
propertyName | String | The name of the property to store the value | n/a | yes |
Example:
<jsoneditor file="${file.path.to.json}">
<get key="some.key.to.get" />
</jsoneditor>
The bumpversion task will bump the sementic version from the key in the JSON.
Parameter | Type | Description | Default | Required |
---|---|---|---|---|
key (1) | String | The key (in dot.notation ) to the version . |
n/a | yes |
type | String | Type of bump. Possible values: - major - minor - patch - alpha - beta - RC |
n/a | yes |
startPreRelease | Boolean | Whether or not to start with a prerelease (alpha ) when bumping. |
false | no |
overwrite | Boolean | Whether to overwrite the key when it already exists. | true | no |
- It is not only the
version
like inComposer.json
. When the key contains semantic versioning, it can be bumped.
Example:
<jsoneditor file="${file.path.to.json}">
<get key="some.key.to.get" />
</jsoneditor>