sodapopcan

sodapopcan

Phoenix bootstrapping scripts

I’ve been doing a lot of prototypes in Phoenix lately. After doing the same annoying things over and over after each invocation of $ mix phx.new blah I ended up with script to bootstrap a new app the way I like it. I’m sure many have done this and was wondering if others had scripts to share. Or perhaps there is a library I’m missing to better define this stuff? https://phx.new/ does not do it for me. I’d love to see your scripts or get feedback on mine (I’m no shell expert).

The things I always do are:

  • Tell ecto to use utc_datetime_usec for timestamps
  • Use binary ids for primary keys
  • Create a custom Schema module for setting said primary keys as well as use and import the usual stuff
  • Remove page controller and associated files
  • Remove other boilerplate (phoenix css, phoenix logo)
  • Create a HomeLive LiveView (I pretty much always have a HomeLive to start which maybe gets renamed later)

Here’s my script:

#!/usr/bin/env bash

function snake_to_pascal {
  echo $1 | perl -pe 's/(?:\b|_)(\p{Ll})/\u$1/g'
}

mix phx.new "$@" --binary-id

cd "$1"

# Delete page controller
rm "lib/$1_web/controllers/page_controller.ex"
rm "test/$1_web/controllers/page_controller_test.exs"

# Delete templates
rm "lib/$1_web/templates/page/index.html.heex"
rmdir "lib/$1_web/templates/page"

# Delete view
rm "lib/$1_web/views/page_view.ex"

# Remove header from the root layout
sed -I '' '13,27d' "lib/$1_web/templates/layout/root.html.heex"

# Remove getext thing (if it's there)
sed -I '' '10s/.*/      compilers: Mix.compilers(),/' mix.exs

# Remove CSS
rm assets/css/phoenix.css
echo > assets/css/app.css

# Use utc_datetime_usec in migrations
subt="s/pool_size: 10/pool_size: 10,\n  migration_timestamps: \[type: :utc_datetime_usec\]/"
sed -I '' "$subt" "config/dev.exs"
sed -I '' "$subt" "config/test.exs"

# Setup a HomeLive
sed -I '' 's/get "\/", PageController, :index/live "\/", HomeLive, :index/' "lib/$1_web/router.ex"

mkdir "lib/$1_web/live"

module=$(snake_to_pascal "$1")

cat <<EOF > "lib/$1/schema.ex" 
defmodule ${module}.Schema do
  defmacro __using__(_) do
    quote do
      use Ecto.Schema

      import Ecto.Changeset, warn: false

      @timestamps_opts type: :utc_datetime_usec

      @primary_key {:id, :binary_id, autogenerate: true}
      @foreign_key_type :binary_id
    end
  end
end
EOF

cat <<EOF > "lib/$1_web/live/home_live.ex" 
defmodule ${module}Web.HomeLive do
  use ${module}Web, :live_view

  def render(assigns) do
    ~H"""
    <h1>${module}</h1>
    """
  end
end
EOF

# Delete phoenix logo
rm priv/static/images/phoenix.png

# Use the heex formatting plugin
cat <<EOF > ".formatter.exs"
[
  import_deps: [:ecto, :phoenix],
  plugins: [Phoenix.LiveView.HTMLFormatter],
  inputs: ["*.{heex,ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{heex,ex,exs}"],
  subdirectories: ["priv/*/migrations"]
]
EOF

I realize this is coming hot on the heels of the next phoenix release so these could end up changing a little. But I’m glad I didn’t take the time to make this thing add tailwind!

Most Liked

dmitriid

dmitriid

I have the same feeling. I’m now starting a third project this year, and every time after running phx.new I have the same reaction: “now what?” :slight_smile: Oh, right: clean out views and layouts, remember how to tie layouts together for a live view, add seeds and repo details,… and a thousand of other small things

Too bas these small things are different for different people, otherwise it would be nice to have them done by the generator

LostKobrakai

LostKobrakai

dogweather

dogweather

I’d like to see a guide like this for making simple JSON servers in Phoenix: the steps to exclude all the templating, HTML, even database sometimes.

Last Post!

sodapopcan

sodapopcan

I’ve recently been getting into OCaml and even have a call today with a company that uses it :smiley:

@LostKobrakai Thanks for the link, I was unaware of this project!

Where Next?

Popular in Discussions Top

New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 20070 166
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement