Skip to content

Instantly share code, notes, and snippets.

@chulkilee
Created March 2, 2018 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chulkilee/a2685c95204b3d07f8379a1f0fbf12cc to your computer and use it in GitHub Desktop.
Save chulkilee/a2685c95204b3d07f8379a1f0fbf12cc to your computer and use it in GitHub Desktop.
new-phx.sh
#!/bin/bash -eux
# tested with phoenix 1.3.0
shopt -s nullglob
app_path=$1
elixir_version=${ELIXIR_VERSION:-1.6.1}
echo n | mix phx.new "$app_path" --no-brunch --no-html
cd "$app_path"
git init
if [ ! -z "${GIT_USER_NAME:-}" ]; then
git config user.name "$GIT_USER_NAME"
fi
if [ ! -z "${GIT_USER_EMAIL:-}" ]; then
git config user.email "$GIT_USER_EMAIL"
fi
git add .
git commit -m "initial commit"
mix deps.get
rm mix.lock && mix deps.get
git add mix.lock
git commit -m "add mix.lock"
cat README.md | head -n 1 > README.md.tmp
mv README.md.tmp README.md
git add README.md
git commit -m "empty README.md"
if grep -q socket lib/*_web/endpoint.ex; then
git rm -rf lib/*_web/channels test/support/channel_case.ex
gsed -i '/ *socket/d' lib/*_web/endpoint.ex
gsed -i '/^$/N;/^\n$/D' lib/*_web/endpoint.ex
rmdir test/*_web/channels
git add lib/*_web/endpoint.ex
git commit -m "remove channel/socket"
fi
if grep -q gettext mix.exs; then
git rm -rf lib/*_web/gettext.ex priv/gettext
gsed -i '/{:gettext/d' mix.exs
gsed -i 's/:phoenix, :gettext/:phoenix/' mix.exs
gsed -i '/Web\.Gettext/d' lib/*_web.ex
gsed -i '/^ @doc/,/^ end/d' lib/*_web/views/error_helpers.ex
gsed -i '/^$/d' lib/*_web/views/error_helpers.ex
rm mix.lock && mix deps.get
git add .
git commit -m "remove gettext"
fi
if grep -q Supervisor.Spec lib/**/application.ex; then
gsed -i '/import Supervisor.Spec/,+1d' lib/**/application.ex
gsed -i 's/supervisor(\(.\+\)),/{\1},/' lib/**/application.ex
git add lib/**/application.ex
git commit -m "use tuple for child_spec"
fi
gsed -i '/^ *#/d' $(git ls-files)
gsed -i '/^$/N;/^\n$/D' $(git ls-files | grep -v .md)
git add .
git commit -m "remove comments and extra new lines"
if grep -q "pass:" lib/*_web/endpoint.ex; then
gsed -i '/pass:/d' lib/*_web/endpoint.ex
gsed -i 's/:urlencoded, :multipart, :json/:json/' lib/*_web/endpoint.ex
git add lib/*_web/endpoint.ex
git commit -m "accept only json"
fi
if grep -q "~> 1.4" mix.exs; then
gsed -i "s/elixir: \"~> 1.4\"/elixir: \"$elixir_version\"/" mix.exs
git add mix.exs
git commit -m "elixir $elixir_version"
fi
if [ ! -f .formatter.exs ]; then
echo '[
inputs: [
"{config,lib,test}/**/*.{ex,exs}",
"mix.exs"
]
]' > .formatter.exs
git add .formatter.exs
git ci -m "add .formatter.exs"
fi
mix format
git add .
git ci -m "run mix format"
if grep -q "secret.exs" config/prod.exs; then
gsed -i '/^\/config\/\*\.secret.exs/d' .gitignore
gsed -i '/^import_config/d' config/prod.exs
gsed -i '/^$/N;/^\n$/D' .gitignore config/prod.exs
perl -pe 'chomp if eof' config/prod.exs > t && mv t config/prod.exs
rm -f config/prod.secret.exs
git add .gitignore config/prod.exs
git commit -m "remove prod.secret.exs"
fi
if grep -q "adapter: Ecto.Adapters.Postgres" config/dev.exs; then
repo_line=$(grep .Repo config/dev.exs)
gsed -i -e "s/^import/$repo_line adapter: Ecto.Adapters.Postgres\\
\\
import/" config/config.exs
gsed -i '/adapter:/d' config/dev.exs config/test.exs
gsed -i '/username: "postgres"/d' config/dev.exs config/test.exs
gsed -i '/password: "postgres"/d' config/dev.exs config/test.exs
mix format
git add config/*.exs
git ci -m "update postgresql config"
fi
mix compile
if grep -q secret_key_base config/config.exs; then
gsed -i '/secret_key_base/d' config/config.exs
gsed -i "/^ http:/a\\ secret_key_base: \"\\$(mix phx.gen.secret | tr -d '\n')\"," config/dev.exs
gsed -i "/^ http:/a\\ secret_key_base: \"\\$(mix phx.gen.secret | tr -d '\n')\"," config/test.exs
fi
gsed -i '/load_from_system_env: true/a \
\ http: [ip: {0, 0, 0, 0}, port: 4000],' config/prod.exs
gsed -i '/url:/d' config/config.exs config/prod.exs
gsed -i 's/http: \[port:/http: \[ip: {127, 0, 0, 1}, port:/' config/dev.exs config/test.exs
gsed -i '/port =/d' lib/*_web/endpoint.ex
gsed -i 's/Keyword.put(config, :http, \[:inet6, port: port\])/config_with_env(config)/' lib/*_web/endpoint.ex
gsed -i '/^end$/d' lib/*_web/endpoint.ex
echo '
defp config_with_env(config) do
config
|> Keyword.put(:secret_key_base, System.get_env("SECRET_KEY_BASE"))
|> Keyword.put(
:url,
scheme: System.get_env("URL_SCHEME") || "https",
port: System.get_env("URL_PORT") || 443,
host: System.get_env("URL_HOST")
)
end
end' >> lib/*_web/endpoint.ex
echo 'SECRET_KEY_BASE=
# URL_SCHEME=https
# URL_PORT=443
URL_HOST=example.com' > .env.example
git add .
git ci -m 'use System.get_env for configuration'
echo "FROM elixir:$elixir_version-alpine
WORKDIR /opt/app
ENV MIX_ENV=prod
RUN apk add --no-cache build-base git \
&& mix local.hex --force \
&& mix local.rebar --force
COPY mix.exs mix.lock ./
RUN mix deps.get && mix deps.compile
COPY . .
RUN mix compile && mix phx.digest
CMD [\"mix\", \"phx.server\"]" > Dockerfile
cp .gitignore .dockerignore
gsed -i 's/^\///g' .dockerignore
echo '
.dockerignore
.git
.gitignore
docker-compose.yml
Dockerfile' >> .dockerignore
echo "version: '2'
services:
app:
build: .
environment:
- DATABASE_URL=ecto://postgres:postgres@postgres/${app_path}_dev?pool_size=10
volumes:
- ~/.erlang.cookie:/root/.erlang.cookie
ports:
- 0.0.0.0:4000:4000
postgres:
image: postgres:10.1-alpine" > docker-compose.yml
git add .
git ci -m "add docker configuration"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment