How to use the command 'gleam' (with examples)
Gleam is a modern programming language designed to build type-safe systems that scale reliably. It serves as a compiler, build tool, package manager, and code formatter all rolled into one, making it an efficient choice for developers. The commands associated with Gleam cover a wide range of functionalities such as project creation, building, running, testing, and formatting code, among others.
Use case 1: Creating a New Gleam Project
Code:
gleam new project_name
Motivation: When starting a new software development project using Gleam, initializing a new project structure is essential. This command helps set up a new Gleam project with all necessary files and directories, providing a starting point and a standard layout for development.
Explanation:
gleam
: This part of the command calls the Gleam tool itself, invoking its functionalities.new
: This subcommand tells Gleam to create a new project.project_name
: Replace this with the desired name of your project. It’s the identifier for your new project and usually should be descriptive of the project’s purpose.
Example Output:
Your Gleam project project_name has been created!
Use case 2: Building and Running a Gleam Project
Code:
gleam run
Motivation: To execute the code contained within your Gleam project, you need to build it and run it. This command automates the process of compiling your code and immediately executing it, making it incredibly efficient to test out changes quickly.
Explanation:
gleam
: This indicates the command line tool being used, which is Gleam in this case.run
: This subcommand indicates that the tool should compile and execute the current project code.
Example Output:
Compiling application project_name ...
Running project_name...
Use case 3: Building the Project
Code:
gleam build
Motivation: Separating the build process from running the application can be useful when you want to check for compilation errors without executing the program, or when preparing the project for deployment.
Explanation:
gleam
: This calls the Gleam tool.build
: This subcommand signals that the tool should compile the project’s codebase without running it.
Example Output:
Compiling application project_name ...
Build complete!
Use case 4: Running a Project for a Particular Platform and Runtime
Code:
gleam run --target platform --runtime runtime
Motivation: Different platforms and runtimes can have varying requirements or optimizations. Running the Gleam project on a specified platform and runtime allows developers to test and ensure compatibility across different environments.
Explanation:
gleam
: The base command line tool.run
: Specifies that the compiled code should be executed.--target platform
: This option allows specifying the platform target for the execution, such as a specific operating system.--runtime runtime
: This option allows specifying which runtime environment should execute the project, optimizing the running process for the intended environment.
Example Output:
Compiling application for platform on runtime ...
Running on platform with runtime...
Use case 5: Adding a Hex Dependency to Your Project
Code:
gleam add dependency_name
Motivation: Dependencies are external libraries your project might rely upon. Adding them allows your project to use functionalities provided by others, thus enhancing or extending the capabilities of your Gleam application.
Explanation:
gleam
: The command for utilizing Gleam’s features.add
: This subcommand indicates the addition of something to the project, in this case, a dependency.dependency_name
: This should be replaced with the actual name of the dependency you wish to add.
Example Output:
Adding dependency dependency_name to your project...
Dependency added successfully!
Use case 6: Running Project Tests
Code:
gleam test
Motivation: Running tests is a vital part of ensuring that recent changes or added features do not break existing functionality. Continuous testing helps maintain code quality and application reliability.
Explanation:
gleam
: This calls the Gleam runner.test
: This subcommand invokes any test cases defined within the project, executing them to verify the code functionality.
Example Output:
Running all tests...
All tests passed successfully!
Use case 7: Formatting Source Code
Code:
gleam format
Motivation: Consistent code formatting improves readability and maintainability. Automatically formatting the source code can save time and ensure conformity to style guides without manual effort.
Explanation:
gleam
: Start by specifying the Gleam command.format
: This subcommand is used to apply consistent coding styles to the source code automatically.
Example Output:
Formatting files...
Files formatted!
Use case 8: Type Checking the Project
Code:
gleam check
Motivation: Type checking is crucial for catching errors during the development phase. It ensures that the data types used in the program are accurate and compatible, reducing runtime errors and improving code stability.
Explanation:
gleam
: The command to execute Gleam functions.check
: This subcommand performs a type check across the codebase, verifying type correctness.
Example Output:
Type checking application ...
No type errors found!
Conclusion:
Utilizing the Gleam command line tool effectively can enhance the development experience by providing straightforward commands for project creation, dependency management, building, running, and maintaining code quality. Whether you’re creating a new project, running tests, or checking type consistency, Gleam’s comprehensive toolset offers robust support for building scalable, type-safe systems.