Native
A Viash component can target one or more platforms. When targeting the native platform, a single executable is generated.
Requirements
The example targets the Native platform, which means you will need to have the required packages installed on your system depending on your chosen scripting language. Take a look at the Scripting Languages page to learn more about a specific language.
If you want to get started quickly, use Bash as that doesn’t have any external dependencies.
Creating the Viash component
Writing the script
For this example, you’ll use a tiny script that simply outputs “Hello World”, with the “World” part being a variable. To start off, create a folder named hello_world and follow the instructions below based on your preferred scripting language.
Create a new file named script.sh and copy the following content inside of it:
echo "Hello $par_input"Create a new file named script.csx and copy the following content inside of it:
Console.WriteLine("Hello " + par.input);Create a new file named script.js and copy the following content inside of it:
console.log('Hello' + par['input']);Create a new file named script.py and copy the following content inside of it:
print("Hello", par["input"])Create a new file named script.scala and copy the following content inside of it:
println("Hello " + par.input.get)Create a new file named script.R and copy the following content inside of it:
cat("Hello ", par$input)This outputs Hello to the console, followed by an input variable. Notice that this variable isn’t defined in the script itself as Viash will generate it for you based on the config file you’ll create in the next step.
Every scripting language has its own way of using variables generated by Viash. Check out the Scripting Languages for more information and examples.
Adding the config
A Viash config file is a YAML file that describes the functionality of a component and the platform(s) it targets. Create a file name config.vsh.yaml and add the contents below based on your chosen scripting language.
functionality:
name: hello_world
description: A minimal example component.
arguments:
- type: string
name: --input
default: "World"
resources:
- type: bash_script
path: script.sh
platforms:
- type: nativefunctionality:
name: hello_world
description: A minimal example component.
arguments:
- type: string
name: --input
default: "World"
resources:
- type: csharp_script
path: script.csx
platforms:
- type: nativefunctionality:
name: hello_world
description: A minimal example component.
arguments:
- type: string
name: --input
default: "World"
resources:
- type: javascript_script
path: script.js
platforms:
- type: nativefunctionality:
name: hello_world
description: A minimal example component.
arguments:
- type: string
name: --input
default: "World"
resources:
- type: python_script
path: script.py
platforms:
- type: nativefunctionality:
name: hello_world
description: A minimal example component.
arguments:
- type: string
name: --input
default: "World"
resources:
- type: scala_script
path: script.scala
platforms:
- type: nativefunctionality:
name: hello_world
description: A minimal example component.
arguments:
- type: string
name: --input
default: "World"
resources:
- type: r_script
path: script.R
platforms:
- type: nativeHere’s a breakdown of the different sections:
functionalitydescribes what the component does and the resources it needs. It contains thename, a shortdescriptionand more importantly, theargumentsandresourcesthe component uses.argumentscontains all arguments the component accepts and will be passed to the script. In this case, a singlestringnamed--inputresides here with a default value ofWorld.resourcesholds references to all necessary files and folders to make the component work. For this example, a single script is all that’s needed.
platformslists what platforms the component can target. This example targets thenativeplatform, which will generate an executable that’s run using the system resources.
Running
With the two necessary files present to create a component, you can call upon Viash to generate a temporary executable and run it:
viash run config.vsh.yamlWithout passing any arguments, this will use the default value for --input, which is World. This results in the following output:
Hello World
By providing the a value for --input, you can customize the greeting:
viash run config.vsh.yaml -- --input EarthThe double dash (--) between the viash command and the arguments is used to signify the end of the arguments passed to viash and the start of those passed to the component. If you forgot to add these, you’ll get an error similar to this:
[scallop] Error: Unknown option 'input'The example above results in this output:
Hello Earth
What’s next?
To learn how to use Viash to build an executable and run it, see the Native Executable Build Target page.