From 501e792824d33be0ac899bd1aef0835522a79c51 Mon Sep 17 00:00:00 2001 From: Darwin D Wu Date: Sat, 18 Jan 2025 01:20:01 -0800 Subject: [PATCH 1/4] Add nix for dev dependencies --- .envrc | 1 + .gitignore | 3 ++- flake.lock | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 39 ++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 .envrc create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore index 09f5471..42f2ef1 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,5 @@ node_modules/ /.vscode/ /.ctags.d/ -.tags \ No newline at end of file +.tags +/.direnv/ diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..2537edd --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1737062831, + "narHash": "sha256-Tbk1MZbtV2s5aG+iM99U8FqwxU/YNArMcWAv6clcsBc=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "5df43628fdf08d642be8ba5b3625a6c70731c19c", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..cce43ac --- /dev/null +++ b/flake.nix @@ -0,0 +1,39 @@ +{ + description = "Phoenix Liveview component library inspired by shadcn UI"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, flake-utils, ... }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { inherit system; }; + + # NOTE: + # override since `elixir` is defaulting to 1.18 atm, and lexical doesn't support that version yet + lexical = + pkgs.lexical.override { elixir = pkgs.beam27Packages.elixir_1_17; }; + in { + devShells.default = pkgs.mkShell { + packages = [ lexical ]; + + buildInputs = with pkgs; [ + beam27Packages.elixir_1_17 + beam27Packages.erlang + nodejs_20 + + # LSPs + # lexical + erlang-ls + + # Tools + # yamllint + # yaml-language-server + # shfmt + # shellcheck + ]; + }; + }); +} From f6759de82b7075ac8b1e544c8059942917ebe40d Mon Sep 17 00:00:00 2001 From: Darwin D Wu Date: Sat, 18 Jan 2025 10:40:56 -0800 Subject: [PATCH 2/4] replace npm deps with tailwindcss-animate source download --- lib/mix/tasks/salad.init.ex | 34 ++++++++++++++++-------- lib/salad_ui/patcher/tailwind_patcher.ex | 2 +- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/mix/tasks/salad.init.ex b/lib/mix/tasks/salad.init.ex index 27d0b80..9f0968a 100644 --- a/lib/mix/tasks/salad.init.ex +++ b/lib/mix/tasks/salad.init.ex @@ -16,6 +16,7 @@ defmodule Mix.Tasks.Salad.Init do @default_components_path "lib/%APP_NAME%_web/components" @color_schemes ~w(zinc slate stone gray neutral red rose orange green blue yellow violet) @default_color_scheme "gray" + @default_tailwind_animate_version "1.0.7" @impl true def run(argv) do @@ -38,8 +39,6 @@ defmodule Mix.Tasks.Salad.Init do assets_path = build_assets_path(env) application_file_path = Path.join(File.cwd!(), "lib/#{app_name}/application.ex") - node_opts = if env == "test", do: [skip: true], else: [] - File.mkdir_p!(component_path) with :ok <- write_config(component_path), @@ -50,7 +49,7 @@ defmodule Mix.Tasks.Salad.Init do :ok <- patch_tailwind_config(opts), :ok <- maybe_write_helpers_module(component_path, app_name, opts), :ok <- maybe_write_component_module(component_path, app_name, opts), - :ok <- install_node_dependencies(node_opts) do + :ok <- install_tailwind_animate(opts) do if opts[:as_lib] do Mix.shell().info("Done. Now you can use any component by `import SaladUI.` in your project.") else @@ -218,15 +217,28 @@ defmodule Mix.Tasks.Salad.Init do File.write!(target_path, source_code) end - defp install_node_dependencies(opts) do - if Keyword.get(opts, :skip, false) do - Mix.shell().info("Skipping npm install (running in test environment)") - else - Mix.shell().info("Installing tailwindcss-animate") - Mix.shell().cmd("npm install -D tailwindcss-animate --prefix assets") - end + defp install_tailwind_animate(opts) do + tag = Keyword.get(opts, :tailwind_animate_version, @default_tailwind_animate_version) + Mix.shell().info("Downloading tailwindcss-animate.js v#{tag}") - :ok + url = "https://raw.githubusercontent.com/jamiebuilds/tailwindcss-animate/refs/tags/v#{tag}/index.js" + output_path = Keyword.get(opts, :output_path, Path.join(File.cwd!(), "assets/vendor/tailwindcss-animate.js")) + + :inets.start() + :ssl.start() + + case :httpc.request(:get, {url, []}, [], body_format: :binary) do + {:ok, {{_version, 200, _reason_phrase}, _headers, body}} -> + # Write the body to file + File.write!(output_path, body) + :ok + + {:ok, {{_version, status_code, _reason_phrase}, _headers, _body}} -> + {:error, "Failed to download tailwindcss-animate with status #{status_code}"} + + {:error, reason} -> + {:error, "Failed to download tailwindcss-animate.js: #{inspect(reason)}"} + end end defp print_usage, do: Mix.shell().info(@moduledoc) diff --git a/lib/salad_ui/patcher/tailwind_patcher.ex b/lib/salad_ui/patcher/tailwind_patcher.ex index d80c785..7136c3b 100644 --- a/lib/salad_ui/patcher/tailwind_patcher.ex +++ b/lib/salad_ui/patcher/tailwind_patcher.ex @@ -5,7 +5,7 @@ defmodule SaladUI.Patcher.TailwindPatcher do # so the user does not to manually format his `tailwind.config.js` # file after patching it. - @plugins ["@tailwindcss/typography", "tailwindcss-animate"] + @plugins ["@tailwindcss/typography", "./vendor/tailwindcss-animate"] @tailwind_colors "./tailwind.colors.json" def patch(tailwind_config_path, opts \\ []) do From 9b212d3f9931b14333b024ab4b0e7b902d898d6e Mon Sep 17 00:00:00 2001 From: Darwin D Wu Date: Sat, 18 Jan 2025 10:51:38 -0800 Subject: [PATCH 3/4] update tests --- test/mix/salad.init_test.exs | 1 + test/salad_ui/patcher/tailwind_patcher_test.exs | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/test/mix/salad.init_test.exs b/test/mix/salad.init_test.exs index 745306f..a344532 100644 --- a/test/mix/salad.init_test.exs +++ b/test/mix/salad.init_test.exs @@ -29,6 +29,7 @@ defmodule Mix.Tasks.Salad.InitTest do File.mkdir_p!(Path.dirname(@application_file_path)) File.mkdir_p!("assets/css") File.mkdir_p!("assets/js") + File.mkdir_p!("assets/vendor") File.mkdir_p!(@default_components_path) File.write!("config/config.exs", "import Config\n") diff --git a/test/salad_ui/patcher/tailwind_patcher_test.exs b/test/salad_ui/patcher/tailwind_patcher_test.exs index 926ec7b..93431e1 100644 --- a/test/salad_ui/patcher/tailwind_patcher_test.exs +++ b/test/salad_ui/patcher/tailwind_patcher_test.exs @@ -21,7 +21,7 @@ defmodule SaladUI.Patcher.TailwindPatcherTest do patched_content = File.read!(@tailwind_config) assert patched_content =~ "plugins: [" assert patched_content =~ "require(\"@tailwindcss/typography\")" - assert patched_content =~ "require(\"tailwindcss-animate\")" + assert patched_content =~ "require(\"./vendor/tailwindcss-animate\")" end test "patch/1 doesn't duplicate existing plugins" do @@ -29,7 +29,7 @@ defmodule SaladUI.Patcher.TailwindPatcherTest do module.exports = { plugins: [ require("@tailwindcss/typography"), - require("tailwindcss-animate") + require("./vendor/tailwindcss-animate") ] } """ @@ -41,11 +41,11 @@ defmodule SaladUI.Patcher.TailwindPatcherTest do patched_content = File.read!(@tailwind_config) assert patched_content =~ "require(\"@tailwindcss/typography\")" - assert patched_content =~ "require(\"tailwindcss-animate\")" + assert patched_content =~ "require(\"./vendor/tailwindcss-animate\")" # Count occurrences of each plugin typography_count = patched_content |> String.split("@tailwindcss/typography") |> length() |> Kernel.-(1) - animate_count = patched_content |> String.split("tailwindcss-animate") |> length() |> Kernel.-(1) + animate_count = patched_content |> String.split("./vendor/tailwindcss-animate") |> length() |> Kernel.-(1) assert typography_count == 1 assert animate_count == 1 From 2f5737380dd56eb5146a5a1418453e2496db4f3f Mon Sep 17 00:00:00 2001 From: Darwin D Wu Date: Sat, 18 Jan 2025 10:52:52 -0800 Subject: [PATCH 4/4] remove flake comments --- flake.nix | 7 ------- 1 file changed, 7 deletions(-) diff --git a/flake.nix b/flake.nix index cce43ac..3c7f690 100644 --- a/flake.nix +++ b/flake.nix @@ -22,17 +22,10 @@ buildInputs = with pkgs; [ beam27Packages.elixir_1_17 beam27Packages.erlang - nodejs_20 # LSPs # lexical erlang-ls - - # Tools - # yamllint - # yaml-language-server - # shfmt - # shellcheck ]; }; });