Skip to content

Commit

Permalink
Follow-up for #3643
Browse files Browse the repository at this point in the history
- Setup CI
- Load fontawesome files by using $fa-font-path
  • Loading branch information
mshibuya committed Aug 20, 2023
1 parent a8a2584 commit 0e12e5b
Show file tree
Hide file tree
Showing 24 changed files with 227 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ jobs:
orm: active_record
adapter: postgresql
asset: sprockets
- ruby: "3.0"
gemfile: gemfiles/rails_7.0.gemfile
orm: active_record
adapter: sqlite3
asset: vite
- ruby: "3.0"
gemfile: gemfiles/composite_primary_keys.gemfile
orm: active_record
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem 'appraisal', '>= 2.0'
gem 'devise'
gem 'net-smtp', require: false
gem 'rails'
gem 'vite_rails', require: false
gem 'webpacker', require: false
gem 'webrick'

Expand Down
1 change: 1 addition & 0 deletions gemfiles/composite_primary_keys.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem "appraisal", ">= 2.0"
gem "devise", "~> 4.8"
gem "net-smtp", require: false
gem "rails", "~> 7.0.0"
gem "vite_rails", require: false
gem "webpacker", require: false
gem "webrick"
gem "sassc-rails", "~> 2.1"
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_6.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem "appraisal", ">= 2.0"
gem "devise", "~> 4.7"
gem "net-smtp", require: false
gem "rails", "~> 6.0.0"
gem "vite_rails", require: false
gem "webpacker", require: false
gem "webrick"
gem "sassc-rails", "~> 2.1"
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_6.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem "appraisal", ">= 2.0"
gem "devise", "~> 4.7"
gem "net-smtp", require: false
gem "rails", "~> 6.1.0"
gem "vite_rails", require: false
gem "webpacker", require: false
gem "webrick"
gem "sassc-rails", "~> 2.1"
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_7.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem "appraisal", ">= 2.0"
gem "devise", "~> 4.8"
gem "net-smtp", require: false
gem "rails", "~> 7.0.0"
gem "vite_rails", require: false
gem "webpacker", require: false
gem "webrick"
gem "importmap-rails", require: false
Expand Down
3 changes: 2 additions & 1 deletion lib/generators/rails_admin/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ def configure_for_webpacker5

def configure_for_vite
vite_source_code_dir = ViteRuby.config.source_code_dir
run "yarn add rails_admin@#{RailsAdmin::Version.js}"
run "yarn add rails_admin@#{RailsAdmin::Version.js} sass"
template('rails_admin.vite.js', File.join(vite_source_code_dir, 'entrypoints', 'rails_admin.js'))
@fa_font_path = '@fortawesome/fontawesome-free/webfonts'
template('rails_admin.scss.erb', File.join(vite_source_code_dir, 'stylesheets', 'rails_admin.scss'))
end

Expand Down
5 changes: 2 additions & 3 deletions lib/generators/rails_admin/templates/rails_admin.vite.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
import '~/stylesheets/rails_admin.scss'
import "@fortawesome/fontawesome-free/css/all.css"
import 'rails_admin/src/rails_admin/base';
import "~/stylesheets/rails_admin.scss";
import "rails_admin/src/rails_admin/base";
1 change: 1 addition & 0 deletions spec/dummy_app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
/public/assets
/public/packs
/public/packs-test
/public/vite*
/node_modules
/yarn-error.log
/yarn.lock
Expand Down
1 change: 1 addition & 0 deletions spec/dummy_app/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ gem 'mlb', '>= 0.7', github: 'mshibuya/mlb', branch: 'ruby-3'
gem 'paperclip', '>= 3.4'
gem 'rails_admin', path: '../../'
gem 'shrine', '~> 3.0'
gem 'vite_rails', require: false
gem 'webpacker', require: false
gem 'webrick'

Expand Down
1 change: 1 addition & 0 deletions spec/dummy_app/Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
web: bin/rails server -p 3000
css: yarn build:css --watch
vite: bin/vite dev
2 changes: 2 additions & 0 deletions spec/dummy_app/app/frontend/entrypoints/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import "@rails/ujs";
import "@hotwired/turbo-rails";
17 changes: 17 additions & 0 deletions spec/dummy_app/app/frontend/entrypoints/rails_admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import "~/stylesheets/rails_admin.scss";
import "rails_admin/src/rails_admin/base";
import "flatpickr/dist/l10n/fr.js";
import "trix";
import "@rails/actiontext";
import * as ActiveStorage from "@rails/activestorage";
ActiveStorage.start();

window.domReadyTriggered = [];

document.addEventListener("rails_admin.dom_ready", function () {
window.domReadyTriggered.push("plainjs/dot");
});

$(document).on("rails_admin.dom_ready", function () {
window.domReadyTriggered.push("jquery/dot");
});
2 changes: 2 additions & 0 deletions spec/dummy_app/app/frontend/stylesheets/rails_admin.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$fa-font-path: "@fortawesome/fontawesome-free/webfonts";
@import "rails_admin/src/rails_admin/styles/base";
3 changes: 3 additions & 0 deletions spec/dummy_app/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<%= javascript_pack_tag "application" %>
<% when :importmap %>
<%= javascript_importmap_tags %>
<% when :vite %>
<%= vite_client_tag %>
<%= vite_javascript_tag 'application' %>
<% else %>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application", type: 'module' %>
Expand Down
115 changes: 113 additions & 2 deletions spec/dummy_app/bin/bundle
Original file line number Diff line number Diff line change
@@ -1,3 +1,114 @@
#!/usr/bin/env ruby
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
load Gem.bin_path('bundler', 'bundle')
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'bundle' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require "rubygems"

