diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..47fd776 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,27 @@ +name: Ruby + +on: + push: + branches: + - main + + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + name: Ruby ${{ matrix.ruby }} + strategy: + matrix: + ruby: + - '3.1.2' + + steps: + - uses: actions/checkout@v3 + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + - name: Run the default task + run: bundle exec rake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b04a8c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +/.bundle/ +/.yardoc +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ + +# rspec failure tracking +.rspec_status diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..34c5164 --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--format documentation +--color +--require spec_helper diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..e3462a7 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,13 @@ +AllCops: + TargetRubyVersion: 2.6 + +Style/StringLiterals: + Enabled: true + EnforcedStyle: double_quotes + +Style/StringLiteralsInInterpolation: + Enabled: true + EnforcedStyle: double_quotes + +Layout/LineLength: + Max: 120 diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..87e4fbf --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +## [Unreleased] + +## [0.1.0] - 2024-07-28 + +- Initial release diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..e9563bc --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,84 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience +* Focusing on what is best not just for us as individuals, but for the overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at guilherme.andrade.ao@gmail.com. All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series of actions. + +**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, +available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity). + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see the FAQ at +https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations. diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..173c610 --- /dev/null +++ b/Gemfile @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +# Specify your gem's dependencies in tailwindcss.gemspec +gemspec + +gem "rake", "~> 13.0" + +gem "rspec", "~> 3.0" + +gem "rubocop", "~> 1.21" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..f1fb86a --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,95 @@ +PATH + remote: . + specs: + tailwindcss-rb (0.1.0) + activesupport (~> 7.0) + deep_merge (~> 1.0) + dry-configurable (~> 1.0) + +GEM + remote: https://rubygems.org/ + specs: + activesupport (7.1.3.4) + base64 + bigdecimal + concurrent-ruby (~> 1.0, >= 1.0.2) + connection_pool (>= 2.2.5) + drb + i18n (>= 1.6, < 2) + minitest (>= 5.1) + mutex_m + tzinfo (~> 2.0) + ast (2.4.2) + base64 (0.2.0) + bigdecimal (3.1.8) + concurrent-ruby (1.3.3) + connection_pool (2.4.1) + deep_merge (1.2.2) + diff-lcs (1.5.1) + drb (2.2.1) + dry-configurable (1.2.0) + dry-core (~> 1.0, < 2) + zeitwerk (~> 2.6) + dry-core (1.0.1) + concurrent-ruby (~> 1.0) + zeitwerk (~> 2.6) + i18n (1.14.5) + concurrent-ruby (~> 1.0) + json (2.7.2) + language_server-protocol (3.17.0.3) + minitest (5.24.1) + mutex_m (0.2.0) + parallel (1.25.1) + parser (3.3.4.0) + ast (~> 2.4.1) + racc + racc (1.8.0) + rainbow (3.1.1) + rake (13.2.1) + regexp_parser (2.9.2) + rexml (3.3.2) + strscan + rspec (3.13.0) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.0) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-support (3.13.1) + rubocop (1.65.0) + json (~> 2.3) + language_server-protocol (>= 3.17.0) + parallel (~> 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 2.4, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.31.1, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.31.3) + parser (>= 3.3.1.0) + ruby-progressbar (1.13.0) + strscan (3.1.0) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) + unicode-display_width (2.5.0) + zeitwerk (2.6.16) + +PLATFORMS + arm64-darwin-22 + +DEPENDENCIES + rake (~> 13.0) + rspec (~> 3.0) + rubocop (~> 1.21) + tailwindcss-rb! + +BUNDLED WITH + 2.4.5 diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..a0bd7b1 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2024 Guilherme Andrade + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..35f4d16 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# Tailwindcss + +TODO: Delete this and the text below, and describe your gem + +Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tailwindcss`. To experiment with that code, run `bin/console` for an interactive prompt. + +## Installation + +TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org. + +Install the gem and add to the application's Gemfile by executing: + + $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG + +If bundler is not being used to manage dependencies, install the gem by executing: + + $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG + +## Usage + +TODO: Write usage instructions here + +## Development + +After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. + +To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org). + +## Contributing + +Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tailwindcss. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/tailwindcss/blob/main/CODE_OF_CONDUCT.md). + +## License + +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). + +## Code of Conduct + +Everyone interacting in the Tailwindcss project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/tailwindcss/blob/main/CODE_OF_CONDUCT.md). diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..cca7175 --- /dev/null +++ b/Rakefile @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require "bundler/gem_tasks" +require "rspec/core/rake_task" + +RSpec::Core::RakeTask.new(:spec) + +require "rubocop/rake_task" + +RuboCop::RakeTask.new + +task default: %i[spec rubocop] diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..e31cedd --- /dev/null +++ b/bin/console @@ -0,0 +1,15 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require "bundler/setup" +require "tailwindcss" + +# You can add fixtures and/or initialization code here to make experimenting +# with your gem easier. You can also use a different console, if you like. + +# (If you use this, don't forget to add pry to your Gemfile!) +# require "pry" +# Pry.start + +require "irb" +IRB.start(__FILE__) diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..dce67d8 --- /dev/null +++ b/bin/setup @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' +set -vx + +bundle install + +# Do any other automated setup that you need to do here diff --git a/lib/tailwindcss.rb b/lib/tailwindcss.rb new file mode 100644 index 0000000..48dfb71 --- /dev/null +++ b/lib/tailwindcss.rb @@ -0,0 +1,44 @@ +require "dry/configurable" +require "deep_merge/rails_compat" + +require "tailwindcss/version" +require "tailwindcss/constants" +require "tailwindcss/types" +require "tailwindcss/compiler/runner" + +module Tailwindcss + extend Dry::Configurable + include Constants + extend self + + setting :output_path, default: proc { "./tmp" } + setting :content, default: proc { [] } + setting :prefix, default: "" + + setting :watch_content, default: false + + setting :breakpoints, default: BREAKPOINTS + setting :pseudo_selectors, default: PSEUDO_SELECTORS + setting :pseudo_elements, default: PSEUDO_ELEMENTS + setting :theme, default: THEME + + setting :logger, default: proc { Logger.new(STDOUT) } + + module ExtendTheme + def extend_theme(**overrides) + self.theme = theme.deeper_merge(overrides) + end + end + + config.extend ExtendTheme + + def configure(&blk) + super(&blk) + init! + end + + def init! + require "tailwindcss/style" + Compiler::Runner.new.call + end +end diff --git a/lib/tailwindcss/arbitrary_value.rb b/lib/tailwindcss/arbitrary_value.rb new file mode 100644 index 0000000..e017cd1 --- /dev/null +++ b/lib/tailwindcss/arbitrary_value.rb @@ -0,0 +1,15 @@ +module Tailwindcss + class ArbitraryValue + def initialize(value) + @value = value + end + + def to_s + "[#{@value}]" + end + + def inspect + to_s + end + end +end diff --git a/lib/tailwindcss/asset_helper.rb b/lib/tailwindcss/asset_helper.rb new file mode 100644 index 0000000..b90c8ad --- /dev/null +++ b/lib/tailwindcss/asset_helper.rb @@ -0,0 +1,19 @@ +module Tailwindcss + module AssetHelper + include ActionView::Helpers::AssetTagHelper + + extend self + + def vite_asset_path(name) + manifest = JSON.parse(File.read(Rails.root.join("public", "assets", "manifest.json"))) + File.join("/assets", manifest.dig(name, "file")) + end + + def view_component_ui_asset_tags + safe_join [ + content_tag(:script, "", src: vite_asset_path("assets/main.ts")), + content_tag(:link, "", rel: "stylesheet", href: vite_asset_path("style.css")) + ] + end + end +end diff --git a/lib/tailwindcss/compiler/channel.rb b/lib/tailwindcss/compiler/channel.rb new file mode 100644 index 0000000..b6f76ae --- /dev/null +++ b/lib/tailwindcss/compiler/channel.rb @@ -0,0 +1,18 @@ +require "tailwindcss/asset_helper" + +module Tailwindcss + module Compiler + class Channel < ActionCable::Channel::Base + extend AssetHelper + + def subscribed + stream_from "compiler_channel" + end + + def self.broadcast_css_changed + css_path = vite_asset_path("style.css") + ActionCable.server.broadcast("compiler_channel", {css_path:}) + end + end + end +end diff --git a/lib/tailwindcss/compiler/connection.rb b/lib/tailwindcss/compiler/connection.rb new file mode 100644 index 0000000..4013197 --- /dev/null +++ b/lib/tailwindcss/compiler/connection.rb @@ -0,0 +1,6 @@ +module Tailwindcss + module Compiler + class Connection < ActionCable::Connection::Base + end + end +end diff --git a/lib/tailwindcss/compiler/file_classes_extractor.rb b/lib/tailwindcss/compiler/file_classes_extractor.rb new file mode 100644 index 0000000..a66c8dc --- /dev/null +++ b/lib/tailwindcss/compiler/file_classes_extractor.rb @@ -0,0 +1,24 @@ +module Tailwindcss + module Compiler + class FileClassesExtractor + require "tailwindcss/compiler/output" + require "tailwindcss/compiler/file_parser" + + def call(file_path:) + ast = file_parser.call(file_path:) + return unless ast + + hash_args = hash_args_extractor.call(ast:) + hash_args.map { class_list_builder.call(**_1) }.flatten.compact + end + + private + + def hash_args_extractor = HashArgsExtractor.new + + def file_parser = FileParser.new + + def class_list_builder = StyleAttributesToListConverter.new + end + end +end diff --git a/lib/tailwindcss/compiler/file_parser.rb b/lib/tailwindcss/compiler/file_parser.rb new file mode 100644 index 0000000..3a499b2 --- /dev/null +++ b/lib/tailwindcss/compiler/file_parser.rb @@ -0,0 +1,54 @@ +module Tailwindcss + module Compiler + class FileParser + require "parser/current" + require "tailwindcss/compiler/hash_args_extractor" + + def call(file_path:) + file_content = File.read(file_path) + code = if file_is_ruby?(file_path:) + extract_ruby_from_rb(file_content:) + elsif file_is_erb?(file_path:) + extract_ruby_from_erb(erb: file_content) + else + return + end + + buffer = Parser::Source::Buffer.new(file_path) + buffer.source = code + + parser = Parser::CurrentRuby.new + parser.parse(buffer) + rescue Parser::SyntaxError + Rails.logger.error("Tailwindcss: Failed to parse #{file_path}. Skipping...") + end + + private + + def file_is_ruby?(file_path:) + file_path.end_with?(".rb") + end + + def file_is_erb?(file_path:) + file_path.end_with?(".html.erb") + end + + def erb_template_in_ruby?(file_path:) + file_is_ruby?(file_path:) && File.read(file_path).match?(/<<(~|-)ERB/) + end + + def extract_ruby_from_rb(file_content:) + [file_content, erb_code_from_rb(file_content:)].flatten.compact.join("\n") + end + + def erb_code_from_rb(file_content:) + extract_ruby_from_erb(erb: file_content.match(/<<[~-]ERB(.*)ERB/m)[1]) + end + + def extract_ruby_from_erb(erb:) + # Use a regex to extract ruby code from ERB + erb.scan(/<%=?([\s\S]*?)%>/m).flatten.compact.join("\n") + end + end + end +end diff --git a/lib/tailwindcss/compiler/hash_args_extractor.rb b/lib/tailwindcss/compiler/hash_args_extractor.rb new file mode 100644 index 0000000..627716b --- /dev/null +++ b/lib/tailwindcss/compiler/hash_args_extractor.rb @@ -0,0 +1,87 @@ +require 'active_support/core_ext/object/blank' + +module Tailwindcss + module Compiler + class HashArgsExtractor + def call(ast:) + extract_hash_arguments_from_ast_nodes(ast) + end + + private + + def extract_hash_arguments_from_ast_nodes(node) + hash_args = [] + return unless node.is_a?(Parser::AST::Node) + + if node.type == :send + hash_args += extract_hashes(node).flatten(10) + end + + node.children.each do |child| + next unless child.is_a?(Parser::AST::Node) + + hash_args += extract_hash_arguments_from_ast_nodes(child) + end + + hash_args.flatten.compact + end + + def extract_hashes(node) + scan_for_hash_children(node).each_with_object([]) { |hash_node, acc| extract_value(hash_node, acc) }.compact + end + + def scan_for_hash_children(node) + node.children[2..].select { |child| child.type == :hash } + end + + def extract_value(node, acc) + node.children.select { |n| n.type == :pair }.each do |key_value_node| + key_node = key_value_node.children.first + value_node = key_value_node.children.last + value = pair_node_value(key_node, value_node) + + acc << value if value.present? + end + end + + def pair_node_value(key_node, value_node) + key = source_code(key_node) + case value_node.type + when :hash + hashes = [] + extract_value(value_node, hashes) + hashes.flatten.map { |h| { key.to_sym => h } } + when :int, :str, :sym, :float + { key.to_sym => node_text(value_node) } + when :true + { key.to_sym => true } + when :false + { key.to_sym => false } + else + extract_color_scheme_calls(key_node, value_node) + end + end + + def extract_color_scheme_calls(key_node, value_node) + value = source_code(value_node) + return unless value.include?('props_color_scheme_token') + + match = /props_color_scheme_token\((\d+)\)/.match(value) + return unless match + + weight = match[1].to_i + Tailwindcss.config.theme.color_scheme.values.map do |color| + { source_code(key_node).to_sym => "#{color}-#{weight}" } + end + end + + def node_text(node) + source_code(node).delete(':').delete('\'').to_s + end + + def source_code(node) + node.location.expression.source + end + end + end +end diff --git a/lib/tailwindcss/compiler/output.rb b/lib/tailwindcss/compiler/output.rb new file mode 100644 index 0000000..7aeae4f --- /dev/null +++ b/lib/tailwindcss/compiler/output.rb @@ -0,0 +1,35 @@ +require "fileutils" + +module Tailwindcss + module Compiler + class Output + def add_entry(file_path:, classes:) + return if classes.blank? + + path = compiled_file_path(file_path:) + create_folder(file_path: path) + + path += ".classes" + File.open(path, "wb") do |file| + file << classes.join("\n") + end + File.delete(path) if File.empty?(path) + end + + def output_path + Tailwindcss.config.output_path.call.to_s + end + + def create_folder(file_path:) + dir_name = File.dirname(file_path) + FileUtils.mkdir_p(dir_name) + end + + def compiled_file_path(file_path:) + Tailwindcss.config.content.each do |folder| + return File.join(output_path, file_path.delete_prefix(folder.to_s)) if file_path.include?(folder.to_s) + end + end + end + end +end diff --git a/lib/tailwindcss/compiler/runner.rb b/lib/tailwindcss/compiler/runner.rb new file mode 100644 index 0000000..2b49c81 --- /dev/null +++ b/lib/tailwindcss/compiler/runner.rb @@ -0,0 +1,56 @@ +module Tailwindcss + module Compiler + class Runner + require "listen" + require "tailwindcss/compiler/file_classes_extractor" + require "tailwindcss/compiler/output" + require "tailwindcss/compiler/connection" if defined?(ActionCable) + require "tailwindcss/compiler/channel" if defined?(ActionCable) + + def call # rubocop:disable Metrics/AbcSize, Metrics/MethodLength + output = Output.new + file_classes_extractor = FileClassesExtractor.new + + content.each do |location| + if Tailwindcss.config.watch_content + listener = Listen.to(File.dirname(location.to_s), only: /\.(rb|erb)$/) do |modified, added, removed| + Tailwindcss.config.logger.info "Recompiling Tailwindcss..." + Tailwindcss.config.logger.info "Modified: #{modified}" + Tailwindcss.config.logger.info "Added: #{added}" + Tailwindcss.config.logger.info "Removed: #{removed}" + (modified + added + removed).each do |file_path| + classes = file_classes_extractor.call(file_path:) + next unless classes.present? + + output.add_entry(file_path:, classes:) + end + + system "yarn build:view-component-ui" + Channel.broadcast_css_changed + end + + listener.start + end + + Dir.glob("#{location}/**/*").each do |file_path| + next unless File.file?(file_path) + + classes = file_classes_extractor.call(file_path:) + next unless classes.present? + + output.add_entry(file_path:, classes:) + end + end + + system "yarn build:view-component-ui" + Channel.broadcast_css_changed if defined?(ActionCable) + end + + private + + def content + Tailwindcss.config.content.respond_to?(:call) ? Tailwindcss.config.content.call : Tailwindcss.config.content + end + end + end +end diff --git a/lib/tailwindcss/constants.rb b/lib/tailwindcss/constants.rb new file mode 100644 index 0000000..01d15e6 --- /dev/null +++ b/lib/tailwindcss/constants.rb @@ -0,0 +1,323 @@ +module Tailwindcss + module Constants + COLOR_WEIGHTS = ([nil] + %i[50 100 200 300 400 500 600 700 800 900]).freeze + BREAKPOINTS = %i[xs sm md lg xl 2xl].freeze + PSEUDO_SELECTORS = %i[hover focus active visited disabled first last first_of_type last_of_type odd even group_hover].freeze + PSEUDO_ELEMENTS = %i[before after file first_letter first_line selection backdrop marker].freeze + + ALIGN_CONTENT = %i[normal center start end between around evenly baseline stretch].freeze + ALIGN_ITEMS = %i[start end center baseline stretch].freeze + BACKFACE_VISIBILITY = %i[visible hidden].freeze + BACKGROUND_ATTACHMENT = %i[fixed local scroll].freeze + BACKGROUND_BLEND_MODE = %i[normal multiply screen overlay darken lighten color-dodge color-burn hard-light + soft-light difference exclusion hue saturation color luminosity].freeze + BACKGROUND_CLIP = %i[border padding content].freeze + BACKGROUND_IMAGE = %i[none gradient-to-t gradient-to-tr gradient-to-r gradient-to-br gradient-to-b gradient-to-bl + gradient-to-l gradient-to-tl].freeze + BACKGROUND_ORIGIN = %i[border padding content].freeze + BACKGROUND_POSITION = %i[bottom center left left-bottom left-top right right-bottom right-top + top].freeze + BACKGROUND_REPEAT = %i[repeat repeat-x repeat-y no-repeat round space].freeze + BORDER_RADIUSES = %i[none sm md lg full].freeze + BACKGROUND_SIZE = %i[auto cover contain].freeze + BORDER_COLLAPSE = %i[collapse separate].freeze + BORDER_RADIUS = %i[none sm true md lg xl 2xl 3xl full] + [true].freeze + CONTENT = %i[none].freeze + SPACING = %i[0 px 0.5 1 1.5 2 2.5 3 3.5 4 5 6 7 8 9 10 11 12 14 16 20 24 28 32 36 40 44 48 52 56 60 64 72 + 80 96 auto].freeze + BORDER_STYLE = %i[solid dashed dotted double none].freeze + BORDER = [true, false].freeze + BORDER_WIDTH = %i[0 1 2 4 8].freeze + BOX_DECORATION_BREAK = %i[slice clone].freeze + BOX_SHADOW = (%i[sm md lg xl 2xl inner none] + [true]).freeze + BOX_SIZING = %i[border content].freeze + CAPTION_SIDE = %i[top bottom].freeze + CLEAR = %i[left right both none].freeze + COLORS = %i[transparent current inherit black white neutral zync stone gray red amber yellow lime green blue indigo + violet purple pink slate emerald fuchsia rose cyan teal sky].freeze + COLOR_SCHEME = { + primary: :purple, + secondary: :indigo, + success: :emerald, + danger: :rose, + warning: :yellow, + info: :teal, + neutral: :gray, + dark: :black, + light: :white + }.freeze + COLUMNS = %i[auto 1 2 3 4 5 6 7 8 9 10 11 12 3xs 2xs xs sm md lg xl 2xl 3xl 4xl 5xl 6xl 7xl].freeze + COLUMN_SPAN = %i[auto 1 2 3 4 5 6 7 8 9 10 11 12 all].freeze + COLUMN_START = %i[auto 1 2 3 4 5 6 7 8 9 10 11 12 13].freeze + COLUMN_END = %i[auto 1 2 3 4 5 6 7 8 9 10 11 12 13].freeze + CONTENT = %i[none].freeze + CURSOR = %i[auto default pointer wait text move help not-allowed].freeze + DISPLAY = %i[block inline-block inline inline-table table table-caption table-cell table-column table-column-group + table-footer-group table-header-group table-row table-row-group flow-root grid inline-grid contents + list-item hidden flex inline-flex].freeze + EMPTY_CELLS = %i[show hide].freeze + FLEX_BASIS = %i[auto 0].freeze + FLEX_DIRECTION = %i[row row-reverse col col-reverse].freeze + FLEX_GROW = [0, true].freeze + FLEX_SHRINK = [0, true].freeze + FLEX_WRAP = %i[wrap nowrap wrap-reverse].freeze + FLEX = %i[1 auto initial none].freeze + FONT_FAMILY = %i[sans serif mono].freeze + FONT_SIZE = %i[xs sm base lg xl 2xl 3xl 4xl 5xl 6xl 7xl 8xl 9xl].freeze + FONT_STYLE = %i[italic non-italic].freeze + FONT_VARIANT_NUMERIC = %i[normal-nums ordinal slashed-zero lining-nums oldstyle-nums proportional-nums + tabular-nums diagonal-fractions stacked-fractions].freeze + FONT_WEIGHT = %i[hairline thin light normal medium semibold bold extrabold black].freeze + GRID_AUTO_COLUMNS = %i[auto min max fr].freeze + GRID_AUTO_FLOW = %i[row column dense row-dense column-dense].freeze + GRID_AUTO_ROWS = %i[auto min max fr].freeze + GRID_TEMPLATE_COLUMNS = %i[none 1 2 3 4 5 6 7 8 9 10 11 12].freeze + GRID_TEMPLATE_ROWS = %i[none 1 2 3 4 5 6].freeze + GROUP = [true, false].freeze + SIZES = %i[0 px 0.5 1 1.5 2 2.5 3 3.5 4 5 6 7 8 9 10 11 12 14 16 20 24 28 32 36 40 44 48 52 56 60 64 72 + 80 96 auto 1/2 1/3 2/3 1/4 2/4 3/4 1/5 2/5 3/5 4/5 1/6 2/6 3/6 4/6 5/6 full screen min max fit].freeze + HYPHENS = %i[none manual auto].freeze + ISOLATION = %i[isolate auto].freeze + JUSTIFY_CONTENT = %i[start end center between around evenly].freeze + JUSTIFY_ITEMS = %i[start end center baseline stretch].freeze + JUSTIFY_SELF = %i[start end center auto stretch].freeze + LETTER_SPACING = %i[tighter tight normal wide wider widest].freeze + LINE_HEIGHT = %i[none tight snug normal relaxed loose 3 4 5 6 7 8 9 10].freeze + LIST_STYLE_IMAGE = %i[none].freeze + LIST_STYLE_POSITION = %i[inside outside].freeze + LIST_STYLE_TYPE = %i[none disc decimal].freeze + MAX_SIZE = %i[0 xs sm md lg xl 2xl 3xl 4xl 5xl 6xl 7xl full screen min max fit prose screen-sm screen-md + screen-lg screen-xl screen-2xl].freeze + MIN_SIZE = [0, :full, :min, :max, :fit].freeze + MIX_BLEND_MODE = %i[normal multiply screen overlay darken lighten color-dodge color-burn hard-light soft-light + difference exclusion hue saturation color luminosity].freeze + OBJECT_FIT = %i[contain cover fill none scale-down].freeze + OBJECT_POSITION = %i[top right bottom left center].freeze + OPACITY = %i[0 5 10 20 25 30 40 50 60 70 75 80 90 95 100].freeze + ORDER = %i[first last none 1 2 3 4 5 6 7 8 9 10 11 12].freeze + OUTLINE_STYLE = %i[dotted dashed solid double none].freeze + OUTLINE_WIDTH = %i[0 1 2 4 8].freeze + OVERFLOW = %i[auto hidden visible scroll x-hidden y-hidden x-scroll y-scroll x-auto y-auto].freeze + PLACE_ITEMS = %i[start end center stretch].freeze + PLACE_SELF = %i[start end center stretch].freeze + POINTER_EVENTS = %i[none auto].freeze + POSITION = %i[static fixed absolute relative sticky].freeze + RESIZE = %i[true false none x y].freeze + ROTATE = %i[0 1 2 3 6 12 45 90 180].map { [_1, :"-#{_1}"] }.flatten.freeze + ROW_SPAN = %i[auto 1 2 3 4 5 6 full].freeze + ROW_START = %i[auto 1 2 3 4 5 6 7].freeze + ROW_END = %i[auto 1 2 3 4 5 6 7].freeze + SCALE = %i[0 50 75 90 95 100 105 110 125 150 200].freeze + SCROLL_BEHAVIOR = %i[auto smooth].freeze + SCROLL_SNAP_ALIGN = %i[start end center none].freeze + SCROLL_SNAP_STOP = %i[always normal].freeze + SCROLL_SNAP_TYPE = %i[none mandatory proximity].freeze + TABLE_LAYOUT = %i[auto fixed].freeze + TEXT_ALIGN = %i[left center right justify start end].freeze + TEXT_DECORATION = %i[underline line-through no-underline].freeze + TEXT_DECORATION_STYLE = %i[solid double dotted dashed wavy].freeze + TEXT_DECORATION_THICKNESS = %i[auto from-font 0 1 2 4 8].freeze + TEXT_OVERFLOW = %i[ellipsis truncate].freeze + TEXT_TRANSFORM = %i[uppercase lowercase capitalize normal].freeze + TEXT_UNDERLINE_OFFSET = %i[auto 0 1 2 4 8].freeze + TRANSFORM_ORIGIN = %i[center top-right top top-left right bottom-right bottom bottom-left left].freeze + TRANSITION = %i[none all colors opacity shadow transform].freeze + TRANSLATE = %i[0 0.5 px 1 1.5 2 2.5 3 3.5 4 5 6 7 8 9 10 11 12 14 16 20 24 28 32 36 40 44 48 52 56 60 64 72 80 96 1/2 1/3 2/3 1/4 2/3 2/4 3/4 full].map { [_1, :"-#{_1}"] }.flatten.freeze + USER_SELECT = %i[none text all auto].freeze + VERTICAL_ALIGN = %i[top middle bottom baseline text-top text-bottom sub super].freeze + VISIBILITY = %i[visible invisible collapse].freeze + WHITESPACE = %i[normal nowrap pre pre-line pre-wrap break-words].freeze + WILL_CHANGE = %i[auto scroll contents opacity transform].freeze + WORD_BREAK = %i[normal words all keep].freeze + Z_INDEX = %i[0 10 20 30 40 50 auto].freeze + + COLOR_WITH_WEIGHTS = proc { + COLORS.map do |color| + COLOR_WEIGHTS.map { |weight| [color, weight].compact.join("-") } + end.flatten.freeze + } + + THEME = { + align_content: {values: ALIGN_CONTENT, token: "content"}, + align_items: {values: ALIGN_ITEMS, token: "items", alias: :items}, + backface_visibility: {values: BACKFACE_VISIBILITY, token: "backface"}, + background: {values: COLOR_WITH_WEIGHTS, token: "bg", alias: :bg}, + background_attachment: {values: BACKGROUND_ATTACHMENT, token: "bg"}, + background_blend_mode: {values: BACKGROUND_BLEND_MODE, token: "bg"}, + background_clip: {values: BACKGROUND_CLIP, token: "bg"}, + background_color: {values: COLOR_WITH_WEIGHTS, token: "bg"}, + background_image: {values: BACKGROUND_IMAGE, token: "bg"}, + background_origin: {values: BACKGROUND_ORIGIN, token: "bg"}, + background_position: {values: BACKGROUND_POSITION, token: "bg"}, + background_repeat: {values: BACKGROUND_REPEAT, token: "bg"}, + background_size: {values: BACKGROUND_SIZE, token: "bg"}, + border_bottom_left_radius: {values: BORDER_RADIUS, token: "rounded-bl"}, + border_bottom_right_radius: {values: BORDER_RADIUS, token: "rounded-br"}, + border_top_left_radius: {values: BORDER_RADIUS, token: "rounded-tl"}, + border_top_right_radius: {values: BORDER_RADIUS, token: "rounded-tr"}, + border_top_radius: {values: BORDER_RADIUS, token: "rounded-t"}, + border_bottom_radius: {values: BORDER_RADIUS, token: "rounded-b"}, + border_left_radius: {values: BORDER_RADIUS, token: "rounded-l"}, + border_right_radius: {values: BORDER_RADIUS, token: "rounded-r"}, + border_collapse: {values: BORDER_COLLAPSE, token: "border"}, + border_color: {values: COLOR_WITH_WEIGHTS, token: "border"}, + border_radius: {values: BORDER_RADIUS, token: "rounded", alias: :rounded}, + border_spacing: {values: SPACING, token: "border-spacing"}, + border_spacing_x: {values: SPACING, token: "border-spacing-x"}, + border_spacing_y: {values: SPACING, token: "border-spacing-y"}, + border_style: {values: BORDER_STYLE, token: "border"}, + border: {values: BORDER, token: "border"}, + border_top: {values: BORDER + BORDER_WIDTH, token: "border-t"}, + border_bottom: {values: BORDER + BORDER_WIDTH, token: "border-b"}, + border_left: {values: BORDER + BORDER_WIDTH, token: "border-l"}, + border_right: {values: BORDER + BORDER_WIDTH, token: "border-r"}, + border_width: {values: BORDER_WIDTH, token: "border"}, + border_y: {values: BORDER_WIDTH, token: "border-y"}, + border_x: {values: BORDER_WIDTH, token: "border-x"}, + border_start: {values: BORDER_WIDTH, token: "border-s"}, + border_end: {values: BORDER_WIDTH, token: "border-e"}, + bottom: {values: SIZES, token: "bottom"}, + box_decoration_break: {values: BOX_DECORATION_BREAK, token: "box-decoration-break"}, + box_shadow: {values: BOX_SHADOW, token: "shadow", alias: :shadow}, + box_sizing: {values: BOX_SIZING, token: "box"}, + caption_side: {values: CAPTION_SIDE, token: "caption"}, + clear: {values: CLEAR, token: "clear"}, + color: {values: COLOR_WITH_WEIGHTS, token: "text"}, + column_gap: {values: SPACING, token: "gap"}, + column_span: {values: COLUMN_SPAN, token: "col-span", alias: :col_span}, + column_start: {values: COLUMN_START, token: "col-start", alias: :col_start}, + column_end: {values: COLUMN_END, token: "col-end", alias: :col_end}, + columns: {values: COLUMNS, token: "columns"}, + content: {values: CONTENT, token: "content"}, + cursor: {values: CURSOR, token: "cursor"}, + display: {values: DISPLAY, token: nil, alias: :d}, + empty_cells: {values: EMPTY_CELLS, token: "empty"}, + end: {values: SIZES, token: "end"}, + fill: {values: COLOR_WITH_WEIGHTS, token: "fill"}, + flex_basis: {values: FLEX_BASIS, token: "flex"}, + flex_direction: {values: FLEX_DIRECTION, token: "flex", alias: :direction}, + flex_grow: {values: FLEX_GROW, token: "flex", alias: :grow}, + flex_shrink: {values: FLEX_SHRINK, token: "flex", alias: :shrink}, + flex_wrap: {values: FLEX_WRAP, token: "flex"}, + flex: {values: FLEX, token: "flex"}, + font_family: {values: FONT_FAMILY, token: "font"}, + font_size: {values: FONT_SIZE, token: "text"}, + font_style: {values: FONT_STYLE, token: nil}, + font_variant_numeric: {values: FONT_VARIANT_NUMERIC, token: nil}, + font_weight: {values: FONT_WEIGHT, token: "font"}, + grid_auto_columns: {values: GRID_AUTO_COLUMNS, token: "auto-cols"}, + grid_auto_flow: {values: GRID_AUTO_FLOW, token: "grid-flow"}, + grid_auto_rows: {values: GRID_AUTO_ROWS, token: "auto-rows"}, + group: {values: GROUP, token: "group"}, + gap: {values: SPACING, token: "gap"}, + gap_x: {values: SPACING, token: "gap-x"}, + gap_y: {values: SPACING, token: "gap-y"}, + grid_template_columns: {values: GRID_TEMPLATE_COLUMNS, token: "grid-cols", alias: :grid_cols}, + grid_template_rows: {values: GRID_TEMPLATE_ROWS, token: "grid-rows"}, + height: {values: SIZES, token: "h", alias: :h}, + hyphens: {values: HYPHENS, token: "hyphens"}, + inset: {values: SIZES, token: "inset"}, + inset_x: {values: SIZES, token: "inset-x"}, + inset_y: {values: SIZES, token: "inset-y"}, + isolation: {values: ISOLATION, token: "isolation"}, + justify_content: {values: JUSTIFY_CONTENT, token: "justify", alias: :justify}, + justify_items: {values: JUSTIFY_ITEMS, token: "justify"}, + justify_self: {values: JUSTIFY_SELF, token: "justify"}, + left: {values: SIZES, token: "left"}, + letter_spacing: {values: LETTER_SPACING, token: "tracking"}, + line_height: {values: LINE_HEIGHT, token: "leading"}, + list_style_image: {values: LIST_STYLE_IMAGE, token: "list-image"}, + list_style_position: {values: LIST_STYLE_POSITION, token: "list"}, + list_style_type: {values: LIST_STYLE_TYPE, token: "list"}, + margin_bottom: {values: SPACING, token: "mb", alias: :mb}, + margin_end: {values: SPACING, token: "me", alias: :me}, + margin_left: {values: SPACING, token: "ml", alias: :ml}, + margin_right: {values: SPACING, token: "mr", alias: :mr}, + margin_start: {values: SPACING, token: "ms", alias: :ms}, + margin_top: {values: SPACING, token: "mt", alias: :mt}, + margin_x: {values: SPACING, token: "mx", alias: :mx}, + margin_y: {values: SPACING, token: "my", alias: :my}, + margin: {values: SPACING, token: "m", alias: :m}, + max_height: {values: SIZES, token: "max-h", alias: :max_h}, + max_width: {values: SIZES, token: "max-w", alias: :max_w}, + min_height: {values: SIZES, token: "min-h", alias: :min_h}, + min_width: {values: SIZES, token: "min-w", alias: :min_w}, + mix_blend_mode: {values: MIX_BLEND_MODE, token: "mix-blend"}, + object_fit: {values: OBJECT_FIT, token: "object"}, + object_position: {values: OBJECT_POSITION, token: "object"}, + opacity: {values: OPACITY, token: "opacity"}, + order: {values: ORDER, token: "order"}, + outline_color: {values: COLOR_WITH_WEIGHTS, token: "outline"}, + outline_style: {values: OUTLINE_STYLE, token: "outline", alias: :outline}, + outline_width: {values: OUTLINE_WIDTH, token: "outline"}, + overflow: {values: OVERFLOW, token: "overflow"}, + overflow_x: {values: OVERFLOW, token: "overflow-x"}, + overflow_y: {values: OVERFLOW, token: "overflow-y"}, + padding: {values: SPACING, token: "p", alias: :p}, + padding_bottom: {values: SPACING, token: "pb", alias: :pb}, + padding_left: {values: SPACING, token: "pl", alias: :pl}, + padding_right: {values: SPACING, token: "pr", alias: :pr}, + padding_top: {values: SPACING, token: "pt", alias: :pt}, + padding_x: {values: SPACING, token: "px", alias: :px}, + padding_y: {values: SPACING, token: "py", alias: :py}, + place_items: {values: PLACE_ITEMS, token: "place"}, + place_self: {values: PLACE_SELF, token: "place"}, + pointer_events: {values: POINTER_EVENTS, token: "pointer"}, + position: {values: POSITION, token: nil}, + resize: {values: RESIZE, token: "resize"}, + right: {values: SIZES, token: "right"}, + rotate: {values: ROTATE, token: proc { _1.to_s.start_with?("-") ? "-rotate-#{_1.to_s[1..-1]}" : "rotate-#{_1}" }}, + row_gap: {values: SPACING, token: "gap"}, + row_span: {values: ROW_SPAN, token: "row-span"}, + row_start: {values: ROW_START, token: "row-start"}, + row_end: {values: ROW_END, token: "row-end"}, + scale: {values: SCALE, token: "scale"}, + scale_x: {values: SCALE, token: "scale-x"}, + scale_y: {values: SCALE, token: "scale-y"}, + scroll_behavior: {values: SCROLL_BEHAVIOR, token: "scroll"}, + scroll_margin_bottom: {values: SPACING, token: "scroll-mb"}, + scroll_margin_left: {values: SPACING, token: "scroll-ml"}, + scroll_margin_right: {values: SPACING, token: "scroll-mr"}, + scroll_margin_top: {values: SPACING, token: "scroll-mt"}, + scroll_margin_x: {values: SPACING, token: "scroll-mx"}, + scroll_margin_y: {values: SPACING, token: "scroll-my"}, + scroll_margin_start: {values: SPACING, token: "scroll-ms"}, + scroll_margin_end: {values: SPACING, token: "scroll-me"}, + scroll_padding_bottom: {values: SPACING, token: "scroll-pb"}, + scroll_padding_left: {values: SPACING, token: "scroll-pl"}, + scroll_padding_right: {values: SPACING, token: "scroll-pr"}, + scroll_padding_top: {values: SPACING, token: "scroll-pt"}, + scroll_padding_x: {values: SPACING, token: "scroll-px"}, + scroll_padding_y: {values: SPACING, token: "scroll-py"}, + scroll_padding_start: {values: SPACING, token: "scroll-ps"}, + scroll_padding_end: {values: SPACING, token: "scroll-pe"}, + scroll_snap_align: {values: SCROLL_SNAP_ALIGN, token: "snap"}, + scroll_snap_stop: {values: SCROLL_SNAP_STOP, token: "snap"}, + scroll_snap_type: {values: SCROLL_SNAP_TYPE, token: "snap"}, + start: {values: SIZES, token: "start"}, + table_layout: {values: TABLE_LAYOUT, token: "table"}, + text_align: {values: TEXT_ALIGN, token: "text"}, + text_decoration_color: {values: COLOR_WITH_WEIGHTS, token: "decoration"}, + text_decoration_style: {values: TEXT_DECORATION_STYLE, token: "decoration"}, + text_decoration_thickness: {values: TEXT_DECORATION_THICKNESS, token: "decoration"}, + text_decoration: {values: TEXT_DECORATION, token: proc { _1 }}, + text_indent: {values: SPACING, token: "indent"}, + text_overflow: {values: TEXT_OVERFLOW, token: proc { |v| (v == "truncate") ? "truncate" : "text-#{v}" }}, + text_transform: {values: TEXT_TRANSFORM, token: nil}, + text_underline_offset: {values: TEXT_UNDERLINE_OFFSET, token: "underline-offset"}, + top: {values: SIZES, token: "top"}, + transform_origin: {values: TRANSFORM_ORIGIN, token: "origin"}, + transition: {values: TRANSITION + [true], token: "transition"}, + translate_x: {values: TRANSLATE, token: proc { _1.to_s.start_with?("-") ? "-translate-#{_1.to_s[1..-1]}" : "translate-#{_1}" }}, + translate_y: {values: TRANSLATE, token: proc { _1.to_s.start_with?("-") ? "-translate-y-#{_1.to_s[1..-1]}" : "translate-y-#{_1}" }}, + user_select: {values: USER_SELECT, token: "select"}, + vertical_align: {values: VERTICAL_ALIGN, token: "align"}, + visibility: {values: VISIBILITY, token: nil}, + whitespace: {values: WHITESPACE, token: "whitespace", alias: :white_space}, + width: {values: SIZES, token: "w", alias: :w}, + will_change: {values: WILL_CHANGE, token: "will-change"}, + word_break: {values: WORD_BREAK, token: "break"}, + z_index: {values: Z_INDEX, token: "z", alias: :z} + }.freeze + end +end diff --git a/lib/tailwindcss/helpers.rb b/lib/tailwindcss/helpers.rb new file mode 100644 index 0000000..2865072 --- /dev/null +++ b/lib/tailwindcss/helpers.rb @@ -0,0 +1,11 @@ +module Tailwindcss + module Helpers + def tailwind(**style_attributes) + Style.new(**style_attributes).to_s + end + + def ab(value) + ArbitraryValue.new(value) + end + end +end diff --git a/lib/tailwindcss/installer.rb b/lib/tailwindcss/installer.rb new file mode 100644 index 0000000..f8b8601 --- /dev/null +++ b/lib/tailwindcss/installer.rb @@ -0,0 +1,114 @@ +module Tailwindcss + class Installer + def call + generate_vite_config_file + generate_tailwind_config_file + add_vite_package_to_package_json unless package_json["devDependencies"]["vite"] + add_vite_scripts_to_package_json unless package_json["scripts"]["build:view-component-ui"] + save_package_json + end + + private + + def generate_vite_config_file + File.write(ViewComponentUI.config.vite.config_file_path.call, vite_config_file_content) + end + + def generate_tailwind_config_file + File.write(File.join(File.dirname(package_json_path), "tailwind.config.js"), tailwind_config_file_content) + end + + def add_vite_package_to_package_json + package_json["devDependencies"].merge!(vite_package_config) + end + + def add_vite_scripts_to_package_json + package_json["scripts"].merge!(vite_scripts) + end + + def save_package_json + File.write(package_json_path, JSON.pretty_generate(package_json)) + end + + def package_json + @package_json ||= JSON.parse(File.read(package_json_path)) + end + + def vite_package_config + {vite: "^4.4.5"} + end + + def vite_scripts + {"build:view-component-ui": "vite build --config #{vite_config_file_path}"} + end + + def package_json_path + value = ViewComponentUI.config.package_json_path + return value unless value.respond_to?(:call) + + value.call + end + + def vite_config_file_path + value = ViewComponentUI.config.vite.config_file_path + return value unless value.respond_to?(:call) + + value.call + end + + def content + value = ViewComponentUI.config.content + return value unless value.respond_to?(:call) + + value.call + end + + def output_path + value = ViewComponentUI.config.output_path + return value unless value.respond_to?(:call) + + value.call + end + + def tailwind_config_file_content + content_option = (content + [output_path]).map { "#{_1}/**/*" } + <<~TAILWIND_CONFIG + /** @type {import('tailwindcss').Config} */ + export default { + content: #{content_option.to_json}, + prefix: '#{ViewComponentUI.config.prefix}', + plugins: [], + } + TAILWIND_CONFIG + end + + def vite_config_file_content + <<~VITE_CONFIG + // vite.config.js + import { defineConfig } from "vite"; + + export default defineConfig({ + build: { + lib: { + entry: "#{Engine.root.join("assets", "main.ts")}", + name: "ViewComponentUI", + fileName: "view-component-ui", + }, + rollupOptions: { + external: ["@hotwired/stimulus"], + output: { + assetFileNames: (assetInfo) => { + if (assetInfo.name === 'style.css') return 'view-component-ui.css'; + + return assetInfo.name; + }, + }, + }, + outDir: "#{Rails.root.join("public", "assets")}", + manifest: true + }, + }); + VITE_CONFIG + end + end +end diff --git a/lib/tailwindcss/style.rb b/lib/tailwindcss/style.rb new file mode 100644 index 0000000..9ad9896 --- /dev/null +++ b/lib/tailwindcss/style.rb @@ -0,0 +1,30 @@ +require "ostruct" +require "tailwindcss/types" +require "tailwindcss/style_attributes_to_list_converter" + +module Tailwindcss + class Style < ::OpenStruct + def initialize(attributes = {}) + super + @attributes = attributes + end + + def to_a + to_string_converter.call(**@attributes) + end + + def to_s + to_a.join(" ") + end + + def to_html_attribute + {class: to_s} + end + + private + + def to_string_converter + @to_string_converter ||= StyleAttributesToListConverter.new + end + end +end diff --git a/lib/tailwindcss/style_attributes_to_list_converter.rb b/lib/tailwindcss/style_attributes_to_list_converter.rb new file mode 100644 index 0000000..776ae67 --- /dev/null +++ b/lib/tailwindcss/style_attributes_to_list_converter.rb @@ -0,0 +1,42 @@ +require "dry/initializer" +require "pry" +require "active_support/core_ext/object/blank" +require "active_support/core_ext/module/delegation" + +module Tailwindcss + class StyleAttributesToListConverter + extend Dry::Initializer + + def call(**style) + add_prefix(build_style_prop_classes(style.to_h)) + end + + private + + def add_prefix(strings) + strings.map do |style_prop_class| + [Tailwindcss.config.prefix, style_prop_class].compact_blank.join("-") + end + end + + def build_style_prop_classes(style) + style.flat_map { |(style_prop, value)| classes_for_style_prop(style_prop, value) } + .compact_blank + .sort + end + + def classes_for_style_prop(style_prop, value) + token = get_token(style_prop) + return build_style_prop_classes(value).map { "#{token}:#{_1}" } if style_prop.start_with?("_") + return token if value.to_s == "true" + + [token, value.to_s.dasherize].compact_blank.join("-") + end + + def get_token(name_or_alias) + return name_or_alias[1..] if name_or_alias.start_with?("_") + + Tailwindcss.config.theme.find { _1 == name_or_alias || _2[:alias] == name_or_alias }.last[:token] + end + end +end diff --git a/lib/tailwindcss/types.rb b/lib/tailwindcss/types.rb new file mode 100644 index 0000000..191da0f --- /dev/null +++ b/lib/tailwindcss/types.rb @@ -0,0 +1,23 @@ +require "dry-types" +require "active_support/core_ext/string/inflections" +require "active_support/core_ext/enumerable" +require "active_support/core_ext/hash/keys" +require "tailwindcss/arbitrary_value" + +module Tailwindcss + module Types + include Dry.Types() + + def self.Directive(name) + (Types::Interface(:call) | Types::Any).constructor do |value| + values = Tailwindcss.config.theme[name].fetch(:values, []) + values = values.call if values.respond_to?(:call) + next value if value.nil? || value.respond_to?(:call) || + values.any? { _1.to_s.dasherize == value.to_s.dasherize } || + (value.to_s.start_with?("[") && value.to_s.end_with?("]")) || value.is_a?(ArbitraryValue) + + raise ArgumentError, "attribute #{name} has invalid value #{value.inspect}. must be one of #{values.join(", ")}" + end + end + end +end diff --git a/lib/tailwindcss/version.rb b/lib/tailwindcss/version.rb new file mode 100644 index 0000000..53a8e75 --- /dev/null +++ b/lib/tailwindcss/version.rb @@ -0,0 +1,3 @@ +module Tailwindcss + VERSION = "0.1.0" +end diff --git a/sig/tailwindcss.rbs b/sig/tailwindcss.rbs new file mode 100644 index 0000000..1c1ee32 --- /dev/null +++ b/sig/tailwindcss.rbs @@ -0,0 +1,4 @@ +module Tailwindcss + VERSION: String + # See the writing guide of rbs: https://github.com/ruby/rbs#guides +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..9d62ef3 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require "tailwindcss" + +RSpec.configure do |config| + # Enable flags like --only-failures and --next-failure + config.example_status_persistence_file_path = ".rspec_status" + + # Disable RSpec exposing methods globally on `Module` and `main` + config.disable_monkey_patching! + + config.expect_with :rspec do |c| + c.syntax = :expect + end +end diff --git a/spec/tailwindcss/style_spec.rb b/spec/tailwindcss/style_spec.rb new file mode 100644 index 0000000..2a2f2c0 --- /dev/null +++ b/spec/tailwindcss/style_spec.rb @@ -0,0 +1,103 @@ +require "tailwindcss" +require "dry/configurable/test_interface" + +Tailwindcss.enable_test_interface + +Tailwindcss.init! + +RSpec.describe Tailwindcss::Style do + after do + Tailwindcss.reset_config + end + + describe "#to_a" do + it "returns an array of style attributes" do + style = Tailwindcss::Style.new( + bg: :red, + color: :white, + _hover: {bg: :blue, _sm: {mt: 10}, _after: {p: 10, _lg: {p: 14}}}, + _before: {content: '[""]'}, + _lg: {mt: 10} + ) + expect(style.to_a).to match_array( + %w[bg-red text-white hover:bg-blue hover:sm:mt-10 hover:after:p-10 + hover:after:lg:p-14 before:content-[""] lg:mt-10] + ) + end + + context "when there is configured prefix" do + it "returns an array of style attributes with prefix" do + Tailwindcss.configure do |config| + config.prefix = "tw" + end + + style = Tailwindcss::Style.new( + bg: :red, + color: :white, + _hover: {bg: :blue, _sm: {mt: 10}, _after: {p: 10, _lg: {p: 14}}}, + _before: {content: '[""]'}, + _lg: {mt: 10} + ) + expect(style.to_a).to match_array( + %w[tw-bg-red tw-text-white tw-hover:bg-blue tw-hover:sm:mt-10 tw-hover:after:p-10 + tw-hover:after:lg:p-14 tw-before:content-[""] tw-lg:mt-10] + ) + end + end + end + + describe "#to_s" do + it "returns a string of style attributes" do + style = Tailwindcss::Style.new( + bg: :red, + color: :white, + _hover: {bg: :blue, _sm: {mt: 10}, _after: {p: 10, _lg: {p: 14}}}, + _before: {content: '[""]'}, + _lg: {mt: 10} + ) + + expect(style.to_s.split(' ')).to match_array( + %w[bg-red text-white hover:bg-blue hover:sm:mt-10 hover:after:p-10 + hover:after:lg:p-14 before:content-[""] lg:mt-10] + ) + end + end + + describe "#to_h" do + it "returns a hash of style attributes" do + style = Tailwindcss::Style.new( + bg: :red, + color: :white, + _hover: {bg: :blue, _sm: {mt: 10}, _after: {p: 10, _lg: {p: 14}}}, + _before: {content: '[""]'}, + _lg: {mt: 10} + ) + + expect(style.to_h).to eq( + bg: :red, + color: :white, + _hover: {bg: :blue, _sm: {mt: 10}, _after: {p: 10, _lg: {p: 14}}}, + _before: {content: '[""]'}, + _lg: {mt: 10} + ) + end + end + + describe "#to_html_attribute" do + it "returns a hash of style attributes" do + style = Tailwindcss::Style.new( + bg: :red, + color: :white, + _hover: {bg: :blue, _sm: {mt: 10}, _after: {p: 10, _lg: {p: 14}}}, + _before: {content: '[""]'}, + _lg: {mt: 10} + ) + + expect(style.to_html_attribute).to have_key(:class) + expect(style.to_html_attribute[:class].split(' ')).to match_array( + %w[bg-red text-white hover:bg-blue hover:sm:mt-10 hover:after:p-10 + hover:after:lg:p-14 before:content-[""] lg:mt-10] + ) + end + end +end diff --git a/spec/tailwindcss_spec.rb b/spec/tailwindcss_spec.rb new file mode 100644 index 0000000..7738cad --- /dev/null +++ b/spec/tailwindcss_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +RSpec.describe Tailwindcss do + it "has a version number" do + expect(Tailwindcss::VERSION).not_to be nil + end +end diff --git a/tailwindcss.gemspec b/tailwindcss.gemspec new file mode 100644 index 0000000..e06ccb5 --- /dev/null +++ b/tailwindcss.gemspec @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require_relative "lib/tailwindcss/version" + +Gem::Specification.new do |spec| + spec.name = "tailwindcss-rb" + spec.version = Tailwindcss::VERSION + spec.authors = ["guilherme-andrade"] + spec.email = ["inbox@guilherme-andrade.com"] + + spec.summary = "A Ruby wrapper for Tailwind CSS" + spec.description = "A Ruby wrapper for Tailwind CSS" + spec.homepage = "https://guilherme-andrade.com/tailwindcss" + spec.license = "MIT" + spec.required_ruby_version = ">= 2.6.0" + + spec.metadata["homepage_uri"] = spec.homepage + spec.metadata["source_code_uri"] = "https://github.com/guilherme-andrade/tailwindcss" + spec.metadata["changelog_uri"] = "https://github.com/guilherme-andrade/tailwindcss/blob/main/CHANGELOG.md" + + spec.files = Dir.chdir(__dir__) do + `git ls-files -z`.split("\x0").reject do |f| + (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)}) + end + end + spec.bindir = "exe" + spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } + spec.require_paths = ["lib"] + + spec.add_dependency "dry-configurable", "~> 1.0" + spec.add_dependency "deep_merge", "~> 1.0" + spec.add_dependency "activesupport", "~> 7.0" +end