sodapopcan

sodapopcan

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!

Showing Posts 15 to 6

sodapopcan

sodapopcan OP

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!

dimitarvp

dimitarvp

Yep, started using it, sparingly, and so far I like it.

There’s also recode that I like a bit more.

LostKobrakai

LostKobrakai

dimitarvp

dimitarvp

OCaml and Racket are excellent if you want produce other DSLs so yeah, shell scripts would only be a first crippled prototype and nothing else. OCaml would be my first stop. They have a ton of libraries dealing with parsing and transforming stuff.

But that requires good tooling around parsing the languages themselves into a structured and well-standardized AST – and not necessarily the one their own compiler / runtime uses, too. And some languages, Elixir included, don’t have a standardized AST at all so this is still an area that the community must one day step up to fix.

sodapopcan

sodapopcan OP

You should get that looked at :upside_down_face:

The next step would be to use something better than a shell script. I’m half trying to gauge how a big a pain this is for people and if it’s worth writing something a little nicer with options. Something like Rails’ bootstrapping templates would be nice (although I never actually used those).

cevado

cevado

I usually do this manually, because I change some naming convetion and organization on phoenix apps.
But I guess this automatic cleanup as your script does, would be cool.
I think it could even be mix task on phoenix… mix phx.clean_examples or something like that.

sodapopcan

sodapopcan OP

A bunch of them, yes, but I haven’t worked that into the script yet. There are some I think are useful to leave if it was expecting to grow, although I’ve never started such a project on my own before.

cevado

cevado

i usually clean up comments. but I only use a replace regex applyied to all .ex files in the project.

webuhu

webuhu

Anyone else also cleaning up the code comments?

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

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
AstonJ
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
maennchen
:warning: Security advisory: Decimal DoS vulnerability A vulnerability has been published for decimal where very large exponents can cau...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New

Other Trending Topics Top

marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews