### Amber Framework: Project Setup and Management Commands Source: https://github.com/amberframework/docs/blob/master/getting-started/README.md This snippet provides a sequence of commands to initialize a new Amber application, navigate into its directory, install dependencies, generate a scaffold for a new model, create and migrate the database, and start the development server for live watching. ```shell amber new pet-tracker cd pet-tracker shards install amber generate scaffold Pet name:string breed:string age:integer amber db create migrate amber watch ``` -------------------------------- ### Terminal Example: Creating and Running Amber App with Recipe Source: https://github.com/amberframework/docs/blob/master/cli/recipes.md Illustrates a complete workflow for initializing an Amber application using a specific recipe (`damianham/amber_modular`), installing its dependencies, and starting the development server. This example demonstrates the output of `amber new`, `shards install`, and `amber watch` commands, showcasing the rapid setup process. ```text $ amber new microsecond-blog -r damianham/amber_modular Rendering App microsecond-blog in ./microsecond-blog new .amber.yml new .amber_secret_key new .gitignore new .travis.yml new config/application.cr new config/database.yml ... [clipped] new src/views/layouts/_nav.slang new src/views/layouts/application.slang new src/microsecond-blog.cr $ cd microsecond-blog/ $ shards install Updating https://github.com/amberframework/amber.git Updating https://github.com/luislavena/radix.git ... [clipped] Installing garnet_spec (0.1.1) Installing selenium (0.3.0) $ amber watch 02:58:23 Watcher | (INFO) Watching 22 files (server reload)... 02:58:23 Watcher | (INFO) Building project App01... 02:58:31 Watcher | (INFO) Terminating app App01... 02:58:31 Watcher | (INFO) Starting App01... 02:58:31 NodeJS | (INFO) Installing dependencies... 02:58:31 NodeJS | (INFO) Watching public directory 02:58:31 Server | (INFO) Amber 0.10.0 serving application "App01" at http://0.0.0.0:3000 02:58:31 Server | (INFO) Server started in development. 02:58:31 Server | (INFO) Startup Time 00:00:00.000182000 02:58:31 Watcher | Watching 10 client files... ``` -------------------------------- ### Install Amber Framework and Crystal Source: https://github.com/amberframework/docs/blob/master/getting-started/README.md Instructions for installing the Amber web framework and its Crystal language dependency on macOS (Homebrew, MacPorts) and Debian/Ubuntu (apt-get, Linuxbrew). This covers installing Crystal first for Debian/Ubuntu, then Amber from source or via package managers. ```Bash brew tap amberframework/amber brew install amber ``` ```Bash sudo port selfupdate sudo port install amber ``` ```Bash curl -fsSL https://crystal-lang.org/install.sh | sudo bash ``` ```Bash sudo apt-get install libreadline-dev libsqlite3-dev libpq-dev libmysqlclient-dev libssl-dev libyaml-dev libpcre3-dev libevent-dev git clone https://github.com/amberframework/amber.git cd amber git checkout v1.4.1 shards install make sudo make install ``` ```Bash brew tap amberframework/amber brew install amber ``` -------------------------------- ### Build and Run Amber Application with Watch Mode Source: https://github.com/amberframework/docs/blob/master/getting-started/README.md Start the Amber development server using `amber watch`. This command builds the application binary, launches the server, and automatically recompiles and restarts the application upon detecting code changes, facilitating rapid development. ```Bash amber watch ``` -------------------------------- ### Create and Migrate Amber Database Source: https://github.com/amberframework/docs/blob/master/getting-started/README.md Execute `amber db create migrate` to set up the database for the Amber application. This command first creates the database and then runs all pending migrations, typically setting up tables based on generated scaffolds. ```Bash amber db create migrate ``` -------------------------------- ### Generate a New Amber Application Source: https://github.com/amberframework/docs/blob/master/getting-started/README.md Use the `amber new` command to create a new Amber project, followed by changing into the new project directory. This command scaffolds a new full-stack web application. ```Bash amber new pet-tracker cd pet-tracker ``` -------------------------------- ### Amber CLI: Full Project Generation and Setup Workflow Source: https://github.com/amberframework/docs/blob/master/cli/new.md Illustrates the complete terminal output when creating a new Amber project, installing dependencies, and starting the development server. It shows the files generated, dependency installation process, and server startup logs. ```Shell $ amber new microsecond-blog Rendering App microsecond-blog in ./microsecond-blog new .amber.yml new .amber_secret_key new .gitignore new .travis.yml new config/application.cr new config/database.yml ... [clipped] new src/views/layouts/_nav.slang new src/views/layouts/application.slang new src/microsecond-blog.cr $ cd microsecond-blog/ $ shards install Updating https://github.com/amberframework/amber.git Updating https://github.com/luislavena/radix.git ... [clipped] Installing garnet_spec (0.1.1) $ amber watch 02:58:23 Watcher | (INFO) Watching 22 files (server reload)... 02:58:23 Watcher | (INFO) Building project microsecond-blog... 02:58:31 Watcher | (INFO) Terminating app microsecond-blog... 02:58:31 Watcher | (INFO) Starting microsecond-blog... 02:58:31 Server | (INFO) Amber 0.7.2 serving application "microsecond-blog" at http://0.0.0.0:3000 02:58:31 Server | (INFO) Server started in development. 02:58:31 Server | (INFO) Startup Time 00:00:00.000182000 02:58:31 Watcher | Watching 10 client files... ``` -------------------------------- ### Generate a RESTful Resource with Amber Scaffold Source: https://github.com/amberframework/docs/blob/master/getting-started/README.md Utilize `amber generate scaffold` (or `amber g scaffold`) to create a new RESTful resource. This command generates models, views, controllers, and migrations for the specified resource, including defined attributes like `name`, `breed`, and `age`. ```Bash amber g scaffold Pet name:string breed:string age:integer ``` -------------------------------- ### Install Amber CLI Dependencies and Build from Source on Linux Source: https://github.com/amberframework/docs/blob/master/guides/installation.md This snippet provides commands to install necessary development libraries for compiling Amber CLI on Debian/Ubuntu-based systems, clone the Amber repository, checkout a specific version, install shards, and build/install the CLI. ```bash sudo apt-get install libreadline-dev libsqlite3-dev libpq-dev libmysqlclient-dev libssl-dev libyaml-dev libpcre3-dev libevent-dev git clone https://github.com/amberframework/amber.git cd amber git checkout v1.4.1 shards install make sudo make install ``` -------------------------------- ### Install Crystal project dependencies with shards Source: https://github.com/amberframework/docs/blob/master/cookbook/from-scratch.md Output of the `shards install` command, illustrating the process of fetching and installing Amber and its various transitive dependencies from their respective GitHub repositories. This command resolves and downloads all required libraries. ```text $ shards install Fetching https://github.com/amberframework/amber.git Fetching https://github.com/amberframework/router.git Fetching https://github.com/amberframework/cli.git Fetching https://github.com/mosop/optarg.git Fetching https://github.com/mosop/callback.git Fetching https://github.com/mosop/string_inflection.git Fetching https://github.com/elorest/compiled_license.git Fetching https://github.com/jeromegn/kilt.git Fetching https://github.com/amberframework/micrate.git Fetching https://github.com/crystal-lang/crystal-db.git Fetching https://github.com/crystal-lang/crystal-mysql.git Fetching https://github.com/will/crystal-pg.git Fetching https://github.com/stefanwille/crystal-redis.git Fetching https://github.com/jwaldrip/shell-table.cr.git Fetching https://github.com/jeromegn/slang.git Fetching https://github.com/crystal-lang/crystal-sqlite3.git Fetching https://github.com/phoffer/inflector.cr.git Fetching https://github.com/amberframework/teeplate.git Installing amber (0.7.2) Installing amber_router (0.0.3) Installing cli (0.7.0) Installing optarg (0.5.8) Installing callback (0.6.3) Installing string_inflection (0.2.1) Installing compiled_license (0.1.3) Installing kilt (0.4.0) Installing micrate (0.3.1) Installing db (0.5.0) Installing mysql (0.4.0) Installing pg (0.14.1) Installing redis (1.9.0) Installing shell-table (0.9.2) Installing slang (1.7.1) Installing sqlite3 (0.9.0) Installing inflector (0.1.8) Installing teeplate (0.5.0) ``` -------------------------------- ### Build and run Amber project Source: https://github.com/amberframework/docs/blob/master/cookbook/from-scratch.md Demonstrates the commands to compile an Amber project using `shards build myapp` and then run the compiled executable with `bin/myapp`. The output shows the server starting up and listening on a specified port. ```text $ shards build myapp Dependencies are satisfied Building: myapp $ bin/myapp 06:52:37 Server | (INFO) Amber 0.7.2 serving application "Amber_app" at http://localhost:3000 06:52:37 Server | (INFO) Server started in development. 06:52:37 Server | (INFO) Startup Time 00:00:00.000273000 ``` -------------------------------- ### Verify Amber CLI Installation Source: https://github.com/amberframework/docs/blob/master/guides/create-new-app.md Checks the installed version of the Amber CLI to confirm successful installation and readiness for project creation. ```bash $ amber -v Amber CLI (amberframework.org) - vX.Y.Z ``` -------------------------------- ### Amber CLI: Quick Start with `amber new` and SQLite Source: https://github.com/amberframework/docs/blob/master/cli/new.md Demonstrates a quick workflow to generate, navigate into, and run a new Amber web application using the `amber new` command with a SQLite database. It shows how to start the development server and open the application in a browser. ```Shell amber new microsecond-blog -d sqlite cd microsecond-blog amber watch open http://localhost:3000 ``` -------------------------------- ### Main application entry point in src/myapp.cr Source: https://github.com/amberframework/docs/blob/master/cookbook/from-scratch.md Crystal code for `src/myapp.cr`, which serves as the main entry point of the application. It requires all configuration files and then starts the Amber server, making the application ready to handle incoming requests. ```Crystal require "../config/*" Amber::Server.start ``` -------------------------------- ### Install Amber CLI Compilation Dependencies for RedHat/CentOS Source: https://github.com/amberframework/docs/blob/master/guides/installation.md These commands install development tools and libraries required for compiling the Amber CLI on RedHat and CentOS systems. ```bash sudo yum groupinstall development tools sudo yum install readline-devel sqlite-devel openssl-devel libyaml-devel gc-devel libevent-devel ``` -------------------------------- ### Install Amber CLI using Homebrew on macOS Source: https://github.com/amberframework/docs/blob/master/guides/installation.md These commands add the Amber Homebrew tap and then install the Amber CLI, which also installs Crystal as a dependency. ```bash brew tap amberframework/amber brew install amber ``` -------------------------------- ### Run the Amber development server Source: https://github.com/amberframework/docs/blob/master/examples/amber-auth.md Start the Amber application's local development server using `amber watch`, which compiles the project and serves it, typically with live-reloading capabilities. ```bash amber watch ``` -------------------------------- ### Full Example: Controller, View, and Layout Rendering Source: https://github.com/amberframework/docs/blob/master/guides/views/README.md Provides a comprehensive example demonstrating how a Crystal controller renders a Slang view template, which is then injected into a Slang layout. It illustrates the interaction between the controller, view, and layout files, including the use of the `content` variable for view injection. ```Crystal class HomeController < ApplicationController LAYOUT = "application.slang" def index render "index.slang" end end ``` ```Slang div h2 Welcome to Amber Framework! p Thank you for trying out the Amber Framework. We are working hard to provide a super fast and reliable framework that provides all the productivity tools you are used too but not sacrificing the speed. ``` ```Slang doctype html html head title Blog using Amber meta charset="utf-8" link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" body .masthead .container nav.nav == render(partial: "layouts/_nav.slang") .container .row .col-sm-12.main == content script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" script src="/dist/main.bundle.js" ``` -------------------------------- ### Example Output of Amber Watch Command Source: https://github.com/amberframework/docs/blob/master/cli/watch.md This snippet shows a typical output when running `amber watch`, illustrating the build, termination, and startup phases, along with server and client-side watcher information. ```bash $ amber watch 02:58:23 Watcher | (INFO) Watching 22 files (server reload)... 02:58:23 Watcher | (INFO) Building project App01... 02:58:31 Watcher | (INFO) Terminating app App01... 02:58:31 Watcher | (INFO) Starting App01... 02:58:31 NodeJS | (INFO) Installing dependencies... 02:58:31 NodeJS | (INFO) Watching public directory 02:58:31 Server | (INFO) Amber 0.7.2 serving application "App01" at http://0.0.0.0:3000 02:58:31 Server | (INFO) Server started in development. 02:58:31 Server | (INFO) Startup Time 00:00:00.000182000 02:58:31 Watcher | Watching 10 client files... ``` -------------------------------- ### Install Amber CLI via AUR on ArchLinux Source: https://github.com/amberframework/docs/blob/master/guides/installation.md This command uses `yaourt` to install the Amber CLI from the Arch User Repository (AUR), automatically handling dependencies. ```bash yaourt -S amber ``` -------------------------------- ### Install Amber CLI Compilation Dependencies for Debian/Ubuntu Source: https://github.com/amberframework/docs/blob/master/guides/installation.md This command installs essential build tools and development libraries required to compile the Amber CLI on Debian and Ubuntu distributions. ```bash sudo apt-get install build-essential libreadline-dev libsqlite3-dev libpq-dev libmysqlclient-dev libssl-dev libyaml-dev ``` -------------------------------- ### Installing a Modified Amber Framework Plugin Locally Source: https://github.com/amberframework/docs/blob/master/cli/plugins.md This sequence of shell commands demonstrates how to download, modify, and install an Amber Framework plugin from a local directory. It involves cloning the plugin repository, making changes, navigating to your web application, and then using `amber plugin add` to install the local version, followed by configuration and database migration steps. ```Shell git clone https://github.com/amberplugin/authorize.git ~/myauthorize # modify the plugin in ~/myauthorize in some way cd ~/myamber_webapp amber plugin add ~/myauthorize configure the plugin initializers and hooks integrate the plugin routes into your Amber application amber db migrate ``` -------------------------------- ### Example Amber System Tests for User Interactions Source: https://github.com/amberframework/docs/blob/master/guides/testing/system-tests.md Demonstrates how to write system tests in Amber Framework using the `GarnetSpec::System::Test` class. Examples include scenarios for visiting web pages, clicking elements, waiting for actions, and asserting content or attributes on the page. ```ruby class SomeFeature < GarnetSpec::System::Test scenario "user visits amber framework and sees getting started button" do visit "http://www.amberframework.org" timeout 1000 click_on(:css, "header a.btn.btn-primary") wait 2000 element(:tag_name, "body").text.should contain "Introduction" end scenario "user visits amberframwork homepage and sees logo" do visit "http://www.amberframework.org" wait 2000 element(:class_name, "img-amber-logo").attribute("src").should match( %r(https://www.amberframework.org/assets/img/amber-logo-t-bg.png) ) end end ``` -------------------------------- ### Installing and Contributing Amber Framework Plugins from GitHub Source: https://github.com/amberframework/docs/blob/master/cli/plugins.md After forking and modifying an existing Amber Framework plugin on GitHub, this command allows users to install it directly from their GitHub repository. To contribute the plugin to the Amber community, add the 'amber-plugin' topic to the repository, making it discoverable in the official list. ```Shell amber plugin add your_github_name/your_plugin_name ``` -------------------------------- ### Install Project Shards Source: https://github.com/amberframework/docs/blob/master/guides/models/jennifer/README.md Runs the `shards update` command to install all specified dependencies, including Jennifer and its associated components, into the Amber project. This command fetches and installs the libraries defined in `shard.yml`. ```bash shards update ``` -------------------------------- ### Start Amber Server in Watch Mode Source: https://github.com/amberframework/docs/blob/master/guides/create-new-app.md Starts the Amber development server, which automatically recompiles and reloads the application whenever source files are changed, facilitating rapid development. ```bash amber watch ``` -------------------------------- ### Install Amber CLI using MacPorts on macOS Source: https://github.com/amberframework/docs/blob/master/guides/installation.md These commands update MacPorts and then install the Amber CLI, which also installs Crystal as a dependency. ```bash sudo port selfupdate sudo port install amber ``` -------------------------------- ### Output of running development server Source: https://github.com/amberframework/docs/blob/master/examples/amber-auth.md This output shows the Amber development server starting up, including the compilation and building process, and confirms the application is being served at `http://0.0.0.0:3000` in development mode. ```text 08:53:52 Watcher | Terminating app Blogsy... 08:53:52 Watcher | Compiling Blogsy... 08:53:52 Watcher | Building project Blogsy... 08:54:05 Server | [Amber 0.6.1] serving application "Blogsy" at http://0.0.0.0:3000 08:54:05 Server | Server started in development. 08:54:05 Server | Startup Time 00:00:00.000596000 ``` -------------------------------- ### Create Amber Database Source: https://github.com/amberframework/docs/blob/master/cli/database.md Demonstrates how to create a new database for an Amber application using `amber db create`. Includes an example of creating a new project and then its database, highlighting changes in database naming conventions and default PostgreSQL user from version 0.3.5. ```text $ amber db create Created database blog_development ``` ```text $ amber new microsecond-blog $ cd microsecond-blog $ amber db create Created database microsecond_blog_development ``` -------------------------------- ### Install Chromedriver and Selenium on macOS Source: https://github.com/amberframework/docs/blob/master/guides/testing/system-tests.md Instructions for installing `chromedriver` and `selenium-server-standalone` on macOS using Homebrew, which places them in the default system path `/usr/local/bin/chromedriver`. ```bash brew install selenium-server-standalone brew install chromedriver ``` -------------------------------- ### Running Amber Database Initialization Commands Source: https://github.com/amberframework/docs/blob/master/troubleshooting.md This command ensures the database is properly set up and migrated for an Amber project. It drops, recreates, and migrates the database schema, which is often necessary before executing the project. This is particularly useful after configuration changes or during initial project setup. ```bash bin/amber db drop create migrate ``` -------------------------------- ### Getting Amber CLI Command Help Source: https://github.com/amberframework/docs/blob/master/cli/README.md Demonstrates how to access help documentation for any Amber CLI command by appending the `-h` or `--help` flag. ```text $ amber --help ``` -------------------------------- ### Configure 'Hello World' route in Amber Source: https://github.com/amberframework/docs/blob/master/cookbook/hello-world.md This snippet adds a new GET route for '/hello' to the Amber application's `config/routes.cr` file. It maps the '/hello' path to the `hello` method of the `HelloController`, making the 'Hello World' response accessible via the web. ```Crystal Amber::Server.configure do |app| pipeline :web do # pipelines... end routes :web do # other routes,,, get "/hello", HelloController, :hello end end ``` -------------------------------- ### Resolve Memory Allocation Error with Swapfile on Linux Source: https://github.com/amberframework/docs/blob/master/guides/installation.md If compilation fails due to insufficient memory, these commands create and activate a swapfile to provide additional virtual memory, resolving the 'Cannot allocate memory' error. ```bash sudo dd if=/dev/zero of=/swapfile bs=2k count=1024k sudo mkswap /swapfile sudo chmod 600 /swapfile sudo swapon /swapfile ``` -------------------------------- ### Amber View Example: Slang Template for User List Source: https://github.com/amberframework/docs/blob/master/guides/overview.md This Slang template demonstrates a basic view file (`index.slang`) for displaying a list of users. It includes HTML structure, inline CSS, and iterates over an `@users` collection to display each user's name and email, showcasing how views are populated with model data. ```Slang # Slang - /views/users/index.slang doctype html html head meta name="viewport" content="width=device-width,initial-scale=1.0" title This is a title css: h1 {color: red;} p {color: green;} style h2 {color: blue;} body ul - @users.each do |user| li = "#{user.name} - #{user.email}" ``` -------------------------------- ### Example: Handling File Upload in Amber Controller Source: https://github.com/amberframework/docs/blob/master/cookbook/file-upload.md Provides a practical example of implementing a `create` method in an Amber controller to handle file uploads via a POST request. It shows how to access the `Amber::Router::File` object for the uploaded file and other related parameters from the request body. ```Crystal # header: `accept: audio/mp3` # # POST body # { # "audio_file": // binary data for the audio file # "file_name": "some_file_name.mp3" # } def create uploaded_file = params.files["audio_file"] uploaded_file_name = params["file_name"] # Do whatever you want with the files and your method 😄 end ``` -------------------------------- ### Initialize Git and Deploy Amber App to Heroku Source: https://github.com/amberframework/docs/blob/master/deployment/heroku.md These commands guide you through initializing a new Git repository for your Amber project, associating it with your Heroku application, staging and committing your code, and finally pushing it to the Heroku remote. This initiates the deployment process on Heroku. ```bash $ git init $ heroku git:remote -a [app-name] $ git add -A $ git commit -m "My first Amber app" $ git push heroku master ``` -------------------------------- ### Initialize Quartz Mailer in Amber Source: https://github.com/amberframework/docs/blob/master/guides/configuration.md Example of an initializer file (`config/initializers/mailer.cr`) that configures the Quartz mailer with SMTP settings, pulling values from Amber settings or environment variables. ```crystal # config/initializers/mailer.cr require "quartz_mailer" Quartz.config do |c| c.smtp_enabled = Amber.settings.smtp.enabled c.smtp_address = ENV["SMTP_ADDRESS"]? || Amber.settings.smtp.host c.smtp_port = ENV["SMTP_PORT"]? || Amber.settings.smtp.port c.username = ENV["SMTP_USERNAME"]? || Amber.settings.smtp.username c.password = ENV["SMTP_PASSWORD"]? || Amber.settings.smtp.password c.use_authentication = !c.password.blank? } ``` -------------------------------- ### Generating a New Controller with Actions Source: https://github.com/amberframework/docs/blob/master/cli/generate.md This command generates a new controller named 'Person' and defines specific actions with their HTTP methods. It includes `index` (GET), `show` (GET), `create` (POST), and `update` (PATCH) actions, along with the controller spec file. ```bash $ amber g controller Person index:get show:get create:post update:patch ``` -------------------------------- ### Amber Controller Example: UsersController Source: https://github.com/amberframework/docs/blob/master/guides/overview.md This Ruby code defines a `UsersController` in an Amber application, inheriting from `ApplicationController`. The `index` method retrieves all users and renders the `index.slang` view, demonstrating basic controller logic for handling requests and rendering views. ```Ruby class UsersController < ApplicationController def index @users = Users.all render("index.slang") end } ``` -------------------------------- ### Compile local Amber CLI executable Source: https://github.com/amberframework/docs/blob/master/cookbook/from-scratch.md Command to compile a local Amber CLI executable from the framework's source. This allows developers to use project-specific CLI operations like scaffolding and migrations without needing a global Amber CLI installation. ```Crystal crystal build lib/amber/src/amber/cli.cr -o bin/amber -s ``` -------------------------------- ### Example Crystal Script for Amber Exec File Mode Source: https://github.com/amberframework/docs/blob/master/cli/exec.md A simple Crystal script demonstrating basic string interpolation and output, intended to be run via `amber exec` in file mode. ```Crystal statement = "cool stuff happened" emoticon = "^_^" puts "#{statement} #{emoticon}" ``` -------------------------------- ### Get Current Amber Environment Source: https://github.com/amberframework/docs/blob/master/guides/configuration.md Demonstrates how to retrieve the current application environment using `Amber.env` or check specific environments with helper methods like `Amber.env.development?`. ```ruby Amber.env # returns => :production ``` -------------------------------- ### Using a Modified Amber Recipe Locally Source: https://github.com/amberframework/docs/blob/master/cli/recipes.md This snippet demonstrates how to clone an existing Amber recipe, modify it, and then use the local path to generate a new Amber application. It also includes steps for installing dependencies and running the application. ```sh git clone https://github.com/damianham/amber_modular.git ~/mymodular # modify the recipe in ~/mymodular some way amber new microsecond-blog -r ~/mymodular cd microsecond-blog shards install amber watch ``` -------------------------------- ### Automate Amber Deployment with Git post-receive Hook Source: https://github.com/amberframework/docs/blob/master/deployment/manual-deploy.md This Bash script serves as a `post-receive` Git hook to automate the deployment of an Amber Framework application. It sets the production environment, checks out the latest code, installs Crystal shards, compiles the Amber CLI and the application in release mode, migrates the database, and restarts the systemd service. This enables a 'git push production' deployment workflow. ```Bash #!/bin/bash set -e export AMBER_ENV=production if [ "$GIT_DIR" = "." ]; then # The script has been called as a hook; chdir to the working copy cd .. unset GIT_DIR fi # try to obtain the usual system PATH if [ -f /etc/profile ]; then PATH=$(source /etc/profile; echo $PATH) export PATH fi # get the current branch head="$(git symbolic-ref HEAD)" # read the STDIN to detect if this push changed the current branch while read oldrev newrev refname do [ "$refname" = "$head" ] && break done # abort if there's no update, or in case the branch is deleted if [ -z "${newrev//0}" ]; then exit fi # check out the latest code into the working copy umask 002 git reset --hard logfile=log/deploy.log if [ -z "${oldrev//0}" ]; then # this is the first push; this branch was just created mkdir -p log tmp bin chmod 0775 log tmp bin touch $logfile chmod 0664 $logfile # init submodules git submodule update --recursive --init 2>&1 | tee -a $logfile else # log timestamp echo ==== $(date) ==== >> $logfile ################################################################################ #### BEGIN: Amberframework specific Code ##### ################################################################################ shards install if [ -f bin/amber ]; then echo "amber already installed" else crystal build lib/amber/src/amber/cli.cr -o bin/amber --stats fi echo "Migrating..." ./bin/amber db create migrate || true echo "Building application in release mode" crystal build src/amberframework.cr -o bin/amberframework --release --stats systemctl status amberframework sudo systemctl restart amberframework systemctl status amberframework fi ``` -------------------------------- ### Generate a new Amber application Source: https://github.com/amberframework/docs/blob/master/examples/amber-auth.md Use the `amber new` command to create a new Amber project, specifying the application name, automatic dependency installation (`--deps`), and the database type (`-d sqlite`). ```bash amber new blogsy --deps -d sqlite ``` -------------------------------- ### Run Amber Application Locally with Heroku CLI Source: https://github.com/amberframework/docs/blob/master/deployment/heroku.md After compiling your Amber application and configuring your `Procfile` and `.env` file, this command starts your application locally using the `heroku local` command. It simulates the Heroku runtime environment, allowing you to test your application's behavior before deploying it to Heroku. ```bash heroku local ``` -------------------------------- ### Amber CLI: `amber new` Command Syntax and Options Source: https://github.com/amberframework/docs/blob/master/cli/new.md Documents the `amber new` command, which generates a new Amber project. It details the arguments and various options available for customizing the project creation, such as database engine, template engine, and dependency installation. ```APIDOC amber new [OPTIONS] NAME Generates a new Amber project Arguments: NAME name/path of project Options: -d Select the database database engine, can be one of: pg | mysql | sqlite (default: pg) --minimal Does not install npm dependencies --no-color Disable colored output --no-deps Does not install dependencies, this avoids running shards update -r Use a named recipe. See documentation at https://docs.amberframework.org/amber/cli/recipes. -t Selects the template engine language, can be one of: slang | ecr (default: slang) -y, --assume-yes Assume yes to disable interactive mode -h, --help show this help ``` -------------------------------- ### Handling Array Parameters in Amber Source: https://github.com/amberframework/docs/blob/master/guides/controllers/params.md Illustrates how to send and retrieve array-based parameters in Amber. It shows the URL format for array parameters and how to use `params.fetch_all` to get all values for a given key as an array. ```Text ?brand[]=brand1&brand[]=brand2&brand[]=brand3 ``` ```Ruby params.fetch_all("brand[]") # => { "brand[]" => ["brand1", "brand2", "brand3"] } ``` -------------------------------- ### Install Amber Plugin via CLI Source: https://github.com/amberframework/docs/blob/master/cli/plugins.md Adds a plugin to your Amber application. The plugin_spec can be a plugin from the amberplugin GitHub organization (e.g., authorize), a plugin from another GitHub account (e.g., githubaccount/plugin_name), or a URL to a zip file containing plugin templates. This command fetches and integrates the plugin's files into your project. ```Shell $ amber plugin add ``` -------------------------------- ### Configure Static File Handling in Amber Source: https://github.com/amberframework/docs/blob/master/cookbook/file-download.md This configuration snippet for `config/application.cr` sets up a pipeline named `static` to process static content, including `PoweredByAmber`, `Error`, and `Static` pipes. It also defines a `get` route for `/*` that uses `Amber::Controller::Static` to serve files from the `./public` directory. ```ruby Amber::Server.configure do |app| # All static content will run these transformations pipeline :static do plug Amber::Pipe::PoweredByAmber.new plug Amber::Pipe::Error.new plug Amber::Pipe::Static.new("./public") end routes :static do get "/*", Amber::Controller::Static, :index end end ``` -------------------------------- ### Initialize a new Crystal application Source: https://github.com/amberframework/docs/blob/master/cookbook/from-scratch.md Demonstrates the output of the `crystal init app myapp` command, showing the default files and directories created for a new Crystal project, including configuration files, source files, and test files. ```text $ crystal init app myapp create myapp/.gitignore create myapp/.editorconfig create myapp/LICENSE create myapp/README.md create myapp/.travis.yml create myapp/shard.yml create myapp/src/myapp.cr create myapp/src/myapp/version.cr create myapp/spec/spec_helper.cr create myapp/spec/myapp_spec.cr Initialized empty Git repository in /home/main/myapp/.git/ ``` -------------------------------- ### Amber Model Example: User Class Definition Source: https://github.com/amberframework/docs/blob/master/guides/overview.md This Ruby (Crystal) code defines a simple `User` model with `name` and `email` properties. It includes a constructor to initialize these properties, illustrating how model objects represent central data entities and business logic in an Amber application. ```Ruby class User getter name : String getter email : String def initialize(@name, @email) end } ``` -------------------------------- ### Define Heroku Procfile for Amber Application Source: https://github.com/amberframework/docs/blob/master/deployment/heroku.md This `Procfile`, placed at the root of your Amber application, defines the processes Heroku should run. The `release` process ensures database migrations are executed before the application starts, while the `web` process specifies the command to launch your compiled Amber application, making it accessible via HTTP. ```yaml release: bin/amber db migrate web: bin/{your-app-name} ``` -------------------------------- ### Amber Generator Multi-Word Resource Naming Conventions Source: https://github.com/amberframework/docs/blob/master/cli/generate.md This example illustrates how Amber generators handle multi-word resource names. It demonstrates that both `CamelCase` and `snake_case` inputs are accepted on the command line, which Amber then consistently converts to `CamelCase` for class names and `snake_case` for file names. ```text $ amber generate [TYPE] MultiWordName [FIELD1 FIELD2 ...] # OR $ amber generate [TYPE] multi_word_name [FIELD1 FIELD2 ...] ``` -------------------------------- ### Initialize Sam Task Runner with Jennifer Dependencies (Crystal) Source: https://github.com/amberframework/docs/blob/master/guides/models/jennifer/README.md This Crystal code snippet demonstrates the initial setup for `sam.cr`, which operationalizes the `sam` task runner. It includes necessary `require` statements for Jennifer ORM and its PostgreSQL adapter, along with project-specific configurations and migrations, enabling `sam` to execute database-related tasks. ```Crystal require "jennifer" require "jennifer/adapter/postgres" require "../config/jennifer" require "../db/migrations/*" require "sam" require "jennifer/sam" load_dependencies "jennifer" Sam.help ``` -------------------------------- ### Set up Dokku Application and PostgreSQL Database Source: https://github.com/amberframework/docs/blob/master/deployment/dokku.md Commands to create a new Dokku application and provision a PostgreSQL database, then link it to the application. This sets up the necessary infrastructure on the Dokku host. ```text # on the Dokku host dokku apps:create amber-app # Setup postgres db sudo dokku plugin:install https://github.com/dokku/dokku-postgres.git dokku postgres:create amber-db # This sets the DATABASE_URL env var dokku postgres:link amber-db amber-app ``` -------------------------------- ### Simplify Amber Views with Partials Source: https://github.com/amberframework/docs/blob/master/guides/views/README.md Partials can be used as subroutines to move common content out of main views, making them easier to read and manage. This example illustrates how '_ad_banner.html.ecr' and '_footer.html.ecr' partials can be rendered to include shared content without cluttering the main view's logic. ```markup <%= render "shared/ad_banner" %>

Products

Here are a few of our fine products:

... <%= render "shared/footer" %> ``` -------------------------------- ### Full Example: Recovering an Archived Article via Amber Exec Source: https://github.com/amberframework/docs/blob/master/cli/exec.md Illustrates a complete scenario of using `amber exec` to find and update an archived article's status. It shows how to inspect an article, modify its `archived` flag, and verify the changes, highlighting the compilation time and the importance of nil checks. ```text # Be patient, it takes a few seconds - this is compiling your entire app $ amber exec 'Article.find(1)' # # Those if statements are important, if you have a nilable type (Article | Nil) $ amber exec 'a = Article.find(1); (a.archived = false) if a; a.save if a;' true # Notice the update `@archived=false` $ amber exec 'Article.find(1)' # ``` -------------------------------- ### Minimal Amber Application Configuration Source: https://github.com/amberframework/docs/blob/master/examples/minimal-configuration.md Demonstrates how to configure and run a basic Amber application from a single Ruby file, including a controller and routes. ```ruby require "amber" class HelloController < Amber::Controller::Base def index "hello world" end end Amber::Server.configure do |app| pipeline :api do end routes :api do get "/", HelloController, :index end end Amber::Server.start ``` -------------------------------- ### Configure Amber application in config/application.cr Source: https://github.com/amberframework/docs/blob/master/cookbook/from-scratch.md Crystal code for `config/application.cr` that requires the Amber framework and all controller files from the `src/controllers` directory. This file is crucial for setting up the application's initial dependencies and loading controllers. ```Crystal require "amber" require "../src/controllers/**" ``` -------------------------------- ### Basic Amber project directory structure Source: https://github.com/amberframework/docs/blob/master/cookbook/from-scratch.md Illustrates the minimal directory structure required for an Amber project. It shows the `config` folder containing `application.cr` and `routes.cr`, and the `src` folder with `controllers` and the main application file `myapp.cr`. ```text . ├── config │ ├── application.cr │ └── routes.cr └── src ├── controllers │ └── application_controller.cr └── myapp.cr ``` -------------------------------- ### Seed Amber Database Source: https://github.com/amberframework/docs/blob/master/cli/database.md Demonstrates how to add initial data to the database after creation using `amber db seed`. ```text $ amber db seed Seeded database ``` -------------------------------- ### Deploy Amber Application to Dokku via Git Source: https://github.com/amberframework/docs/blob/master/deployment/dokku.md Standard Git commands to initialize a repository, add the Dokku remote, stage changes, commit, and push the application to the Dokku host for deployment. ```text $ git init $ git remote add dokku dokku@{yourdokku.com}:{your-app-name} $ git add -A $ git commit -m "My first Amber app" $ git push dokku master ``` -------------------------------- ### Get Amber Database Version Source: https://github.com/amberframework/docs/blob/master/cli/database.md Shows how to retrieve the current database version using `amber db version`. ```text $ amber db version 20170928204246 ``` -------------------------------- ### Define an enum column for MySQL Source: https://github.com/amberframework/docs/blob/master/guides/models/jennifer/migrations.md Example of creating an enum column in MySQL by specifying the enum values directly within the table definition. ```Crystal create_table(:contacts) do |t| t.enum(:gender, values: ["male", "female"]) end ``` -------------------------------- ### Create and migrate the database Source: https://github.com/amberframework/docs/blob/master/examples/amber-auth.md Run the `amber db create migrate` command to initialize the database for the application and apply all pending migrations, setting up the necessary tables like the 'users' table. ```bash amber db create migrate ``` -------------------------------- ### Amber DB Command Syntax Source: https://github.com/amberframework/docs/blob/master/cli/database.md Shows the general syntax for the `amber database` command, including accepted arguments for various database operations. ```text amber database [COMMANDS1 COMMANDS2...] Arguments: COMMANDS (Accepts multiple) drop create migrate rollback redo status version seed ``` -------------------------------- ### Require FormBuilder in Amber Application Source: https://github.com/amberframework/docs/blob/master/guides/views/form-builder.md After installing the shard, require the `form_builder` library within your `config/application.cr` file to make its functionalities available throughout your Amber project. ```crystal require "form_builder" ``` -------------------------------- ### Create Procfile for Amber Application Deployment Source: https://github.com/amberframework/docs/blob/master/deployment/dokku.md Defines the commands Dokku should run for the application's release phase (database migrations) and web process. This file should be named `Procfile` and placed at the root of the Amber application. ```yaml release: bin/amber db migrate web: bin/{your-app-name} ``` -------------------------------- ### Install FormBuilder.cr Shard Dependency Source: https://github.com/amberframework/docs/blob/master/guides/views/form-builder.md Add the `form_builder` shard to your application's `shard.yml` file to include it as a project dependency. This allows your Amber application to use the FormBuilder library. ```yaml dependencies: form_builder: github: westonganger/form_builder.cr ``` -------------------------------- ### Navigate to Application Directory Source: https://github.com/amberframework/docs/blob/master/guides/create-new-app.md Changes the current working directory to the newly created Amber application's root, preparing for further configuration and commands. ```bash cd weblog ``` -------------------------------- ### Creating an Amber App with a GitHub-Hosted Recipe Source: https://github.com/amberframework/docs/blob/master/cli/recipes.md This snippet shows the command to create a new Amber application using a recipe hosted on GitHub, referencing it by the GitHub username and repository name. ```sh amber new myapp -r your_github_name/your_recipe_name ``` -------------------------------- ### Configure Chromedriver Path in GarnetSpec Source: https://github.com/amberframework/docs/blob/master/guides/testing/system-tests.md Example of configuring the `chromedriver` path within the `GarnetSpec` module for Amber system tests. This is useful for specifying a custom path, especially when running on operating systems other than macOS. ```ruby module GarnetSpec DRIVER = :chrome PATH = "/usr/local/bin/chromedriver" end ``` -------------------------------- ### Configure Amber Server to Use Environment Variables Source: https://github.com/amberframework/docs/blob/master/deployment/dokku.md Illustrates how to configure the Amber server in `settings.cr` to dynamically pick up host, port, Redis URL, and database URL from environment variables, which is essential for Dokku deployments. ```Crystal Amber::Server.configure do |settings| settings.host = "0.0.0.0" settings.port = ENV["PORT"].to_i if ENV["PORT"]? settings.redis_url = ENV["REDIS_URL"] if ENV["REDIS_URL"]? settings.database_url = ENV["DATABASE_URL"] if ENV["DATABASE_URL"]? end ``` -------------------------------- ### Bootstrap New Amber Application Source: https://github.com/amberframework/docs/blob/master/guides/create-new-app.md Generates a new Amber application with a specified directory path. This command creates the basic directory structure and necessary files, with options for database driver and templating engine. ```bash $ amber new weblog ``` -------------------------------- ### Conditionally Displaying Flash Values in Markup Source: https://github.com/amberframework/docs/blob/master/guides/controllers/flash.md Provides an example of checking for the presence of a specific flash key (`:just_signed_up`) and conditionally rendering content. This highlights that the flash can store arbitrary values, not just predefined notices or alerts. ```markup <% if flash[:just_signed_up] %>

Welcome to our site!

<% end %> ``` -------------------------------- ### Generating a New Amber Project Source: https://github.com/amberframework/docs/blob/master/cli/README.md Illustrates the basic command to create a new Amber application, allowing specification of the database and template engine, and then navigating into the newly created project directory. The `--deps` flag automatically runs `crystal deps`. ```bash amber new [your_app] -d [pg | mysql | sqlite] -t [slang | ecr] --deps cd [your_app] ``` -------------------------------- ### Create Amber Application Database Source: https://github.com/amberframework/docs/blob/master/guides/create-new-app.md Executes the command to create the PostgreSQL database for the Amber application based on the configured credentials in the environment file. ```bash amber db create ``` -------------------------------- ### Specify Layout for Amber Controllers Source: https://github.com/amberframework/docs/blob/master/guides/views/README.md Amber allows overriding default layout conventions by using a 'LAYOUT' constant within a controller class. This example demonstrates how to explicitly set a layout file, such as 'inventory.slang', for a specific controller. ```ruby class ProductsController < ApplicationController LAYOUT = "inventory.slang" #... end ``` -------------------------------- ### Launch Docker Compose Environment Source: https://github.com/amberframework/docs/blob/master/guides/docker.md Launches all the Docker containers defined in the `docker-compose.yml` file, including database, mailcatcher, app, and web services, bringing the Amber project online. ```bash docker-compose up ``` -------------------------------- ### Define an Amber Controller with an Action Source: https://github.com/amberframework/docs/blob/master/guides/controllers/README.md Controllers in Amber are Crystal classes that inherit from `ApplicationController`. This example shows a basic `ApiController` with an `index` method, which is invoked when the corresponding route is matched. It demonstrates instantiating a client and rendering a view. ```Crystal class ApiController < ApplicationController def index client = Client.new render("index.slang") end end ``` -------------------------------- ### Define Controller Route in Amber Source: https://github.com/amberframework/docs/blob/master/guides/controllers/README.md This snippet demonstrates how to define a route that maps to a specific controller and action in Amber, using the `ApiController` and its `index` action as an example. The routing mechanism determines which controller to use for an incoming request. ```Ruby get "/", ApiController, :index ``` -------------------------------- ### Resolve Linker Problems for Amber CLI Build on macOS Source: https://github.com/amberframework/docs/blob/master/guides/installation.md If encountering linker errors like 'library not found for -lssl' when building Amber from source on macOS, these environment variables need to be set to correctly locate OpenSSL libraries. ```bash export PKG_CONFIG_PATH="/usr/local/opt/openssl/lib/pkgconfig" export LDFLAGS="-L/usr/local/opt/openssl/lib" export CPPFLAGS="-I/usr/local/opt/openssl/include" ``` -------------------------------- ### Setting a Cookie in an Amber Controller Source: https://github.com/amberframework/docs/blob/master/cookbook/cookies.md This Crystal code defines a `set_cookie` action within `SomeController` that sets an HTTP-only and secure cookie named 'example' with a specified value. It demonstrates how to use the `cookies` helper to manage cookie properties. ```Crystal class SomeController < ApplicationController def set_cookie cookies[:example] = { value: "a yummy cookie with amber color", http_only: true, secure: true } "Your example cookie has been cooked successfully!" end end ``` -------------------------------- ### Amber CLI `new` Command and Subcommands Reference Source: https://github.com/amberframework/docs/blob/master/cli/README.md Detailed help output for the Amber CLI, specifically illustrating the `amber new` command's usage, its parameters, and a comprehensive list of available subcommands with their descriptions. This serves as an API-like reference for the CLI's capabilities. ```APIDOC amber [OPTIONS] SUBCOMMAND Amber - Command Line Interface The `amber new` command creates a new Amber application with a default directory structure and configuration at the path you specify. You can specify extra command-line arguments to be used every time `amber new` runs in the .amber.yml configuration file in your project root directory Note that the arguments specified in the .amber.yml file does not affect the defaults values shown above in this help message. Usage: amber new [app_name] -d [pg | mysql | sqlite] -t [slang | ecr] -m [granite, crecto] --deps Subcommands: db, database # Performs database maintenance tasks e, encrypt # Encrypts environment YAML file. [env | -e --editor | --noedit] x, exec # Executes Crystal code within the application scope g, generate # Generate Amber classes n, new # Generates a new Amber project routes # Prints all defined application routes w, watch # Starts amber development server and rebuilds on file changes Options: -d, --database # Preconfigure for selected database. Options: pg | mysql | sqlite (default: pg) -m, --model # Preconfigure for selected model. Options: granite (default: granite) -t, --template # Preconfigure for selected template engine. Options: slang | ecr (default: slang) -h, --help # Describe available commands and usages -v, --version # Prints Amber version Example: amber new ~/Code/Projects/weblog This generates a skeletal Amber installation in ~/Code/Projects/weblog. ``` -------------------------------- ### Define After Action Filters in Ruby Source: https://github.com/amberframework/docs/blob/master/guides/controllers/filters.md This Ruby code snippet illustrates how to define an `after_action` filter. These filters execute after the request cycle, typically used for cleanup operations. The example demonstrates applying the filter to specific actions using the `only` keyword. ```Ruby after_action do # runs for specified actions only [:index, :world] { increment(1) } end ``` -------------------------------- ### Defining a Route for Cookie Setting in Amber Source: https://github.com/amberframework/docs/blob/master/cookbook/cookies.md This Crystal code snippet configures the application's routes, mapping the GET request to `/set_cookie` to the `set_cookie` action of `SomeController`. This allows the previously defined controller action to be accessible via a web endpoint. ```Crystal Amber::Server.configure do |app| pipeline :web do # pipelines... end routes :web do # other routes,,, get "/set_cookie", SomeController, :set_cookie end end ``` -------------------------------- ### Define HelloController for 'Hello World' in Amber Source: https://github.com/amberframework/docs/blob/master/cookbook/hello-world.md This code defines a `HelloController` class inheriting from `ApplicationController`. It contains a `hello` method that returns the string 'Hello Amber!'. This controller handles the logic for the '/hello' route. ```Crystal class HelloController < ApplicationController def hello "Hello Amber!" end end ```