Skip to content

Commit

Permalink
Provide a sample live view on the default app
Browse files Browse the repository at this point in the history
  • Loading branch information
aforward committed May 1, 2019
1 parent 31303ee commit bddac6b
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 5 deletions.
25 changes: 24 additions & 1 deletion installer/lib/phx_new/single.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ defmodule Phx.New.Single do
{:keep, "phx_assets/vendor", :web, "assets/vendor"},
]

template :live, [
{:eex, "phx_assets/live.js", :web, "assets/js/live.js"},
{:eex, "phx_live/controllers/page_controller.ex", :project, "lib/:lib_web_name/controllers/page_controller.ex"},
{:eex, "phx_web/templates/layout/app.html.eex", :project, "lib/:lib_web_name/templates/layout/app.html.eex"},
{:eex, "phx_live/templates/page/index.html.eex", :project, "lib/:lib_web_name/templates/page/index.html.eex"},
{:eex, "phx_live/templates/page/hero.html.leex", :project, "lib/:lib_web_name/templates/page/hero.html.leex"},
{:eex, "phx_web/views/layout_view.ex", :project, "lib/:lib_web_name/views/layout_view.ex"},
{:eex, "phx_web/views/page_view.ex", :project, "lib/:lib_web_name/views/page_view.ex"},
{:eex, "phx_live/live/page_live_view.ex", :project, "lib/:lib_web_name/live/page_live_view.ex"},
{:eex, "phx_test/controllers/page_controller_test.exs", :project, "test/:lib_web_name/controllers/page_controller_test.exs"},
{:eex, "phx_test/views/layout_view_test.exs", :project, "test/:lib_web_name/views/layout_view_test.exs"},
{:eex, "phx_test/live/page_live_view_test.exs", :project, "test/:lib_web_name/live/page_live_view_test.exs"},
{:eex, "phx_test/views/page_view_test.exs", :project, "test/:lib_web_name/views/page_view_test.exs"},
]

template :bare, []

template :static, [
Expand Down Expand Up @@ -108,7 +123,11 @@ defmodule Phx.New.Single do
copy_from project, __MODULE__, :gettext

if Project.ecto?(project), do: gen_ecto(project)
if Project.html?(project), do: gen_html(project)
cond do
Project.live?(project) -> gen_live(project)
Project.html?(project) -> gen_html(project)
:else -> :ok
end

case {Project.webpack?(project), Project.html?(project)} do
{true, _} -> gen_webpack(project)
Expand All @@ -123,6 +142,10 @@ defmodule Phx.New.Single do
copy_from project, __MODULE__, :html
end

defp gen_live(project) do
copy_from project, __MODULE__, :live
end

def gen_ecto(project) do
copy_from project, __MODULE__, :ecto
gen_ecto_config(project)
Expand Down
25 changes: 24 additions & 1 deletion installer/lib/phx_new/web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ defmodule Phx.New.Web do
{:eex, "phx_test/views/page_view_test.exs", :web, "test/:web_app/views/page_view_test.exs"},
]

template :live, [
{:eex, "phx_assets/live.js", :web, "lib/:web_app/assets/js/live.js"},
{:eex, "phx_live/controllers/page_controller.ex", :web, "lib/:web_app/controllers/page_controller.ex"},
{:eex, "phx_web/templates/layout/app.html.eex", :web, "lib/:web_app/templates/layout/app.html.eex"},
{:eex, "phx_live/templates/page/index.html.eex", :web, "lib/:web_app/templates/page/index.html.eex"},
{:eex, "phx_live/templates/page/hero.html.leex", :web, "lib/:web_app/templates/page/hero.html.leex"},

{:eex, "phx_web/views/layout_view.ex", :web, "lib/:web_app/views/layout_view.ex"},
{:eex, "phx_web/views/page_view.ex", :web, "lib/:web_app/views/page_view.ex"},
{:eex, "phx_live/live/page_live_view.ex", :web, "lib/:web_app/live/page_live_view.ex"},
{:eex, "phx_test/controllers/page_controller_test.exs", :web, "test/:web_app/controllers/page_controller_test.exs"},
{:eex, "phx_test/views/layout_view_test.exs", :web, "test/:web_app/views/layout_view_test.exs"},
{:eex, "phx_test/views/page_view_test.exs", :web, "test/:web_app/views/page_view_test.exs"},
]

def prepare_project(%Project{app: app} = project) when not is_nil(app) do
web_path = Path.expand(project.base_path)
project_path = Path.dirname(Path.dirname(web_path))
Expand All @@ -65,7 +80,11 @@ defmodule Phx.New.Web do
copy_from project, __MODULE__, :new
copy_from project, __MODULE__, :gettext

if Project.html?(project), do: gen_html(project)
cond do
Project.live?(project) -> gen_live(project)
Project.html?(project) -> gen_html(project)
:else -> :ok
end

case {Project.webpack?(project), Project.html?(project)} do
{true, _} -> Phx.New.Single.gen_webpack(project)
Expand All @@ -79,4 +98,8 @@ defmodule Phx.New.Web do
defp gen_html(%Project{} = project) do
copy_from project, __MODULE__, :html
end

defp gen_live(%Project{} = project) do
copy_from project, __MODULE__, :live
end
end
1 change: 1 addition & 0 deletions installer/templates/phx_assets/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ import css from "../css/app.css"
//
// Local files can be imported directly using relative paths, for example:
// import socket from "./socket"
<%= if live do %>// import liveSocket from "./live"<% end %>
9 changes: 9 additions & 0 deletions installer/templates/phx_assets/live.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// NOTE: The contents of this file will only be executed if
// you need the following statement in "assets/js/app.js"
// import liveSocket from "./live"

import {LiveSocket} from "phoenix_live_view"
let liveSocket = new LiveSocket("/live")
liveSocket.connect()

export default liveSocket
Binary file added installer/templates/phx_live/.DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions installer/templates/phx_live/controllers/page_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule <%= web_namespace %>.PageController do
use <%= web_namespace %>, :controller

def index(conn, _params) do
render(conn, "index.html")
end
end
43 changes: 43 additions & 0 deletions installer/templates/phx_live/live/page_live_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule <%= web_namespace %>.PageLiveView do
use Phoenix.LiveView
import <%= web_namespace %>.Gettext

@colours [
"#5ED0FA",
"#54D1DB",
"#A368FC",
"#F86A6A",
"#FADB5F",
"#65D6AD",
"#127FBF",
"#0E7C86",
"#690CB0",
"#AB091E",
"#CB6E17",
"#147D64"
]

def render(assigns) do
<%= web_namespace %>.PageView.render("hero.html", assigns)
end

def mount(_session, socket) do
if connected?(socket), do: tick()
{:ok, socket |> assign(tock: 1000) |> assign_colours()}
end

def handle_info(:tick, socket) do
tick()
{:noreply, socket |> update(:tock, &(&1 + 1)) |> assign_colours()}
end

defp colour(), do: @colours |> Enum.random()

defp assign_colours(socket) do
socket
|> assign(:bgcolor, colour())
|> assign(:fgcolor, colour())
end

defp tick(), do: Process.send_after(self(), :tick, 1000)
end
Binary file not shown.
4 changes: 4 additions & 0 deletions installer/templates/phx_live/templates/page/hero.html.leex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<section class="phx-hero" data-tock="<%%= @tock %>" style="background-color: <%%= @bgcolor %>; color: <%%= @fgcolor %>">
<h1><%%= gettext "Welcome to %{name}!", name: "Phoenix" %></h1>
<p>A productive web framework that<br/>does not compromise speed or maintainability.</p>
</section>
35 changes: 35 additions & 0 deletions installer/templates/phx_live/templates/page/index.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<%%= live_render(@conn, <%= web_namespace %>.PageLiveView) %>

<section class="row">
<article class="column">
<h2>Resources</h2>
<ul>
<li>
<a href="https://hexdocs.pm/phoenix/overview.html">Guides &amp; Docs</a>
</li>
<li>
<a href="https://github.com/phoenixframework/phoenix">Source</a>
</li>
<li>
<a href="https://github.com/phoenixframework/phoenix/blob/<%= phoenix_github_version_tag %>/CHANGELOG.md"><%= phoenix_github_version_tag %> Changelog</a>
</li>
</ul>
</article>
<article class="column">
<h2>Help</h2>
<ul>
<li>
<a href="https://elixirforum.com/c/phoenix-forum">Forum</a>
</li>
<li>
<a href="https://webchat.freenode.net/?channels=elixir-lang">#elixir-lang on Freenode IRC</a>
</li>
<li>
<a href="https://twitter.com/elixirphoenix">Twitter @elixirphoenix</a>
</li>
<li>
<a href="https://elixir-slackin.herokuapp.com/">Elixir on Slack</a>
</li>
</ul>
</article>
</section>
3 changes: 3 additions & 0 deletions installer/templates/phx_test/live/page_live_view_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule <%= web_namespace %>.PageLiveViewTest do
use <%= web_namespace %>.ConnCase, async: true
end
28 changes: 26 additions & 2 deletions installer/test/phx_new_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ defmodule Mix.Tasks.Phx.NewTest do
assert_file "phx_blog/test/support/conn_case.ex"
assert_file "phx_blog/test/test_helper.exs"

assert_file "phx_blog/lib/phx_blog_web/controllers/page_controller.ex",
~r/defmodule PhxBlogWeb.PageController/
assert_file "phx_blog/lib/phx_blog_web/controllers/page_controller.ex", fn file ->
assert file =~ ~r/defmodule PhxBlogWeb.PageController/
assert file =~ ~s[render(conn, "index.html")]
end

assert_file "phx_blog/lib/phx_blog_web/views/page_view.ex",
~r/defmodule PhxBlogWeb.PageView/
Expand Down Expand Up @@ -232,6 +234,8 @@ defmodule Mix.Tasks.Phx.NewTest do
# No LiveView
assert_file "phx_blog/mix.exs", &refute(&1 =~ ~r":phoenix_live_view")

refute_file "phx_blog/assets/js/live.js"

# No HTML
assert File.exists?("phx_blog/test/phx_blog_web/controllers")

Expand Down Expand Up @@ -285,9 +289,29 @@ defmodule Mix.Tasks.Phx.NewTest do
Mix.Tasks.Phx.New.run([@app_name, "--live"])
assert_file "phx_blog/mix.exs", &assert(&1 =~ ~r":phoenix_live_view")

assert_file "phx_blog/lib/phx_blog_web/controllers/page_controller.ex", fn file ->
assert file =~ ~r/defmodule PhxBlogWeb.PageController/
assert file =~ ~s[render(conn, "index.html")]
end

assert_file "phx_blog/lib/phx_blog_web/live/page_live_view.ex"

assert_file "phx_blog/lib/phx_blog_web/templates/page/index.html.eex", fn file ->
assert file =~ ~s[<%= live_render(@conn, PhxBlogWeb.PageLiveView) %>]
end

assert_file "phx_blog/lib/phx_blog_web/templates/page/hero.html.leex"

assert_file "phx_blog/assets/package.json",
~s["phoenix_live_view": "file:../deps/phoenix_live_view"]

assert_file "phx_blog/assets/js/live.js", fn file ->
assert file =~ ~s[import {LiveSocket} from "phoenix_live_view"]
assert file =~ ~s[let liveSocket = new LiveSocket("/live")]
assert file =~ ~s[liveSocket.connect()]
assert file =~ ~s[export default liveSocket]
end

assert_file "phx_blog/config/config.exs", fn file ->
assert file =~ ~s[live_view: []
assert file =~ ~s[signing_salt:]
Expand Down
20 changes: 19 additions & 1 deletion installer/test/phx_new_umbrella_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ defmodule Mix.Tasks.Phx.New.UmbrellaTest do
assert file =~ "{:phx_umb, in_umbrella: true}"
assert file =~ "{:phoenix,"
assert file =~ "{:phoenix_pubsub,"
refute file =~ "{:phoenix_live_view,"
assert file =~ "{:gettext,"
assert file =~ "{:plug_cowboy,"
end
Expand Down Expand Up @@ -243,6 +244,9 @@ defmodule Mix.Tasks.Phx.New.UmbrellaTest do

# No LiveView (in web_path)
assert_file web_path(@app, "mix.exs"), &refute(&1 =~ ~r":phoenix_live_view")
refute File.exists?(web_path(@app, "lib/#{@app}_web/templates/page/hero.html.leex"))

refute_file web_path(@app, "assets/js/live.js")

# No HTML
assert File.exists?(web_path(@app, "test/#{@app}_web/controllers"))
Expand Down Expand Up @@ -294,6 +298,20 @@ defmodule Mix.Tasks.Phx.New.UmbrellaTest do
test "new with live" do
in_tmp "new with live", fn ->
Mix.Tasks.Phx.New.run([@app, "--umbrella", "--live"])

assert_file web_path(@app, "lib/#{@app}_web/controllers/page_controller.ex"), fn file ->
assert file =~ ~r/defmodule PhxUmbWeb.PageController/
assert file =~ ~s[render(conn, "index.html")]
end

assert_file web_path(@app, "lib/#{@app}_web/live/page_live_view.ex")

assert_file web_path(@app, "lib/#{@app}_web/templates/page/index.html.eex"), fn file ->
assert file =~ ~s[<%= live_render(@conn, PhxUmbWeb.PageLiveView) %>]
end

assert_file web_path(@app, "lib/#{@app}_web/templates/page/hero.html.leex")

assert_file web_path(@app, "mix.exs"), &assert(&1 =~ ~r":phoenix_live_view")

assert_file web_path(@app, "assets/package.json"),
Expand All @@ -305,7 +323,7 @@ defmodule Mix.Tasks.Phx.New.UmbrellaTest do
assert file =~ ~s[signing_salt:]
end

assert_file web_path(@app, "lib/phx_umb_web.ex"), fn file ->
assert_file web_path(@app, "lib/#{@app}_web.ex"), fn file ->
assert file =~ "import Phoenix.LiveView, only: [live_render: 2, live_render: 3]"
assert file =~ ~s[import Phoenix.LiveView.Router]
assert file =~ "import Phoenix.LiveView.Controller, only: [live_render: 3]"
Expand Down

0 comments on commit bddac6b

Please sign in to comment.