m = Module.new do
module_function

def invoked_as_script?
File.expand_path($0) == File.expand_path(__FILE__)
end

def env_var_version
ENV["BUNDLER_VERSION"]
end

def cli_arg_version
return unless invoked_as_script? # don't want to hijack other binstubs
return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update`
bundler_version = nil
update_index = nil
ARGV.each_with_index do |a, i|
if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN
bundler_version = a
end
next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/
bundler_version = $1
update_index = i
end
bundler_version
end

def gemfile
gemfile = ENV["BUNDLE_GEMFILE"]
return gemfile if gemfile && !gemfile.empty?

File.expand_path("../Gemfile", __dir__)
end

def lockfile
lockfile =
case File.basename(gemfile)
when "gems.rb" then gemfile.sub(/\.rb$/, gemfile)
else "#{gemfile}.lock"
end
File.expand_path(lockfile)
end

def lockfile_version
return unless File.file?(lockfile)
lockfile_contents = File.read(lockfile)
return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/
Regexp.last_match(1)
end

def bundler_requirement
@bundler_requirement ||=
env_var_version || cli_arg_version ||
bundler_requirement_for(lockfile_version)
end

def bundler_requirement_for(version)
return "#{Gem::Requirement.default}.a" unless version

bundler_gem_version = Gem::Version.new(version)

requirement = bundler_gem_version.approximate_recommendation

return requirement unless Gem.rubygems_version < Gem::Version.new("2.7.0")

requirement += ".a" if bundler_gem_version.prerelease?

requirement
end

def load_bundler!
ENV["BUNDLE_GEMFILE"] ||= gemfile

activate_bundler
end

def activate_bundler
gem_error = activation_error_handling do
gem "bundler", bundler_requirement
end
return if gem_error.nil?
require_error = activation_error_handling do
require "bundler/version"
end
return if require_error.nil? && Gem::Requirement.new(bundler_requirement).satisfied_by?(Gem::Version.new(Bundler::VERSION))
warn "Activating bundler (#{bundler_requirement}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_requirement}'`"
exit 42
end

def activation_error_handling
yield
nil
rescue StandardError, LoadError => e
e
end
end

m.load_bundler!

if m.invoked_as_script?
load Gem.bin_path("bundler", "bundle")
end
27 changes: 27 additions & 0 deletions spec/dummy_app/bin/vite
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'vite' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

bundle_binstub = File.expand_path("bundle", __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require "rubygems"
require "bundler/setup"

load Gem.bin_path("vite_ruby", "vite")
2 changes: 2 additions & 0 deletions spec/dummy_app/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
when :importmap
require 'sprockets/railtie'
require 'importmap-rails'
when :vite
require 'vite_rails'
end

# Require the gems listed in Gemfile, including any gems
Expand Down
15 changes: 15 additions & 0 deletions spec/dummy_app/config/vite.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"all": {
"sourceCodeDir": "app/frontend",
"watchAdditionalPaths": ["../../src"]
},
"development": {
"autoBuild": true,
"publicOutputDir": "vite",
"port": 3036
},
"test": {
"autoBuild": false,
"publicOutputDir": "vite"
}
}
2 changes: 2 additions & 0 deletions spec/dummy_app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"webpack-cli": "^3.3.12"
},
"devDependencies": {
"vite": "^4.3.0",
"vite-plugin-ruby": "^3.2.0",
"webpack-dev-server": "^3"
},
"scripts": {
Expand Down
6 changes: 6 additions & 0 deletions spec/dummy_app/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from "vite";
import RubyPlugin from "vite-plugin-ruby";

export default defineConfig({
plugins: [RubyPlugin()],
});
12 changes: 12 additions & 0 deletions spec/rails_admin/install_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@
contains 'webpack --config webpack.config.js'
contains 'sass ./app/assets/stylesheets/rails_admin.scss:./app/assets/builds/rails_admin.css'
end
when :vite
file 'app/frontend/entrypoints/rails_admin.js' do
contains 'import "~/stylesheets/rails_admin.scss"'
contains 'import "rails_admin/src/rails_admin/base"'
end
file 'app/frontend/stylesheets/rails_admin.scss' do
contains '$fa-font-path: "@fortawesome/fontawesome-free/webfonts";'
contains '@import "rails_admin/src/rails_admin/styles/base"'
end
file 'package.json' do
contains 'sass'
end
end
end,
)
Expand Down
7 changes: 6 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@
end

config.before(:all) do
Webpacker.instance.compiler.compile if CI_ASSET == :webpacker
case CI_ASSET
when :webpacker
Webpacker.instance.compiler.compile
when :vite
ViteRuby.instance.commands.build
end
end

config.before do |example|
Expand Down
4 changes: 3 additions & 1 deletion src/rails_admin/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ import "./sidescroll";
import "./ui";
import "./widgets";

Rails.start();
if (!window._rails_loaded) {
Rails.start();
}

0 comments on commit 0e12e5b

Please sign in to comment.